]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
analyzer: handle <error.h> at -O0 [PR115724]
authorDavid Malcolm <dmalcolm@redhat.com>
Thu, 4 Jul 2024 18:44:51 +0000 (14:44 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Thu, 4 Jul 2024 18:44:51 +0000 (14:44 -0400)
At -O0, glibc's:

__extern_always_inline void
error (int __status, int __errnum, const char *__format, ...)
{
  if (__builtin_constant_p (__status) && __status != 0)
    __error_noreturn (__status, __errnum, __format, __builtin_va_arg_pack ());
  else
    __error_alias (__status, __errnum, __format, __builtin_va_arg_pack ());
}

becomes just:

__extern_always_inline void
error (int __status, int __errnum, const char *__format, ...)
{
  if (0)
    __error_noreturn (__status, __errnum, __format, __builtin_va_arg_pack ());
  else
    __error_alias (__status, __errnum, __format, __builtin_va_arg_pack ());
}

and thus calls to "error" are calls to "__error_alias" by the
time -fanalyzer "sees" them.

Handle them with more special-casing in kf.cc.

gcc/analyzer/ChangeLog:
PR analyzer/115724
* kf.cc (register_known_functions): Add __error_alias and
__error_at_line_alias.

gcc/testsuite/ChangeLog:
PR analyzer/115724
* c-c++-common/analyzer/error-pr115724.c: New test.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/analyzer/kf.cc
gcc/testsuite/c-c++-common/analyzer/error-pr115724.c [new file with mode: 0644]

index 4213b89ac9fb4ff11994cf2c35f15a281be3b024..5c3a71fbb49c9e8285a0e9f8d385f24ead9d364e 100644 (file)
@@ -2325,6 +2325,10 @@ register_known_functions (known_function_manager &kfm,
     kfm.add ("__errno_location", make_unique<kf_errno_location> ());
     kfm.add ("error", make_unique<kf_error> (3));
     kfm.add ("error_at_line", make_unique<kf_error> (5));
+    /* Variants of "error" and "error_at_line" seen by the
+       analyzer at -O0 (PR analyzer/115724).  */
+    kfm.add ("__error_alias", make_unique<kf_error> (3));
+    kfm.add ("__error_at_line_alias", make_unique<kf_error> (5));
   }
 
   /* Other implementations of C standard library.  */
diff --git a/gcc/testsuite/c-c++-common/analyzer/error-pr115724.c b/gcc/testsuite/c-c++-common/analyzer/error-pr115724.c
new file mode 100644 (file)
index 0000000..ae606ad
--- /dev/null
@@ -0,0 +1,86 @@
+/* Verify that the analyzer handles the no-optimization case in
+   glibc's <error.h> when error,error_at_line calls become
+   __error_alias and __error_at_line_alias.  */
+
+typedef __SIZE_TYPE__ size_t;
+#define EXIT_FAILURE 1
+#define __extern_always_inline  extern inline __attribute__ ((__always_inline__)) __attribute__ ((__gnu_inline__))
+
+int errno;
+
+/* Adapted from glibc's bits/error.h.  */
+
+extern void __error_alias (int __status, int __errnum,
+                          const char *__format, ...)
+  __attribute__ ((__format__ (__printf__, 3, 4)));
+extern void __error_noreturn (int __status, int __errnum,
+                              const char *__format, ...)
+  __attribute__ ((__noreturn__, __format__ (__printf__, 3, 4)));
+
+/* If we know the function will never return make sure the compiler
+   realizes that, too.  */
+__extern_always_inline void
+error (int __status, int __errnum, const char *__format, ...)
+{
+  if (__builtin_constant_p (__status) && __status != 0)
+    __error_noreturn (__status, __errnum, __format, __builtin_va_arg_pack ());
+  else
+    __error_alias (__status, __errnum, __format, __builtin_va_arg_pack ());
+}
+
+extern void __error_at_line_alias (int __status, int __errnum,
+                                  const char *__fname,
+                                  unsigned int __line,
+                                  const char *__format, ...)
+  __attribute__ ((__format__ (__printf__, 5, 6)));
+extern void __error_at_line_noreturn (int __status, int __errnum,
+                                     const char *__fname,
+                                     unsigned int __line,
+                                     const char *__format,
+                                     ...)
+  __attribute__ ((__noreturn__, __format__ (__printf__, 5, 6)));
+
+/* If we know the function will never return make sure the compiler
+   realizes that, too.  */
+__extern_always_inline void
+error_at_line (int __status, int __errnum, const char *__fname,
+              unsigned int __line, const char *__format, ...)
+{
+  if (__builtin_constant_p (__status) && __status != 0)
+    __error_at_line_noreturn (__status, __errnum, __fname, __line, __format,
+                             __builtin_va_arg_pack ());
+  else
+    __error_at_line_alias (__status, __errnum, __fname, __line,
+                          __format, __builtin_va_arg_pack ());
+}
+
+
+struct list {
+        size_t size;
+        void (*destroy)(void *data);
+        struct list_node *head;
+        struct list_node *tail;
+};
+
+struct list *list_create(void (*destroy)(void *data))
+{
+        struct list *result = (struct list *)__builtin_calloc(1, sizeof(*result));
+        if (!result)
+                error(EXIT_FAILURE,errno,"%s:%d %s()",__FILE__,__LINE__,__func__);
+
+        result->destroy = destroy; /* { dg-bogus "dereference of NULL 'result'" } */
+
+        return result;
+}
+
+struct list *list_create_using_at_line(void (*destroy)(void *data))
+{
+        struct list *result = (struct list *)__builtin_calloc(1, sizeof(*result));
+        if (!result)
+               error_at_line(EXIT_FAILURE,errno,__FILE__,__LINE__,
+                             "%s()", __func__);
+
+        result->destroy = destroy; /* { dg-bogus "dereference of NULL 'result'" } */
+
+        return result;
+}