From f92cf610668971cfd99821a427896bb10551789f Mon Sep 17 00:00:00 2001 From: Borin Ouch Date: Fri, 5 Apr 2024 11:58:36 -0700 Subject: [PATCH] Validate file type and file size before loading --- src/interface.cpp | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/interface.cpp b/src/interface.cpp index dcb68a9..f0cbed2 100644 --- a/src/interface.cpp +++ b/src/interface.cpp @@ -18,6 +18,8 @@ #include #include +namespace fs = std::filesystem; + constexpr int kMaxSamples = 512; constexpr int kMaxSamplesPerUpdate = 4096; constexpr int kAudioSampleRate = 44100; @@ -120,7 +122,21 @@ bool Interface::update() { FilePathList droppedFiles = LoadDroppedFiles(); if (droppedFiles.count > 0) { spdlog::info("Dropped file: {}", droppedFiles.paths[0]); - load_rom(droppedFiles.paths[0]); + + fs::path filepath(droppedFiles.paths[0]); + + if (fs::is_symlink(filepath)) { + filepath.replace_filename(fs::read_symlink(filepath)); + filepath = fs::absolute(filepath); + } + + const std::string ext = filepath.extension().string(); + + if (fs::is_regular_file(filepath) && (ext == ".ch8" || ext == ".c8")) { + load_rom(filepath.string()); + } else { + spdlog::warn("Invalid file type: {}", filepath.string()); + } } UnloadDroppedFiles(droppedFiles); @@ -497,11 +513,20 @@ void Interface::open_load_rom_dialog() { } void Interface::load_rom(const std::string &filename) { + spdlog::debug("Loading rom: {}", filename); + rom_loaded = true; - std::filesystem::path rom_path = filename; + fs::path rom_path = filename; SetWindowTitle( fmt::format("CHIP-8 - {}", rom_path.filename().string()).c_str()); - std::ifstream in(filename, std::ios::binary); + std::ifstream in(filename, std::ios::binary | std::ifstream::ate ); + size_t pos = in.tellg(); + if (pos > (kMemSize - kRomStartIndex)) { + spdlog::error("File exceeds memory size, NOT loading rom"); + return; + } + + in.seekg(0, in.beg); rom = {std::istreambuf_iterator(in), {}}; interpreter->stop();