[Bug c/89678] New: Bogus -Wstringop-truncation error

marxin at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Mar 12 11:12:00 GMT 2019


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89678

            Bug ID: 89678
           Summary: Bogus -Wstringop-truncation error
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marxin at gcc dot gnu.org
  Target Milestone: ---

I see following isolated from bctoolbox package:

$ cat log.c
#include <stdlib.h>
#include <string.h>

char *bctbx_strcat_vprintf(char *dst, char *ret) {
  size_t dstlen, retlen;

  if (!dst)
    return ret;

  dstlen = strlen(dst);
  retlen = strlen(ret);

  if ((dst = __builtin_realloc(dst, dstlen + retlen + 1)) != 0) {
    strncat(dst, ret, retlen);
    dst[dstlen + retlen] = '\0';
    return dst;
  } else {
    return 0;
  }
}

char *a;
char *b;

int main(int argc, char **argv) {
  a = malloc(10);
  b = malloc(10);

  if (argc) {
    strcpy(a, "one");
    strcpy(b, "two");
  }

  __builtin_printf(bctbx_strcat_vprintf(a, b));
  return 0;
}

$ gcc log.c -c -O2 -Wstringop-truncation
log.c: In function ‘bctbx_strcat_vprintf.part.0’:
log.c:14:5: warning: ‘strncat’ output truncated before terminating nul copying
as many bytes from a string as its length [-Wstringop-truncation]
   14 |     strncat(dst, ret, retlen);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
log.c:11:12: note: length computed here
   11 |   retlen = strlen(ret);
      |            ^~~~~~~~~~~


More information about the Gcc-bugs mailing list