Skip to content
Myr

Fix heap buffer overflow in `jvp_string_append` and `jvp_string_copy_replace_bad`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

In `jvp_string_append`, the allocation size `(currlen + len) * 2` could overflow `uint32_t` when `currlen + len` exceeds `INT_MAX`, causing a small allocation followed by a large `memcpy`. In `jvp_string_copy_replace_bad`, the output buffer size calculation `length * 3 + 1` could overflow `uint32_t`, again resulting in a small allocation followed by a large write. Add overflow checks to both functions to return an error for strings that would exceed `INT_MAX` in length. Fixes CVE-2026-32316.

authored by itchyny · committed by itchyny · parent 9f778b4 · Browse files · 1 file+10 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+10 added−1 removed

  • other:+10 added−1 removed

1 file+10 added−1 removed

modified:src/jv.c+10 added-1 removed
1112 unmodified lines
1113static jv jvp_string_copy_replace_bad(const char* data, uint32_t length) {1114 const char* end = data + length;1115 const char* i = data;11161117 uint32_t maxlength = length * 3 + 1; // worst case: all bad bytes, each becomes a 3-byte U+FFFD1117 // worst case: all bad bytes, each becomes a 3-byte U+FFFD1118 uint64_t maxlength = (uint64_t)length * 3 + 1;1119 if (maxlength >= INT_MAX) {1120 return jv_invalid_with_msg(jv_string("String too long"));1121 }11221123 jvp_string* s = jvp_string_alloc(maxlength);1124 char* out = s->data;1125 int c = 0;1126
51 unmodified lines
11781179static jv jvp_string_append(jv string, const char* data, uint32_t len) {1180 jvp_string* s = jvp_string_ptr(string);1181 uint32_t currlen = jvp_string_length(s);1182 if ((uint64_t)currlen + len >= INT_MAX) {1183 jv_free(string);1184 return jv_invalid_with_msg(jv_string("String too long"));1185 }11861187 if (jvp_refcnt_unshared(string.u.ptr) &&1188 jvp_string_remaining_space(s) >= len) {1189 // the next string fits at the end of a
892 unmodified lines