Stop the value command on the untrusted-safe allowlist crashing the daemon

glibc leaves endptr unset for a small invalid base; ERANGE throws instead of clamping.
This commit is contained in:
xirvik
2026-09-19 20:02:11 +00:00
committed by Jari Sundell
parent c59da0848e
commit 7916e6022f
3 changed files with 43 additions and 2 deletions
+12 -2
View File
@@ -2,6 +2,7 @@
#include <sys/types.h>
#include <cerrno>
#include <ctime>
#include <limits>
#include <regex>
@@ -126,11 +127,20 @@ apply_value([[maybe_unused]] rpc::target_type target, const torrent::Object::lis
if (args.front().is_value()) {
val = args.front().as_value();
} else {
int base = args.size() > 1 ? args.back().is_value() ?
args.back().as_value() : strtol(args.back().as_string().c_str(), NULL, 10) : 10;
int64_t base = args.size() > 1 ? args.back().is_value() ?
args.back().as_value() : strtoll(args.back().as_string().c_str(), NULL, 10) : 10;
if (base != 0 && (base < 2 || base > 36))
throw torrent::input_error("'value' base must be 0 or between 2 and 36!");
char* endptr = 0;
errno = 0;
val = strtoll(args.front().as_string().c_str(), &endptr, base);
if (errno == ERANGE)
throw torrent::input_error("Number out of range: " + args.front().as_string());
while (*endptr == ' ' || *endptr == '\n') ++endptr;
if (*endptr)
throw torrent::input_error("Junk at end of number: " + args.front().as_string());