MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / append_json_string

Function append_json_string

src/pipeline/pass_definitions.c:181–205  ·  view source on GitHub ↗

Appends are ATOMIC: a field is emitted only if the WHOLE serialized form * fits (with PD_ESC_SPACE bytes reserved for the closing '}' + NUL). Cutting a * field mid-value produced unterminated strings/arrays — malformed properties * JSON that aborts every json_extract()-based consumer downstream (seen on the * Linux kernel: 50-param functions truncated at the 2 KB cap). Dropping an * oversized

Source from the content-addressed store, hash-verified

179 * Linux kernel: 50-param functions truncated at the 2 KB cap). Dropping an
180 * oversized optional field whole keeps the JSON valid. */
181static void append_json_string(char *buf, size_t bufsize, size_t *pos, const char *key,
182 const char *val) {
183 if (!val || val[0] == '\0') {
184 return;
185 }
186 /* ,"key":"<escaped>" — comma + 2 key quotes + colon + 2 value quotes */
187 size_t required = strlen(key) + def_json_escaped_len(val) + PD_JSON_FIELD_OVERHEAD;
188 if (*pos + required + PD_ESC_SPACE > bufsize) {
189 return; /* whole field would not fit — skip it atomically */
190 }
191 size_t p = *pos;
192 int w = snprintf(buf + p, bufsize - p, ",\"%s\":\"", key);
193 if (w <= 0 || (size_t)w >= bufsize - p) {
194 return;
195 }
196 p += (size_t)w;
197 for (const char *s = val; *s && p < bufsize - PD_ESC_MARGIN; s++) {
198 p += (size_t)def_json_escape_char(buf + p, bufsize - p - PD_ESC_SPACE, *s);
199 }
200 if (p < bufsize - SKIP_ONE) {
201 buf[p++] = '"';
202 }
203 buf[p] = '\0';
204 *pos = p;
205}
206
207/* Append an already-serialized JSON value verbatim: ,"key":<json>. Atomic like
208 * append_json_string. For `variants`, which extraction builds as a JSON array

Callers 2

build_def_propsFunction · 0.70
pd_add_file_docFunction · 0.70

Calls 2

def_json_escaped_lenFunction · 0.85
def_json_escape_charFunction · 0.85

Tested by

no test coverage detected