berry-lang / berry

A ultra-lightweight embedded scripting language optimized for microcontrollers.
https://berry-lang.github.io
MIT License
782 stars 95 forks source link

fix: raise exception when doing modulo ('%') by zero, instead of crashing with SIGFPE #417

Closed alufers closed 2 months ago

alufers commented 2 months ago

Makes the behavior consistent with division ('/' operator), prevents full VM crash. Similar behavior is observed in Python (v. 3.11.8).

Additionally I have moved the actual float modulo and division into the else branch of the check, to prevent UB even when the division by zero was caught.

Added testcase to check this behavior.

s-hadinger commented 2 months ago

Awesome, thanks. I never realized that 1%0 causes a crash of the embedded system.

s-hadinger commented 2 months ago

Additionally I have moved the actual float modulo and division into the else branch of the check, to prevent UB even when the division by zero was caught.

This is not strictly necessary but does not harm indeed. vm_error(vm, "divzero_error", "division by zero"); never returns.