Skip to content

Fix unsoundness in git2::diff::DiffBinaryFile::data() for empty data#1279

Open
DanielEScherzer wants to merge 2 commits into
rust-lang:mainfrom
DanielEScherzer:empty-binary-data
Open

Fix unsoundness in git2::diff::DiffBinaryFile::data() for empty data#1279
DanielEScherzer wants to merge 2 commits into
rust-lang:mainfrom
DanielEScherzer:empty-binary-data

Conversation

@DanielEScherzer

Copy link
Copy Markdown
Contributor

Fixes #1278

When a `Diff` is created without the information of *how* a binary file
changed, the data is empty and the pointer to it is NULL; this violates the
preconditions of `slice::from_raw_parts()`, which requires that the provided
pointer be non-null.

The new test is currently failing and exists to show the error - it could not
be split up and marked with `#[should_panic]`, which does not appear to work
here. This may be because the error message is that "thread caused
non-unwinding panic. aborting." after the problematic `slice::from_raw_parts()`
call, and the attribute only works with unwinding panics, I'm not sure.

Also add a test for a binary diff where the data is not missing to confirm that
in that case `DiffBinaryFile::data()` works properly.
When there is no data, rather than using `slice::from_raw_parts()` with a null
pointer (which is unsound), just return an empty slice directly.

Fixes rust-lang#1278
@rustbot rustbot added the S-waiting-on-review Status: Waiting on review label Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Waiting on review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unsound slice::from_raw_parts() in git2::diff::DiffBinaryFile::data()

2 participants