commit 772a40188bf1c8a2612ac13967bad94228c561a4
Author:     Michael Forney <[email protected]>
AuthorDate: Fri Feb 3 14:56:49 2017 -0800
Commit:     Hiltjo Posthuma <[email protected]>
CommitDate: Tue Aug 22 13:35:29 2017 +0200

    tar: Explain reason for malformed header

diff --git a/tar.c b/tar.c
index f213039..53a737c 100644
--- a/tar.c
+++ b/tar.c
@@ -404,30 +404,38 @@ sanitize(struct header *h)
 static void
 chktar(struct header *h)
 {
-       char tmp[8], *err;
-       char *p = (char *)h;
+       char tmp[8], *err, *p = (char *)h;
+       const char *reason;
        long s1, s2, i;
 
-       if (h->prefix[0] == '\0' && h->name[0] == '\0')
+       if (h->prefix[0] == '\0' && h->name[0] == '\0') {
+               reason = "empty filename";
                goto bad;
-       if (h->magic[0] && strncmp("ustar", h->magic, 5))
+       }
+       if (h->magic[0] && strncmp("ustar", h->magic, 5)) {
+               reason = "not ustar format";
                goto bad;
+       }
        memcpy(tmp, h->chksum, sizeof(tmp));
        for (i = 0; i < sizeof(tmp); i++)
                if (tmp[i] == ' ')
                        tmp[i] = '\0';
        s1 = strtol(tmp, &err, 8);
-       if (s1 < 0 || *err != '\0')
+       if (s1 < 0 || *err != '\0') {
+               reason = "invalid checksum";
                goto bad;
+       }
        memset(h->chksum, ' ', sizeof(h->chksum));
        for (i = 0, s2 = 0; i < sizeof(*h); i++)
                s2 += (unsigned char)p[i];
-       if (s1 != s2)
+       if (s1 != s2) {
+               reason = "incorrect checksum";
                goto bad;
+       }
        memcpy(h->chksum, tmp, sizeof(h->chksum));
        return;
 bad:
-       eprintf("malformed tar archive\n");
+       eprintf("malformed tar archive: %s\n", reason);
 }
 
 static void

Reply via email to