Repository navigation
Fix stash_foreach crash on NULL and non-UTF-8 stash messages - #1321
Open
shivampwm2020 wants to merge 1 commit into
Open
shivampwm2020 wants to merge 1 commit into
shivampwm2020 wants to merge 1 commit into
Conversation
stash_cb passed the libgit2 message pointer straight to CStr::from_ptr(...).to_str().unwrap(). libgit2 documents the message as possibly NULL, so this dereferenced a null pointer, and a non-UTF-8 message made the unwrap panic. Convert the pointer with the existing opt_bytes helper, returning an empty string for NULL, and use String::from_utf8_lossy for invalid UTF-8. The public StashCb signature is unchanged. Fixes rust-lang#1320
This branch has not been deployed
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.
Fixes #1320
Problem
Repository::stash_foreachcrashes when a stash reflog entry has no messageor a message that is not valid UTF-8. The no-message case is a segfault; the
non-UTF-8 case is a panic.
Root cause
src/stash.rs:165in thestash_cbcallback:libgit2 documents the
messageparameter ofgit_stash_cbas optional, andit is
NULLfor a reflog entry with no message, soCStr::from_ptris calledwith a null pointer (undefined behavior, observed as SIGSEGV). A message that
is not valid UTF-8 makes
to_str()returnErrand theunwrap()panic.Fix
Convert the pointer with the crate's existing
opt_byteshelper, which isalready the idiom for optional C strings, and map the result:
Behavior decisions:
NULLmessage: the callback now receives an empty string, which is exactlywhat the issue requests and matches the existing contract for absent
messages elsewhere in the crate.
String::from_utf8_lossy, i.e. invalid sequences becomeU+FFFD. This is the same approach the issue reporter used as a workaround
(
String::from_utf8_lossyoverentry.message_bytes()), and it keeps thecallback infallible, so it does not turn a crash into a new error users must
handle.
Public API / semver
The public
StashCbtype is unchanged:&stris still passed, so there is no semver-visible change and no updateneeded for existing callers.
Testing
Added
stash::tests::stash_foreach_missing_and_non_utf8_message, which createsa stash, rewrites its reflog message to be first empty and then
caf\xe9, andasserts
stash_foreachyields""and"caf\u{fffd}".Against the unpatched callback the new test reproduces the reported crash:
With the fix: