Skip to content

Batch scanners swallow a NUL byte into the previous token (since 2.6.5) #885

Closed

Description

@1-Olivier

Summary

In a batch scanner with compressed tables, when the input contains a NUL byte that cannot extend the current match, the NUL is swallowed into the previous token.

flex 2.6.4 is correct. 2.6.5, 2.6.15 and current master (v2.7.2-1-gaa34df3) are wrong. Interactive scanners and full tables are not affected. The default, C++ and c99 back ends are all affected.

We hit it with a scanner for a binary format, where a one-byte token is often followed by a NUL byte.

Reproducer

nul.l:

%option noyywrap nounput noinput
%{
#include <stdio.h>
%}
%%
"a"     { printf("rule a:   yyleng=%d\n", (int)yyleng); }
\0      { printf("rule NUL: yyleng=%d\n", (int)yyleng); }
.|\n    { printf("rule any: yyleng=%d '%c'\n", (int)yyleng, yytext[0]); }
%%
int main(void)
{
        /* 'a' then a NUL byte then 'b'. Expected: a, NUL, any 'b'. */
        static const char input[] = { 'a', '\0', 'b' };
        yy_scan_bytes(input, sizeof input);
        yylex();
        return 0;
}
flex -B -o nul.c nul.l && cc -o nul nul.c && ./nul

flex 2.6.4 (expected):

rule a:   yyleng=1
rule NUL: yyleng=1
rule any: yyleng=1 'b'

flex 2.6.5 and later:

rule a:   yyleng=2
rule any: yyleng=1 'b'

Cause

In the YY_END_OF_BUFFER case of yylex(), when yy_try_NUL_trans() finds no transition, 2.6.4 generated:

} else {
        yy_cp = (yy_last_accepting_cpos);
        yy_current_state = (yy_last_accepting_state);
        goto yy_find_action;
}

2.6.5 and later generate only goto yy_find_action;, leaving yy_cp one byte past the NUL.

In the skeletons (src/cpp-flex.skl, src/c99-flex.skl and src/go-flex.skl), that back-up has been guarded by m4_ifdef([[M4_NOT_MODE_INTERACTIVE]], ...) since e7b3a67. Nothing defines M4_NOT_MODE_INTERACTIVE; src/main.c defines M4_MODE_NO_INTERACTIVE instead.

A pull request with the fix and tests follows.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions