Bug 833220

Summary: Optimization changes behaviour of division by zero
Product: [Fedora] Fedora Reporter: r3obh <Robert.Harley>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: low Docs Contact:
Priority: unspecified    
Version: 17CC: jakub, law
Target Milestone: ---   
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2012-06-18 22:07:14 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description r3obh 2012-06-18 22:05:32 UTC
Description of problem:
Compiling with -O produces a different result:

$ cat bla.c
#include <stdio.h>

int main() {

  printf( "%d\n", 0 / 0 );

  return 0;
}
$ gcc bla.c
bla.c: In function 'main':
bla.c:5:21: warning: division by zero [-Wdiv-by-zero]
$ ./a.out 
Floating point exception
$ gcc -O bla.c
bla.c: In function 'main':
bla.c:5:21: warning: division by zero [-Wdiv-by-zero]
$ ./a.out 
0

Version-Release number of selected component (if applicable):
gcc version 4.7.0 20120507 (Red Hat 4.7.0-5) (GCC) 

How reproducible:
Always.

Comment 1 Jakub Jelinek 2012-06-18 22:07:14 UTC
The testcase is invoking undefined behavior, therefore it is invalid and anything can happen.

Comment 2 r3obh 2012-06-19 15:41:17 UTC
The behaviour in this case is left up to the implementation.  A good implementation should throw an exception at runtime.