ethereum / solidity

Solidity, the Smart Contract Programming Language

Home Page:https://soliditylang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proposal: Throw on division by zero

chriseth opened this issue · comments

Division by zero currently behaves exactly like division by zero on the EVM: It results in zero.

This could be changed into throwing an exception, just like array access out of bounds and other cases.

Do we want to break backwards compatibility to add this feature?

The check will of course be optimized away for division by constants. An improved optimizer (it cannot do it yet) could also optimize it away if the developer checked for zero just prior to the division. The downside of the comparison is also that block-level optimization cannot be done anymore.

Yes, absolutely. Most languages define division by zero as NaN or throw an exception/panic/halt; we should do the same rather than silently returning an incorrect answer. Ideally a hard fork will fix this in the EVM, but in the meantime Solidity should definitely add this check itself, just like it checks array bounds.

On the object-level, I support this change. Failing silently is the worst possible thing to do from a safety perspective.

On the meta-level, I am fine with backwards-compatibility breaking changes in Solidity, especially since I think the language will evolve over the next six months as a result of the community discovering incremental changes to make the risk of bugs much smaller. One possible way to make changes like this graceful is to put a hashbang tag into the file, or some similar parameter, specifying a version.

Got here from the ethereum wiki. The issue is closed, so this means that division by zero is now "safe"?

@leonprou seems so: #888