Bug 1609876 - gcc 8.1.1 - incorrect compile
Summary: gcc 8.1.1 - incorrect compile
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Fedora
Classification: Fedora
Component: gcc
Version: 28
Hardware: x86_64
OS: Linux
unspecified
unspecified
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2018-07-30 17:27 UTC by Alex Scheel
Modified: 2018-08-03 12:53 UTC (History)
11 users (show)

Fixed In Version:
Clone Of:
Environment:
Last Closed: 2018-07-30 17:31:36 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)
test case (2.49 KB, text/x-csrc)
2018-07-30 17:27 UTC, Alex Scheel
no flags Details

Description Alex Scheel 2018-07-30 17:27:31 UTC
Created attachment 1471601 [details]
test case

Description of problem:

Incorrect compilation of input code.


Consider the paste here (also attached): 

https://paste.fedoraproject.org/paste/H5sSuizGND6RZBp0G4Amfg/raw



Under -O2, this code is compiled incorrectly. In particular, the function sets errno = ERANGE, because (n != INT64_MIN)  -- on line 82, even though the contents of memory show that n == INT64_MIN, that (n*p < 0), and s[-1] == '8'. Hence the if test should fail (as !(true && true) == false) and errno should be zero.

See:

$ gcc -O2 tc.c -o tc && ./tc
errno: 34
out: -9223372036854775808

$ gcc -O1 tc.c -o tc && ./tc
errno: 0
out: -9223372036854775808



This also happens under -O3, and does not happen under -Og or -O0. Clang compiles this code correctly.


Version-Release number of selected component (if applicable):

gcc (GCC) 8.1.1 20180712 (Red Hat 8.1.1-5)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


How reproducible:

Completely, see test case. 


Steps to Reproduce:
1. Download the paste, inspect the code.
2. Compile with -O2 or higher, run the resulting executable.

Actual results:

strto_int64_dec exits with status 34.

Expected results:

strto_int64_dec exists with status 0.


Additional info:

Have not tested on non-Intel architectures.

Comment 1 Florian Weimer 2018-07-30 17:31:36 UTC
$ gcc -fsanitize=undefined -O3 t.c 
$ ./a.out
t.c:70:19: runtime error: signed integer overflow: 9223372036854775800 + 8 cannot be represented in type 'long int'
t.c:75:11: runtime error: signed integer overflow: -9223372036854775808 * -1 cannot be represented in type 'long int'
t.c:82:22: runtime error: signed integer overflow: -9223372036854775808 * -1 cannot be represented in type 'long int'
errno: 0
out: -9223372036854775808
$ gcc -fsanitize=undefined -O3 t.c -fwrapv
$ ./a.out
errno: 0
out: -9223372036854775808

This matches the source code, so there is no compiler bug.

Comment 2 Alex Scheel 2018-08-03 12:53:19 UTC
Thanks Florian! I'll remember -fsanitize=undefined for next time :)


Note You need to log in before you can comment on or make changes to this bug.