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.

Make definition-properties JSON appends atomic (no mid-value truncation)

Martin Vogel4b317c8779f0
diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.c
index b5aad9c4c..b83cf5b80 100644
--- a/src/pipeline/pass_definitions.c
+++ b/src/pipeline/pass_definitions.c
@@ -13,6 +13,9 @@
#include "foundation/constants.h"
enum { PD_RING = 4, PD_RING_MASK = 3, PD_JSON_MARGIN = 10, PD_ESC_MARGIN = 3, PD_ESC_SPACE = 2 };
+/* Fixed bytes around a serialized JSON field: ,"key":"value" / ,"key":[...]
+ * -> comma + 2 key quotes + colon + 2 value quotes (resp. brackets). */
+enum { PD_JSON_FIELD_OVERHEAD = 6 };
#include "pipeline/pipeline.h"
#include <stdint.h>
#include "pipeline/pipeline_internal.h"
@@ -111,13 +114,41 @@ static int def_json_escape_char(char *buf, size_t avail, char ch) {
return PD_ESC_SPACE;
}
+/* Escaped length of a string under def_json_escape_char's rules: escaped
+ * characters expand to 2 bytes, everything else stays 1. */
+static size_t def_json_escaped_len(const char *s) {
+ size_t n = 0;
+ for (; *s; s++) {
+ switch (*s) {
+ case '"':
+ case '\\':
+ case '\n':
+ case '\r':
+ case '\t':
+ n += PD_ESC_SPACE;
+ break;
+ default:
+ n += SKIP_ONE;
+ }
+ }
+ return n;
+}
+
+/* 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 optional field whole keeps the JSON valid. */
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 - PD_JSON_MARGIN) {
- return;
+ /* ,"key":"<escaped>" — comma + 2 key quotes + colon + 2 value quotes */
+ size_t required = strlen(key) + def_json_escaped_len(val) + PD_JSON_FIELD_OVERHEAD;
+ if (*pos + required + PD_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);
@@ -135,12 +166,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 - PD_JSON_MARGIN) {
return;
}
+ /* ,"key":[ + per item "<escaped>" + separating commas + ] */
+ size_t required = strlen(key) + PD_JSON_FIELD_OVERHEAD;
+ for (int i = 0; arr[i]; i++) {
+ required += def_json_escaped_len(arr[i]) + PD_ESC_SPACE + (i > 0 ? SKIP_ONE : 0);
+ }
+ if (*pos + required + PD_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 - PD_ESC_SPACE) {
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 7af64d0ae..436ee3e1a 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -11,6 +11,7 @@
#include "pipeline/pipeline.h"
#include "pipeline/pipeline_internal.h"
#include "store/store.h"
+#include <yyjson/yyjson.h> // properties-JSON validity (oversized-props regression)
#include <stdlib.h>
#include <string.h>
@@ -467,6 +468,83 @@ TEST(pipeline_definitions_properties) {
PASS();
}
+/* Node properties must remain VALID JSON even when a definition's serialized
+ * properties exceed the fixed 2 KB build buffer. Found on the Linux kernel:
+ * 135 nodes (50-param functions with struct-typed signatures) had properties
+ * truncated mid-string at 2047 bytes — malformed JSON that aborts EVERY
+ * json_extract()-based consumer (arch_entry_points, partial indexes, user
+ * Cypher on properties). Oversized optional fields must be dropped whole,
+ * never cut mid-value. */
+TEST(pipeline_def_props_valid_json_when_oversized) {
+ if (setup_test_repo() != 0) {
+ FAIL("failed to create temp dir");
+ }
+
+ /* Sweep of C functions with growing signatures. The corruption fires only
+ * when the serialized position lands in a narrow window just under the
+ * 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");
+ }
+ for (int n = 10; n < 70; n++) {
+ 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);
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/test_huge_props.db", g_tmpdir);
+ cbm_pipeline_t *p = cbm_pipeline_new(g_tmpdir, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+ const char *project = cbm_pipeline_project_name(p);
+
+ cbm_node_t *funcs = NULL;
+ int func_count = 0;
+ ASSERT_EQ(cbm_store_find_nodes_by_label(s, project, "Function", &funcs, &func_count),
+ CBM_STORE_OK);
+ int checked = 0;
+ for (int i = 0; i < func_count; i++) {
+ if (strncmp(funcs[i].name, "sweep_fn_", 9) != 0) {
+ continue;
+ }
+ ASSERT_NOT_NULL(funcs[i].properties_json);
+ yyjson_doc *doc =
+ yyjson_read(funcs[i].properties_json, strlen(funcs[i].properties_json), 0);
+ if (!doc) {
+ printf(" INVALID properties JSON for %s (%zu bytes): ...%s\n", funcs[i].name,
+ strlen(funcs[i].properties_json),
+ funcs[i].properties_json + (strlen(funcs[i].properties_json) > 60
+ ? strlen(funcs[i].properties_json) - 60
+ : 0));
+ }
+ ASSERT_NOT_NULL(doc); /* valid JSON for EVERY sweep size */
+ yyjson_doc_free(doc);
+ checked++;
+ }
+ ASSERT_EQ(checked, 60); /* all sweep functions present */
+
+ cbm_store_free_nodes(funcs, func_count);
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ teardown_test_repo();
+ PASS();
+}
+
/* ── Calls pass tests ──────────────────────────────────────────── */
TEST(pipeline_calls_resolution) {
@@ -5531,6 +5609,7 @@ SUITE(pipeline) {
RUN_TEST(pipeline_definitions_function_nodes);
RUN_TEST(pipeline_definitions_defines_edges);
RUN_TEST(pipeline_definitions_properties);
+ RUN_TEST(pipeline_def_props_valid_json_when_oversized);
/* Complexity propagation pass (Tier B) */
RUN_TEST(pipeline_complexity_transitive_loop_depth);
/* Calls pass */