java-topology/defects/dosbox-x/patch/dosbox-x-0002-overlay-deleted-files-vector-scan.patch

24 lines
1.4 KiB
Diff

# UNDF: UNDF-2026-000000899
--- a/src/dos/drives.h
+++ b/src/dos/drives.h
@@ -1331 +1331 @@
- std::vector<std::string> deleted_files_in_base; //Set is probably better, or some other solution (involving the disk).
+ std::unordered_set<std::string, CaseInsensitiveHash, CaseInsensitiveEqual> deleted_files_set;
--- a/src/dos/drive_overlay.cpp
+++ b/src/dos/drive_overlay.cpp
@@ -1689,3 +1689,3 @@
for(std::vector<std::string>::iterator it = deleted_files_in_base.begin(); it != deleted_files_in_base.end(); it++) {
- if (!strcasecmp((*it).c_str(), name)||!strcasecmp((*it).c_str(), tname)||(strlen(fname)&&!strcasecmp((*it).c_str(), fname))) return true;
+ // Replace O(N) linear scan with O(1) hash lookups
+ if (deleted_files_set.count(name) || deleted_files_set.count(tname) || (strlen(fname) && deleted_files_set.count(fname))) return true;
}
#
# CWE-407: Overlay_Drive::is_deleted_file scans deleted_files_in_base vector
# with strcasecmp for every file operation (open, stat, attr, etc.) — O(N)
# per query where N = number of deleted files. Called from FileOpen,
# FileExists, GetFileAttr, FindFirst, and more. The code itself comments
# "Set is probably better, or some other solution."
# Fix: replace std::vector with std::unordered_set using case-insensitive
# hash/equal functors.
# Severity: HIGH — is_deleted_file is on every file I/O hot path; with many
# deleted files in overlay, every open/stat/attr degrades to O(N).