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 1vVxLc-00A3nE-02 for pgsql-hackers@arkaria.postgresql.org; Wed, 17 Dec 2025 19:38:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vVxLZ-00G3l8-2Q for pgsql-hackers@arkaria.postgresql.org; Wed, 17 Dec 2025 19:38:06 +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 1vVxLZ-00G3l0-18 for pgsql-hackers@lists.postgresql.org; Wed, 17 Dec 2025 19:38:06 +0000 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vVxLX-001Hnw-16 for pgsql-hackers@postgresql.org; Wed, 17 Dec 2025 19:38:05 +0000 Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-34ccbf37205so2014975a91.2 for ; Wed, 17 Dec 2025 11:38:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1766000281; x=1766605081; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=sBvhOpoT+QKuLA8yaG8onlC7ulrNnUZtndvwLhKMDU0=; b=CUzS2dCqhzlOGoJkR3NhnMciCz0bDeAejik2LP+krfoQ8u1oRVqH1nN2aHCpRRdBFU wZ9le70cDCvYAbHAk+3C/tqqC3sYVCLNokQPhEH55ePfk4Y5KyLsKn2eOUjBP88wSljq tI1AvVGRcrrqiKeK8VgVPxSGBaPhHjK0sbzX0k/6+k/u6ZvAdizNZMzAWxwrrK25beRF 8OioayXz69PsrMQ3zpHlEoCXaa9nyE5WIvyXib2IskL7OVINN3yYWJlNdYWkqT3plBMR cnSEmqnT47EABEZ7ryMk6ATJxvZ6NEOpxUxzCPhFh9F8IeahD7iCPGZ2QPcunxet6bYC YObw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766000281; x=1766605081; h=mime-version:user-agent:content-transfer-encoding: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; bh=sBvhOpoT+QKuLA8yaG8onlC7ulrNnUZtndvwLhKMDU0=; b=ZeMdOqmGkcGFUgUZvh19kSYKdtH64MlrZhx5sQLLUUog1lNFZlk27r3KJMq8/ZhxW5 A4lkH0AvDNuIyLCvRzi2hfNeKMQ7JbcyCuV6LN3RquiJz4w1Yzzixy+SpX9av1YB5hMS EVnixcJdcE9VYemCv/fVQ6nsrB6GSAQnXkK7Jt/4Yx3YuUq2dHZQbQmXY3ORluppwFgt 4WoO734hBD5oN0A2OXJO34K8TjJ8dM1AAqPukkifCz+RdY1nne6O3VIXX+61wWfb9I48 bhqUDKk752/5ENtKp4SydF+9IzzJyvO4x1coxLKgRdLuMaFy0aq7zUF90Os7wKFo1hL8 6g/A== X-Gm-Message-State: AOJu0YxqjmPUH35MypEz+tKqm06TLblg/RRR3sn4NFp1pH1F12UNdajx Zo6cmuDDP4bxgqjc7KWA5cD+9aoksOrtHw9qb++GFduUqdO79sHFNjtRmmpn8qtS2A== X-Gm-Gg: AY/fxX7xNZXf5XVUTYEXFXLPXRCP7ugw412GgQwIbAq7vBlllI/2P4WcAAHXYL0BIHo QFZwIq4EyJR+gtqeejGzHmp5tudzIaleByp3e38QD2LQ4NrYrKc6YOBpVGB2xSBgNwBNMoYPNWx wbYRTJZM60RMq8UJRx4Pfv4duOrYcJwoIy/yo+pWLtyD3wwY12OpzHMqMCNfUxKMWWuaH00r/ve +sjx+IO5FbRKm6+UfjyKFKq+0CyVhK1OK4SQ59YoPV4d3M0shWXRXFHYa9hF0EP4Ko82esYZL4r hqCL/Tre9wr8nDh5nsXcdgK4HCzCggaFjbOYmtZTLGHk/US1UhEnMIMbYSELBymu/qG/c7+rxsi sWhdTEYAAAi6T8vs1XMZvP5U+gasgIbcDDQX5OMDP6QYwbpZzqbd14MtkTdzr33rGvvhcuA7SAd eLMghUwD2XF5nGZvb+I68egCnNuEflf7WaL8hlwiykHrI= X-Google-Smtp-Source: AGHT+IEwzKSASJCbzJVbMDspFCx2mvqA8znLSpu9Zq+976bkfB9yLdrbMqoBiEpnov2MNLodoJqlsw== X-Received: by 2002:a17:90b:180e:b0:32e:7c34:70cf with SMTP id 98e67ed59e1d1-34abd8356ccmr16055048a91.36.1766000281106; Wed, 17 Dec 2025 11:38:01 -0800 (PST) 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-34e70d4f7ffsm363805a91.1.2025.12.17.11.38.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Dec 2025 11:38:00 -0800 (PST) Message-ID: Subject: Re: Small patch to improve safety of utf8_to_unicode(). From: Jeff Davis To: Chao Li Cc: pgsql-hackers@postgresql.org Date: Wed, 17 Dec 2025 11:37:59 -0800 In-Reply-To: References: <541F240E-94AD-4D65-9794-7D6C316BC3FF@gmail.com> 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 Tue, 2025-12-16 at 07:34 +0800, Chao Li wrote: > > >=20 > V2 LGTM. On second thought, if we're going to change something here, we should probably have a more flexible API for both utf8_to_unicode() and unicode_to_utf8(). Looking at the callers, I think we want to have signatures something like: /* returns number of bytes consumed, or -1 */ static inline ssize_t utf8_to_unicode(char32_t *cp, const unsigned char *src, size_t srclen) { ... } /* returns number of bytes written, or -1 */ static inline ssize_t unicode_to_utf8(unsigned char *dst, size_t dstsize, char32_t cp) { ... } That would make both APIs safer, and the caller wouldn't need to call unicode_utf8len() or pg_utf8_mblen() separately. We could also do more validation, but of course then the callers would need to do something if they encounter a failure. We could also try to catch NUL terminators in the middle of a sequence, which might be useful. Regards, Jeff Davis