https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97398

            Bug ID: 97398
           Summary: Enhancement request: Warning when multiply assigning
                    to same struct field
           Product: gcc
           Version: 4.8.5
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Ulrich.Windl at rz dot uni-regensburg.de
  Target Milestone: ---

Created attachment 49360
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49360&action=edit
Simple test case that doesn't trigger any warning with gcc 7.5.0 and "-O -Wall"

After having found some minor mistake in my C code for "gcc -O2 -Wall" (gcc
4.8.5), I verified that gcc 7.5.0 reacts similar in a test case.

I had (re-)initialized several fields of a structure in a function that got the
pointer to the struct.

The code looks like this (S is the pointer to the struct, and the real code is
way more complex):

s->a = s->b = s->c;
s->d = s->e = s->c;
...
s->f = s->e = s->c
...

So s->e is set to s->c multiple times, and gcc does not warn about that. In
this case s->c is even set to the same value.

To help avoiding this type of mistake, I suggest that gcc should warn when a
structure field is set more then once without reading the value before
re-setting it.

Note: In the simple test program gcc optimizes away everything, but I still
think that it could warn about the mistake.  At least you should get the idea.
  • [Bug c/97398] New: E... Ulrich.Windl at rz dot uni-regensburg.de via Gcc-bugs

Reply via email to