public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/51834] New: -Wsequence-point fails when convoluted expressions with multiple side effects are used
@ 2012-01-12  6:29 prasoonsaurav.nit at gmail dot com
  2012-01-12  8:51 ` [Bug c/51834] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: prasoonsaurav.nit at gmail dot com @ 2012-01-12  6:29 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51834

             Bug #: 51834
           Summary: -Wsequence-point fails when convoluted expressions
                    with multiple side effects are used
    Classification: Unclassified
           Product: gcc
           Version: 4.4.6
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: prasoonsaurav.nit@gmail.com


Consider the following example


int main()
{ 
      int i=10;
      i += (i , i++, i) + i; // also invokes UB
}


prasoon@Prasoon:~/test_code$ cat ub.c
int main()
{ 
      int i=10;
      i += (i , i++, i) + i; // also invokes UB
}
prasoon@Prasoon:~/test_code$ gcc -Wsequence-point ub.c
prasoon@Prasoon:~/test_code$ 

I don't get any warning like 'operation on 'i' may be undefined.

Another similar example

int main()
{
    char *str;
    char array[100]= "Hello";
    if((str = array)[0] == 'H'){
         //do something
    }
}

As per my understanding (str = array)[0] also invokes UB but no warning is
given by gcc even after using that option.


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

* [Bug c/51834] -Wsequence-point fails when convoluted expressions with multiple side effects are used
  2012-01-12  6:29 [Bug c/51834] New: -Wsequence-point fails when convoluted expressions with multiple side effects are used prasoonsaurav.nit at gmail dot com
@ 2012-01-12  8:51 ` rguenth at gcc dot gnu.org
  2012-01-12  9:08 ` prasoonsaurav.nit at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-01-12  8:51 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51834

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-01-12 08:51:03 UTC ---
There is no undefined behavior in these testcases.


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

* [Bug c/51834] -Wsequence-point fails when convoluted expressions with multiple side effects are used
  2012-01-12  6:29 [Bug c/51834] New: -Wsequence-point fails when convoluted expressions with multiple side effects are used prasoonsaurav.nit at gmail dot com
  2012-01-12  8:51 ` [Bug c/51834] " rguenth at gcc dot gnu.org
@ 2012-01-12  9:08 ` prasoonsaurav.nit at gmail dot com
  2012-01-12  9:20 ` schwab@linux-m68k.org
  2012-04-19 15:07 ` vincent-gcc at vinc17 dot net
  3 siblings, 0 replies; 5+ messages in thread
From: prasoonsaurav.nit at gmail dot com @ 2012-01-12  9:08 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51834

--- Comment #2 from Prasoon <prasoonsaurav.nit at gmail dot com> 2012-01-12 09:07:52 UTC ---
@Richard Guenther 

Considering the expression i += (i,i++,i) +i;

(i,i++,i) involves change in the value of i, however comma introduces a
sequence point so very roughly considering it equivalent to
func(_some_side_effect_on_i)

we have i += func(_some_side_effect_on_i) + i;

Do you still think the behavior is well defined?

Another C++ example would be ++a = 10; where 'a'is of type int.
This invokes UB but there is no warning from the compiler side even after we
use this flag. Raising a similar bug for C++ because the differ may be
different in C and C++.

Thanks
Prasoon


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

* [Bug c/51834] -Wsequence-point fails when convoluted expressions with multiple side effects are used
  2012-01-12  6:29 [Bug c/51834] New: -Wsequence-point fails when convoluted expressions with multiple side effects are used prasoonsaurav.nit at gmail dot com
  2012-01-12  8:51 ` [Bug c/51834] " rguenth at gcc dot gnu.org
  2012-01-12  9:08 ` prasoonsaurav.nit at gmail dot com
@ 2012-01-12  9:20 ` schwab@linux-m68k.org
  2012-04-19 15:07 ` vincent-gcc at vinc17 dot net
  3 siblings, 0 replies; 5+ messages in thread
From: schwab@linux-m68k.org @ 2012-01-12  9:20 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51834

Andreas Schwab <schwab@linux-m68k.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |NEW
   Last reconfirmed|                            |2012-01-12
         Resolution|INVALID                     |
     Ever Confirmed|0                           |1

--- Comment #3 from Andreas Schwab <schwab@linux-m68k.org> 2012-01-12 09:20:10 UTC ---
(i++, i) + i is undefined.  The sequence point only orders i++ and i inside the
parens, but not the operands of +.  The third example is not undefined.


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

* [Bug c/51834] -Wsequence-point fails when convoluted expressions with multiple side effects are used
  2012-01-12  6:29 [Bug c/51834] New: -Wsequence-point fails when convoluted expressions with multiple side effects are used prasoonsaurav.nit at gmail dot com
                   ` (2 preceding siblings ...)
  2012-01-12  9:20 ` schwab@linux-m68k.org
@ 2012-04-19 15:07 ` vincent-gcc at vinc17 dot net
  3 siblings, 0 replies; 5+ messages in thread
