kit/package: Error on short file read

* It's safe to assume that if the file is shorter than
  the provided header, things will go poorly.
* Avoids a random vauge ReadBuffer error.
* This doesn't fix #15230, but makes the issue clearer.

Change-Id: I3471e6de384a0c9be94049ad891c01be980f7846
Reviewed-on: https://review.haiku-os.org/c/1679
Reviewed-by: Adrien Destugues <pulkomandy@gmail.com>
This commit is contained in:
Alexander von Gluck IV 2019-08-05 08:34:14 -05:00 committed by Alex von Gluck IV
parent 02e836dc3c
commit 6c331fc7e1

View File

@ -451,6 +451,13 @@ ReaderImplBase::Init(BPositionIO* file, bool keepFile, Header& header, uint32 fl
fileSize = -1;
}
// validate file is longer than header (when not a stream)
if (fileSize >= 0 && fileSize < (off_t)sizeof(header)) {
ErrorOutput()->PrintError("Error: Invalid %s file: Length shorter than "
"header!\n", fFileType);
return B_BAD_DATA;
}
// read the header
if ((error = ReadBuffer(0, &header, sizeof(header))) != B_OK)
return error;