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 1wjKA1-0003JF-1v for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Jul 2026 17:09:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wjK9z-000RWE-15 for pgsql-hackers@arkaria.postgresql.org; Mon, 13 Jul 2026 17:09:40 +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 1wjK9y-000RW6-32 for pgsql-hackers@lists.postgresql.org; Mon, 13 Jul 2026 17:09:40 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wjK9x-00000000138-1XVV for pgsql-hackers@postgresql.org; Mon, 13 Jul 2026 17:09:39 +0000 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-2ceaf8a1265so21113565ad.2 for ; Mon, 13 Jul 2026 10:09:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20251104.gappssmtp.com; s=20251104; t=1783962577; x=1784567377; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:content-type :references:in-reply-to:date:cc:to:from:subject:message-id:from:to :cc:subject:date:message-id:reply-to:content-type; bh=QThL67BHh2SSKPwb5GiztGet8maG87Cv/cbpLomWkjg=; b=m4DZuAjciA2LZcCThUey6kUhAblAfPpzPPXhDuBXh7LNKSswweFzJ/MENGGH/VxJaA 1kQfX6NPpynGfQPh2kD0sKnFVvZRM7skEL12mFR9eW6ehXUKygqxbK39VvXq2J73l8dJ ivnLxYJB4mScKvi8v/IgHWARznEL7CcyeQMRqq6tAW9O4zVRaudScrNGukBAeCu1VCqW V3g2WKVsQMRYOkpbZLFwA0rTntYAWPyDALRxvJEH98djMbapR2dM5XxAKtXzAjeoadlc 2k7WX6aJWlm+/GZzepYIX8ibtq25rOO+Kaf4bN0vC+irujJqHAHqMycm4e4gc9FO5bCd 9Npg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783962577; x=1784567377; h=mime-version:user-agent:content-transfer-encoding:content-type :references:in-reply-to:date:cc:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=QThL67BHh2SSKPwb5GiztGet8maG87Cv/cbpLomWkjg=; b=D8WnOVZTR9tbyXVaokXvXLl2d8v4hSYodC+iWEerPUlA4IN2QCOm6C8q13/B6CPH6l ty2iegqyMf9IgOt5kzY4D7WBdzKBgMN3AHT6LW3JmnK+HGupEvSI8bvJiqWkzjugKF6T JIv2c8pO+OIVeXWIcUcAWJcvpBfTW62Do5gzBEkelSMeH40BCPwMRvi9o2Hf49zc9UHF pq8q7ezgpIgqgq/917JxsvmjL7eueum5/wIRIty+9J3jZOxQymCU9qRyOhbuLymaLS6j ZrjcxsPjXEmPx64w6EJwJcTnnZbZOXRl54p8ueHAIbN5ypND5jkQAoWto5N6+fMmTj02 RwjA== X-Gm-Message-State: AOJu0Yz9ocnoedNyDE/CtmTAXb1L7ue5MgMZadoOK8nAQRDNb4D1S9Wo vOTFjvamj0CFa64FBPKTZ31hAS6D+QTmk7EYhKo/3QPtI8rLsOsrwtpHvAT1D6xnWw== X-Gm-Gg: AfdE7cl/xqDUq9B8vE8YK+QpdiuHEdqNws0WH4rQT2ZhLAI8jgS22plLXISGu0JL+Ba HJwZ+Vx5oxuYmcb+y3F3lqM+UQ3gH/A5XXD5tDO63rR7NSQjxaC5kbU0GLPiB47x8hqs5C6uBCg VS/b4GoQAaYKZ53gZU3Lk0F0A0SgLX6x+Q3XYOo5jLWxxzyTmlER4Jv+JjvA6/swe6xUJqUpF8V j66/2IHOjn6mvhysF3/bdBlUqYaiyl07UTNsByvSHLuQxLj4Cy8ZQRZGY41K+TXXRecUV0CCmpp 8ZEjaNbcUUgcSjdlEE3LSQFn1WSFHCihGm0V8k2U19nOHAK3aAorlqsFomqr20SIvnf37ZkCH2k RwFtzi2vZOog4F6H3jaDf417cZlBM2Lv+w6yh0S0Skuhgfa6eHc5gOW8j5Syb/W4yO+g0b8VRwa IbqrL05sOjlvToee97NBqZUJ10zseJp+RgZza5yGYUAr/qAA== X-Received: by 2002:a17:90b:314c:b0:380:94de:155e with SMTP id 98e67ed59e1d1-38dc73c1d67mr8968892a91.5.1783962576847; Mon, 13 Jul 2026 10:09:36 -0700 (PDT) Received: from jeff-ws-bridge.lan (c-24-7-19-3.hsd1.ca.comcast.net. [24.7.19.3]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-38e1745a596sm195479a91.15.2026.07.13.10.09.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jul 2026 10:09:35 -0700 (PDT) Message-ID: Subject: Re: Small patch to improve safety of utf8_to_unicode(). From: Jeff Davis To: Tom Lane , Chao Li Cc: pgsql-hackers@postgresql.org Date: Mon, 13 Jul 2026 10:09:34 -0700 In-Reply-To: <3213927.1783950167@sss.pgh.pa.us> 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> <3213927.1783950167@sss.pgh.pa.us> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.3-0ubuntu1.1 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2026-07-13 at 09:42 -0400, Tom Lane wrote: > 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.=C2=A0 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. >=20 > 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.=C2=A0 If it can, the > overflow > might be reachable on 32-bit platforms, resulting in a silently- > broken > result (but no memory clobber AFAICS). From Unicode 17.0 section 5.18.2: "the maximum string expansion as a result of case mapping in the Unicode Standard is three". Along with a potential for 4X difference in byte length for a single code point in UTF8, that makes a maximum of 12X expansion. > Catering for this case seems like it would require messy warts on the > casefolding functions' API.=C2=A0 So if we can convince ourselves that > overflow can't really occur, I'd be content to add a comment > demonstrating that. Based on the actual data I expect the worst case to be smaller, probably 3X. That would be convenient because I think that means it can't overflow: the largest text value we support is 1GB, while 3GB would still fit in a size_t. Unfortunately, even if that's true today, it would be hard to be confident it won't change in a future version of Unicode. > =C2=A0 But if we can't prove that, I think we need some > warts :-( 07211f64ac at least has an error path already, so we could set *pconsumed=3D0 on overflow and return SIZE_MAX. It would be extra work for the caller if it wants to distinguish between overflow and an encoding error, but if we aren't expecting to hit it in practice, then maybe it's OK to give an incorrect error. Regards, Jeff Davis