Skip to content

apparent bad interaction between math and short-circuit #3726

@regehr

Description

@regehr
Bugzilla Link 3354
Resolution FIXED
Resolved on Jan 19, 2009 19:16
Version unspecified
OS Linux
Attachments testcase
CC @lattner,@efriedma-quic

Extended Description

The attached program displays this behavior on x86 Linux:

[regehr@babel tmp30]$ llvm-gcc -O small.c -o small
small.c: In function ‘main’:
small.c:19: warning: division by zero
small.c:19: warning: division by zero
[regehr@babel tmp30]$ ./small
x=1
Floating point exception

The thing is, the divide-by-zero behaviors are all protected by short-circuiting operations, and should never fire. I claim that LLVM's -O0 behavior is what must occur at all optimization levels:

[regehr@babel tmp30]$ llvm-gcc -O0 small.c -o small
small.c: In function ‘main’:
small.c:19: warning: division by zero
small.c:19: warning: division by zero
[regehr@babel tmp30]$ ./small
x=1
x=0

The macro in the attached code is simply a macroized version of the 2nd safe multiplication fragment here:

https://www.securecoding.cert.org/confluence/display/cplusplus/INT32-CPP.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzilla

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions