Skip to content

Keep embedded NULs when reading and writing BSON strings - #171

Merged
quinnj-bot[bot] merged 2 commits into
masterfrom
claude/strings-with-nul
Nov 19, 2026
Merged

quinnj-bot[bot] merged 2 commits into
masterfrom
claude/strings-with-nul

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Why

BSON strings can contain NUL bytes, and other drivers and the server store them. Mongoc mishandled them in both directions.

  • Reading cut the value without an error. A stored "ab\0bab\0babab" came back as "ab" from getindex, 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.
  • Writing threw an error. bson["s"] = "a\0b" threw ArgumentError: 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_value reads UTF-8 and code values with the length libbson reports, using bson_iter_utf8 and bson_iter_code with a length argument. Before, it used unsafe_string on a C string.
  • setindex! for strings passes sizeof(value) to bson_append_utf8 instead of -1, which meant strlen.

Keys are still C strings, as the BSON spec requires, so a key with a NUL still throws ArgumentError. Writing a BSONCode with a NUL also still throws, because bson_append_code has no length argument.

Verification

  • New testset "Strings with embedded NUL" in test/bson_tests.jl. It reads the corpus bytes for string and code and writes String and SubString values. On master: 2 failures (Evaluated: "ab" == "ab\0bab\0babab") and 1 ArgumentError. On this branch: 4/4 pass.
  • Full 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

🤖 Generated with Claude Code

quinnj and others added 2 commits November 19, 2026 04:10
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

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.86%. Comparing base (4cdcae7) to head (8112367).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@quinnj-bot
quinnj-bot Bot merged commit c029db4 into master Nov 19, 2026
14 checks passed
@quinnj-bot
quinnj-bot Bot deleted the claude/strings-with-nul branch November 19, 2026 12:07
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