Skip to content

fix(gen): keep a full-speed scanner inside yy_transition after a NUL - #908

Merged
westes merged 4 commits into
westes:masterfrom
anshal-lang:fullspd-nul-jam-entries
Nov 26, 2026
Merged

westes merged 4 commits into
westes:masterfrom
anshal-lang:fullspd-nul-jam-entries

Conversation

@anshal-lang

Copy link
Copy Markdown
Contributor

a NUL in the scanned input has the same equivalence class as the end-of-buffer character, so a -F/-CF scanner takes the end-of-buffer transition on it and lands on the state that genctbl() and mkctbl() put at base[lastdfa + 1] = tblend + 2, the last entry of yy_transition. the scanning loop indexes a state once more after it has taken a transition, with the class of the next character, so it then reads yy_transition[tblend + 2 + yy_c] for any class from 1 up. both emitters say they assume the only character that can index that state is the end-of-buffer one, which holds at a real end of buffer, where a second end-of-buffer character follows, but not for a NUL in the middle of the input. scanning a b NUL c d \377 e f gives an ASan global-buffer-overflow READ in yylex with inline tables and a heap-buffer-overflow with --tables-file, and an entry past the table that happens to match yy_verify sends yy_current_state to an arbitrary offset. giving that state its missing jam entries keeps the change in the two places that lay out the table and leaves every existing entry alone, so these scanners jam there for any class as they already did for class 0, and count the same tokens as the compressed and full-table ones.

tests/fast_nul fails before and passes after, without needing a sanitizer. the rest of the suite is unchanged here, including the tableopts serialize and verify tests for every table option on the nr, r and c99 back ends, and flex still regenerates its own scanner bit-identically. C++ scanners are not affected, since flex refuses -+ with -CF.

Refs: #628

anshal-lang and others added 4 commits November 26, 2026 14:01
A NUL in the input has the same equivalence class as the end-of-buffer
character, so a -F/-CF scanner takes the end-of-buffer transition on it
and lands on the state at the last entry of yy_transition. The scanning
loop then indexes that state again with the class of the next character
and reads past the table. Emit a jam for every class after that state in
genctbl() and mkctbl(), so it jams for any class as it already did for
the end-of-buffer one.

Refs: westes#628
Since an <<EOF>> rule is reduced before the scanner has read its
action, the directive and opening quote for it went in before we knew
whether the action was '|'. A '|' rule has no action text of its own and
so nothing closes that quote, and m4 stopped with "end of file in
string".

Emit them from the sect2 rule in parse.y instead, once the end of the
rule has been read, and skip them for '|' as finish_rule() already does.
tests/eof_continued.l covers an <<EOF>> rule with a '|' action both
outside and inside a start condition.

fixes westes#906
fast_nul only covers a scanner with its tables built in. A scanner that
loads them from a tables file reads them from a separate block of
memory, so the same read past the end of the table looks different.

tests/fast_nul_tables.l gives such a scanner a NUL in its input and uses
the allocator in tables_bad.h, which puts each block against an
unreadable page, so a read past the table stops the test without a
sanitizer.

Refs: westes#628
@westes
westes merged commit 1b77c6c into westes:master Nov 26, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants