GNU bug report logs -
#15924
[PATCH] dfa: avoid undefined behavior of "1 << 31"
Previous Next
Reported by: Jim Meyering <jim <at> meyering.net>
Date: Tue, 19 Nov 2013 01:56:02 UTC
Severity: normal
Tags: patch
Done: Jim Meyering <jim <at> meyering.net>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
On Mon, Nov 18, 2013 at 6:16 PM, Paul Eggert <eggert <at> cs.ucla.edu> wrote:
> Jim Meyering wrote:
>> static int
>> tstbit (unsigned int b, charclass const c)
>> {
>> - return c[b / INTBITS] & 1 << b % INTBITS;
>> + return c[b / INTBITS] & 1U << b % INTBITS;
>> }
>
> On a machine with 32-bit int and where b % INTBITS is 31,
> the expression c[b / INTBITS] & 1U << b % INTBITS
> is of type 'unsigned' and can have the value 2**31, and
> this will overflow when tstbit converts that value as an int,
> leading to implementation-defined behavior, which can include
> raising a signal.
>
> Better would be something like this:
>
> static bool
> tstbit (unsigned int b, charclass const c)
> {
> return c[b / INTBITS] >> b % INTBITS & 1;
> }
>
> and it'd probably be better to encourage this style in
> other places where the problem occurs, e.g., quotearg.
Good point. "bool" is a better return type, too.
I will adjust.
Thanks, Paul.
This bug report was last modified 11 years and 184 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.