From: vincent-gcc at vinc17 dot net @ 2012-04-19 15:07 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51834

--- Comment #4 from Vincent Lefèvre <vincent-gcc at vinc17 dot net> 2012-04-19 15:06:58 UTC ---
(In reply to comment #3)
> (i++, i) + i is undefined.  The sequence point only orders i++ and i inside the
> parens, but not the operands of +.  The third example is not undefined.

The example is not (i++, i) + i, but (i, i++, i) + i, which is different
because there is a sequence point before and after the i++. Still, there seem
to be disagreements on how to interpret the standard.

There's a discussion "On sequence points and evaluation order" [1] in
comp.std.c in 1995-12 (though that's a bit old), from which there are arguments
to see the above expressions as UB. But "sequence points and evaluation order"
[2] in comp.lang.c in 2006-09 and a message from Keith Thompson [3] in
comp.std.c in 2010-10 both contradict it: they both say something like sin(x) +
cos(x) has defined behavior even if sin() and cos() both modify errno (and that
these functions can be implemented by a macro, as long as it has a sequence
point).

[1]
http://groups.google.com/group/comp.std.c/browse_thread/thread/d133e9c51bef572b/0b6545278c23d37f
[2]
http://groups.google.com/group/comp.lang.c/browse_thread/thread/c4bc836b783b91be/d807a3ad7202b45b
[3] http://groups.google.com/group/comp.std.c/msg/2dc8d2e8a0f4e572

What's strange is that GCC (4.4 to 4.7 at least) complains on

  (i ? (j |= 1, 0) : 0) | (i ? (j |= 1, 0) : 0);

but not on

  (j |= 1, 0) | (j |= 1, 0);

Contrary to GCC, I would say that the latter is UB (because from the root of
the expression, one can evaluate both j |= 1 without getting a sequence point
yet -- GCC should have output a warning, and that's bug 51562), but not the
former (similar to the errno case).

Here's a simple testcase I've used, with more tests:

int i, j;

static inline int set_flag (void)
{
  j |= 1;
  return 0;
}

#define FOO (i ? (j |= 1, 0) : 0)
#define BAR (i ? set_flag () : 0)

void fct (void)
{
  FOO || FOO;
  FOO | FOO;
  BAR | BAR;
  set_flag () + set_flag ();
  j = (++i, j) + (j, ++i);
  return;
}

GCC 4.7.0 warns only for "FOO | FOO;" (and I think that's incorrect, as said
above).


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

end of thread, other threads:[~2012-04-19 15:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-12  6:29 [Bug c/51834] New: -Wsequence-point fails when convoluted expressions with multiple side effects are used prasoonsaurav.nit at gmail dot com
2012-01-12  8:51 ` [Bug c/51834] " rguenth at gcc dot gnu.org
2012-01-12  9:08 ` prasoonsaurav.nit at gmail dot com
2012-01-12  9:20 ` schwab@linux-m68k.org
2012-04-19 15:07 ` vincent-gcc at vinc17 dot net

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).