From c310db4b9510fba59c9930fa975f7791661d2791 Mon Sep 17 00:00:00 2001 From: Benoist <14257866+Kenzzer@users.noreply.github.com> Date: Wed, 14 Jan 2026 03:02:27 +0100 Subject: [PATCH] Throw error if using invalid open mode with OpenFile native (#2387) --- core/logic/smn_filesystem.cpp | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/core/logic/smn_filesystem.cpp b/core/logic/smn_filesystem.cpp index 4ca57e62..f5124e3d 100644 --- a/core/logic/smn_filesystem.cpp +++ b/core/logic/smn_filesystem.cpp @@ -499,6 +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') { + 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) { + is_invalid = true; + } + has_plus = true; + } else if (mode[pos] == 'b') { + if (has_b) { + is_invalid = true; + } + has_b = 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];