Skip to content

Type of yy_n_chars changed to yy_size_t causes warnings with comparison to zero #53

@srivasta

Description

@srivasta

Hi,

This was reported by a Debian user in Bug 770161

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.

Manoj

Here is how to reproduce:

credit.l.txt

flex -Cr credit.l
_> gcclint lex.yy.c
<>

lex.yy.c:1203:3: note: in expansion of macro ‘YY_INPUT’
   YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]),
   ^
lex.yy.c:781:20: warning: conversion to ‘yy_size_t {aka long unsigned int}’ from ‘ssize_t {aka long int}’ may change the sign of the result [-Wsign-conversion]
  while ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \
                    ^
lex.yy.c:1203:3: note: in expansion of macro ‘YY_INPUT’
   YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]),
   ^
lex.yy.c:781:66: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
  while ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \
                                                                  ^
lex.yy.c:1203:3: note: in expansion of macro ‘YY_INPUT’
   YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]),
   ^
lex.yy.c: In function ‘input’:
lex.yy.c:1363:23: warning: conversion to ‘yy_size_t {aka long unsigned int}’ from ‘long int’ may change the sign of the result [-Wsign-conversion]
    yy_size_t offset = (yy_c_buf_p) - (yytext_ptr);
                       ^

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions