objdump, nm: sprintf sanitizer null destination pointer
authorAlan Modra <amodra@gmail.com>
Wed, 2 Aug 2023 21:59:58 +0000 (07:29 +0930)
committerAlan Modra <amodra@gmail.com>
Thu, 3 Aug 2023 11:49:04 +0000 (21:19 +0930)
commit760fb390fd4ce506abd401e8a75fc0a510b82d48
tree57f837f9737f009df0d000d3e3b8f23afd09cb12
parent1720b64f735ff2798ab50ea9e2a40ab42af6cc6e
objdump, nm: sprintf sanitizer null destination pointer

Seen on Ubuntu 23.04 x86_64-linux using gcc-12.2 and gcc-12.3 with
CFLAGS="-m32 -g -O2 -fsanitize=address,undefined".

  CC       objdump.o
In file included from /usr/include/stdio.h:906,
                 from /home/alan/src/binutils-gdb/binutils/sysdep.h:24,
                 from /home/alan/src/binutils-gdb/binutils/objdump.c:51:
In function 'sprintf',
    inlined from 'display_utf8' at /home/alan/src/binutils-gdb/binutils/objdump.c:621:14,
    inlined from 'sanitize_string.part.0' at /home/alan/src/binutils-gdb/binutils/objdump.c:742:11:
/usr/include/bits/stdio2.h:30:10: error: null destination pointer [-Werror=format-overflow=]
   30 |   return __builtin___sprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   31 |                                   __glibc_objsize (__s), __fmt,
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   32 |                                   __va_arg_pack ());
      |                                   ~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

The warning is bogus of course.  xmalloc is guaranteed to return
non-NULL, but apparently this isn't seen in display_utf6.  The same
doesn't happen with -m64, maybe due to inlining differences, I haven't
investigated fully.  Easily avoided as we hardly need to use sprintf
for a single char, or a two char string.

* objdump.c (display_utf8): Avoid bogus sprintf sanitizer warning.
Use hex ESC to switch back to default colour.
(sanitize_string): Comment.  Bump buffer size by one.  Fix overlong
line.
* nm.c (display_utf8, sanitize_string): As above.
binutils/nm.c
binutils/objdump.c