cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[SUGGESTION] Add runtime checks for C++ integer modulo by zero

Open Xeverous opened this issue 7 months ago • 1 comments

This is #1184 but for modulo... sounds like it was forgotten that you can not mod by zero either.

There is also a case of division and modulo of std::numeric_limits<T>::min() / -1 which is UB aswell. Not sure if you want to check it too - it requires to check both operands.

Xeverous avatar Jun 02 '25 12:06 Xeverous

Reproduction (cppfront built from 5aa32aef7c74679994d6da39e6d0cf9b9714e1ee):

div: (x: int, y: int) -> int = { return x / y; }
mod: (x: int, y: int) -> int = { return x % y; }

Result:

[[nodiscard]] auto div(cpp2::impl::in<int> x, cpp2::impl::in<int> y) -> int{return x / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(x),y); }
[[nodiscard]] auto mod(cpp2::impl::in<int> x, cpp2::impl::in<int> y) -> int{return x % y; }

Xeverous avatar Jun 02 '25 12:06 Xeverous