[Bug c/107010] iterate section("mysection") struct mystruct fails if first node is const and compiling with O2 or greater
pinskia at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Sep 22 19:18:12 GMT 2022
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107010
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
See Also| |https://gcc.gnu.org/bugzill
| |a/show_bug.cgi?id=77964
Resolution|--- |INVALID
Status|UNCONFIRMED |RESOLVED
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
That is because GCC knows that first+1 does not point to a valid pointer
really.
You need to hide the assignment of iterator from GCC.
This will fix the issue:
const struct linkinfostruct *iterator = &first;
asm("":"+r"(iterator));
while (iterator->name)
{
fprintf (stderr, "name=%s\n", iterator->name);
iterator++;
fprintf (stderr, " next iteration should be (iterator=%p,
&iterator->name=%p name=%p \"%s\"\n", iterator, &iterator->name,
iterator->name, iterator->name);
}
See PR 77964 and PR 106939 also.
More information about the Gcc-bugs
mailing list