From 88742c058074294c633dbdcaee1e064fe84456f3 Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Thu, 2 Jul 2026 03:14:52 -0700 Subject: [PATCH] Guard memcpy against NULL when storing an empty string value Signed-off-by: Sai Asish Y --- src/value.c | 3 ++- tests/test-json.c | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/value.c b/src/value.c index 550e2bc..9f3f98a 100644 --- a/src/value.c +++ b/src/value.c @@ -435,7 +435,8 @@ value_init_string_(VALUE* v, const char* str, size_t len) } payload[off++] = tmplen & 0x7f; - memcpy(payload + off, str, len); + if(len > 0) + memcpy(payload + off, str, len); payload[off + len] = '\0'; return 0; } diff --git a/tests/test-json.c b/tests/test-json.c index e9f4f01..a236a8e 100644 --- a/tests/test-json.c +++ b/tests/test-json.c @@ -1519,6 +1519,26 @@ test_issue3(void) value_fini(&root); } +static void +test_issue13(void) +{ + static const char input[] = "{\"\":1}"; + + VALUE root; + VALUE* v; + + /* An empty object key must parse without passing a NULL pointer to + * memcpy() while storing the zero-length key string. */ + TEST_CHECK(json_dom_parse(input, strlen(input), NULL, 0, &root, NULL) == 0); + TEST_CHECK(value_dict_size(&root) == 1); + + v = value_dict_get_(&root, "", 0); + if(TEST_CHECK(v != NULL)) + TEST_CHECK(value_int32(v) == 1); + + value_fini(&root); +} + TEST_LIST = { { "pos-tracking", test_pos_tracking }, @@ -1548,5 +1568,6 @@ TEST_LIST = { { "crazy-double", test_crazy_double }, { "bug-issue2", test_issue2 }, { "bug-issue3", test_issue3 }, + { "bug-issue13", test_issue13 }, { 0 } };