Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Apply atomic properties-JSON appends to the parallel pipeline twin

Martin Vogel9f465e5109a6
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index 60fc67282..4713b77c6 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -18,6 +18,9 @@ enum {
PP_JSON_MARGIN = 10,
PP_ESC_MARGIN = 3,
PP_ESC_SPACE = 2,
+ /* Fixed bytes around a serialized JSON field: ,"key":"value" / ,"key":[...]
+ * -> comma + 2 key quotes + colon + 2 value quotes (resp. brackets). */
+ PP_JSON_FIELD_OVERHEAD = 6,
PP_ARGS_MARGIN = 20,
PP_LOG_THRESH = 24,
PP_LOG_INTERVAL = 10,
@@ -153,13 +156,41 @@ static int json_escape_char(char *buf, size_t avail, char ch) {
return PP_ESC_SPACE;
}
+/* Escaped length of a string under json_escape_char's rules: escaped
+ * characters expand to 2 bytes, everything else stays 1. */
+static size_t pp_json_escaped_len(const char *s) {
+ size_t n = 0;
+ for (; *s; s++) {
+ switch (*s) {
+ case '"':
+ case '\\':
+ case '\n':
+ case '\r':
+ case '\t':
+ n += PP_ESC_SPACE;
+ break;
+ default:
+ n += SKIP_ONE;
+ }
+ }
+ return n;
+}
+
+/* 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 optional field whole keeps the JSON valid. Twin of
+ * pass_definitions.c — keep both in sync. */
static void append_json_string(char *buf, size_t bufsize, size_t *pos, const char *key,
const char *val) {
if (!val || val[0] == '\0') {
return;
}
- if (*pos >= bufsize - PP_JSON_MARGIN) {
- return;
+ size_t required = strlen(key) + pp_json_escaped_len(val) + PP_JSON_FIELD_OVERHEAD;
+ if (*pos + required + PP_ESC_SPACE > bufsize) {
+ return; /* whole field would not fit — skip it atomically */
}
size_t p = *pos;
int w = snprintf(buf + p, bufsize - p, ",\"%s\":\"", key);
@@ -178,12 +209,21 @@ static void append_json_string(char *buf, size_t bufsize, size_t *pos, const cha
*pos = p;
}
-/* Append a JSON array of strings: ,"key":["a","b","c"] */
+/* Append a JSON array of strings: ,"key":["a","b","c"]. Atomic like
+ * append_json_string: emitted only if the whole array fits. */
static void append_json_str_array(char *buf, size_t bufsize, size_t *pos, const char *key,
const char **arr) {
if (!arr || !arr[0] || *pos >= bufsize - PP_JSON_MARGIN) {
return;
}
+ /* ,"key":[ + per item "<escaped>" + separating commas + ] */
+ size_t required = strlen(key) + PP_JSON_FIELD_OVERHEAD;
+ for (int i = 0; arr[i]; i++) {
+ required += pp_json_escaped_len(arr[i]) + PP_ESC_SPACE + (i > 0 ? SKIP_ONE : 0);
+ }
+ if (*pos + required + PP_ESC_SPACE > bufsize) {
+ return; /* whole array would not fit — skip it atomically */
+ }
size_t p = *pos;
int n = snprintf(buf + p, bufsize - p, ",\"%s\":[", key);
if (n <= 0 || p + (size_t)n >= bufsize - PP_ESC_SPACE) {
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 436ee3e1a..d2ed468fd 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -485,23 +485,26 @@ TEST(pipeline_def_props_valid_json_when_oversized) {
* buffer margin as the param_types array starts — the array is then cut
* mid-item (`"param_types":["enum`), exactly the kernel failure shape.
* Sweeping 10..69 params deterministically hits the window (pre-fix:
- * sweep_fn_30 -> 2047-byte malformed properties). */
- char path[512];
- snprintf(path, sizeof(path), "%s/huge.c", g_tmpdir);
- FILE *f = fopen(path, "w");
- if (!f) {
- teardown_test_repo();
- FAIL("failed to write huge.c");
- }
+ * sweep_fn_30 -> 2047-byte malformed properties). ONE FUNCTION PER FILE so
+ * the file count exceeds MIN_FILES_FOR_PARALLEL (50) and the test covers
+ * the PARALLEL pipeline's duplicated props builder (pass_parallel.c) — the
+ * path large repos take; the serial twin lives in pass_definitions.c. */
for (int n = 10; n < 70; n++) {
+ char path[512];
+ snprintf(path, sizeof(path), "%s/sweep_%02d.c", g_tmpdir, n);
+ FILE *f = fopen(path, "w");
+ if (!f) {
+ teardown_test_repo();
+ FAIL("failed to write sweep file");
+ }
fprintf(f, "int sweep_fn_%02d(", n);
for (int i = 0; i < n; i++) {
fprintf(f, "%sstruct long_struct_type_name_padding_padding_%02d *par_%02d",
i ? ", " : "", i, i);
}
fprintf(f, ") { return 0; }\n");
+ fclose(f);
}
- fclose(f);
char db_path[512];
snprintf(db_path, sizeof(db_path), "%s/test_huge_props.db", g_tmpdir);