]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Change little endian read routines to compute in unsigned math.
authorJoerg Sonnenberger <joerg@bec.de>
Sat, 29 Apr 2017 16:56:16 +0000 (18:56 +0200)
committerJoerg Sonnenberger <joerg@bec.de>
Sat, 29 Apr 2017 16:56:16 +0000 (18:56 +0200)
Shifting a negative value is UB in ISO C.

libarchive/test/test_write_format_zip_compression_store.c
libarchive/test/test_write_format_zip_large.c

index 9d703a1df25304adc066d112cb1c03035a76d584..c969a41d4d41c94da4e7a57679c0fd50f92e7efe 100644 (file)
@@ -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)
 {
index 88788b56d50dc736c5488bf9ad8e106643acbf23..2f98c6d4db8a528b027cb21010fbe696e447df15 100644 (file)
@@ -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