Skip to content

ConfigManager: null-terminate config buffer before parsing - #25

Open
jamesmulcahy wants to merge 1 commit into
NSPManager:develfrom
jamesmulcahy:fix/load-config-null-terminate
Open

jamesmulcahy wants to merge 1 commit into
NSPManager:develfrom
jamesmulcahy:fix/load-config-null-terminate

Conversation

@jamesmulcahy

Copy link
Copy Markdown
Contributor

Summary

ConfigManager::load_config allocates exactly file_size bytes, reads config.json into the buffer and passes it to cJSON_Parse. cJSON_Parse expects a null-terminated string, so the parser can read past the end of the allocation: whatever heap bytes follow can make a valid config fail to parse, or in the worst case be misread. A config that fails to load puts the panel into AP mode with default settings.

Two smaller issues on the same path:

  • The fread result is ignored, so a short read is parsed as if it were the whole file.
  • ftell returning -1 passes the file_size != 0 check and is used as an allocation size.

This PR:

  • Allocates file_size + 1 bytes and null-terminates the buffer after reading.
  • Rejects non-positive file sizes.
  • Fails the load if fewer bytes were read than the file size.

🤖 Generated with Claude Code

https://claude.ai/code/session_01X7aYyRjzk53Sd7gxF46E3u

load_config allocated exactly file_size bytes, read the file into it and
passed it to cJSON_Parse, which expects a null-terminated string. The
parser could read past the end of the allocation, so trailing heap bytes
could make a valid config fail to parse (or be misread). The fread result
was also ignored, and ftell returning -1 was treated as a valid size.

Allocate one extra byte and terminate the buffer, reject non-positive
sizes, and fail if fewer bytes were read than expected.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X7aYyRjzk53Sd7gxF46E3u
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.

1 participant