fix: harden arithmetic, string ops, and lexer error handling

- S4: add isfinite checks after all arithmetic ops (Add, Sub, Mul, Div,
  Pow, Neg) so NaN/Infinity can never enter JSON output
- S5: fix is_integral_number INT64_MAX boundary check to eliminate
  undefined behaviour from double rounding
- S6: cap string concatenation result at MAX_STRING_SIZE (1MB)
- S7: add optional max_source_length parameter to test/eval_val/eval_true
- S8: catch std::out_of_range in lexNumber() and report a descriptive
  ParseError instead of a bare 'stod' message
This commit is contained in:
2026-08-20 14:00:17 +03:00
parent 9d5571b7d2
commit cda2e9daa7
5 changed files with 121 additions and 16 deletions

View File

@@ -7,8 +7,11 @@
namespace mbs {
TestResult test(std::string_view source) {
TestResult test(std::string_view source, std::size_t max_source_length) {
try {
if (max_source_length > 0 && source.size() > max_source_length) {
return TestResult{false, "source exceeds maximum length", std::nullopt};
}
const auto ast = parse(source);
if (const auto err = semantic_check(*ast); err.has_value()) {
return TestResult{false, *err, std::nullopt};
@@ -22,8 +25,12 @@ TestResult test(std::string_view source) {
}
}
EvalValueResult eval_val(const std::string_view source, const nlohmann::json& context) {
EvalValueResult eval_val(const std::string_view source, const nlohmann::json& context,
std::size_t max_source_length) {
try {
if (max_source_length > 0 && source.size() > max_source_length) {
return EvalErr{"source exceeds maximum length", std::nullopt};
}
const auto ast = parse(source);
if (const auto err = semantic_check(*ast); err.has_value()) {
return EvalErr{*err, std::nullopt};
@@ -37,8 +44,9 @@ EvalValueResult eval_val(const std::string_view source, const nlohmann::json& co
}
}
EvalBoolResult eval_true(const std::string_view source, const nlohmann::json& context) {
const auto r = eval_val(source, context);
EvalBoolResult eval_true(const std::string_view source, const nlohmann::json& context,
std::size_t max_source_length) {
const auto r = eval_val(source, context, max_source_length);
// Check if it resulted in an error
if (std::holds_alternative<EvalErr>(r)) {