From 8907569dd8de6145eb1bf34cee32d89cefdd07f7 Mon Sep 17 00:00:00 2001 From: Benoist <14257866+Kenzzer@users.noreply.github.com> Date: Wed, 21 Jan 2026 15:58:24 +0100 Subject: [PATCH] Correct OpenFile filemode check (#2390) --- core/logic/smn_filesystem.cpp | 49 +++++++++++++++++++---------------- plugins/include/files.inc | 3 ++- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/core/logic/smn_filesystem.cpp b/core/logic/smn_filesystem.cpp index f5124e3d..1c42227a 100644 --- a/core/logic/smn_filesystem.cpp +++ b/core/logic/smn_filesystem.cpp @@ -499,39 +499,44 @@ static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params) pContext->LocalToString(params[1], &name); pContext->LocalToString(params[2], &mode); - if (mode == nullptr || mode[0] == '\0') { - return pContext->ThrowNativeError("File open mode cannot be empty!"); - } - - if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { + if (mode == nullptr || mode[0] == '\0') { + return pContext->ThrowNativeError("File open mode cannot be empty!"); + } + + if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { return pContext->ThrowNativeError("File open mode is invalid \"%s\"!", mode); - } + } - bool has_plus = 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) { + 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_plus = true; + } else if (mode[pos] == 'b') { + if (has_b || has_t) { is_invalid = true; } - has_b = true; - } else if (mode[pos] == 'x') { - if (has_x) { + 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') { + if (mode[0] != 'w') { is_invalid = true; } - has_x = true; - } else { - is_invalid = true; - } - } + has_x = true; + } else { + is_invalid = true; + } + } if (is_invalid) { return pContext->ThrowNativeError("File open mode is invalid \"%s\"!", mode); diff --git a/plugins/include/files.inc b/plugins/include/files.inc index a54f1996..180c6050 100644 --- a/plugins/include/files.inc +++ b/plugins/include/files.inc @@ -305,6 +305,7 @@ native bool ReadDirEntry(Handle dir, char[] buffer, int maxlength, FileType &typ * "r": Open an existing file for reading. * "w": Create a file for writing, or truncate (delete the contents of) an * existing file and then open it for writing. + * "wx": Create file for writing, fails if file already exists. * "a": Create a file for writing, or open an existing file such that writes * will be appended to the end. * "r+": Open an existing file for both reading and writing. @@ -314,7 +315,7 @@ native bool ReadDirEntry(Handle dir, char[] buffer, int maxlength, FileType &typ * such that writes will be appended to the end. * * The open mode may also contain an additional character after "r", "w", or "a", - * but before any "+" sign. This character may be "b" (indicating binary mode) or + * but before the "+" sign. This character may be "b" (indicating binary mode) or * "t" (indicating text mode). By default, "text" mode is implied. On Linux and * Mac, this has no distinction from binary mode. On Windows, it causes the '\n' * character (0xA) to be written as "\r\n" (0xD, 0xA).