Appends are ATOMIC: a field is emitted only if the WHOLE serialized form * fits (with PP_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
| 411 | * oversized optional field whole keeps the JSON valid. Twin of |
| 412 | * pass_definitions.c — keep both in sync. */ |
| 413 | static void append_json_string(char *buf, size_t bufsize, size_t *pos, const char *key, |
| 414 | const char *val) { |
| 415 | if (!val || val[0] == '\0') { |
| 416 | return; |
| 417 | } |
| 418 | size_t required = strlen(key) + pp_json_escaped_len(val) + PP_JSON_FIELD_OVERHEAD; |
| 419 | if (*pos + required + PP_ESC_SPACE > bufsize) { |
| 420 | return; /* whole field would not fit — skip it atomically */ |
| 421 | } |
| 422 | size_t p = *pos; |
| 423 | int w = snprintf(buf + p, bufsize - p, ",\"%s\":\"", key); |
| 424 | if (w <= 0 || (size_t)w >= bufsize - p) { |
| 425 | return; |
| 426 | } |
| 427 | p += (size_t)w; |
| 428 | for (const char *s = val; *s && p < bufsize - PP_ESC_MARGIN; s++) { |
| 429 | int n = json_escape_char(buf + p, bufsize - p - PP_ESC_SPACE, *s); |
| 430 | p += (size_t)n; |
| 431 | } |
| 432 | if (p < bufsize - SKIP_ONE) { |
| 433 | buf[p++] = '"'; |
| 434 | } |
| 435 | buf[p] = '\0'; |
| 436 | *pos = p; |
| 437 | } |
| 438 | |
| 439 | /* Append a JSON array of strings: ,"key":["a","b","c"]. Atomic like |
| 440 | * append_json_string: emitted only if the whole array fits. */ |
no test coverage detected