From 13e073d9ffddded1ca418f24e91fca01da13a2b4 Mon Sep 17 00:00:00 2001 From: xirvik Date: Mon, 17 Aug 2026 15:19:31 +0000 Subject: [PATCH] Reject division overflow in math.div and math.mod. Dividing the smallest integer by -1 traps and takes the process with it. --- src/command_ui.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/command_ui.cc b/src/command_ui.cc index f6222ac2..2658e00c 100644 --- a/src/command_ui.cc +++ b/src/command_ui.cc @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -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::min() && rhs == -1) + throw torrent::input_error(std::string(name) + ": Division overflow!"); + } val = itr == args.begin() ? rhs : op(val, rhs);