Skip to content

Fix truncation of strings containing non-terminal NUL characters - #1880

Open
nesevis wants to merge 3 commits into
groue:developmentfrom
nesevis:fix/string-with-nul-character-persistence-and-retrieval
Open

Fix truncation of strings containing non-terminal NUL characters#1880
nesevis wants to merge 3 commits into
groue:developmentfrom
nesevis:fix/string-with-nul-character-persistence-and-retrieval

Conversation

@nesevis

@nesevis nesevis commented Aug 18, 2026

Copy link
Copy Markdown

See #1878 for more context on this fix.

This PR resolves an issue where strings containing non-terminal NUL characters were truncated when persisted, and again when retrieved. It adds regression and performance tests.

This change also reduces allocation and saves a strlen call during writes, at the cost of incurring a new sqlite3_column_bytes call during reads. The largest tangible benefit will be had by users of Swift string functions (capitalize, lowercase, uppercase, etc), with some benefits to writes in general.

The fix itself is the last commit in the branch, so regression and performance tests can be confirmed by checking out the first and second commits.

Pull Request Checklist

  • CONTRIBUTING: You have read https://github.com/groue/GRDB.swift/blob/master/CONTRIBUTING.md
  • BRANCH: This pull request is submitted against the development branch.
  • DOCUMENTATION: Inline documentation has been updated.
  • DOCUMENTATION: README.md or another dedicated guide has been updated.
  • TESTS: Changes are tested.
  • TESTS: The make smokeTest terminal command runs without failure.

`sqlite3_bind_text` and `sqlite3_result_text` treat a negative length as "the number of bytes up to the first zero terminator", so a Swift string containing a NUL was truncated on the way in.

Passing the byte count of the UTF-8 buffer makes those strings roundtrip. It also avoids the length scan SQLite performs for a negative length value, and the NUL-terminated copy that `withCString` allocates.
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