Skip to content

Issue 5908 - Optimizer generates wrong value with divide-by-zero.#3810

Merged
WalterBright merged 1 commit intodlang:masterfrom
yebblies:issue5908
Nov 13, 2014
Merged

Issue 5908 - Optimizer generates wrong value with divide-by-zero.#3810
WalterBright merged 1 commit intodlang:masterfrom
yebblies:issue5908

Conversation

@yebblies
Copy link
Contributor

Reinstate optimizer error for integer division by zero

https://issues.dlang.org/show_bug.cgi?id=5908

@dnadlinger
Copy link
Contributor

Huh – shouldn't this produce a runtime FP exception?

@yebblies
Copy link
Contributor Author

It does if it can't catch it at compile time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary since OPremquo is only for integral operands. You can document this by inserting assert(!tyfloating(tym)) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Updated.

@WalterBright
Copy link
Member

Integer divide by zero should not raise a FP exception.

There are also issues with the following code:

int b = 0;
...
if (condition that is false when b is 0 but the optimizer doesn't know that)
    x = a / b;

The optimizer will, because of constant propagation, see a/0 and issue a bogus error message.

@yebblies
Copy link
Contributor Author

There are also issues with the following code:

I'm not so sure about that example. b is a constant, so I think the division by zero is always a bug, whether it's guarded by an if or not.

We have the same issue with null dereference. It can easily be worked around by adding && b to the if condition.

Reinstate optimizer error for integer division by zero
@WalterBright
Copy link
Member

I'm not so sure about that example. b is a constant, so I think the division by zero is always a bug, whether it's guarded by an if or not. We have the same issue with null dereference. It can easily be worked around by adding && b to the if condition.

That's right, we do have the same issue with null dereference being detected by the optimizer using data flow analysis, and I had to disable the error message because real code would trip the error even though the code logic ensured it would never happen. Here's another example:

if (condition)
    b = 0;
else
    b = 1;
...
if (!condition)
    a = c / b;   // uh-oh! b could be 0!

The optimizer sees a path where b could be 0, and would issue the error. But that path would never happen.

@yebblies
Copy link
Contributor Author

The optimizer sees a path where b could be 0, and would issue the error. But that path would never happen.

This error doesn't appear when b could be 0, only when it must be zero. My understanding is it's conservative.

@ghost
Copy link

ghost commented Aug 3, 2014

@WalterBright: Feedback on the previous comment?

@WalterBright
Copy link
Member

Auto-merge toggled on

@WalterBright
Copy link
Member

Thought we'd give it a try and see what happens.

@yebblies
Copy link
Contributor Author

Fingers crossed!

WalterBright added a commit that referenced this pull request Nov 13, 2014
Issue 5908 - Optimizer generates wrong value with divide-by-zero.
@WalterBright WalterBright merged commit ec66c97 into dlang:master Nov 13, 2014
@yebblies yebblies deleted the issue5908 branch November 13, 2014 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants