From 28085731f1d25de0aee5e5809e9780a0715bb84f Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 6 Nov 2022 14:14:06 +0100 Subject: [PATCH] Not all 0-sized reads are errors If you requested 0 bytes, the kernel gives you 0 bytes back --- src/util.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/util.c b/src/util.c index 23efd44..4bdd8e7 100644 --- a/src/util.c +++ b/src/util.c @@ -66,6 +66,9 @@ void minitar_parse_tar_header(struct tar_header* hdr, struct minitar_entry_metad int minitar_validate_header(struct tar_header* hdr) { + if (hdr->typeflag != '\0' && hdr->typeflag != '0' && hdr->typeflag != '1' && hdr->typeflag != '2' && + hdr->typeflag != '3' && hdr->typeflag != '4' && hdr->typeflag != '5' && hdr->typeflag != '6') + return 0; return !strncmp(hdr->magic, "ustar", 5); } @@ -73,6 +76,7 @@ int minitar_read_header(struct minitar* mp, struct tar_header* hdr) { size_t rc = fread(hdr, 1, sizeof *hdr, mp->stream); if (rc == 0 && feof(mp->stream)) return 0; + if (rc == 0 && ferror(mp->stream)) minitar_panic("Error while reading file header from tar archive"); if (rc < sizeof *hdr) minitar_panic("Valid tar files should be split in 512-byte blocks"); return 1; } @@ -93,14 +97,22 @@ char* minitar_read_file(struct minitar_entry_metadata* metadata, struct minitar* size_t nread = fread(buf, 1, metadata->size, mp->stream); if (!nread) { - free(buf); - if (feof(mp->stream)) return NULL; - if (ferror(mp->stream)) minitar_panic("Error while reading file data from tar archive"); - __builtin_unreachable(); + if (feof(mp->stream)) + { + free(buf); + return NULL; + } + if (ferror(mp->stream)) + { + free(buf); + minitar_panic("Error while reading file data from tar archive"); + } + } + else + { + long rem = 512 - (nread % 512); + fseek(mp->stream, rem, SEEK_CUR); // move the file offset over to the start of the next block } - - long rem = 512 - (nread % 512); - fseek(mp->stream, rem, SEEK_CUR); // move the file offset over to the start of the next block buf[nread] = 0;