Append a JSON array of strings: ,"key":["a","b","c"]. Atomic like * append_json_string: emitted only if the whole array fits. */
| 225 | /* Append a JSON array of strings: ,"key":["a","b","c"]. Atomic like |
| 226 | * append_json_string: emitted only if the whole array fits. */ |
| 227 | static void append_json_str_array(char *buf, size_t bufsize, size_t *pos, const char *key, |
| 228 | const char **arr) { |
| 229 | if (!arr || !arr[0] || *pos >= bufsize - PD_JSON_MARGIN) { |
| 230 | return; |
| 231 | } |
| 232 | /* ,"key":[ + per item "<escaped>" + separating commas + ] */ |
| 233 | size_t required = strlen(key) + PD_JSON_FIELD_OVERHEAD; |
| 234 | for (int i = 0; arr[i]; i++) { |
| 235 | required += def_json_escaped_len(arr[i]) + PD_ESC_SPACE + (i > 0 ? SKIP_ONE : 0); |
| 236 | } |
| 237 | if (*pos + required + PD_ESC_SPACE > bufsize) { |
| 238 | return; /* whole array would not fit — skip it atomically */ |
| 239 | } |
| 240 | size_t p = *pos; |
| 241 | int n = snprintf(buf + p, bufsize - p, ",\"%s\":[", key); |
| 242 | if (n <= 0 || p + (size_t)n >= bufsize - PD_ESC_SPACE) { |
| 243 | return; |
| 244 | } |
| 245 | p += (size_t)n; |
| 246 | for (int i = 0; arr[i]; i++) { |
| 247 | if (i > 0 && p < bufsize - SKIP_ONE) { |
| 248 | buf[p++] = ','; |
| 249 | } |
| 250 | if (p < bufsize - SKIP_ONE) { |
| 251 | buf[p++] = '"'; |
| 252 | } |
| 253 | /* Full escaping (not just quote/backslash): items like C param types |
| 254 | * sliced from multi-line declarations carry raw \n/\t bytes, which are |
| 255 | * invalid inside JSON strings. */ |
| 256 | for (const char *s = arr[i]; *s && p < bufsize - PD_ESC_SPACE; s++) { |
| 257 | p += (size_t)def_json_escape_char(buf + p, bufsize - p - PD_ESC_SPACE, *s); |
| 258 | } |
| 259 | if (p < bufsize - SKIP_ONE) { |
| 260 | buf[p++] = '"'; |
| 261 | } |
| 262 | } |
| 263 | if (p < bufsize - SKIP_ONE) { |
| 264 | buf[p++] = ']'; |
| 265 | } |
| 266 | buf[p] = '\0'; |
| 267 | *pos = p; |
| 268 | } |
| 269 | |
| 270 | /* Build properties JSON for a definition node. */ |
| 271 | static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def) { |
no test coverage detected