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 1wjGvt-0001UM-2m for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Jul 2026 13:42:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wjGvs-00AKMA-1j for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Jul 2026 13:42:53 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wjGvs-00AKM1-0R for pgsql-hackers@lists.postgresql.org; Mon, 13 Jul 2026 13:42:52 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wjGvr-00000000UEG-0xuY for pgsql-hackers@postgresql.org; Mon, 13 Jul 2026 13:42:52 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 66DDgls63213928; Mon, 13 Jul 2026 09:42:47 -0400 From: Tom Lane To: Chao Li cc: Jeff Davis , pgsql-hackers@postgresql.org Subject: Re: Small patch to improve safety of utf8_to_unicode(). In-reply-to: References: <541F240E-94AD-4D65-9794-7D6C316BC3FF@gmail.com> <313BCA0E-2E74-4EB6-9AA8-10DB7F439151@gmail.com> <592441c1d871a4ed0ac1708e132f447caa35869c.camel@j-davis.com> <3058CAA1-40E0-4098-893B-615F77CD35E1@gmail.com> <0cedb517aee8b79b8e31a4e42885ed88c2a67c5f.camel@j-davis.com> <16CAD4DB-9D4A-481A-BB4E-78775C4F4B2C@gmail.com> Comments: In-reply-to Chao Li message dated "Wed, 08 Jul 2026 09:48:02 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3213926.1783950167.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 13 Jul 2026 09:42:47 -0400 Message-ID: <3213927.1783950167@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Chao Li writes: > On Jul 7, 2026, at 17:56, Jeff Davis wrote: >> I'd like to wait for more comments before I commit these last two >> patches, to see if the functions are generally useful for other callers >> as well. > V6 looks good to me. Coverity is unimpressed, although after reading 07211f64a I wonder if this patch didn't simply trigger it to pick up a pre-existing problem (it does seem to act that way sometimes). It now complains: /srv/coverity/git/pgsql-git/postgresql/src/common/unicode_case.c: 324 = in convert_case() 318 } 319 = 320 if (result_len < dstsize) 321 dst[result_len] =3D '\0'; 322 = 323 *pconsumed =3D srcoff; >>> CID 1696680: (INTEGER_OVERFLOW) >>> "result_len", which might have overflowed, is returned from the fu= nction. 324 return result_len; 325 } What I think this is pointing out is that even if the input string's length fits in size_t, the length of the case-converted equivalent string might not. The code won't write past dstsize, but it will try to compute the full output length required, and it won't notice if result_len overflows and wraps around. I would have written this off as an unreachable edge case, but our recent experience with unicode_normalize (cf. commit 066b7b144) makes me hesitant to assume that case-folding can't result in integer-multiple growth of the string length. If it can, the overflow might be reachable on 32-bit platforms, resulting in a silently-broken result (but no memory clobber AFAICS). Catering for this case seems like it would require messy warts on the casefolding functions' API. So if we can convince ourselves that overflow can't really occur, I'd be content to add a comment demonstrating that. But if we can't prove that, I think we need some warts :-( regards, tom lane