[Bug c/126757] New: please diagnose invalid 'const' attribute on functions
bruno at clisp dot org via Gcc-bugs <[email protected]>
| Newsgroups | gmane.comp.gcc.bugs |
|---|---|
| Message-ID | <[email protected]/bugzilla/> |
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126757
Bug ID: 126757
Summary: please diagnose invalid 'const' attribute on functions
Product: gcc
Version: 16.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: bruno at clisp dot org
Target Milestone: ---
As GCC makes more and more optimizations, 'const' attributes on
functions that are in fact not const (as defined in
https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html)
can lead to undefined behaviour. See e.g.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78463
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119041
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125916
https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/42361
https://gitlab.gnome.org/GNOME/glib/-/work_items/3984
It would therefore be useful to diagnose common cases of such
invalid 'const' attributes. (I'm aware that it cannot diagnose
all such cases, because that would be equivalent to solving the
halting problem.)
How to reproduce:
1. Save this file as foo.c.
================================================================
double phi (void) __attribute__ ((__const__));
double phi (void)
{
static int counter;
return (++counter) * 1.618033988749894848;
}
#include <stdio.h>
int main ()
{
for (int repeat = 10; repeat > 0; repeat--)
printf ("%g\n", phi ());
}
================================================================
2.
$ gcc -Wall -O2 foo.c
<no warning>