Commits

Leyorus committed 2d4bbcf

Fix bad input file format handling

  • Participants
  • Parent commits 5341836

Comments (0)

Files changed (5)

File src/CMakeLists.txt

 
 pkg_check_modules(GTKMM gtkmm-2.4) # look into FindPkgConfig.cmake, 
 
-set(CMAKE_CXX_FLAGS "-O2 -Wall")
+set(CMAKE_CXX_FLAGS "-g -Wall")
 
 
 file(GLOB_RECURSE core_source_files	core/* )

File src/core/MkvInfoParser.cpp

 
 	Reader loader;
 
-	uint64 bytesRead;
-	bytesRead = infos.loadSeekHead(srcFile, srcFileSize);
-	bytesRead += infos.loadViaSeekHead(srcFile, &loader, false);
+	uint64 bytesRead = 0;
+	bool file_isok = infos.loadSeekHead(srcFile, srcFileSize);
 
-	if (!bytesRead || infos.segments_.empty()) {
-		std::cerr << "Error : the input file is not a valid matroska file : " << mkvFileName << std::endl;
+	if (file_isok) {
+		file_isok = infos.loadViaSeekHead(srcFile, &loader, false);
 	}
 
-	MatroskaDoc::TSegment & segment = infos.segments_.back();
-	typedef std::deque<Segment::TTracks::TPayload::TTrack> TTrackList;
-	TTrackList trackList = segment.payload_.tracks_.payload_.tracks_;
-
-	for (TTrackList::iterator it = trackList.begin(); it != trackList.end(); it++) {
-		track_info_t track;
-		track.num = toString(it->payload_.trackNumber_.payload_.get() - 1); // (-1) used for compatiblity with mkvextract utility (to solve later)
-		track.type = getTrackTypeName(it->payload_.trackType_.payload_.get());
-		track.codec = it->payload_.codecID_.payload_.get();
-		track.language = it->payload_.language_.payload_.get();
-		tracks.push_back(track);
+	if (!file_isok || infos.segments_.empty()) {
+		std::cerr << "Error : the input file is not a valid matroska file : "
+				<< mkvFileName << std::endl;
+	} else {
+		MatroskaDoc::TSegment & segment = infos.segments_.back();
+		typedef std::deque<Segment::TTracks::TPayload::TTrack> TTrackList;
+		TTrackList trackList = segment.payload_.tracks_.payload_.tracks_;
+
+		for (TTrackList::iterator it = trackList.begin(); it != trackList.end();
+				it++) {
+			track_info_t track;
+			track.num = toString(it->payload_.trackNumber_.payload_.get() - 1); // (-1) used for compatiblity with mkvextract utility (to solve later)
+			track.type = getTrackTypeName(
+					it->payload_.trackType_.payload_.get());
+			track.codec = it->payload_.codecID_.payload_.get();
+			track.language = it->payload_.language_.payload_.get();
+			tracks.push_back(track);
+		}
 	}
 
 	// closing file handles:

File src/gtk-gui/MainWindow.cpp

 
 void MainWindow::onFileSet() {
 
-	trackList.set_sensitive(true);
-
 	tracksToExtract.clear();
 
-	printTracksInfos(Core::MkvInfoParser::parseTracksInfos(getInputFileName()));
+	std::vector<Core::track_info_t> tracks_infos = Core::MkvInfoParser::parseTracksInfos(getInputFileName());
+	printTracksInfos(tracks_infos);
 
 	outputFileButton.set_current_folder(dirName(getInputFileName()));
-	labelStatus.set_text(statusLabelTextChooseTracks);
 
 	checkUserSelection();
+
+	if (tracks_infos.size() == 0) {
+		trackList.set_sensitive(false);
+		labelStatus.set_text(statusLabelTextChooseInputFile);
+		Gtk::MessageDialog dialog(*this, _("Wrong input file format"), false, Gtk::MESSAGE_ERROR);
+		dialog.set_secondary_text(_("The input file is not a valid matroska file or doesn't contain any track.\n"
+								  "Please choose another input file."));
+		dialog.run();
+	} else {
+		trackList.set_sensitive(true);
+		labelStatus.set_text(statusLabelTextChooseTracks);
+	}
+
 }
 
 void MainWindow::onFolderChanged()

File src/yamka/yamkaEBML.cpp

     Bytes oneByte(1);
     uint64 bytesReadTotal = 0;
     
-    // skip forward until we load EBML head element:
-    while (bytesToRead)
-    {
+//    // skip forward until we load EBML head element:
+//    while (bytesToRead)
+//    {
       uint64 headSize = EbmlDoc::head_.load(storage, bytesToRead, loader);
       if (headSize)
       {
         bytesToRead -= headSize;
         bytesReadTotal += headSize;
-        break;
+//        break;
       }
-      
-      storage.load(oneByte);
-      bytesToRead--;
-    }
+//
+//      storage.load(oneByte);
+//      bytesToRead--;
+//    }
     
     return bytesReadTotal;
   }

File src/yamka/yamkaMatroska.cpp

                                    IDelegateLoad * loader)
   {
     // let the base class load the EBML header:
+
     uint64 bytesReadTotal = EbmlDoc::load(storage, bytesToRead, loader);
+
     bytesToRead -= bytesReadTotal;
     
-    // read Segments:
-    while (true)
-    {
-      uint64 prevBytesToRead = bytesToRead;
-      
-      bytesToRead -= eltsLoad(segments_, storage, bytesToRead, loader);
-      
-      uint64 bytesRead = prevBytesToRead - bytesToRead;
-      bytesReadTotal += bytesRead;
-      
-      if (!bytesRead)
-      {
-        break;
-      }
-    }
-    
-    // resolve positional references (seeks, cues, clusters, etc...):
-    resolveReferences();
+    if (bytesReadTotal != 0) { // check if ebml header was read
+		// read Segments:
+		while (true) {
+			uint64 prevBytesToRead = bytesToRead;
+
+			bytesToRead -= eltsLoad(segments_, storage, bytesToRead, loader);
+
+			uint64 bytesRead = prevBytesToRead - bytesToRead;
+			bytesReadTotal += bytesRead;
+
+			if (!bytesRead) {
+				break;
+			}
+		}
+
+		// resolve positional references (seeks, cues, clusters, etc...):
+		resolveReferences();
+	}
     
     return bytesReadTotal;
   }