Repository navigation
Keep embedded NULs when reading and writing BSON strings - #171
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
get_value read UTF-8 and JavaScript code values as C strings, so a value like "ab\0bab" came back as "ab" with no error. setindex! passed strings to libbson as C strings, so writing one threw ArgumentError. Read with the length libbson reports and append UTF-8 values with their byte length. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #171 +/- ##
==========================================
+ Coverage 85.84% 85.86% +0.02%
==========================================
Files 11 11
Lines 1674 1677 +3
==========================================
+ Hits 1437 1440 +3
Misses 237 237 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
BSON strings can contain NUL bytes, and other drivers and the server store them. Mongoc mishandled them in both directions.
"ab\0bab\0babab"came back as"ab"fromgetindex, iteration,as_dict, and cursors. JavaScript code values were cut the same way. The BSON corpus "Embedded nulls" cases for string and code both fail this way on master.bson["s"] = "a\0b"threwArgumentError: embedded NULs are not allowed in C strings.Code that read a document and wrote it back lost everything after the first NUL.
What changed
get_valuereads UTF-8 and code values with the length libbson reports, usingbson_iter_utf8andbson_iter_codewith a length argument. Before, it usedunsafe_stringon a C string.setindex!for strings passessizeof(value)tobson_append_utf8instead of-1, which meantstrlen.Keys are still C strings, as the BSON spec requires, so a key with a NUL still throws
ArgumentError. Writing aBSONCodewith a NUL also still throws, becausebson_append_codehas no length argument.Verification
test/bson_tests.jl. It reads the corpus bytes for string and code and writesStringandSubStringvalues. On master: 2 failures (Evaluated: "ab" == "ab\0bab\0babab") and 1ArgumentError. On this branch: 4/4 pass.Pkg.test()passes on Julia 1.12.6 with MongoC_jll 1.30.8 and MongoDB 5.0.33. BSON 177/177, MongoDB 255/255.Self-review
bson_append_utf8,bson_iter_utf8, andbson_iter_codeeach have one caller, and the PR updates each one.String(value)does not copy aString. For otherAbstractStrings it makes the same copy the oldCstringconversion made.setindex!(::BSON, ::AbstractString, ...). Keep this branch's two lines and use Alternative PR to address uniqueness ordering and properties, introducing handle(bson) #152'shandle(document).🤖 Generated with Claude Code