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

            Bug ID: 114920
           Summary: null_terminated_string_arg attribute does not warn for
                    non-nul-terminated strings
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: bruno at clisp dot org
  Target Milestone: ---

Created attachment 58085
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58085&action=edit
test case foo.c

The documentation of null_terminated_string_arg in
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html gave me the
expectation that the analyzer would produce a warning when I pass a
non-NUL-terminated string to a function declared with this attribute. But it
doesn't, neither for stack-allocated strings nor for heap-allocated strings.

Test case:
============================= foo.c ===========================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern size_t strlen (const char *s) __attribute__
((__null_terminated_string_arg__ (1)));
int main ()
{
  {
    char s[7];
    memcpy (s, "FOOBAR!", 7);
    printf ("n=%d\n", (int) strlen (s));
  }
  {
    char *s = malloc (7);
    if (s != NULL) {
      memcpy (s, "FOOBAR!", 7);
      printf ("n=%d\n", (int) strlen (s));
    }
  }
}
===============================================================
$ gcc -c foo.c -Wall -Wextra -fanalyzer -Wanalyzer-use-of-uninitialized-value
-Wanalyzer-out-of-bounds

Expected: two warnings
Actual: no warning.

$ gcc --version
gcc (GCC) 14.0.1 20240411 (Red Hat 14.0.1-0)

Reply via email to