Skip to content

Commit 478ce25

Browse files
committed
fix(extract): resolve Go route paths built via string concatenation
r.HandleFunc(base+"/login", handler) fell through extract_positional_url with no branch for binary_expression, so first_string_arg stayed NULL. A real BFF with 47 such registrations (all base+"literal") produced only 9 Route nodes. Recover the literal suffix from the right-hand operand of a "+" concatenation; a right side that is not itself a literal is left unresolved rather than guessed. Fixes DeusData#1249 Signed-off-by: Amir Fathi <amirfathi.me@gmail.com>
1 parent d6be58e commit 478ce25

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,24 @@ static const char *extract_keyword_url(CBMExtractCtx *ctx, TSNode arg) {
17071707
return extract_string_value(ctx, val_node);
17081708
}
17091709

1710+
// `prefixVar + "/route"` (Go's idiomatic configurable-base-path pattern):
1711+
// recover the literal suffix. A right side that is not itself a literal is
1712+
// left unresolved rather than guessed (issue #1249).
1713+
static const char *extract_binary_concat_suffix(CBMExtractCtx *ctx, TSNode node) {
1714+
TSNode op_node = ts_node_child_by_field_name(node, TS_FIELD("operator"));
1715+
if (!ts_node_is_null(op_node)) {
1716+
char *op = cbm_node_text(ctx->arena, op_node, ctx->source);
1717+
if (!op || strcmp(op, "+") != 0) {
1718+
return NULL;
1719+
}
1720+
}
1721+
TSNode rhs = ts_node_child_by_field_name(node, TS_FIELD("right"));
1722+
if (ts_node_is_null(rhs) || !is_string_like(ts_node_type(rhs))) {
1723+
return NULL;
1724+
}
1725+
return strip_and_validate_string_arg(ctx->arena, cbm_node_text(ctx->arena, rhs, ctx->source));
1726+
}
1727+
17101728
// Try to extract URL/topic from a positional argument (string or constant).
17111729
static const char *extract_positional_url(CBMExtractCtx *ctx, TSNode arg, const char *ak) {
17121730
/* JS/TS template literals: `/things/${id}` normalizes to "/things/{}" so the
@@ -1717,6 +1735,12 @@ static const char *extract_positional_url(CBMExtractCtx *ctx, TSNode arg, const
17171735
return strip_and_validate_string_arg(ctx->arena, (char *)flat);
17181736
}
17191737
}
1738+
if (strcmp(ak, "binary_expression") == 0) {
1739+
const char *suffix = extract_binary_concat_suffix(ctx, arg);
1740+
if (suffix) {
1741+
return suffix;
1742+
}
1743+
}
17201744
if (is_string_like(ak)) {
17211745
char *text = cbm_node_text(ctx->arena, arg, ctx->source);
17221746
const char *validated = strip_and_validate_string_arg(ctx->arena, text);

‎tests/test_extraction.c‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,6 +3181,57 @@ TEST(extract_ts_template_string_url_issue1006) {
31813181
PASS();
31823182
}
31833183

3184+
/* Issue #1249: a mux route built as `configVar + "/literal"` (Go's idiomatic
3185+
* configurable-base-path pattern) must index the literal suffix, both for a
3186+
* route registration and for an outbound URL built the same way. A real BFF
3187+
* with 47 such registrations produced only 9 Route nodes before this fix. */
3188+
TEST(extract_go_binary_concat_url_issue1249) {
3189+
CBMFileResult *r = extract("package main\n"
3190+
"import \"net/http\"\n"
3191+
"func setup(mux *http.ServeMux, base string) {\n"
3192+
" mux.HandleFunc(base+\"/login\", loginHandler)\n"
3193+
"}\n"
3194+
"func report(host string, port string) {\n"
3195+
" http.Get(\"http://\" + host + \":\" + port + \"/log\")\n"
3196+
"}\n",
3197+
CBM_LANG_GO, "t", "routes.go");
3198+
ASSERT_NOT_NULL(r);
3199+
ASSERT_FALSE(r->has_error);
3200+
3201+
const CBMCall *reg = find_call_by_callee(r, "mux.HandleFunc");
3202+
ASSERT_NOT_NULL(reg);
3203+
ASSERT_NOT_NULL(reg->first_string_arg);
3204+
ASSERT_STR_EQ(reg->first_string_arg, "/login");
3205+
3206+
const CBMCall *out = find_call_by_callee(r, "http.Get");
3207+
ASSERT_NOT_NULL(out);
3208+
ASSERT_NOT_NULL(out->first_string_arg);
3209+
ASSERT_STR_EQ(out->first_string_arg, "/log");
3210+
3211+
cbm_free_result(r);
3212+
PASS();
3213+
}
3214+
3215+
/* Same issue: when the right side of the concatenation is not itself a
3216+
* literal (`base + suffixVar`), there is no literal route to recover. The
3217+
* fix must leave this unresolved rather than fabricate a path. */
3218+
TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249) {
3219+
CBMFileResult *r = extract("package main\n"
3220+
"func setup(mux *http.ServeMux, base string, suffix string) {\n"
3221+
" mux.HandleFunc(base+suffix, dynHandler)\n"
3222+
"}\n",
3223+
CBM_LANG_GO, "t", "routes.go");
3224+
ASSERT_NOT_NULL(r);
3225+
ASSERT_FALSE(r->has_error);
3226+
3227+
const CBMCall *reg = find_call_by_callee(r, "mux.HandleFunc");
3228+
ASSERT_NOT_NULL(reg);
3229+
ASSERT_NULL(reg->first_string_arg);
3230+
3231+
cbm_free_result(r);
3232+
PASS();
3233+
}
3234+
31843235

31853236
/* Reproduce-first: Java module QN must derive from the CONTAINING DIRECTORY, not
31863237
* the filename stem, so a top-level class `Outer` in `Outer.java` is `t.Outer`,
@@ -5315,6 +5366,8 @@ SUITE(extraction) {
53155366
RUN_TEST(extract_java_method_annotations_issue382);
53165367
RUN_TEST(extract_java_jaxrs_path_composition_issue1005);
53175368
RUN_TEST(extract_ts_template_string_url_issue1006);
5369+
RUN_TEST(extract_go_binary_concat_url_issue1249);
5370+
RUN_TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249);
53185371
RUN_TEST(extract_java_no_double_class_qn);
53195372
RUN_TEST(extract_go_no_filename_in_module_qn);
53205373
RUN_TEST(extract_large_ts_has_functions_issue213);

0 commit comments

Comments
 (0)