[Bug c/126758] New: please diagnose invalid 'pure' 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=126758
Bug ID: 126758
Summary: please diagnose invalid 'pure' 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, 'pure' attributes on
functions that are in fact not pure (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=109914
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115237
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119041
https://lists.gnu.org/archive/html/bug-gettext/2026-08/msg00000.html
It would therefore be useful to diagnose common cases of such
invalid 'pure' 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.
================================================================
int lookup (int key, int *value) __attribute__ ((__pure__));
int lookup (int key, int *value)
{
if (key == 42)
{
*value = 47;
return 0;
}
return -1;
}
#include <stdio.h>
int main ()
{
int misses = 0;
for (int repeat = 10; repeat > 0; repeat--)
{
int value = 0;
misses += (lookup (42, &value) < 0);
printf ("%d\n", value);
}
printf ("misses = %d\n", misses);
}
================================================================
2.
$ gcc -Wall -O2 foo.c
<no warning>