tar: Explain reason for malformed header

This commit is contained in:
Michael Forney 2017-02-03 14:56:49 -08:00 committed by Hiltjo Posthuma
parent 46495ea902
commit 772a40188b

22
tar.c
View File

@ -404,30 +404,38 @@ sanitize(struct header *h)
static void static void
chktar(struct header *h) chktar(struct header *h)
{ {
char tmp[8], *err; char tmp[8], *err, *p = (char *)h;
char *p = (char *)h; const char *reason;
long s1, s2, i; 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; 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; goto bad;
}
memcpy(tmp, h->chksum, sizeof(tmp)); memcpy(tmp, h->chksum, sizeof(tmp));
for (i = 0; i < sizeof(tmp); i++) for (i = 0; i < sizeof(tmp); i++)
if (tmp[i] == ' ') if (tmp[i] == ' ')
tmp[i] = '\0'; tmp[i] = '\0';
s1 = strtol(tmp, &err, 8); s1 = strtol(tmp, &err, 8);
if (s1 < 0 || *err != '\0') if (s1 < 0 || *err != '\0') {
reason = "invalid checksum";
goto bad; goto bad;
}
memset(h->chksum, ' ', sizeof(h->chksum)); memset(h->chksum, ' ', sizeof(h->chksum));
for (i = 0, s2 = 0; i < sizeof(*h); i++) for (i = 0, s2 = 0; i < sizeof(*h); i++)
s2 += (unsigned char)p[i]; s2 += (unsigned char)p[i];
if (s1 != s2) if (s1 != s2) {
reason = "incorrect checksum";
goto bad; goto bad;
}
memcpy(h->chksum, tmp, sizeof(h->chksum)); memcpy(h->chksum, tmp, sizeof(h->chksum));
return; return;
bad: bad:
eprintf("malformed tar archive\n"); eprintf("malformed tar archive: %s\n", reason);
} }
static void static void