]> git.sesse.net Git - nageru/commitdiff
Fix handling of truncated frame files.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 28 Mar 2019 08:25:13 +0000 (09:25 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 28 Mar 2019 08:25:13 +0000 (09:25 +0100)
futatabi/main.cpp

index e6556d37aa91bff568111d4da8cadc4e609f8ac5..a4b1e422937887652b5821000f824cc446537f9b 100644 (file)
@@ -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) {