parser.load(string_view) does not respect view length when opening files by jmestwa-coder · Pull Request #2659 · simdjson/simdjson · GitHub
Skip to content

parser.load(string_view) does not respect view length when opening files#2659

Merged
lemire merged 1 commit intosimdjson:masterfrom
jmestwa-coder:parser-load-string-view-length
Apr 3, 2026
Merged

parser.load(string_view) does not respect view length when opening files#2659
lemire merged 1 commit intosimdjson:masterfrom
jmestwa-coder:parser-load-string-view-length

Conversation

@jmestwa-coder
Copy link
Copy Markdown
Contributor

Short title (summary)

parser.load(string_view) does not respect view length when opening files


Description

parser.load(std::string_view) currently forwards path.data() directly to std::fopen, which treats the input as a null-terminated C string and ignores the view length.

When the string_view refers to a substring of a larger buffer, this can result in incorrect file resolution (e.g., attempting to open a suffixed path instead of the intended one).

This change ensures that the provided string_view is respected by creating a null-terminated copy before calling fopen. The behavior is now consistent with user expectations and aligns with existing file-loading logic elsewhere in the codebase.


Type of change

  • Bug fix

How to verify / test

  • Added a regression test using a substring std::string_view over a larger backing string (valid path + suffix).
  • Before this change: parser.load(view) returns IO_ERROR because the full backing string is used.
  • After this change: parser.load(view) succeeds and produces the same result as parser.load(std::string).

Checklist before submitting

  • I added/updated tests covering my change (if applicable)
  • Code builds locally and passes my check
  • Commits are atomic and messages are clear

Final notes

This is a minimal and localized change that ensures std::string_view is handled correctly when opening files. It avoids relying on implicit null-termination and improves consistency across the API without affecting existing valid usage.

@jmestwa-coder jmestwa-coder force-pushed the parser-load-string-view-length branch from e63bf46 to 695a759 Compare April 3, 2026 18:21
@lemire lemire merged commit 542cd7a into simdjson:master Apr 3, 2026
85 checks passed
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