* Wouter Verhelst <wou...@debian.org>, 2014-04-10, 12:42:
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,
I believe that this code triggers undefined behavior. My C99 draft reads:
The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
bits are filled with zeros. […] If E1 has a signed type and
nonnegative value, and E1 × 2^(E2) is representable in the result
type, then that is the resulting value; otherwise, the behavior is
undefined.
Yes; the standard does this to allow for machine architectures which do
not use two's complement to store negative values. I did mention that
assumption in my previous mail.
I thought you were referring to use of ~ on a signed integer, which is
implementation-defined.
Here's a way to compute OFFT_MAX (hopefully) without any undefined
behavior:
-((off_t)-2 * ((off_t)1 << (sizeof (off_t) * CHAR_BIT - 2)) + 1)
--
Jakub Wilk
--
To UNSUBSCRIBE, email to debian-devel-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: https://lists.debian.org/20140410121827.ga9...@jwilk.net