Re: A few pg_locale.c fixes

Chao Li <li.evan.chao@gmail.com>

From: Chao Li <li.evan.chao@gmail.com>
To: Jeff Davis <pgsql@j-davis.com>
Cc: pgsql-hackers@postgresql.org
Date: 2026-01-28T22:59:02Z
Lists: pgsql-hackers

> 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/







Commits

  1. Fix memory leaks in pg_locale_icu.c.

  2. Fix theoretical memory leaks in pg_locale_libc.c.