properties with transitive_loop_depth/recursive set: rewritten in place * when present, appended exactly as the in-memory pass appends otherwise. * NULL: allocation failure or a malformed existing pair. */
| 407 | * when present, appended exactly as the in-memory pass appends otherwise. |
| 408 | * NULL: allocation failure or a malformed existing pair. */ |
| 409 | static char *complexity_set_props(const char *old, int tld, bool recursive) { |
| 410 | static const char key[] = "\"transitive_loop_depth\":"; |
| 411 | static const char rec_key[] = ",\"recursive\":"; |
| 412 | const char *props = old ? old : "{}"; |
| 413 | size_t olen = strlen(props); |
| 414 | char value[CBM_SZ_64]; |
| 415 | int w = |
| 416 | snprintf(value, sizeof(value), "%s%d%s%s", key, tld, rec_key, recursive ? "true" : "false"); |
| 417 | if (w < 0 || (size_t)w >= sizeof(value)) { |
| 418 | return NULL; |
| 419 | } |
| 420 | const char *at = strstr(props, key); |
| 421 | size_t head = 0; |
| 422 | size_t tail = 0; /* offset where the old pair ends */ |
| 423 | const char *sep = ""; |
| 424 | if (at) { |
| 425 | const char *p = at + sizeof(key) - 1; |
| 426 | if (*p == '-') { |
| 427 | p++; |
| 428 | } |
| 429 | while (*p >= '0' && *p <= '9') { |
| 430 | p++; |
| 431 | } |
| 432 | if (strncmp(p, rec_key, sizeof(rec_key) - 1) != 0) { |
| 433 | return NULL; |
| 434 | } |
| 435 | p += sizeof(rec_key) - 1; |
| 436 | if (strncmp(p, "true", 4) == 0) { |
| 437 | p += 4; |
| 438 | } else if (strncmp(p, "false", 5) == 0) { |
| 439 | p += 5; |
| 440 | } else { |
| 441 | return NULL; |
| 442 | } |
| 443 | head = (size_t)(at - props); |
| 444 | tail = (size_t)(p - props); |
| 445 | } else { |
| 446 | if (olen < 2 || props[olen - 1] != '}') { |
| 447 | return cbm_mem_strdup(CBM_MEM_CLASS_OTHER, props); /* not an object: untouched */ |
| 448 | } |
| 449 | head = olen - 1; |
| 450 | tail = olen - 1; |
| 451 | sep = olen == 2 ? "" : ","; |
| 452 | } |
| 453 | size_t len = head + strlen(sep) + (size_t)w + (olen - tail); |
| 454 | char *neu = cbm_alloc(CBM_MEM_CLASS_OTHER, len + 1); |
| 455 | if (!neu) { |
| 456 | return NULL; |
| 457 | } |
| 458 | memcpy(neu, props, head); |
| 459 | size_t off = head; |
| 460 | memcpy(neu + off, sep, strlen(sep)); |
| 461 | off += strlen(sep); |
| 462 | memcpy(neu + off, value, (size_t)w); |
| 463 | off += (size_t)w; |
| 464 | memcpy(neu + off, props + tail, olen - tail); |
| 465 | neu[len] = '\0'; |
| 466 | return neu; |
no test coverage detected