Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions internal/cbm/vendored/grammars/cobol/scanner.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions tests/repro/repro_extraction.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
* Cluster (TIER A, in-process via cbm_extract_file):
* #554 — C++ out-of-line method CALLS source = Module, not enclosing Method
* (more added per wave: #495 #521 #382 #408 #523 #56 #333)
* #2998 — COBOL external scanner livelock at EOF (hang, not a wrong edge)
*/
#include "test_framework.h"
#include "cbm.h"
#include <string.h>
#if !defined(_WIN32)
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#endif

/* Convenience: extract, return result (caller frees). Mirrors test_extraction.c. */
static CBMFileResult *rx(const char *src, CBMLanguage lang, const char *proj, const char *path) {
Expand Down Expand Up @@ -87,7 +94,88 @@ TEST(repro_issue554_cpp_out_of_line_method_class_qualified) {
PASS();
}

/* ───────────────────────────────────────────────────────────────────
* #2998 — COBOL external scanner livelocks the parser at EOF.
*
* Root cause: the vendored COBOL scanner's LINE_PREFIX_COMMENT loop advanced
* while get_column() <= 5 without checking lookahead == 0. tree-sitter's
* advance() is a no-op at EOF (the column never increments), so any input that
* reaches EOF at column <= 5 — e.g. a .cob file shorter than 6 bytes, or the
* virtual empty line after a trailing "\n" in the right parser state — spun
* the scanner forever at 100% CPU. A second hazard emitted a zero-length
* COMMENT_ENTRY token when start_with_word() bailed at "\n"/EOF without
* consuming, another livelock class.
*
* Expected: extraction RETURNS (the hang itself is the failure mode; the
* fork+deadline harness below turns a regression back into a RED test instead
* of a hung CI job), and the canonical valid-program fixture still extracts
* PROGRAM-ID. Actual (buggy): never returns; last worker-log line
* "pass.start pass=definitions".
* ─────────────────────────────────────────────────────────────────── */
#if !defined(_WIN32)
/* Parse src in a forked child under a wall-clock deadline.
* Returns the child's exit code (0 = extraction returned), -1 on timeout
* (child killed; i.e. the livelock reproduced), -2 on fork failure. */
static int repro2998_parse_with_deadline(const char *src, CBMLanguage lang, const char *path) {
fflush(NULL);
pid_t pid = fork();
if (pid < 0)
return -2;
if (pid == 0) {
CBMFileResult *r = cbm_extract_file(src, (int)strlen(src), lang, "t", path, 0, NULL, NULL);
if (r)
cbm_free_result(r);
_exit(0); /* reaching _exit at all is what the bug took away */
}
int status = 0;
for (int i = 0; i < 1000; i++) { /* 10 s in 10 ms steps */
pid_t got = waitpid(pid, &status, WNOHANG);
if (got == pid)
return WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status);
usleep(10 * 1000);
}
kill(pid, SIGKILL);
waitpid(pid, &status, 0);
return -1;
}

TEST(repro_issue2998_cobol_scanner_eof_livelock) {
/* EOF lands inside the LINE_PREFIX_COMMENT column scan: shorter than
* column 6, or a trailing newline leaving the lexer at column 0 of the
* virtual empty last line. */
ASSERT_EQ(repro2998_parse_with_deadline("X", CBM_LANG_COBOL, "m.cob"), 0);
ASSERT_EQ(repro2998_parse_with_deadline("*<<", CBM_LANG_COBOL, "m.cob"), 0);
ASSERT_EQ(repro2998_parse_with_deadline("X\n", CBM_LANG_COBOL, "m.cob"), 0);
ASSERT_EQ(repro2998_parse_with_deadline("display 1.\n", CBM_LANG_COBOL, "m.cob"), 0);
PASS();
}

TEST(repro_issue2998_cobol_valid_program_still_extracts) {
/* The EOF/zero-length guards must not break the happy path: the canonical
* fixed-format fixture (same as the cobol row in grammar_cases.h) still
* yields its PROGRAM-ID definition. */
CBMFileResult *r = rx(" IDENTIFICATION DIVISION.\n"
" PROGRAM-ID. FOO.\n"
" PROCEDURE DIVISION.\n"
" MAIN-PARA.\n"
" STOP RUN.\n",
CBM_LANG_COBOL, "t", "a.cob");
ASSERT_NOT_NULL(r);
int saw_foo = 0;
for (int i = 0; r && i < r->defs.count; i++)
if (r->defs.items[i].name && strcmp(r->defs.items[i].name, "FOO") == 0)
saw_foo = 1;
cbm_free_result(r);
ASSERT_TRUE(saw_foo);
PASS();
}
#endif /* !_WIN32 */

/* ── Suite ──────────────────────────────────────────────────────── */
SUITE(repro_extraction) {
RUN_TEST(repro_issue554_cpp_out_of_line_method_class_qualified);
#if !defined(_WIN32)
RUN_TEST(repro_issue2998_cobol_scanner_eof_livelock);
RUN_TEST(repro_issue2998_cobol_valid_program_still_extracts);
#endif
}
Loading