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.

fix(pipeline): repair Ensemble routing settings lookup, XML bounds

Martin Vogel6c2f82c83564
diff --git a/src/pipeline/pass_ensemble_routing.c b/src/pipeline/pass_ensemble_routing.c
index ab23316c5..0aa574839 100644
--- a/src/pipeline/pass_ensemble_routing.c
+++ b/src/pipeline/pass_ensemble_routing.c
@@ -74,13 +74,18 @@ typedef struct {
static void extract_xml_attr(const char *xml, int offset, const char *attr, char *out, int outsz) {
char needle[CBM_SZ_64];
snprintf(needle, sizeof(needle), "%s=\"", attr);
- const char *p = strstr(xml + offset, needle);
out[0] = '\0';
- if (!p)
+ const char *tag = xml + offset;
+ /* An attribute belongs to THIS tag only. Without bounding the search at the
+ * tag's closing '>', a tag that omits the attribute silently inherits it
+ * from a later tag, mispairing ClassName/Enabled across production items. */
+ const char *tag_end = strchr(tag, '>');
+ const char *p = strstr(tag, needle);
+ if (!p || (tag_end && p >= tag_end))
return;
p += strlen(needle);
const char *e = strchr(p, '"');
- if (!e)
+ if (!e || (tag_end && e >= tag_end))
return;
int len = (int)(e - p);
if (len >= outsz)
@@ -128,6 +133,7 @@ static ens_prod_def_t *parse_production_xml(const char *xml, const char *class_q
}
const char *item_end = strstr(item_start, "</Item>");
+ const bool item_terminated = item_end != NULL;
if (!item_end)
item_end = item_start + strlen(item_start);
@@ -165,6 +171,10 @@ static ens_prod_def_t *parse_production_xml(const char *xml, const char *class_q
"settings truncated at MAX_SETTINGS cap");
}
def->n_items++;
+ /* item_end is the NUL terminator when the item is unterminated, so
+ * advancing by strlen("</Item>") would read past the allocation. */
+ if (!item_terminated)
+ break;
p = item_end + 7;
}
/* Warn when items were truncated at the cap. */
@@ -191,8 +201,8 @@ static char *read_file(const char *full_path) {
fclose(f);
return NULL;
}
- fread(buf, 1, (size_t)sz, f);
- buf[sz] = '\0';
+ size_t got = fread(buf, 1, (size_t)sz, f);
+ buf[got] = '\0';
fclose(f);
return buf;
}
@@ -586,7 +596,12 @@ void cbm_pipeline_pass_ensemble_routing(cbm_pipeline_ctx_t *ctx) {
continue;
char item_qn[CBM_SZ_512];
snprintf(item_qn, sizeof(item_qn), "%s.%s", def->production_class, item->item_name);
- const cbm_gbuf_node_t *item_node = cbm_gbuf_find_by_qn(ctx->gbuf, item_qn);
+ /* Route nodes are keyed by the prefixed qn (see emit_async_call);
+ * cbm_gbuf_find_by_qn is an exact hashtable lookup, so the bare
+ * item qn never matches and every settings edge was dropped. */
+ char item_route_qn[CBM_SZ_512];
+ snprintf(item_route_qn, sizeof(item_route_qn), "__route__ensemble__%s", item_qn);
+ const cbm_gbuf_node_t *item_node = cbm_gbuf_find_by_qn(ctx->gbuf, item_route_qn);
if (!item_node)
continue;
if (strcmp(setting->setting_name, "TargetConfigNames") == 0) {
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 56284b024..564792d2b 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -12150,6 +12150,185 @@ TEST(pipeline_ensemble_routing_edges) {
PASS();
}
+TEST(pipeline_ensemble_routing_attr_does_not_leak_across_items) {
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_ens_attr_XXXXXX");
+ if (!cbm_mkdtemp(tmpdir))
+ FAIL("failed to create temp dir");
+
+ char path[512];
+
+ /* The first item declares no ClassName. Attribute lookup used to scan
+ * forward from the tag with no bound, so it inherited ClassName from the
+ * SECOND item and emitted a Route for an item that does not name a class,
+ * pairing it with the wrong implementation. Only the well-formed item may
+ * produce a Route. */
+ snprintf(path, sizeof(path), "%s/AttrProduction.cls", tmpdir);
+ FILE *f = fopen(path, "w");
+ if (!f) {
+ th_rmtree(tmpdir);
+ FAIL("fopen production");
+ }
+ fprintf(f, "Class MyApp.AttrProduction Extends Ens.Production\n"
+ "{\n"
+ "XData ProductionDefinition\n"
+ "{\n"
+ "<Production Name=\"MyApp.AttrProduction\">\n"
+ " <Item Name=\"NoClass\" Enabled=\"true\">\n"
+ " </Item>\n"
+ " <Item Name=\"Real\" ClassName=\"MyApp.Real\" Enabled=\"true\">\n"
+ " </Item>\n"
+ "</Production>\n"
+ "}\n"
+ "}\n");
+ fclose(f);
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/ens.db", tmpdir);
+
+ cbm_pipeline_t *p = cbm_pipeline_new(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 *routes = NULL;
+ int route_count = 0;
+ cbm_store_find_nodes_by_label(s, project, "Route", &routes, &route_count);
+ int ens_route_count = 0;
+ for (int i = 0; i < route_count; i++) {
+ if (routes[i].qualified_name &&
+ strstr(routes[i].qualified_name, "__route__ensemble__") != NULL)
+ ens_route_count++;
+ }
+ ASSERT_EQ(ens_route_count, 1);
+ cbm_store_free_nodes(routes, route_count);
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmpdir);
+ PASS();
+}
+
+TEST(pipeline_ensemble_routing_settings_targets) {
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_ens_set_XXXXXX");
+ if (!cbm_mkdtemp(tmpdir))
+ FAIL("failed to create temp dir");
+
+ char path[512];
+
+ /* A production whose ONLY routing information is declarative: a
+ * TargetConfigNames setting on the router item. No class file defines
+ * SendRequestSync anywhere, so every ASYNC_CALLS edge this run produces
+ * must have come from the settings path. */
+ snprintf(path, sizeof(path), "%s/RouterProduction.cls", tmpdir);
+ FILE *f = fopen(path, "w");
+ if (!f) {
+ th_rmtree(tmpdir);
+ FAIL("fopen production");
+ }
+ fprintf(f, "Class MyApp.RouterProduction Extends Ens.Production\n"
+ "{\n"
+ "XData ProductionDefinition\n"
+ "{\n"
+ "<Production Name=\"MyApp.RouterProduction\">\n"
+ " <Item Name=\"Router\" ClassName=\"MyApp.Router\" Enabled=\"true\">\n"
+ " <Setting Target=\"Host\" Name=\"TargetConfigNames\">OpA,OpB</Setting>\n"
+ " </Item>\n"
+ " <Item Name=\"OpA\" ClassName=\"MyApp.OpA\" Enabled=\"true\">\n"
+ " </Item>\n"
+ " <Item Name=\"OpB\" ClassName=\"MyApp.OpB\" Enabled=\"true\">\n"
+ " </Item>\n"
+ "</Production>\n"
+ "}\n"
+ "}\n");
+ fclose(f);
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/ens.db", tmpdir);
+
+ cbm_pipeline_t *p = cbm_pipeline_new(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);
+
+ /* Router -> OpA and Router -> OpB, both derived from TargetConfigNames.
+ * Before the Route-qn fix this was 0: the settings loop looked the source
+ * item up by its bare qn, which cbm_gbuf_find_by_qn (an exact hashtable
+ * lookup) never matches, so every edge was skipped by `continue`. */
+ int async_calls = cbm_store_count_edges_by_type(s, project, "ASYNC_CALLS");
+ ASSERT_EQ(async_calls, 2);
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmpdir);
+ PASS();
+}
+
+TEST(pipeline_ensemble_routing_unterminated_item_is_safe) {
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_ens_bad_XXXXXX");
+ if (!cbm_mkdtemp(tmpdir))
+ FAIL("failed to create temp dir");
+
+ char path[512];
+
+ /* Truncated XData: an <Item that never closes. The parser used to set
+ * item_end to the buffer's NUL terminator and then advance by
+ * strlen("</Item>"), reading 7 bytes past the allocation and scanning
+ * unbounded heap from there. Under ASan this aborts the run. */
+ snprintf(path, sizeof(path), "%s/BadProduction.cls", tmpdir);
+ FILE *f = fopen(path, "w");
+ if (!f) {
+ th_rmtree(tmpdir);
+ FAIL("fopen production");
+ }
+ fprintf(f, "Class MyApp.BadProduction Extends Ens.Production\n"
+ "{\n"
+ "XData ProductionDefinition\n"
+ "{\n"
+ "<Production Name=\"MyApp.BadProduction\">\n"
+ " <Item Name=\"Orphan\" ClassName=\"MyApp.Orphan\" Enabled=\"true\">\n"
+ "</Production>\n"
+ "}\n"
+ "}\n");
+ fclose(f);
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/ens.db", tmpdir);
+
+ cbm_pipeline_t *p = cbm_pipeline_new(tmpdir, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(cbm_pipeline_run(p), 0);
+
+ /* The well-formed prefix is still honoured: the item is parsed and its
+ * Route node emitted. Malformed input degrades, it does not disappear. */
+ 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 *routes = NULL;
+ int route_count = 0;
+ cbm_store_find_nodes_by_label(s, project, "Route", &routes, &route_count);
+ int ens_route_count = 0;
+ for (int i = 0; i < route_count; i++) {
+ if (routes[i].qualified_name &&
+ strstr(routes[i].qualified_name, "__route__ensemble__") != NULL)
+ ens_route_count++;
+ }
+ ASSERT_EQ(ens_route_count, 1);
+ cbm_store_free_nodes(routes, route_count);
+
+ cbm_store_close(s);
+ cbm_pipeline_free(p);
+ th_rmtree(tmpdir);
+ PASS();
+}
+
TEST(pipeline_ensemble_routing_method_scoping) {
/* Verifies scan_source_for_send_targets scopes to the calling method body.
* Two methods in the same class each route to a different operation; each
@@ -12592,6 +12771,9 @@ SUITE(pipeline) {
/* Ensemble routing pass */
RUN_TEST(pipeline_ensemble_routing_edges);
RUN_TEST(pipeline_ensemble_routing_method_scoping);
+ RUN_TEST(pipeline_ensemble_routing_attr_does_not_leak_across_items);
+ RUN_TEST(pipeline_ensemble_routing_settings_targets);
+ RUN_TEST(pipeline_ensemble_routing_unterminated_item_is_safe);
}
/* Focused semantic-manifest and publication contracts. Kept separate from the