[PATCH] locale: Fix buffer overflow for long wide strings (bug 34904)

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Wed Nov 18 16:20:12 GMT 2026



On 01/11/26 17:04, Mathis Huron wrote:
> When localedef reads a string that it must also return as a wide
> string (LC_TIME fields, the LC_NUMERIC and LC_MONETARY separators,
> LC_CTYPE transliteration strings and LC_COLLATE collating elements),
> get_string allocates the wide-character buffer with xmalloc (buf2max),
> where buf2max is 56 * sizeof (uint32_t): 224 bytes, room for 56
> elements.  The ADDWC macro treats buf2max as an element count and only
> grows the buffer once 224 elements are stored, so any such string of
> 56 or more characters (the terminating null counts as one element) is
> written past the end of the allocation.
> 
> For example, running "localedef -i <file> <name>" on this source:
> 
>   LC_TIME
>   d_fmt "%A, %d %B %Y (week %V, day %j of the year, time zone %Z %z)"
>   END LC_TIME
> 
> aborts with "corrupted size vs. prev_size" instead of reporting the
> missing LC_TIME fields.
> 
> Initialize buf2max as an element count and allocate
> buf2max * sizeof (uint32_t) bytes, which matches the reallocation in
> ADDWC.
> 
> No locale in localedata/locales has a wide string this long, so
> compiled locales do not change: all 501 locales in localedata/SUPPORTED
> produce byte-identical output with and without this change.
> 
> Add test8 to tst-locale.  It has strings longer than 56 characters in
> LC_CTYPE, LC_COLLATE and LC_TIME, one of them long enough to grow the
> buffer.  tst-locale fails without the fix and passes with it.
> 
> No regressions on x86_64-linux-gnu.
> 
> Signed-off-by: Mathis Huron <huron.mathis@gmail.com>
> ---
> This is my first glibc contribution, submitted under the DCO (no FSF
> copyright assignment). Like the other files in localedata/tests, the
> new test locale has no license header.

LGTM, with a small fix below (I will do it for you).

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> 
>  locale/programs/linereader.c |  4 ++--
>  localedata/Makefile          |  2 +-
>  localedata/tests/test8.def   | 41 ++++++++++++++++++++++++++++++++++++
>  localedata/tst-locale.sh     |  1 +
>  4 files changed, 45 insertions(+), 3 deletions(-)
>  create mode 100644 localedata/tests/test8.def
> 
> diff --git a/locale/programs/linereader.c b/locale/programs/linereader.c
> index d3c45fea35..2cf6cfcfbf 100644
> --- a/locale/programs/linereader.c
> +++ b/locale/programs/linereader.c
> @@ -813,12 +813,12 @@ get_string (struct linereader *lr, const struct charmap_t *charmap,
>      {
>        bool illegal_string = false;
>        size_t buf2act = 0;
> -      size_t buf2max = 56 * sizeof (uint32_t);
> +      size_t buf2max = 56;
>        int ch;
>  
>        /* We have to provide the wide character result as well.  */
>        if (return_widestr)
> -	buf2 = xmalloc (buf2max);
> +	buf2 = xmalloc (buf2max * sizeof (uint32_t));
>  
>        /* Read until the end of the string (or end of the line or file).  */
>        while ((ch = lr_getc (lr)) != '"' && ch != '\n' && ch != EOF)
> diff --git a/localedata/Makefile b/localedata/Makefile
> index 0fb1d78c29..4a8354506c 100644
> --- a/localedata/Makefile
> +++ b/localedata/Makefile
> @@ -142,7 +142,7 @@ ld-test-names := test1 test2 test3 test4 test5 test6 test7
>  ld-test-srcs := $(addprefix tests/,$(addsuffix .cm,$(ld-test-names)) \
>  				   $(addsuffix .def,$(ld-test-names)) \
>  				   $(addsuffix .ds,test5 test6) \
> -				   test6.c trans.def)
> +				   test6.c test8.def trans.def)
>  
>  fmon-tests = \
>    n01y12 \

The test8 should be listed in generated-dirs as well.

> diff --git a/localedata/tests/test8.def b/localedata/tests/test8.def
> new file mode 100644
> index 0000000000..b3ff9f2810
> --- /dev/null
> +++ b/localedata/tests/test8.def
> @@ -0,0 +1,41 @@
> +# Test locale for bug 34904: localedef must accept strings of 56 or
> +# more characters wherever it reads them as wide strings.  Each such
> +# string below is longer than 56 characters, and d_fmt is longer than
> +# 224 characters so that the buffer has to grow.  Both literal
> +# characters and <Uxxxx> symbols are used.
> +
> +LC_CTYPE
> +translit_start
> +<U00C6> "Latin capital letter AE, transliterated with a long replacement"
> +translit_end
> +END LC_CTYPE
> +
> +LC_COLLATE
> +collating-element <long-element> from "this collating element is \
> +defined from a string that is longer than 56 characters"
> +order_start forward
> +<long-element>
> +order_end
> +END LC_COLLATE
> +
> +LC_TIME
> +abday "Sun";"Mon";"Tue";"Wed";"Thu";"Fri";"Sat"
> +day "Sunday";"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday"
> +abmon "Jan";"Feb";"Mar";"Apr";"May";"Jun";"Jul";"Aug";"Sep";"Oct";"Nov";"Dec"
> +mon "January";"February";"March";"April";"May";"June";"July";"August";\
> +    "September";"October";"November";"December"
> +am_pm "AM";"PM"
> +d_t_fmt "%A, %d %B %Y (week %V, day %j of the year, time zone %Z %z)"
> +d_fmt "%A %d %B %Y, week %V, day %j; %A %d %B %Y, week %V, day %j; \
> +%A %d %B %Y, week %V, day %j; %A %d %B %Y, week %V, day %j; \
> +%A %d %B %Y, week %V, day %j; %A %d %B %Y, week %V, day %j; \
> +%A %d %B %Y, week %V, day %j; %A %d %B %Y, week %V, day %j; \
> +%A %d %B %Y, week %V, day %j; %A %d %B %Y, week %V, day %j; "
> +t_fmt "<U0025><U0048><U003A><U0025><U004D><U003A><U0025><U0053><U0020>\
> +<U0028><U0068><U006F><U0075><U0072><U0073><U0020><U0025><U0048><U002C>\
> +<U0020><U006D><U0069><U006E><U0075><U0074><U0065><U0073><U0020><U0025>\
> +<U004D><U002C><U0020><U0073><U0065><U0063><U006F><U006E><U0064><U0073>\
> +<U0020><U0025><U0053><U002C><U0020><U0074><U0069><U006D><U0065><U0020>\
> +<U007A><U006F><U006E><U0065><U0020><U0025><U005A><U0029>"
> +t_fmt_ampm "%I:%M:%S %p"
> +END LC_TIME
> diff --git a/localedata/tst-locale.sh b/localedata/tst-locale.sh
> index 3289ce9634..7ee061e1ac 100755
> --- a/localedata/tst-locale.sh
> +++ b/localedata/tst-locale.sh
> @@ -67,6 +67,7 @@ test_locale tests/test4.cm tests/test4.def test4
>  test_locale tests/test5.cm tests/test5.def test5 tests/test5.ds
>  test_locale tests/test6.cm tests/test6.def test6 tests/test6.ds
>  test_locale tests/test7.cm tests/test7.def test7
> +test_locale UTF-8 tests/test8.def test8
>  
>  exit 0
>  



More information about the Libc-alpha mailing list