Correct OpenFile filemode check (#2390)

This commit is contained in:
Benoist
2026-01-21 15:58:24 +01:00
committed by GitHub
parent 22d740ef61
commit 8907569dd8
2 changed files with 29 additions and 23 deletions
+27 -22
View File
@@ -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);
+2 -1
View File
@@ -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).