From 15863b10b67224bda3ba8056500af1861218af8f Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Sat, 30 Apr 2022 18:53:39 -0230 Subject: [PATCH] Improve processing files that aren't valid ROMs. Now, the extension is actually checked before opening the ROM. Streaming ROMs (MVC) are already taken care of, but still TODO is limit read to Cart::maxSize(). --- src/common/Stack.hxx | 2 +- src/emucore/OSystem.cxx | 15 +++++++++------ src/gui/FileListWidget.cxx | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/common/Stack.hxx b/src/common/Stack.hxx index a08e55d59..8eb48e4f6 100644 --- a/src/common/Stack.hxx +++ b/src/common/Stack.hxx @@ -43,7 +43,7 @@ class FixedStack bool full() const { return _size >= CAPACITY; } T top() const { return _stack[_size - 1]; } - T get(uInt32 pos) { return _stack[pos]; } + T get(uInt32 pos) const { return _stack[pos]; } void push(const T& x) { _stack[_size++] = x; } T pop() { return std::move(_stack[--_size]); } uInt32 size() const { return _size; } diff --git a/src/emucore/OSystem.cxx b/src/emucore/OSystem.cxx index c1159982e..d2c9606d6 100644 --- a/src/emucore/OSystem.cxx +++ b/src/emucore/OSystem.cxx @@ -782,14 +782,17 @@ string OSystem::getROMMD5(const FilesystemNode& rom) const ByteBuffer OSystem::openROM(const FilesystemNode& rom, size_t& size, bool showErrorMessage) const { - // First check if this is a 'streaming' ROM (one where we only read - // a portion of the file) + // First check if this is a valid ROM filename + const bool isValidROM = rom.isFile() && Bankswitch::isValidRomName(rom); + if(!isValidROM && showErrorMessage) + throw runtime_error("Unrecognized ROM file type"); + + // Next check for a proper file size: + // Streaming ROMs read only a portion of the file + // TODO: Otherwise, cap the size to the maximum Cart size const size_t sizeToRead = CartDetector::isProbablyMVC(rom); - // Next check if rom is a valid size - // TODO: We should check if ROM is < Cart::maxSize(), but only - // if it's not a ZIP file (that size should be higher; still TBD) - + // Now we can try to open the file ByteBuffer image; try { diff --git a/src/gui/FileListWidget.cxx b/src/gui/FileListWidget.cxx index 9c8d831cb..8ad16a624 100644 --- a/src/gui/FileListWidget.cxx +++ b/src/gui/FileListWidget.cxx @@ -166,7 +166,7 @@ FileListWidget::IconType FileListWidget::getIconType(const string& path) const ? IconType::zip : IconType::directory; } else - return node.isFile() && Bankswitch::isValidRomName(node.getName()) + return node.isFile() && Bankswitch::isValidRomName(node) ? IconType::rom : IconType::unknown; }