Skip to content

Commit 49d928b

Browse files
authored
Merge pull request DeusData#1658 from DeusData/fix/eof-missing-terminator
2 parents 8c84a1e + 63f0a6c commit 49d928b

2 files changed

Lines changed: 167 additions & 4 deletions

File tree

‎internal/cbm/cbm.c‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -787,17 +787,53 @@ static void cbm_error_regions_push(cbm_error_regions_t *acc, TSNode n) {
787787
acc->count++;
788788
}
789789

790-
static void cbm_collect_error_regions(TSNode n, cbm_error_regions_t *acc) {
790+
/* #1610: a file that does not end with a newline leaves the grammar's
791+
* mandatory line terminator MISSING. That node is ZERO-WIDTH and sits at EOF.
792+
*
793+
* It is not a miss. The parser consumed no source for it — start_byte ==
794+
* end_byte — so by construction nothing was dropped: no construct can live in
795+
* a zero-byte span, and every real instruction above it parsed normally. This
796+
* is a property of the grammar's terminator rule, not of the file.
797+
*
798+
* Flagging it made the verdict arbitrary. Grammars whose terminator token is
799+
* VISIBLE (dockerfile, tcl, fish, gomod, hyprlang) reported parse_partial for
800+
* a missing final newline; grammars whose terminator is HIDDEN (ini, fsharp,
801+
* beancount, requirements, gitignore, sshconfig, kconfig) reported nothing for
802+
* exactly the same omission, because a hidden node is invisible to
803+
* ts_node_child(). Whether a user was told their file was partially parsed
804+
* depended on a grammar-authoring accident.
805+
*
806+
* The cost was not cosmetic: a phantom parse_partial writes a
807+
* "<project>::missed" shadow row, and until #1609 that row made the project
808+
* fail cross-repo validation as both source and target.
809+
*
810+
* Deliberately narrow — ZERO-WIDTH AT EOF ONLY. A MISSING or ERROR node with
811+
* WIDTH still counts even at EOF (a Makefile whose last recipe line is
812+
* unterminated really does lose the recipe), and anything before EOF is
813+
* untouched. */
814+
static bool cbm_is_eof_terminator_miss(TSNode n, int source_len) {
815+
if (!ts_node_is_missing(n) || source_len < 0) {
816+
return false;
817+
}
818+
uint32_t start = ts_node_start_byte(n);
819+
uint32_t end = ts_node_end_byte(n);
820+
return start == end && end == (uint32_t)source_len;
821+
}
822+
823+
static void cbm_collect_error_regions(TSNode n, cbm_error_regions_t *acc, int source_len) {
791824
if (acc->count >= CBM_MAX_ERROR_REGIONS) {
792825
return;
793826
}
794827
uint32_t k = ts_node_child_count(n);
795828
for (uint32_t i = 0; i < k && acc->count < CBM_MAX_ERROR_REGIONS; i++) {
796829
TSNode c = ts_node_child(n, i);
797830
if (ts_node_is_missing(c) || strcmp(ts_node_type(c), "ERROR") == 0) {
831+
if (cbm_is_eof_terminator_miss(c, source_len)) {
832+
continue; /* absent final newline only — nothing was dropped */
833+
}
798834
cbm_error_regions_push(acc, c); /* top-most region; do not descend */
799835
} else if (ts_node_has_error(c)) {
800-
cbm_collect_error_regions(c, acc);
836+
cbm_collect_error_regions(c, acc, source_len);
801837
}
802838
}
803839
}
@@ -1413,7 +1449,7 @@ CBMFileResult *cbm_extract_file_ex(const char *source, int source_len, CBMLangua
14131449
* already extract. */
14141450
if (ts_node_has_error(root)) {
14151451
cbm_error_regions_t raw_regs = {{0}, {0}, 0};
1416-
cbm_collect_error_regions(root, &raw_regs);
1452+
cbm_collect_error_regions(root, &raw_regs, source_len);
14171453
if (raw_regs.count > 0) {
14181454
int defs_before = result->defs.count;
14191455
cbm_extract_definitions(&pp_ctx);
@@ -1571,7 +1607,7 @@ CBMFileResult *cbm_extract_file_ex(const char *source, int source_len, CBMLangua
15711607
if (strcmp(ts_node_type(root), "ERROR") == 0) {
15721608
cbm_error_regions_push(&regs, root); /* whole file unparseable */
15731609
} else {
1574-
cbm_collect_error_regions(root, &regs);
1610+
cbm_collect_error_regions(root, &regs, source_len);
15751611
}
15761612
cbm_subtract_recovered_regions(&regs, &result->defs);
15771613
/* #1071: don't flag a benign function-like-macro call (defined in-file)

‎tests/test_parse_coverage.c‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,128 @@ TEST(c_trailing_recovered_defs_keep_flag) {
247247

248248
/* ── Suite ────────────────────────────────────────────────────────────────── */
249249

250+
/* ── #1610: a missing FINAL NEWLINE is not a parse failure ────────────────────
251+
*
252+
* A file that does not end with "\n" leaves the grammar's mandatory line
253+
* terminator MISSING. That node is ZERO-WIDTH and sits at EOF: the parser
254+
* consumed no source for it, so by construction nothing was dropped — no
255+
* construct can live in a zero-byte span. Every instruction still parses.
256+
*
257+
* Reported on #1610 for Dockerfile, where a reporter proved with a byte-exact
258+
* matrix that the trigger is independent of BOM, CRLF/LF, exec-form vs
259+
* shell-form and file length — it is purely the absent final newline.
260+
*
261+
* It was never Dockerfile-specific: tcl, fish, gomod and hyprlang flag the same
262+
* way, while ini, fsharp, beancount and others do NOT — only because those
263+
* grammars declare the terminator token hidden rather than visible. Whether a
264+
* user saw a phantom parse_partial came down to a grammar-authoring accident.
265+
*
266+
* The cost was not cosmetic: a phantom flag writes a "<project>::missed" shadow
267+
* row, and until #1609 that row removed the whole project from cross-repo
268+
* linking, as source AND as target. */
269+
TEST(dockerfile_missing_final_newline_not_flagged_issue1610) {
270+
const char *src = "FROM mcr.microsoft.com/dotnet/aspnet:8.0\n"
271+
"ENTRYPOINT [\"dotnet\", \"App.dll\"]"; /* deliberately no \n */
272+
CBMFileResult *r = do_extract(src, CBM_LANG_DOCKERFILE, "Dockerfile");
273+
ASSERT_NOT_NULL(r);
274+
bool flagged = r->parse_incomplete;
275+
cbm_free_result(r);
276+
if (flagged) {
277+
FAIL("a Dockerfile lacking only its final newline must not be parse_partial");
278+
}
279+
PASS();
280+
}
281+
282+
/* The same bytes WITH the newline must stay clean — pins the equivalence the
283+
* reporter's matrix proved, so a future change cannot "fix" one by breaking the
284+
* other. */
285+
TEST(dockerfile_with_final_newline_still_clean_issue1610) {
286+
const char *src = "FROM mcr.microsoft.com/dotnet/aspnet:8.0\n"
287+
"ENTRYPOINT [\"dotnet\", \"App.dll\"]\n";
288+
CBMFileResult *r = do_extract(src, CBM_LANG_DOCKERFILE, "Dockerfile");
289+
ASSERT_NOT_NULL(r);
290+
bool flagged = r->parse_incomplete;
291+
cbm_free_result(r);
292+
if (flagged) {
293+
FAIL("a terminated Dockerfile must not be parse_partial");
294+
}
295+
PASS();
296+
}
297+
298+
/* Language-general, not a Dockerfile patch: these four were each proven to flag
299+
* on a stripped trailing newline. */
300+
TEST(missing_final_newline_not_flagged_across_grammars_issue1610) {
301+
struct {
302+
const char *src;
303+
CBMLanguage lang;
304+
const char *path;
305+
} cases[] = {
306+
{"proc foo {} {}\nproc bar {} {}", CBM_LANG_TCL, "a.tcl"},
307+
{"function foo\n echo hi\nend", CBM_LANG_FISH, "a.fish"},
308+
{"module example.com/m\n\ngo 1.21", CBM_LANG_GOMOD, "go.mod"},
309+
{"general {\n gaps_in = 5\n}", CBM_LANG_HYPRLANG, "hypr.conf"},
310+
};
311+
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
312+
CBMFileResult *r = do_extract(cases[i].src, cases[i].lang, cases[i].path);
313+
ASSERT_NOT_NULL(r);
314+
bool flagged = r->parse_incomplete;
315+
if (flagged) {
316+
fprintf(stderr, " %s flagged: ranges=%s\n", cases[i].path,
317+
r->error_ranges ? r->error_ranges : "(none)");
318+
}
319+
cbm_free_result(r);
320+
if (flagged) {
321+
FAIL("an unterminated final line must not be parse_partial in any grammar");
322+
}
323+
}
324+
PASS();
325+
}
326+
327+
/* GUARD (the reason this suppression is safe rather than convenient): the rule
328+
* is ZERO-WIDTH AT EOF only. A real failure earlier in the file must still be
329+
* reported, and its range must name the broken line — not be swallowed along
330+
* with the terminator. */
331+
TEST(real_error_before_eof_still_flagged_without_final_newline_issue1610) {
332+
/* Built from C_IFDEF_SPLIT, the fixture this suite already proves is
333+
* flagged, with its trailing newline removed. Two conditions now hold at
334+
* once: a genuine width-bearing ERROR mid-file, AND an unterminated last
335+
* line. Suppressing the EOF terminator must not swallow the real one. */
336+
size_t n = strlen(C_IFDEF_SPLIT);
337+
char *unterminated = (char *)malloc(n + 1);
338+
ASSERT_NOT_NULL(unterminated);
339+
memcpy(unterminated, C_IFDEF_SPLIT, n);
340+
unterminated[n - 1] = '\0'; /* drop the final newline */
341+
342+
CBMFileResult *r = do_extract(unterminated, CBM_LANG_C, "split.c");
343+
free(unterminated);
344+
ASSERT_NOT_NULL(r);
345+
bool flagged = r->parse_incomplete;
346+
bool has_ranges = r->error_ranges != NULL;
347+
cbm_free_result(r);
348+
if (!flagged) {
349+
FAIL("a real mid-file parse failure must still be reported when the file also lacks its final newline");
350+
}
351+
if (!has_ranges) {
352+
FAIL("a reported failure must still name its line range");
353+
}
354+
PASS();
355+
}
356+
357+
/* GUARD: a MISSING/ERROR node WITH WIDTH at EOF is a genuine loss and must
358+
* still be flagged. A Makefile whose final recipe line lacks its newline really
359+
* does drop the recipe from the tree — cbm's flag is honest there. */
360+
TEST(width_bearing_error_at_eof_still_flagged_issue1610) {
361+
const char *src = "all:\n\techo hi"; /* no trailing newline; recipe is lost */
362+
CBMFileResult *r = do_extract(src, CBM_LANG_MAKEFILE, "Makefile");
363+
ASSERT_NOT_NULL(r);
364+
bool flagged = r->parse_incomplete;
365+
cbm_free_result(r);
366+
if (!flagged) {
367+
FAIL("a width-bearing parse failure at EOF must still be reported");
368+
}
369+
PASS();
370+
}
371+
250372
SUITE(parse_coverage) {
251373
RUN_TEST(c_ifdef_split_brace_sets_parse_incomplete);
252374
RUN_TEST(c_ifdef_split_brace_neighbors_still_extracted);
@@ -257,4 +379,9 @@ SUITE(parse_coverage) {
257379
RUN_TEST(py_clean_file_not_flagged);
258380
RUN_TEST(error_region_cap_is_honored);
259381
RUN_TEST(c_trailing_recovered_defs_keep_flag);
382+
RUN_TEST(dockerfile_missing_final_newline_not_flagged_issue1610);
383+
RUN_TEST(dockerfile_with_final_newline_still_clean_issue1610);
384+
RUN_TEST(missing_final_newline_not_flagged_across_grammars_issue1610);
385+
RUN_TEST(real_error_before_eof_still_flagged_without_final_newline_issue1610);
386+
RUN_TEST(width_bearing_error_at_eof_still_flagged_issue1610);
260387
}

0 commit comments

Comments
 (0)