Repository navigation
Destroy a KjMaybe's value only when the KjMaybe is dropped - #8453
Merged
Merged
Conversation
Drop for KjMaybe called MaybeItem::drop_in_place, a safe default method of the public MaybeItem trait. It drops the stored value when the KjMaybe holds one and leaves the flag, or the niche, saying that it still does, so calling it and then dropping the KjMaybe dropped the value twice. Remove the method and drop the value in Drop for KjMaybe itself. Nothing else called it, and generated MaybeItem impls do not override it.
There was a problem hiding this comment.
✅ Approved — This is a small, correct soundness fix: Drop does what the removed method did, inline, and it comes with a compile test and unit tests. No issues found.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
📋 Code Quality: ✅ LGTM
The inline Drop matches the old behavior exactly. The method is removed from the public trait, and the compile test makes sure it stays gone. The unit tests cover dropping a full KjMaybe, dropping an empty one, and converting to Option.
📝 Documentation: ✅ LGTM
The comments describe the code as it is now and follow the repository's comment guidelines.
Reviewed commit: 5c65697c
piscisaureus
approved these changes
Nov 13, 2026
5 tasks done
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.
This fixes
kj_rs::maybe::MaybeItem::drop_in_place, a safe public method that destroyed the value in aKjMaybeand left theKjMaybesaying it still held one.KjMaybe<T>mirrorskj::Maybe<T>: a flag (or a niche) plus storage.Drop for KjMaybecalledT::drop_in_place(self), a default method on the publicMaybeItemtrait, which drops the value whenis_some()is true and changes nothing else. Since the method was callable from anywhere, calling it and then letting theKjMaybego out of scope dropped the sameStringorKjOwntwice. Nothing in workerd calls it other thanDrop, and the bridge macro's generatedMaybeItemimpls don't override it.MaybeItem::drop_in_placeis gone;Drop for KjMaybedoes the same work inline.There was no compile-test target for kj-rs, so this adds
//src/rust/cxx/kj-rs/compile-tests(the samewd_rust_compile_testharness asjsg-compile-tests) with one snippet checking that the method can no longer be named.maybe-test.rsadds unit tests for what is left: aKjMaybe<String>that holds a heap value is dropped, an empty one is dropped, and converting toOptionmoves the value out. Under--config=asanthe old method followed by a drop reports a double free; the default test binaries use tcmalloc, which does not notice.