gcc assumes that functions defined with the pure attribute have no side effects and do not modify program state, including through pointers passed to such functions as arguments. The function below does modify the object pointed to by its argument. It would be useful if gcc issued a diagnostic for this and other similar violations of the "pure" assumption, analogously to how it diagnoses noreturn functions that do return. $ cat -n t.C && g++ -c -O2 -Wall -W t.C 1 int f0 () __attribute ((noreturn)); 2 int f0 () { return 0; } 3 4 int f1 (int*) __attribute ((pure)); 5 int f1 (int *i) 6 { 7 *i = 0; 8 return 0; 9 } 10 t.C: In function ‘int f0()’: t.C:2: warning: function declared ‘noreturn’ has a ‘return’ statement t.C:2: warning: ‘noreturn’ function does return
Similarly, functions declared with the const attribute such as f1() in the test case below that violate the compiler's assumptions should be diagnosed: $ cat -n t.C && g++ -c -O2 -Wall -W t.C 1 extern int i; 2 int f1 () __attribute ((const)); 3 int f1 () 4 { 5 return i; 6 } 7
Confirmed.
*** This bug has been marked as a duplicate of 18487 ***