[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Having fun with the following C code (UB)



On Thu, Mar 27, 2014 at 09:07:14AM +0100, Mathieu Malaterre wrote:
> Here is a little bug I just discovered:
> 
> http://stackoverflow.com/questions/22664658/finding-off-t-size
> 
> For reference, here are the packages affected in debian:
> 
> http://codesearch.debian.net/search?q=LARGE_OFF_T
> 
> For reference clang fails as was expected by the initial author, but
> recent gcc (default C compiler on debian), simply issue a warning.

I've had to figure out the size of off_t in nbd-server, and have been
doing it without relying on overflow, for years now. It took quite a few
iterations to get it right, but the current definition has looked like
this since 2006:

#define OFFT_MAX ~((off_t)1<<(sizeof(off_t)*8-1))

i.e., left-shift 1 by enough bits so that the most significant bit is
set, then flip all bits so you end up with the highest positive value
that fits in an off_t.

Obviously that requires an architecture which uses two's complement, but
then I doubt any architecture that doesn't has been popular since the
late seventies.

-- 
It is easy to love a country that is famous for chocolate and beer

  -- Barack Obama, speaking in Brussels, Belgium, 2014-03-26


Reply to: