https://gcc.gnu.org/g:0f26f4f76961cdc7ebf7f07bec0b370fd1a04972
commit r14-10956-g0f26f4f76961cdc7ebf7f07bec0b370fd1a04972 Author: David Malcolm <dmalc...@redhat.com> Date: Thu Jul 4 14:44:51 2024 -0400 analyzer: handle <error.h> at -O0 [PR115724] 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 <dmalc...@redhat.com> (cherry picked from commit a6fdb1a2a2906103afd70fa68cf7c45e896b8fbb) Diff: --- gcc/analyzer/kf.cc | 4 + .../c-c++-common/analyzer/error-pr115724.c | 86 ++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/gcc/analyzer/kf.cc b/gcc/analyzer/kf.cc index c60e220dd1b9..e47786d0bcbf 100644 --- a/gcc/analyzer/kf.cc +++ b/gcc/analyzer/kf.cc @@ -2324,6 +2324,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 index 000000000000..ae606ad89d6a --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/error-pr115724.c @@ -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; +}