public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/97398] New: Enhancement request: Warning when multiply assigning to same struct field
@ 2020-10-13  7:30 Ulrich.Windl at rz dot uni-regensburg.de
  2020-10-13  9:00 ` [Bug c/97398] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Ulrich.Windl at rz dot uni-regensburg.de @ 2020-10-13  7:30 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-12-30  2:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-13  7:30 [Bug c/97398] New: Enhancement request: Warning when multiply assigning to same struct field Ulrich.Windl at rz dot uni-regensburg.de
2020-10-13  9:00 ` [Bug c/97398] " rguenth at gcc dot gnu.org
2020-10-15 20:06 ` egallager at gcc dot gnu.org
2020-10-16  6:53 ` Ulrich.Windl at rz dot uni-regensburg.de
2022-12-30  2:57 ` egallager at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).