]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
MFC r336113:
authorpfg <pfg@FreeBSD.org>
Mon, 16 Jul 2018 00:23:09 +0000 (00:23 +0000)
committerpfg <pfg@FreeBSD.org>
Mon, 16 Jul 2018 00:23:09 +0000 (00:23 +0000)
commit13c822e44d74955b63f7dcd83aad2a86384bef32
treeb9c780dc09b4dabcaa69eef10d74fbb68032dca6
parent0607c1d75822a476323702fc93e0e0f7e00f4a1f
MFC r336113:
gzip: fix for undefined behavior.

Unportable left shift reported with MKSANITIZER=yes
USE_SANITIZER=undefined:

# progress -zf ./games.tgz  tar -xp -C "./" -f -
/public/src.git/usr.bin/gzip/gzip.c:2126:33: runtime error: left shift of
251 by 24 places cannot be represented in type 'int'
100%
|****************************************************************************************************************|
44500 KiB  119.69 MiB/s    00:00 ETA

Refactor the following code into something that is more clear
and fix signed integer shift, by casting all buf[] elements to
(unsigned int):

unsigned char buf[8];
uint32_t usize;
[...]
else {
    usize = buf[4] | buf[5] << 8 |
            buf[6] << 16 | buf[7] << 24;
[...]

New version:

    usize = buf[4];
    usize |= (unsigned int)buf[5] << 8;
    usize |= (unsigned int)buf[6] << 16;
    usize |= (unsigned int)buf[7] << 24;

Only the "<< 24" part needs explicit cast, but for consistency make the
integer promotion explicit and clear to a code reader.

Sponsored by <The NetBSD Foundation>

Obtained from: NetBSD (CVS rev. 1.113)
usr.bin/gzip/gzip.c