Skip to content

ModFS error codes#1311

Open
PeachyPeachSM64 wants to merge 4 commits into
coop-deluxe:devfrom
PeachyPeachSM64:modfs-error-codes
Open

ModFS error codes#1311
PeachyPeachSM64 wants to merge 4 commits into
coop-deluxe:devfrom
PeachyPeachSM64:modfs-error-codes

Conversation

@PeachyPeachSM64

@PeachyPeachSM64 PeachyPeachSM64 commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Suggested a while ago by @kermeow

Adds error codes to all ModFS functions to identify errors more easily.
Error code is returned as second return value:

local ok, err = modFs:create_file("filename", true)
if not ok then
    print(err, mod_fs_get_last_error())
    ...
end

Additionally:

  • Increase ModFS max size to 128 MB and num files to 1024
  • Remove specific code that checked file content
  • Fixed some autogen bug
  • Updated doc

No, Luigi, before you ask, I didn't remove extensions, and I don't plan to

@coderabbitai

coderabbitai Bot commented Jun 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f2be5649-60b8-4db7-9292-b3cd01591b1b

📥 Commits

Reviewing files that changed from the base of the PR and between d8041be and 6b8bab1.

📒 Files selected for processing (1)
  • autogen/convert_structs.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • autogen/convert_structs.py

📝 Walkthrough

Walkthrough

ModFS now defines structured error codes, increases storage limits, and adds error-code out parameters across native APIs. Lua bindings return operation results together with numeric error codes and expose mod_fs_get_last_error_code(). Validation, file operations, reads, writes, and persistence paths propagate specific errors. Generated Lua definitions and documentation describe the updated contracts, filepath rules, limits, and error-handling examples. The signature generator now supports multiple return annotations.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.72% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and directly reflects the main change: adding ModFS error codes.
Description check ✅ Passed The description matches the changeset by describing ModFS error codes, limits increases, docs updates, and related fixes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/pc/mods/mod_fs.cpp (1)

1104-1116: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Defer totalSize mutation until copy actually succeeds.

modFs->totalSize is updated before allocation/create-file steps complete. If later steps fail, size accounting stays mutated and can block valid future writes.

💡 Suggested fix
-    modFs->totalSize = newTotalSize;
-
     // copy file
     u8 *buffer = (u8 *) malloc(srcfile->size);
     if (!buffer) {
         ...
         return false;
     }
@@
     memcpy(dstfile->filepath, ...);
     ...
     dstfile->offset = 0;
+    modFs->totalSize = newTotalSize;
     return true;

