Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/bsd/libarchive/dist/cpio/test Fix undefined behavio...



details:   https://anonhg.NetBSD.org/src/rev/10dcbd490912
branches:  trunk
changeset: 745272:10dcbd490912
user:      kamil <kamil%NetBSD.org@localhost>
date:      Thu Feb 27 10:32:35 2020 +0000

description:
Fix undefined behavior in from_hex()

Cherry-pick upstream patch:

>From 4a5fc0b980a23d1a317c7a564a94cca090e111c2 Mon Sep 17 00:00:00 2001
From: Martin Matuska <martin%matuska.org@localhost>
Date: Thu, 27 Feb 2020 10:42:27 +0100
Subject: [PATCH] bsdcpio_test: from_hex() needs to be an uint32_t

Fixes #1339

https://github.com/libarchive/libarchive/commit/4a5fc0b980a23d1a317c7a564a94cca090e111c2

diffstat:

 external/bsd/libarchive/dist/cpio/test/test_format_newc.c |  33 ++++++++------
 1 files changed, 18 insertions(+), 15 deletions(-)

diffs (114 lines):

diff -r 3c5342cd37ad -r 10dcbd490912 external/bsd/libarchive/dist/cpio/test/test_format_newc.c
--- a/external/bsd/libarchive/dist/cpio/test/test_format_newc.c Thu Feb 27 06:30:55 2020 +0000
+++ b/external/bsd/libarchive/dist/cpio/test/test_format_newc.c Thu Feb 27 10:32:35 2020 +0000
@@ -49,10 +49,11 @@
        return (1);
 }
 
-static int
+/* Convert up to 8 hex characters to unsigned 32-bit decimal integer */
+static uint32_t
 from_hex(const char *p, size_t l)
 {
-       int r = 0;
+       uint32_t r = 0;
 
        while (l > 0) {
                r *= 16;
@@ -82,11 +83,11 @@
 {
        FILE *list;
        int r;
-       int devmajor, devminor, ino, gid;
-       int uid = -1;
+       uint32_t devmajor, devminor, ino, gid, uid;
        time_t t, t2, now;
        char *p, *e;
-       size_t s, fs, ns;
+       size_t s;
+       uint64_t fs, ns;
        char result[1024];
 
        assertUmask(0);
@@ -199,9 +200,11 @@
 #else
        assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */
 #endif 
-       if (uid < 0)
-               uid = from_hex(e + 22, 8);
+#if defined(_WIN32)
+       uid = from_hex(e + 22, 8);
+#else
        assertEqualInt(from_hex(e + 22, 8), uid); /* uid */
+#endif
        gid = from_hex(e + 30, 8); /* gid */
        assertEqualMem(e + 38, "00000003", 8); /* nlink */
        t = from_hex(e + 46, 8); /* mtime */
@@ -215,14 +218,14 @@
            "       first appearance should be empty, so this file size\n"
            "       field should be zero");
        assertEqualInt(0, from_hex(e + 54, 8)); /* File size */
-       fs = from_hex(e + 54, 8);
+       fs = (uint64_t)from_hex(e + 54, 8);
        fs += PAD(fs, 4);
        devmajor = from_hex(e + 62, 8); /* devmajor */
        devminor = from_hex(e + 70, 8); /* devminor */
        assert(is_hex(e + 78, 8)); /* rdevmajor */
        assert(is_hex(e + 86, 8)); /* rdevminor */
        assertEqualMem(e + 94, "00000006", 8); /* Name size */
-       ns = from_hex(e + 94, 8);
+       ns = (uint64_t)from_hex(e + 94, 8);
        ns += PAD(ns + 2, 4);
        assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
        assertEqualMem(e + 110, "file1\0", 6); /* Name contents */
@@ -249,14 +252,14 @@
                    " at t2=%#08jx", (intmax_t)t, (intmax_t)t2);
                assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
                assertEqualMem(e + 54, "00000005", 8); /* File size */
-               fs = from_hex(e + 54, 8);
+               fs = (uint64_t)from_hex(e + 54, 8);
                fs += PAD(fs, 4);
                assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
                assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
                assert(is_hex(e + 78, 8)); /* rdevmajor */
                assert(is_hex(e + 86, 8)); /* rdevminor */
                assertEqualMem(e + 94, "00000008", 8); /* Name size */
-               ns = from_hex(e + 94, 8);
+               ns = (uint64_t)from_hex(e + 94, 8);
                ns += PAD(ns + 2, 4);
                assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
                assertEqualMem(e + 110, "symlink\0\0\0", 10); /* Name contents */
@@ -285,14 +288,14 @@
            "t2=%#08jx", (intmax_t)t, (intmax_t)t2);
        assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
        assertEqualMem(e + 54, "00000000", 8); /* File size */
-       fs = from_hex(e + 54, 8);
+       fs = (uint64_t)from_hex(e + 54, 8);
        fs += PAD(fs, 4);
        assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
        assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
        assert(is_hex(e + 78, 8)); /* rdevmajor */
        assert(is_hex(e + 86, 8)); /* rdevminor */
        assertEqualMem(e + 94, "00000004", 8); /* Name size */
-       ns = from_hex(e + 94, 8);
+       ns = (uint64_t)from_hex(e + 94, 8);
        ns += PAD(ns + 2, 4);
        assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
        assertEqualMem(e + 110, "dir\0\0\0", 6); /* Name contents */
@@ -319,14 +322,14 @@
            "t2=%#08jx", (intmax_t)t, (intmax_t)t2);
        assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */
        assertEqualInt(10, from_hex(e + 54, 8)); /* File size */
-       fs = from_hex(e + 54, 8);
+       fs = (uint64_t)from_hex(e + 54, 8);
        fs += PAD(fs, 4);
        assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */
        assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */
        assert(is_hex(e + 78, 8)); /* rdevmajor */
        assert(is_hex(e + 86, 8)); /* rdevminor */
        assertEqualMem(e + 94, "00000009", 8); /* Name size */
-       ns = from_hex(e + 94, 8);
+       ns = (uint64_t)from_hex(e + 94, 8);
        ns += PAD(ns + 2, 4);
        assertEqualInt(0, from_hex(e + 102, 8)); /* check field */
        assertEqualMem(e + 110, "hardlink\0\0", 10); /* Name contents */



Home | Main Index | Thread Index | Old Index