From 29386a72f6d0d112632215b4d34a02b550fd1adf Mon Sep 17 00:00:00 2001 From: Benoist <14257866+Kenzzer@users.noreply.github.com> Date: Mon, 26 Jan 2026 15:13:20 +0100 Subject: [PATCH] Another OpenFile fix (#2395) Co-authored-by: Fyren <3320115+Fyren@users.noreply.github.com> --- core/logic/smn_filesystem.cpp | 47 +++++++++-------------------------- 1 file changed, 12 insertions(+), 35 deletions(-) diff --git a/core/logic/smn_filesystem.cpp b/core/logic/smn_filesystem.cpp index 1c42227a..6df52b0c 100644 --- a/core/logic/smn_filesystem.cpp +++ b/core/logic/smn_filesystem.cpp @@ -178,7 +178,19 @@ public: } static SystemFile *Open(const char *path, const char *mode) { +#if defined(_WIN32) + static thread_local bool invalid_fopen = false; + static auto handler = [](const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t) { invalid_fopen = true; }; + auto old = _set_thread_local_invalid_parameter_handler(handler); + invalid_fopen = false; +#endif FILE *fp = fopen(path, mode); +#if defined(_WIN32) + _set_thread_local_invalid_parameter_handler(old); + if (invalid_fopen) + return NULL; +#endif + if (!fp) return NULL; return new SystemFile(fp); @@ -507,41 +519,6 @@ static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("File open mode is invalid \"%s\"!", mode); } - bool has_plus = false, has_t = false, has_b = false, has_x = false, is_invalid = false; - for (int pos = 1; mode[pos] && !is_invalid; pos++) { - if (mode[pos] == '+') { - if (has_plus) { - is_invalid = true; - } - has_plus = true; - } else if (mode[pos] == 'b') { - if (has_b || has_t) { - is_invalid = true; - } - has_b = true; - } else if (mode[pos] == 't') { - if (has_b || has_t) { - is_invalid = true; - } - has_t = true; - } else if (mode[pos] == 'x') { - if (has_x) { - is_invalid = true; - } - // 'x' mode is only valid with 'w' - if (mode[0] != 'w') { - is_invalid = true; - } - has_x = true; - } else { - is_invalid = true; - } - } - - if (is_invalid) { - return pContext->ThrowNativeError("File open mode is invalid \"%s\"!", mode); - } - FileObject *file = NULL; if (params[0] <= 2 || !params[3]) { char realpath[PLATFORM_MAX_PATH];