_XlcDefaultMapModifiers: remove conversions between size_t & int

Avoids build failures with gcc 14.2 when MALLOC_0_RETURNS_NULL is defined:

lcWrap.c: In function ‘_XlcDefaultMapModifiers’:
lcWrap.c:149:9: warning: ‘strcpy’ writing between 4294967296 and
 9223372036854775806 bytes into a region of size 1 [-Wstringop-overflow=]
  149 |         strcpy(mods, prog_mods);
      |         ^~~~~~~~~~~~~~~~~~~~~~~
../../include/X11/Xlibint.h:457:24: note: destination object of size 1
 allocated by ‘malloc’
  457 | # define Xmalloc(size) malloc((size_t)((size) == 0 ? 1 : (size)))
      |                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lcWrap.c:147:12: note: in expansion of macro ‘Xmalloc’
  147 |     mods = Xmalloc(i);
      |            ^~~~~~~
lcWrap.c:149:9: error: ‘__builtin_memcpy’ forming offset [1, 4294967295]
 is out of the bounds [0, 1] [-Werror=array-bounds=]
  149 |         strcpy(mods, prog_mods);
      |         ^~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Part-of: <https://gitlab.freedesktop.org/xorg/lib/libx11/-/merge_requests/267>
This commit is contained in:
Alan Coopersmith 2024-09-29 09:06:32 -07:00
parent 86e71472bc
commit 1f01aafa6d

View file

@ -134,16 +134,16 @@ _XlcDefaultMapModifiers(
const char *user_mods,
const char *prog_mods)
{
int i;
size_t i;
char *mods;
if (!_XlcValidModSyntax(prog_mods, im_valid))
return (char *)NULL;
if (!_XlcValidModSyntax(user_mods, im_valid))
return (char *)NULL;
i = (int) strlen(prog_mods) + 1;
i = strlen(prog_mods) + 1;
if (user_mods)
i = (int) ((size_t) i + strlen(user_mods));
i += strlen(user_mods);
mods = Xmalloc(i);
if (mods) {
strcpy(mods, prog_mods);