Reject division overflow in math.div and math.mod.

Dividing the smallest integer by -1 traps and takes the process with it.
This commit is contained in:
xirvik
2026-08-17 15:19:31 +00:00
committed by Jari Sundell
parent d73f245692
commit 13e073d9ff
+8 -2
View File
@@ -3,6 +3,7 @@
#include <sys/types.h>
#include <ctime>
#include <limits>
#include <regex>
#include <torrent/utils/log.h>
@@ -674,8 +675,13 @@ apply_math_basic(const char* name, Comp op, const torrent::Object::list_type& ar
throw torrent::input_error(std::string(name) + ": Wrong argument type");
}
if (divides && !rhs && itr != args.begin())
throw torrent::input_error(std::string(name) + ": Division by zero!");
if (divides && itr != args.begin()) {
if (rhs == 0)
throw torrent::input_error(std::string(name) + ": Division by zero!");
if (val == std::numeric_limits<int64_t>::min() && rhs == -1)
throw torrent::input_error(std::string(name) + ": Division overflow!");
}
val = itr == args.begin() ? rhs : op(val, rhs);