Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vVI6B-0003l3-30 for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Dec 2025 23:35:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vVI6A-003DtT-2F for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Dec 2025 23:35:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vVI6A-003DtL-1L for pgsql-hackers@lists.postgresql.org; Mon, 15 Dec 2025 23:35:27 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vVI68-000wsu-13 for pgsql-hackers@postgresql.org; Mon, 15 Dec 2025 23:35:26 +0000 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-29f30233d8aso38591805ad.0 for ; Mon, 15 Dec 2025 15:35:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765841721; x=1766446521; darn=postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=q5xQdRfpnIn7vxnA8qFMp09wtwZe0CioFsq2t8pb7GA=; b=Zk/HQbsWIUInqsax109sIasTtYg3kwI5FefmKfcPmPjcdtARqScCmAKMBMtLuD8+yk a0ZcBiMQW4idNCcs6x92GfNZ1AbaUsR7p8C8CjoNi9azLVIXXQ4ynMtwTy3z1VTbC1fO 2IB+AHw+ppuH99B+goJBq1D37dRSWNcFwHOdOoy134nv8yPMaL5df5HVqhHXhvXiwVx8 CPC3Sux2o1fk+IiyevXo1wCL5Rneb8deZqb40fQJKDmrZtyC1Fo7IeQp3dsiisS+44eG QqcNm6mOmEEuUWP0ywjKWgO223AZuDfr02kC7Ycax6z6kTakhJJgBvVJbJWf7wa6k3Sa hYxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765841721; x=1766446521; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=q5xQdRfpnIn7vxnA8qFMp09wtwZe0CioFsq2t8pb7GA=; b=Mw7WshgKx18H7f+q0bSP+2geNQbXaFw5zrEp9BRgOSY774a4Z3T3iO3EuJ0FklziRS kZmuvKO2UnhR0C/bHEQ5QOwYbZvlppWbYYbLfhdUByhjAJt6gdqX+XgpyxA1Se5ZHaNy Oy9H0MD2X1XK+hxAGAcIsyKpMR95aJrh/humu8eYmK2CZl7R8T7oA+HLS/40/XfaACQR 0VlBhXnwPYClk4m7SwfbQWd4qlM0tZz+3Fs4kQzzTCNHDVrStkKW+2esO1/fWCP1pp/S LCg/9Lu+t2Ulbz3vAyGq34sNE7Icrojat21/tqUwu+7nxVHSkvb5TQGO3SDB7JT4UvNk ar/A== X-Gm-Message-State: AOJu0YxG+K5CzdbFRjfNuwfTklZNN26cIrmBZ1oD0tHRrcoInv3B9/4O 6fAWJmll9Q5F2l2Lew15c7+YmN7jQ1kHmxsmms4tkLpGuWqcDLetKSQYkmscVVl1 X-Gm-Gg: AY/fxX5Wt84d9SImRy8SZvfpoGkMbN+uC7grG8ny0MMVMUSePwgc/6+F5XXBUzMFBly P/cJ3q4QBO/KUZ8U4QwMLtB9oM9nKaqlJEuTfQxrQzrZY57LX7xhdvymr2TR2TWXsY7Paic0Hme urvq3oEv9s5ZWWbm5OCkeoad5oJcMR1aLRsHyn8J25ObhEHlJ6JIxJYvSlLwgK0N4dxviMxo3Iq UgSkTiHfy+gXeBW7eYEunK/7RjaRnmLUvKlY5McR4uFUeNda4sP4jU3khbPrCMMo1QsKxzoIxlf sx6qHIn7QU/js1HYQ6RqeeENo51iSWYvthRNsyCDYIjrmYdmhU9K7fHtV6G4TH+znnqI1DwcXOR /fBMwgAftx9SJa29TKUoiIJUcjmedh1nMa0fG4f/I2tM1ZywC6qnZEq0q6XsiVSIdgZAYSE0Ys9 bhlmF5fpwN6nPXcwizYEk= X-Google-Smtp-Source: AGHT+IGvBwV9/OgLntG3n4Ztm5dSFyjKwUh7wS8yh1erFOTKETkb6isyI4jJjQkvQCi5ugYv3G6vPg== X-Received: by 2002:a17:903:1663:b0:2a0:f0db:690e with SMTP id d9443c01a7336-2a0f0db6e24mr46876665ad.52.1765841720649; Mon, 15 Dec 2025 15:35:20 -0800 (PST) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a135e7da8asm5156795ad.7.2025.12.15.15.35.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Dec 2025 15:35:20 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81\)) Subject: Re: Small patch to improve safety of utf8_to_unicode(). From: Chao Li In-Reply-To: Date: Tue, 16 Dec 2025 07:34:41 +0800 Cc: pgsql-hackers@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <541F240E-94AD-4D65-9794-7D6C316BC3FF@gmail.com> To: Jeff Davis X-Mailer: Apple Mail (2.3826.700.81) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Dec 16, 2025, at 04:23, Jeff Davis wrote: >=20 > On Sun, 2025-12-14 at 07:22 +0800, Chao Li wrote: >> This patch adds a length check to utf8_to_unicode(), I think which is >> where =E2=80=9Csafety=E2=80=9D comes from. Can you please add an a = little bit more to >> the commit message instead of only saying =E2=80=9Cimprove safety=E2=80= =9D. >=20 > Right: it does not read past pg_mblen(), nor past the supplied length. >=20 > We could go further and check for valid continuation bytes, but the > other routines don't do that. >=20 >> It also deleted two redundant function declarations from pg_wchar.h, >> which may also worth a quick note in the commit message. >=20 > I committed that as an independent change and removed it from this > patch. >=20 >> + /* Assume byte sequence has not been broken. */ >> + c =3D utf8_to_unicode(str, MAX_MULTIBYTE_CHAR_LEN); >> ``` >>=20 >> Here we need an empty line above the new comment. >=20 > Done, and I expanded the comment to explain why it's safe. >=20 >> pg_utf_dsplen(const unsigned char *s) >> { >> - return ucs_wcwidth(utf8_to_unicode(s)); >> + /* trust that input is not a truncated byte sequence */ >> + return ucs_wcwidth(utf8_to_unicode(s, >> MAX_MULTIBYTE_CHAR_LEN)); >> } >> ``` >>=20 >> For the new comment, as a code reader, I wonder why we =E2=80=9Ctrust=E2= =80=9D that? >=20 > We could use strlen(), but I was concerned that it might be used for > string fragments that aren't NUL-terminated, because it's intended for > a single character. A caller might reasonably assume that it wouldn't > read past pg_mblen(). >=20 > So I changed the comment slightly to just say that it requires the > input is a valid UTF-8 sequence. Let me know if you have another > suggestion. >=20 > Regards, > Jeff Davis >=20 >=20 > V2 LGTM. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/