diff options
author | jhahn <mail.jhahn@gmail.com> | 2022-11-09T23·47+0000 |
---|---|---|
committer | jrhahn <mail.jhahn@gmail.com> | 2022-11-10T13·12+0000 |
commit | d00030128e465c8351412b1aefb4260681d8b497 (patch) | |
tree | 0c546e16df84ef098c7eb31322736de3e51c12a1 /tvix/eval/src/vm.rs | |
parent | 40826e664de6c5d243ced77fd6662b004b8fef49 (diff) |
feat(tvix/eval): detect division by zero r/5276
This detects if the second argument of a division is a zero (either as integer or as float). If so, an error message is displayed. This fixes b/219. Change-Id: I50203d14a71482bc757832a2c8dee08eb7d35c49 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7258 Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/eval/src/vm.rs')
-rw-r--r-- | tvix/eval/src/vm.rs | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs index 8c45ee7363e1..127d9bfa513c 100644 --- a/tvix/eval/src/vm.rs +++ b/tvix/eval/src/vm.rs @@ -412,7 +412,20 @@ impl<'o> VM<'o> { OpCode::OpSub => arithmetic_op!(self, -), OpCode::OpMul => arithmetic_op!(self, *), - OpCode::OpDiv => arithmetic_op!(self, /), + OpCode::OpDiv => { + let b = self.peek(0); + + match b { + Value::Integer(0) => return Err(self.error(ErrorKind::DivisionByZero)), + Value::Float(b) => { + if *b == (0.0 as f64) { + return Err(self.error(ErrorKind::DivisionByZero)); + } + arithmetic_op!(self, /) + } + _ => arithmetic_op!(self, /), + }; + } OpCode::OpInvert => { let v = fallible!(self, self.pop().as_bool()); |