[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [gpsd-dev] Warnings on 32 bit Fedora 24
From: |
Juha Nurmela |
Subject: |
Re: [gpsd-dev] Warnings on 32 bit Fedora 24 |
Date: |
Sat, 23 Jul 2016 04:42:44 +0300 |
> > fld &= ~(~0ULL << width)
Isn't the ubits() function little bit dangerous?
The prototype and comments look like that it promises to extract 64 bits wide
fields, but it will quietly fail with unaligned really wide fields. Variable
fld is asked to hold more than 64 bits in certain cases.
Juha
Not wanting to whine with a sweatless shirt,
{
int remain = width;
uint64_t fld = 0;
assert(width <= sizeof(uint64_t) * CHAR_BIT);
buf += start / CHAR_BIT;
start %= CHAR_BIT;
if (start) {
int piece = CHAR_BIT - start; /* might be more than remains */
fld = *buf++ & (0xFF >> start); /* get lsbits */
remain -= piece;
if (remain < 0) {
fld >>= -remain; /* just drop excess bits and all done */
remain = 0;
}
}
while (remain >= CHAR_BIT) {
remain -= CHAR_BIT;
fld <<= CHAR_BIT;
fld |= *buf++;
}
if (remain) {
fld <<= remain;
fld |= *buf >> (CHAR_BIT - remain); /* get msbits, rightjusted */
}
/* was extraction as a little-endian requested? */