Also applies to: 1129-1133

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/pc/mods/mod_fs.cpp` around lines 1104 - 1116, The code updates
modFs->totalSize before the copy/creation actually succeeds; change the logic so
newTotalSize is computed and validated but modFs->totalSize is not assigned
until after the file allocation/create and all subsequent steps complete
successfully (i.e., move the assignment modFs->totalSize = newTotalSize to the
end of the successful copy path and, for any code paths that can fail after
allocation, either perform the assignment only after those checks or roll back
the allocation and leave modFs->totalSize unchanged); apply the same change to
the second identical site that updates totalSize (the block using newTotalSize
and modFs->totalSize around the other copy/create sequence).
🧹 Nitpick comments (1)
src/pc/mods/mod_fs.h (1)

36-36: 💤 Low value

Clarify the comment wording for readability.

The phrase "not ascii" in a comma-separated list reads ambiguously. Consider rewording:

-    MOD_FS_ERR_FILEPATH_INVALID_CHAR,       // Filepath contains invalid characters (not ascii, control, star or backslash)
+    MOD_FS_ERR_FILEPATH_INVALID_CHAR,       // Filepath contains invalid characters (non-ASCII, control characters, asterisks, or backslashes)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/pc/mods/mod_fs.h` at line 36, The comment for the enum value
MOD_FS_ERR_FILEPATH_INVALID_CHAR is ambiguous — reword it to list the invalid
character classes clearly (e.g., "Filepath contains invalid characters
(non-ASCII, control characters, '*' or '\\')") so readers unambiguously
understand which characters are disallowed; update the comment on
MOD_FS_ERR_FILEPATH_INVALID_CHAR accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/pc/mods/mod_fs.cpp`:
- Around line 1050-1065: Guard against identical source and destination paths
before performing overwrite/delete logic: in the code paths that call
modfs::mod_fs_get_file(modFs, newpath, err) and potentially call
modfs::mod_fs_delete_file(modFs, newpath, err) (the blocks that later
snprintf(oldfile->filepath, ...) and return true), add an early check comparing
the current file path (oldfile->filepath) to newpath (e.g.,
strcmp(oldfile->filepath, newpath) == 0) and return true immediately if they are
identical; do this before any deletion/overwrite is attempted so you never
delete/free the same object being moved/copied (apply the same check in both
occurrences where mod_fs_get_file/mod_fs_delete_file and
snprintf(oldfile->filepath, ...) are used).
- Around line 777-786: The function mod_fs_file_check_parameter is narrowing
inputs by declaring parameter, parameterMin, and parameterMax as u8 so values
>=256 wrap and bypass validation; change those three parameters to a
sufficiently wide unsigned type (e.g., unsigned int or uint32_t) in the
mod_fs_file_check_parameter declaration and definition and update every call
site that passes values (from callers interacting with ModFsFile) to pass the
wider type; keep the error reporting via mod_fs_raise_error and parameterName
as-is but ensure formatting specifiers (%u) match the chosen wider type.

---

Outside diff comments:
In `@src/pc/mods/mod_fs.cpp`:
- Around line 1104-1116: The code updates modFs->totalSize before the
copy/creation actually succeeds; change the logic so newTotalSize is computed
and validated but modFs->totalSize is not assigned until after the file
allocation/create and all subsequent steps complete successfully (i.e., move the
assignment modFs->totalSize = newTotalSize to the end of the successful copy
path and, for any code paths that can fail after allocation, either perform the
assignment only after those checks or roll back the allocation and leave
modFs->totalSize unchanged); apply the same change to the second identical site
that updates totalSize (the block using newTotalSize and modFs->totalSize around
the other copy/create sequence).

---

Nitpick comments:
In `@src/pc/mods/mod_fs.h`:
- Line 36: The comment for the enum value MOD_FS_ERR_FILEPATH_INVALID_CHAR is
ambiguous — reword it to list the invalid character classes clearly (e.g.,
"Filepath contains invalid characters (non-ASCII, control characters, '*' or
'\\')") so readers unambiguously understand which characters are disallowed;
update the comment on MOD_FS_ERR_FILEPATH_INVALID_CHAR accordingly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: cea03efa-590e-4739-a870-77a9195a2cc7

📥 Commits

Reviewing files that changed from the base of the PR and between 1374609 and 5957e68.

📒 Files selected for processing (13)
  • autogen/convert_functions.py
  • autogen/convert_structs.py
  • autogen/lua_definitions/constants.lua
  • autogen/lua_definitions/functions.lua
  • autogen/lua_definitions/structs.lua
  • docs/lua/constants.md
  • docs/lua/functions-5.md
  • docs/lua/functions.md
  • docs/lua/guides/modfs.md
  • src/pc/lua/smlua_constants_autogen.c
  • src/pc/lua/smlua_functions_autogen.c
  • src/pc/mods/mod_fs.cpp
  • src/pc/mods/mod_fs.h

Comment thread src/pc/mods/mod_fs.cpp Outdated
Comment thread src/pc/mods/mod_fs.cpp
@xLuigiGamerx

Copy link
Copy Markdown
Contributor

This is peak, i approve
I wasn't gonna ask about it 😔 <- (definitely was gonna ask about it)
I'm probably gonna do it myself tho

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
autogen/convert_structs.py (1)

480-480: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Prefer the already-extracted is_c_array variable over field['is_c_array'].

Line 440 already destructures is_c_array from get_struct_field_info(), which uses field.get('is_c_array', False) defensively. Line 480 bypasses this by accessing field['is_c_array'] directly, creating an inconsistency that could raise KeyError if a future code path constructs fields without going through parse_struct.

♻️ Use the destructured variable
-            if field['is_c_array']:
+            if is_c_array:
                 row.append('true')
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@autogen/convert_structs.py` at line 480, In the relevant struct-field
conversion logic, replace the direct field['is_c_array'] lookup with the
already-destructured is_c_array variable from get_struct_field_info(),
preserving the defensive default behavior for fields that omit this key.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@autogen/convert_structs.py`:
- Line 480: In the relevant struct-field conversion logic, replace the direct
field['is_c_array'] lookup with the already-destructured is_c_array variable
from get_struct_field_info(), preserving the defensive default behavior for
fields that omit this key.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 231a2026-b0cd-4252-b42f-2599909abd17

📥 Commits

Reviewing files that changed from the base of the PR and between 0378a1f and d8041be.

📒 Files selected for processing (9)
  • autogen/convert_structs.py
  • autogen/lua_definitions/constants.lua
  • autogen/lua_definitions/functions.lua
  • autogen/lua_definitions/structs.lua
  • docs/lua/constants.md
  • docs/lua/functions-5.md
  • docs/lua/functions.md
  • src/pc/lua/smlua_constants_autogen.c
  • src/pc/lua/smlua_functions_autogen.c
💤 Files with no reviewable changes (1)
  • docs/lua/functions-5.md
✅ Files skipped from review due to trivial changes (2)
  • docs/lua/functions.md
  • autogen/lua_definitions/structs.lua
🚧 Files skipped from review as they are similar to previous changes (4)
  • docs/lua/constants.md
  • src/pc/lua/smlua_constants_autogen.c
  • autogen/lua_definitions/constants.lua
  • autogen/lua_definitions/functions.lua

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants