abort();
}
+ // Find the actual length of the file, since fseek() past the end of the file
+ // will succeed without an error.
+ if (fseek(fp, 0, SEEK_END) == -1) {
+ perror("fseek(SEEK_END)");
+ abort();
+ }
+ off_t file_len = ftell(fp);
+ if (fseek(fp, 0, SEEK_SET) == -1) {
+ perror("fseek(SEEK_SET)");
+ abort();
+ }
+
size_t magic_offset = 0;
size_t skipped_bytes = 0;
while (!feof(fp) && !ferror(fp)) {
frame.filename_idx = filename_idx;
frame.size = hdr.file_size();
- if (fseek(fp, frame.offset + frame.size, SEEK_SET) == -1) {
+ if (frame.offset + frame.size > file_len ||
+ fseek(fp, frame.offset + frame.size, SEEK_SET) == -1) {
fprintf(stderr, "WARNING: %s: Could not seek past frame (probably truncated).\n", filename);
- continue;
+ break;
}
if (hdr.stream_idx() >= 0 && hdr.stream_idx() < MAX_STREAMS) {