X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=futatabi%2Fmain.cpp;fp=futatabi%2Fmain.cpp;h=a4b1e422937887652b5821000f824cc446537f9b;hb=18199cc2f756bd8eb17a475882dbc738194cbee8;hp=e6556d37aa91bff568111d4da8cadc4e609f8ac5;hpb=9ffd4f03f314cc6e0254449593def95c9bc203d6;p=nageru diff --git a/futatabi/main.cpp b/futatabi/main.cpp index e6556d3..a4b1e42 100644 --- a/futatabi/main.cpp +++ b/futatabi/main.cpp @@ -305,6 +305,18 @@ void load_frame_file(const char *filename, const string &basename, unsigned file 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)) { @@ -360,9 +372,10 @@ void load_frame_file(const char *filename, const string &basename, unsigned file 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) {