From: Joerg Sonnenberger Date: Sat, 29 Apr 2017 16:56:16 +0000 (+0200) Subject: Change little endian read routines to compute in unsigned math. X-Git-Tag: v3.3.2~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=31f4594ca8d7e108c0d13a308ab519eef828541e;p=thirdparty%2Flibarchive.git Change little endian read routines to compute in unsigned math. Shifting a negative value is UB in ISO C. --- diff --git a/libarchive/test/test_write_format_zip_compression_store.c b/libarchive/test/test_write_format_zip_compression_store.c index 9d703a1df..c969a41d4 100644 --- a/libarchive/test/test_write_format_zip_compression_store.c +++ b/libarchive/test/test_write_format_zip_compression_store.c @@ -108,8 +108,19 @@ static void verify_write_uncompressed(struct archive *a) } /* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */ -static int i2(const char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); } -static int i4(const char *p) { return (i2(p) | (i2(p + 2) << 16)); } +static unsigned int +i2(const void *p_) +{ + const unsigned char *p = p_; + return (p[0] | (p[1] << 8)); +} + +static unsigned int +i4(const void *p_) +{ + const unsigned char *p = p_; + return (i2(p) | (i2(p + 2) << 16)); +} static void verify_uncompressed_contents(const char *buff, size_t used) { diff --git a/libarchive/test/test_write_format_zip_large.c b/libarchive/test/test_write_format_zip_large.c index 88788b56d..2f98c6d4d 100644 --- a/libarchive/test/test_write_format_zip_large.c +++ b/libarchive/test/test_write_format_zip_large.c @@ -67,21 +67,19 @@ static int64_t memory_read_skip(struct archive *, void *, int64_t request); static ssize_t memory_read(struct archive *, void *, const void **buff); static ssize_t memory_write(struct archive *, void *, const void *, size_t); -static int16_t le16(const void *_p) { +static uint16_t le16(const void *_p) { const uint8_t *p = _p; - return (0xff & (int16_t)p[0]) | ((0xff & (int16_t)p[1]) << 8); + return p[0] | (p[1] << 8); } -static int32_t le32(const void *_p) { +static uint32_t le32(const void *_p) { const uint8_t *p = _p; - int32_t v = 0xffff & (int32_t)le16(_p); - return v + ((0xffff & (int32_t)le16(p + 2)) << 16); + return le16(p) | ((uint32_t)le16(p + 2) << 16); } -static int64_t le64(const void *_p) { +static uint64_t le64(const void *_p) { const uint8_t *p = _p; - int64_t v = 0xffffffff & (int64_t)le32(_p); - return v + ((0xffffffff & (int64_t)le32(p + 4)) << 32); + return le32(p) | ((uint64_t)le32(p + 4) << 32); } static ssize_t