public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/59961] New: Use of size_t in leading term of computation with subtraction
@ 2014-01-27 18:36 olaf at robots dot ox.ac.uk
  2014-01-27 19:03 ` [Bug c/59961] " schwab@linux-m68k.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: olaf at robots dot ox.ac.uk @ 2014-01-27 18:36 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 59961
           Summary: Use of size_t in leading term of computation with
                    subtraction
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: olaf at robots dot ox.ac.uk

The minimalistic example further down produces a very unexpected result and no
warnings at all. I understand that size_t s1 can not be negative. However, in
my example using "-s1*..." in the leading term of the computation therefore
seems to cause an overflow.

Quite curiously, a similar bug is caused when using "unsigned int" instead of
size_t, but no bugs are caused if using "unsigned short" or "unsigned char".
The bug also does not appear if the + and - terms are swapped. All of this is
on an x86_64 platform.

Even if may be correct in a very obscure way according to some C specification,
it would be very helpful to issue a warning with -Wall -Wextra



/* compile with gcc -Wall -Wextra testcase.c */

#include <stdio.h>

int main(int ac, char**dc)
{
        ac = ac; dc = dc;

        size_t s1 = 100;
        size_t s2 = 200;
        double d1 = 0.123;
        double d2 = 0.234;

        double result = -s1*d1 + s2*d2;

        printf("%lf %lf\n", result, -100*0.123+200*0.234);

        return 0;
}


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

* [Bug c/59961] Use of size_t in leading term of computation with subtraction
  2014-01-27 18:36 [Bug c/59961] New: Use of size_t in leading term of computation with subtraction olaf at robots dot ox.ac.uk
@ 2014-01-27 19:03 ` schwab@linux-m68k.org
  2014-01-27 20:45 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: schwab@linux-m68k.org @ 2014-01-27 19:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andreas Schwab <schwab@linux-m68k.org> ---
-100 is not the same as -s1.  The latter is SIZE_MAX + 1 - 100, a big number,
depending on the size of size_t.  If s1 is declared as unsigned short or
unsigned char, it will be promoted to int before any futher operation is
applied.


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

* [Bug c/59961] Use of size_t in leading term of computation with subtraction
  2014-01-27 18:36 [Bug c/59961] New: Use of size_t in leading term of computation with subtraction olaf at robots dot ox.ac.uk
  2014-01-27 19:03 ` [Bug c/59961] " schwab@linux-m68k.org
@ 2014-01-27 20:45 ` jakub at gcc dot gnu.org
  2014-01-27 22:24 ` olaf at robots dot ox.ac.uk
  2014-01-27 22:30 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2014-01-27 20:45 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |jakub at gcc dot gnu.org
         Resolution|---                         |INVALID

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I don't think there is anything that should be warned about, there is nothing
surprising or unexpected on unsigned negation.


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

* [Bug c/59961] Use of size_t in leading term of computation with subtraction
  2014-01-27 18:36 [Bug c/59961] New: Use of size_t in leading term of computation with subtraction olaf at robots dot ox.ac.uk
  2014-01-27 19:03 ` [Bug c/59961] " schwab@linux-m68k.org
  2014-01-27 20:45 ` jakub at gcc dot gnu.org
@ 2014-01-27 22:24 ` olaf at robots dot ox.ac.uk
  2014-01-27 22:30 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: olaf at robots dot ox.ac.uk @ 2014-01-27 22:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Olaf <olaf at robots dot ox.ac.uk> ---
Well... naive me found it quite surprising and unexpected that the following
two lines produce grossly different results:

double result1 = -s1*d1 + s2*d2;
double result2 =  s2*d2 - s1*d1;

...but never mind.


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

* [Bug c/59961] Use of size_t in leading term of computation with subtraction
  2014-01-27 18:36 [Bug c/59961] New: Use of size_t in leading term of computation with subtraction olaf at robots dot ox.ac.uk
                   ` (2 preceding siblings ...)
  2014-01-27 22:24 ` olaf at robots dot ox.ac.uk
@ 2014-01-27 22:30 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-01-27 22:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Olaf from comment #3)
> Well... naive me found it quite surprising and unexpected that the following
> two lines produce grossly different results:
> 
> double result1 = -s1*d1 + s2*d2;
> double result2 =  s2*d2 - s1*d1;

-s1*d1 + s2*d2; is the same as (-s1)*d1 + s2*d2 that is the unary minus binds
to the left and has a higher precedence than the plus operator.


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

end of thread, other threads:[~2014-01-27 22:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-27 18:36 [Bug c/59961] New: Use of size_t in leading term of computation with subtraction olaf at robots dot ox.ac.uk
2014-01-27 19:03 ` [Bug c/59961] " schwab@linux-m68k.org
2014-01-27 20:45 ` jakub at gcc dot gnu.org
2014-01-27 22:24 ` olaf at robots dot ox.ac.uk
2014-01-27 22:30 ` pinskia 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).