https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85734
Bug ID: 85734 Summary: --suggest-attribute=malloc misdiagnoses static functions Product: gcc Version: 8.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: eggert at gnu dot org Target Milestone: --- GCC 8's --suggest-attribute=malloc diagnoses static functions, even though the malloc attribute is useless for static functions (after all, the compiler has deduced the property on its own). This is leading to my having to litter code with '__attribute__ (malloc)' declarations merely to pacify GCC. GCC should treat the malloc attribute like other attributes (e.g., const), and should issue the diagnostic only for non-static functions where the attribute is in fact useful. This bug report differs from GCC bug 85562, in that this bug is about false alarms whereas bug 85562 is about the wording of the diagnostics. Here is an illustration of the bug. For this program: #include <stdlib.h> void *pe (void *x) { return x; } static void *ps (void *x) { return x; } void *me (size_t n) { return malloc (n); } static void *ms (size_t n) { return malloc (n); } int main (void) { return !pe (me (1)) + !ps (ms (2)); } gcc (GCC) 8.1.1 20180502 (Red Hat 8.1.1-1) x86-64, with the command: gcc -Wsuggest-attribute=malloc -Wsuggest-attribute=const -O2 -S example.c outputs the diagnostics at the end of this bug report. GCC correctly diagnoses 'pe' amd 'me', which are extern. GCC is correctly silent for the pure function 'ps', which is static. However, GCC mistakenly diagnoses 'ms', which is also static. example.c: In function ‘ms’: example.c:5:14: warning: function might be candidate for attribute ‘malloc’ if it is known to return normally [-Wsuggest-attribute=malloc] static void *ms (size_t n) { return malloc (n); } ^~ example.c: In function ‘pe’: example.c:2:14: warning: function might be candidate for attribute ‘const’ [-Wsuggest-attribute=const] void *pe (void *x) { return x; } ^~ example.c: In function ‘me’: example.c:4:14: warning: function might be candidate for attribute ‘malloc’ if it is known to return normally [-Wsuggest-attribute=malloc] void *me (size_t n) { return malloc (n); } ^~