Skip to content

[th/gnulib-alloc-size-warning] gnulib: handle overflow and "-Werror=alloc-size-larger-than=" in convert_to_decimal()

Thomas Haller requested to merge th/gnulib-alloc-size-warning into main

gcc-13.2.1-6.fc39 (with glibc-2.38-14.fc39) warns on x86_64:

    In file included from ../glib/gnulib/vasnprintf.c:60:
    In function ‘convert_to_decimal’,
        inlined from ‘scale10_round_decimal_decoded’ at ../glib/gnulib/vasnprintf.c:1296:12:
    ../glib/gnulib/g-gnulib.h:33:17: error: argument 1 value ‘18446744073709551615’ exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]
       33 | #define malloc  g_malloc
    ../glib/gnulib/vasnprintf.c:867:26: note: in expansion of macro ‘malloc’
      867 |   char *c_ptr = (char *) malloc (xsum (xsum (extra_zeroes, c_len), 1));
          |                          ^~~~~~
    In file included from ../glib/glist.h:34,
                     from ../glib/ghash.h:36,
                     from ../glib/glib.h:52,
                     from ../glib/gnulib/g-gnulib.h:21:
    ../glib/gmem.h: In function ‘scale10_round_decimal_decoded’:
    ../glib/gmem.h:84:10: note: in a call to allocation function ‘g_malloc’ declared here
       84 | gpointer g_malloc         (gsize         n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
          |          ^~~~~~~~

The xsum() pattern for checking for overflow seems cumbersome. Because, you still cannot just call malloc(xsum()) with SIZE_MAX. Instead, you always need to check whether an overflow happened. A xmalloc_sum() macro would be much better. Anyway.

Also, patch the fork of gnulib. That is probably undesirable, however, the gnulib fork already heavily deviated so that ship has sailed.

Merge request reports