fix(mcp): reject integer arguments outside int range

Read signed and unsigned yyjson integers into int64_t or uint64_t in cbm_mcp_get_int_arg at src/mcp/mcp.c before converting to int. Values below INT_MIN or above INT_MAX now return the caller default instead of wrapping; this prevents an oversized auto_dep_limit such as 4294967297 from narrowing to 1 before dependency-limit validation.

tests/test_mcp.c covers positive and negative 64-bit overflow inputs. Verified: CBM_ONLY_SUITE=mcp make -f Makefile.cbm test: 273 passed; git diff --check passed.

Signed-off-by: Andrew Hundt <ATHundt@gmail.com>
This commit is contained in:
Andrew Hundt
2026-07-22 17:06:11 -04:00
parent 8d0816db67
commit d030c8f4e1
2 changed files with 15 additions and 2 deletions
+11 -2
View File
@@ -84,6 +84,7 @@ enum {
#include <fcntl.h>
#endif
#include <yyjson/yyjson.h>
#include <limits.h> // INT_MIN, INT_MAX
#include <stdint.h> // int64_t
#include <stdio.h>
#include <stdlib.h>
@@ -2223,8 +2224,16 @@ int cbm_mcp_get_int_arg(const char *args_json, const char *key, int default_val)
yyjson_val *root = yyjson_doc_get_root(doc);
yyjson_val *val = yyjson_obj_get(root, key);
int result = default_val;
if (val && yyjson_is_int(val)) {
result = yyjson_get_int(val);
if (val && yyjson_is_sint(val)) {
int64_t parsed = yyjson_get_sint(val);
if (parsed >= INT_MIN && parsed <= INT_MAX) {
result = (int)parsed;
}
} else if (val && yyjson_is_uint(val)) {
uint64_t parsed = yyjson_get_uint(val);
if (parsed <= INT_MAX) {
result = (int)parsed;
}
}
yyjson_doc_free(doc);
return result;
+4
View File
@@ -974,6 +974,10 @@ TEST(mcp_get_int_arg) {
ASSERT_EQ(val, 5);
val = cbm_mcp_get_int_arg(args, "missing", 42);
ASSERT_EQ(val, 42);
val = cbm_mcp_get_int_arg("{\"limit\":4294967297}", "limit", 17);
ASSERT_EQ(val, 17);
val = cbm_mcp_get_int_arg("{\"limit\":-9223372036854775808}", "limit", 19);
ASSERT_EQ(val, 19);
PASS();
}