A few pg_locale.c fixes

Started by Jeff Davis10 months ago2 messageshackers
Jump to latest
#1Jeff Davis
pgsql@j-davis.com

Attached patches.

Problems found with AI assistance. Patches by me.

Regards,
Jeff Davis

Attachments:

v1-0001-Fix-memory-leaks-in-pg_locale_libc.c.patchtext/x-patch; charset=UTF-8; name=v1-0001-Fix-memory-leaks-in-pg_locale_libc.c.patchDownload+15-16
v1-0002-Fix-memory-leaks-in-pg_locale_icu.c.patchtext/x-patch; charset=UTF-8; name=v1-0002-Fix-memory-leaks-in-pg_locale_icu.c.patchDownload+5-1
v1-0003-pg_locale.c-Clean-up-srclen-1.patchtext/x-patch; charset=UTF-8; name=v1-0003-pg_locale.c-Clean-up-srclen-1.patchDownload+2-1
#2Chao Li
li.evan.chao@gmail.com
In reply to: Jeff Davis (#1)
Re: A few pg_locale.c fixes

On Jan 29, 2026, at 03:37, Jeff Davis <pgsql@j-davis.com> wrote:

Attached patches.

Problems found with AI assistance. Patches by me.

Regards,
Jeff Davis

<v1-0001-Fix-memory-leaks-in-pg_locale_libc.c.patch><v1-0002-Fix-memory-leaks-in-pg_locale_icu.c.patch><v1-0003-pg_locale.c-Clean-up-srclen-1.patch>

0001 and 0002 looks good.

For 0003, I see some other functions having header comment to explain what -1 of length means, do we want to add the explanation to these two functions’ header comments?
```
/* 'srclen' of -1 means the strings are NUL-terminated */
size_t
strnxfrm_icu(char *dest, size_t destsize, const char *src, ssize_t srclen,
pg_locale_t locale)
```

Whiling reviewing 0001, I noticed the other problem:
```
/*
* char2wchar --- convert multibyte characters to wide characters
*
* This has almost the API of mbstowcs_l(), except that *from need not be
* null-terminated; instead, the number of input bytes is specified as
* fromlen. Also, we ereport() rather than returning -1 for invalid
* input encoding. tolen is the maximum number of wchar_t's to store at *to.
* The output will be zero-terminated iff there is room.
*/
static size_t
char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen,
locale_t loc)
{
size_t result;

if (result == -1)
{
```

This function’s return type is size_t that is unsigned, but the header comment says it may return -1.

Also, result is of type size_t, but it is check against -1.

If you confirm that is a problem, you may include a fix in this patch set as well.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/