Skip to content

cgraph concatPort: simplify string construction

On Ubuntu 23.10, GCC says:

    CC       grammar.lo
  ../../lib/cgraph/grammar.y: In function 'concatPort':
  ../../lib/cgraph/grammar.y:459:17: warning: '%s' directive writing up to 8190 bytes into a region of size between 1 and 8191 [-Wformat-overflow=]
    459 |   sprintf (sym, "%s:%s", s1, s2);
        |                 ^~~~~~~
  In file included from /usr/include/stdio.h:964,
                   from ../../lib/cgraph/grammar.y:22:
  In function 'sprintf',
      inlined from 'concatPort' at ../../lib/cgraph/grammar.y:459:3:
  /usr/include/x86_64-linux-gnu/bits/stdio2.h:30:10: note: '__builtin___sprintf_chk' output between 2 and 16382 bytes into a destination of size 8192
     30 |   return __builtin___sprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,
        |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     31 |                                   __glibc_objsize (__s), __fmt,
        |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     32 |                                   __va_arg_pack ());
        |                                   ~~~~~~~~~~~~~~~~~

This seems like a false positive. The numbers in the two warnings do not correspond to one another and this code appears to be writing strlen(s1) + 1 + strlen(s2) + 1` bytes into a buffer that is at least this big. But we may as well rephrase this to be clearer to humans and the compiler.

Merge request reports