Skip to content

Commit 0843106

Browse files
fix(extract): address review on PHP inline-HTML masking (DeusData#2000)
Signed-off-by: harshitaajoshi <j.harshitaa06@gmail.com>
1 parent 09dc36d commit 0843106

4 files changed

Lines changed: 161 additions & 26 deletions

File tree

‎internal/cbm/php_inline_html.c‎

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,47 @@ static int php_scan_code(const char *s, int len, int i) {
263263
return len;
264264
}
265265

266+
/* The rewritten buffer. It is allocated and filled from the source on the
267+
* first write, so a file whose only `?>` bytes sit in strings or comments
268+
* costs no copy. */
269+
typedef struct {
270+
CBMArena *arena;
271+
const char *source;
272+
int len;
273+
char *out;
274+
bool failed;
275+
} PHPMask;
276+
277+
static char *php_mask_writable(PHPMask *m) {
278+
if (!m->out && !m->failed) {
279+
m->out = (char *)cbm_arena_alloc(m->arena, (size_t)m->len + SKIP_ONE);
280+
if (m->out) {
281+
memcpy(m->out, m->source, (size_t)m->len);
282+
m->out[m->len] = '\0';
283+
} else {
284+
m->failed = true;
285+
}
286+
}
287+
return m->out;
288+
}
289+
290+
/* Blank [from, to) of the rewritten buffer, keeping line breaks. */
291+
static void php_mask_blank(PHPMask *m, int from, int to) {
292+
char *out = php_mask_writable(m);
293+
if (out) {
294+
php_blank(out, from, to);
295+
}
296+
}
297+
298+
/* Turn the `?>` at i into `; `. */
299+
static void php_mask_close_tag(PHPMask *m, int i) {
300+
char *out = php_mask_writable(m);
301+
if (out) {
302+
out[i] = ';';
303+
out[i + SKIP_ONE] = ' ';
304+
}
305+
}
306+
266307
const char *cbm_php_mask_inline_html(CBMArena *arena, const char *source, int source_len) {
267308
if (!arena || !source || source_len <= 0) {
268309
return source;
@@ -273,33 +314,24 @@ const char *cbm_php_mask_inline_html(CBMArena *arena, const char *source, int so
273314
return source;
274315
}
275316

276-
char *out = (char *)cbm_arena_alloc(arena, (size_t)source_len + SKIP_ONE);
277-
if (!out) {
278-
return source;
279-
}
280-
memcpy(out, source, (size_t)source_len);
281-
out[source_len] = '\0';
282-
283-
bool changed = false;
317+
PHPMask m = {.arena = arena, .source = source, .len = source_len};
284318
bool seen_open = false;
285319
int i = 0;
286-
while (i < source_len) {
320+
while (i < source_len && !m.failed) {
287321
/* Inline-HTML mode: blank up to the next open tag. */
288322
int html_start = i;
289323
int tag = 0;
290324
while (i < source_len && (tag = php_open_tag_len(source, source_len, i)) == 0) {
291325
i++;
292326
}
293327
if (i > html_start) {
294-
php_blank(out, html_start, i);
295-
changed = true;
328+
php_mask_blank(&m, html_start, i);
296329
}
297330
if (i >= source_len) {
298331
break;
299332
}
300333
if (seen_open) {
301-
php_blank(out, i, i + tag);
302-
changed = true;
334+
php_mask_blank(&m, i, i + tag);
303335
}
304336
seen_open = true;
305337

@@ -308,10 +340,9 @@ const char *cbm_php_mask_inline_html(CBMArena *arena, const char *source, int so
308340
if (close < 0 || close >= source_len) {
309341
break;
310342
}
311-
out[close] = ';';
312-
out[close + SKIP_ONE] = ' ';
313-
changed = true;
343+
php_mask_close_tag(&m, close);
314344
i = close + PAIR_LEN;
315345
}
316-
return changed ? out : source;
346+
/* On allocation failure parse the file as it is: the old behaviour. */
347+
return (m.out && !m.failed) ? m.out : source;
317348
}

‎tests/test_extraction.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,56 @@ TEST(php_inline_html_close_tag_in_literals_issue2000) {
938938
PASS();
939939
}
940940

941+
/* Markup is blanked, never parsed: code-shaped text outside the PHP tags must
942+
* not produce definitions or calls, wherever it sits. */
943+
TEST(php_inline_html_markup_never_reaches_graph_issue2000) {
944+
CBMFileResult *r = extract("<p>function before() { return leakBefore(); }</p>\n"
945+
"<?php\n"
946+
"function real() { return 1; }\n"
947+
"?>\n"
948+
"<p>function fake() { return leak(); }</p>\n"
949+
"<script>function jsFake() { leakScript(); }</script>\n"
950+
"<?xml version=\"1.0\"?><x>function xmlFake() { leakXml(); }</x>\n"
951+
"<?php\n"
952+
"function after() { return real(); }\n"
953+
"?>\n"
954+
"<p>class Tail { function tailFake() { leakTail(); } }</p>\n",
955+
CBM_LANG_PHP, "t", "markup.php");
956+
ASSERT_NOT_NULL(r);
957+
ASSERT_FALSE(r->has_error);
958+
ASSERT(has_def(r, "Function", "real"));
959+
ASSERT(has_def(r, "Function", "after"));
960+
ASSERT(has_call(r, "real"));
961+
ASSERT_FALSE(has_def_any(r, "before"));
962+
ASSERT_FALSE(has_def_any(r, "fake"));
963+
ASSERT_FALSE(has_def_any(r, "jsFake"));
964+
ASSERT_FALSE(has_def_any(r, "xmlFake"));
965+
ASSERT_FALSE(has_def_any(r, "Tail"));
966+
ASSERT_FALSE(has_def_any(r, "tailFake"));
967+
ASSERT_FALSE(has_call(r, "leak"));
968+
ASSERT_FALSE(r->parse_incomplete);
969+
cbm_free_result(r);
970+
PASS();
971+
}
972+
973+
/* The mask hands back the caller's buffer, uncopied, when nothing needs
974+
* rewriting, and a same-length rewrite otherwise. */
975+
TEST(php_inline_html_mask_copies_only_on_write_issue2000) {
976+
CBMArena arena;
977+
cbm_arena_init(&arena);
978+
const char *plain = "<?php\nfunction a() { return 1; }\n";
979+
const char *quoted = "<?php\n$s = '?>';\n/* ?> */\n$h = <<<EOT\n?>\nEOT;\n";
980+
const char *mixed = "<?php\nfunction a() {}\n?>\n<b>x</b>\n";
981+
ASSERT(cbm_php_mask_inline_html(&arena, plain, (int)strlen(plain)) == plain);
982+
ASSERT(cbm_php_mask_inline_html(&arena, quoted, (int)strlen(quoted)) == quoted);
983+
const char *masked = cbm_php_mask_inline_html(&arena, mixed, (int)strlen(mixed));
984+
ASSERT(masked != mixed);
985+
ASSERT_EQ((int)strlen(masked), (int)strlen(mixed));
986+
ASSERT_STR_EQ(masked, "<?php\nfunction a() {}\n; \n \n");
987+
cbm_arena_destroy(&arena);
988+
PASS();
989+
}
990+
941991
/* A trailing `?>` (common at the end of older class files) is not a partial
942992
* parse, and CRLF line endings keep their line numbers. */
943993
TEST(php_inline_html_trailing_close_tag_crlf_issue2000) {
@@ -13252,6 +13302,8 @@ SUITE(extraction) {
1325213302
RUN_TEST(php_inline_html_tail_reaches_graph_issue2000);
1325313303
RUN_TEST(php_inline_html_template_shapes_issue2000);
1325413304
RUN_TEST(php_inline_html_close_tag_in_literals_issue2000);
13305+
RUN_TEST(php_inline_html_markup_never_reaches_graph_issue2000);
13306+
RUN_TEST(php_inline_html_mask_copies_only_on_write_issue2000);
1325513307
RUN_TEST(php_inline_html_trailing_close_tag_crlf_issue2000);
1325613308
RUN_TEST(ruby_class);
1325713309
RUN_TEST(ruby_module);

‎tests/test_php_lsp.c‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,65 @@ TEST(phplsp_local_method_via_typed_param) {
110110
PASS();
111111
}
112112

113+
/* #2000: with no cached tree (a result spilled under the memory budget comes
114+
* back without one) the cross-file pass re-parses the on-disk source itself.
115+
* It must blank inline HTML exactly as the per-file extract does, or a file's
116+
* tail would lose cross-file resolution only when that file spilled. */
117+
static int find_resolved_arr(const CBMResolvedCallArray *arr, const char *callerSub,
118+
const char *calleeSub) {
119+
for (int i = 0; i < arr->count; i++) {
120+
const CBMResolvedCall *rc = &arr->items[i];
121+
if (rc->caller_qn && strstr(rc->caller_qn, callerSub) && rc->callee_qn &&
122+
strstr(rc->callee_qn, calleeSub))
123+
return i;
124+
}
125+
return -1;
126+
}
127+
128+
TEST(phplsp_cross_reparse_reads_past_inline_html_issue2000) {
129+
const char *src = "<?php\n"
130+
"function gamma() { return 2; }\n"
131+
"?>\n"
132+
"<div class=\"box\">\n"
133+
" <p>plain markup after the closing tag</p>\n"
134+
"</div>\n"
135+
"<?php\n"
136+
"function delta(Mailer $m) { return $m->send(); }\n";
137+
CBMLSPDef defs[] = {
138+
{.qualified_name = "test.main.gamma",
139+
.short_name = "gamma",
140+
.label = "Function",
141+
.def_module_qn = "test.main"},
142+
{.qualified_name = "test.main.delta",
143+
.short_name = "delta",
144+
.label = "Function",
145+
.def_module_qn = "test.main"},
146+
{.qualified_name = "test.mailer.Mailer",
147+
.short_name = "Mailer",
148+
.label = "Class",
149+
.def_module_qn = "test.mailer"},
150+
{.qualified_name = "test.mailer.Mailer.send",
151+
.short_name = "send",
152+
.label = "Method",
153+
.def_module_qn = "test.mailer",
154+
.receiver_type = "test.mailer.Mailer"},
155+
};
156+
const char *imp_names[] = {"Mailer"};
157+
const char *imp_qns[] = {"test.mailer.Mailer"};
158+
159+
CBMArena arena;
160+
cbm_arena_init(&arena);
161+
CBMResolvedCallArray out = {0};
162+
163+
cbm_run_php_lsp_cross(&arena, src, (int)strlen(src), "test.main", defs, 4, imp_names, imp_qns,
164+
1, NULL, &out);
165+
166+
ASSERT_GTE(find_resolved_arr(&out, "main.delta", "Mailer.send"), 0);
167+
168+
cbm_arena_destroy(&arena);
169+
PASS();
170+
}
171+
113172
/* ── 3. Arrow function with typed parameter ───────────────────── */
114173

115174
TEST(phplsp_arrow_function_typed_param) {
@@ -5841,6 +5900,7 @@ SUITE(php_lsp) {
58415900
/* Phase 1 baseline regressions */
58425901
RUN_TEST(phplsp_local_method_via_new_assignment);
58435902
RUN_TEST(phplsp_local_method_via_typed_param);
5903+
RUN_TEST(phplsp_cross_reparse_reads_past_inline_html_issue2000);
58445904
RUN_TEST(phplsp_arrow_function_typed_param);
58455905
RUN_TEST(phplsp_static_call_resolved);
58465906
RUN_TEST(phplsp_self_and_parent);

‎tests/test_pipeline.c‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,15 +1405,7 @@ TEST(pipeline_php_inline_html_calls_resolve_issue2000) {
14051405
"function renderCard($item) { return formatPrice($item['cents']); }\n"},
14061406
};
14071407
for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); i++) {
1408-
char path[512];
1409-
snprintf(path, sizeof(path), "%s/%s", g_tmpdir, files[i].name);
1410-
FILE *f = fopen(path, "w");
1411-
if (!f) {
1412-
teardown_test_repo();
1413-
FAIL("failed to write php fixture");
1414-
}
1415-
fputs(files[i].body, f);
1416-
fclose(f);
1408+
write_temp_file(g_tmpdir, files[i].name, files[i].body);
14171409
}
14181410

14191411
char db[512];

0 commit comments

Comments
 (0)