From 80cfb9cce74c106c2efd9800033f7f169cd59a02 Mon Sep 17 00:00:00 2001 From: sohailshk <122166523+sohailshk@users.noreply.github.com> Date: Fri, 13 Nov 2026 01:45:01 +0530 Subject: [PATCH] Reject invalid numeric command line values instead of wrapping or throwing ArgsBase parsed every integer argument with std::stoi and every float with std::stof, and then always reported success. Consequences: - A negative value for a size_t argument (--top_k=-1, --seq_len=-1) wrapped to a huge value. - Trailing garbage was ignored (--max_generated_tokens=10x parsed as 10). - Non-numbers or values outside int range (--seq_len=3000000000 for a size_t) threw an uncaught std::exception, so the binary terminated without the existing 'Invalid value for' message. Parse with strtoll/strtoull/strtof, require the whole string to be consumed, reject out-of-range values for the target type (including negatives for unsigned types), and return false so ParseVisitor aborts with 'Invalid value for '. Larger size_t values are now accepted. --- gemma/gemma_args_test.cc | 40 ++++++++++++++++++++++++++++++++++++++++ util/args.h | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/gemma/gemma_args_test.cc b/gemma/gemma_args_test.cc index d9ee8b71..1328c9e3 100644 --- a/gemma/gemma_args_test.cc +++ b/gemma/gemma_args_test.cc @@ -71,4 +71,44 @@ TEST(GemmaArgsTest, UnconsumedArgs) { CheckUnconsumed({"gemma", "--UNDEFINED", "--weights=x", "--XXX"}, 1); } +// Parses `args` (including argv[0]) into `InferenceArgs`. +static InferenceArgs ParseInferenceArgs(const std::vector& args) { + std::vector ptrs; + FillPtrs(args, ptrs); + const int argc = static_cast(args.size()); + char** argv = const_cast(ptrs.data()); + ConsumedArgs consumed(argc, argv); + InferenceArgs inference(argc, argv, consumed); + consumed.ClearUnconsumed(); + return inference; +} + +TEST(GemmaArgsTest, ParsesValidNumbers) { + const InferenceArgs a = + ParseInferenceArgs({"gemma", "--top_k=40", "--temperature", "0.5", + "--verbosity=-1", "--seq_len=3000000000"}); + EXPECT_EQ(a.top_k, size_t{40}); + EXPECT_FLOAT_EQ(a.temperature, 0.5f); + EXPECT_EQ(a.verbosity, -1); + EXPECT_EQ(a.seq_len, size_t{3000000000}); +} + +// Invalid numeric values must produce the "Invalid value" abort message rather +// than being silently accepted (negative values for unsigned args wrapped to +// SIZE_MAX, trailing garbage was ignored) or escaping as an uncaught exception. +TEST(GemmaArgsDeathTest, RejectsInvalidNumbers) { + EXPECT_DEATH(ParseInferenceArgs({"gemma", "--top_k=-1"}), + "Invalid value for top_k"); + EXPECT_DEATH(ParseInferenceArgs({"gemma", "--top_k", "abc"}), + "Invalid value for top_k"); + EXPECT_DEATH(ParseInferenceArgs({"gemma", "--max_generated_tokens=10x"}), + "Invalid value for max_generated_tokens"); + EXPECT_DEATH(ParseInferenceArgs({"gemma", "--verbosity=3000000000"}), + "Invalid value for verbosity"); + EXPECT_DEATH(ParseInferenceArgs({"gemma", "--temperature=1.0x"}), + "Invalid value for temperature"); + EXPECT_DEATH(ParseInferenceArgs({"gemma", "--temperature="}), + "Invalid value for temperature"); +} + } // namespace gcpp diff --git a/util/args.h b/util/args.h index 7fab492a..8f8fd41e 100644 --- a/util/args.h +++ b/util/args.h @@ -18,10 +18,16 @@ #ifndef THIRD_PARTY_GEMMA_CPP_UTIL_ARGS_H_ #define THIRD_PARTY_GEMMA_CPP_UTIL_ARGS_H_ +#include +#include // isinf #include +#include // strtoll, strtoull, strtof +#include // strchr #include // std::transform +#include #include +#include #include #include "io/io.h" // Path @@ -179,15 +185,44 @@ class ArgsBase { private: // Returns false if an invalid value is detected. + // Rejects empty strings, trailing characters, values outside the range of + // `T` (including negative values for unsigned `T`) instead of silently + // wrapping or throwing. template static bool SetValue(const char* string, T& t) { - t = std::stoi(string); + // `strtoull` accepts a leading '-' and wraps, so reject it up front. + if (std::is_unsigned_v && strchr(string, '-')) return false; + + errno = 0; + char* end = nullptr; + if constexpr (std::is_unsigned_v) { + const unsigned long long value = strtoull(string, &end, 10); + if (end == string || *end != '\0' || errno == ERANGE || + value > static_cast( + std::numeric_limits::max())) { + return false; + } + t = static_cast(value); + } else { + const long long value = strtoll(string, &end, 10); + if (end == string || *end != '\0' || errno == ERANGE || + value < static_cast(std::numeric_limits::min()) || + value > static_cast(std::numeric_limits::max())) { + return false; + } + t = static_cast(value); + } return true; } template static bool SetValue(const char* string, T& t) { - t = std::stof(string); + errno = 0; + char* end = nullptr; + const float value = strtof(string, &end); + if (end == string || *end != '\0') return false; + if (errno == ERANGE && isinf(value)) return false; // overflow + t = static_cast(value); return true; }