Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#160 gcc -Wtype-limits: In function ‘yy_get_next_buffer’: comparison of unsigned expression < 0

2.5.37
open
nobody
None
5
2014-11-18
2013-09-22
No

I use flex-2.5.37-30-g0a3f0d7 and gcc 4.8.2 20130912 (prerelease)

Compiling a flex-generated file with gcc produces the warning

src/file-lex.c: In function ‘yy_get_next_buffer’:
src/file-lex.c:2046:18: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
while ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \ ^
src/file-lex.c:2642:3: note: in expansion of macro ‘YY_INPUT’
YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]),
^

Can somebody check, if this warning for pointless comparison is justified?

Discussion

  • Manveru
    Manveru
    2014-11-18

    I painfully faced that in 2.5.39, so the bug is still present there. And yes - warning is justified, as the code is wrong. In version 2.5.35 the type of yy_n_chars is int, while in 2.5.39 the type is yy_size_t. The yy_size_t is internal type mapped to size_t which is unsigned. The YY_INPUT macro contains following code:

    #define YY_INPUT(buf,result,max_size) \
        errno=0; \
        while ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \
        { \
                if( errno != EINTR) \
    

    then the macro is then invoked:

    YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]),
                        yyg->yy_n_chars, num_to_read );
    

    So the code casts signed result of read() to unsigned and then compare if it's lower than 0. The bug is obvious. Flex has to be repaired.

    Two options:

    1. Revert yy_n_chars back to int (not much toward 64-bit),
    2. Define yy_ssize_t and use where it's appropriate, including yy_n_chars.