wduquette / molt

Embeddable TCL Interpreter for Rust applications
BSD 3-Clause "New" or "Revised" License
103 stars 12 forks source link

expr command should check for integer overflow. #24

Closed wduquette closed 5 years ago

wduquette commented 5 years ago

TCL 7.6 uses the standard C library's matherr() feature to detect integer and floating point errors. This doesn't exist in Rust; consequently, the Molt expr implementation just does addition, subtraction, etc., and doesn't currently worry about it.

The Rust philosophy, as I understand it, is to check for these kinds of errors and panic in development, and recover (i.e., by wrapping integers around) in operations. Consequently, such errors will cause the Molt interpreter to crash or silently fail, which isn't ideal.

However, the Rust integer types also provide a set of "checked" numeric operations that return Option on overflow or divide by zero. They don't explicitly indicate the error; but for integers, it's either divide by zero or overflow, and we already check for divide by zero.

wduquette commented 5 years ago

This is easy to fix for multiplication, addition, and subtraction; the fix for division and remainder will have to wait until I've tracked down the cause of Issue #27.

wduquette commented 5 years ago

Commit 4382553 fixed this; or at least integer division and remainder now uses the i64::checked_div and i64::checked_rem methods. However, it isn't clear how to generate such an overflow.