Skip to content

Commit 8852d7d

Browse files
authored
Merge pull request #1214 from weihanglo/backport-from-raw-parts
Backport #1213 to git2 0.20.x
2 parents 7cf345c + 0b274f7 commit 8852d7d

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## 0.20.4 - 2026-02-02
4+
[0.20.3...0.20.4](https://github.com/rust-lang/git2-rs/compare/git2-0.20.3...git2-0.20.4)
5+
6+
### Fixed
7+
8+
- Fix undefined behavior when dereferencing empty `Buf`.
9+
[#1213](https://github.com/rust-lang/git2-rs/pull/1213)
10+
311
## 0.20.3 - 2025-12-06
412
[0.20.2...0.20.3](https://github.com/rust-lang/git2-rs/compare/git2-0.20.2...git2-0.20.3)
513

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "git2"
3-
version = "0.20.3"
3+
version = "0.20.4"
44
authors = ["Josh Triplett <josh@joshtriplett.org>", "Alex Crichton <alex@alexcrichton.com>"]
55
license = "MIT OR Apache-2.0"
66
readme = "README.md"

src/buf.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@ impl Buf {
4444
impl Deref for Buf {
4545
type Target = [u8];
4646
fn deref(&self) -> &[u8] {
47+
if self.raw.ptr.is_null() {
48+
return &[];
49+
}
4750
unsafe { slice::from_raw_parts(self.raw.ptr as *const u8, self.raw.size as usize) }
4851
}
4952
}
5053

5154
impl DerefMut for Buf {
5255
fn deref_mut(&mut self) -> &mut [u8] {
56+
if self.raw.ptr.is_null() {
57+
return &mut [];
58+
}
5359
unsafe { slice::from_raw_parts_mut(self.raw.ptr as *mut u8, self.raw.size as usize) }
5460
}
5561
}
@@ -69,3 +75,12 @@ impl Drop for Buf {
6975
unsafe { raw::git_buf_dispose(&mut self.raw) }
7076
}
7177
}
78+
79+
#[test]
80+
fn empty_buf() {
81+
let mut buf = Buf::new();
82+
let x: &[u8] = &*buf;
83+
assert_eq!(x.len(), 0);
84+
let x: &mut [u8] = &mut *buf;
85+
assert_eq!(x.len(), 0);
86+
}

0 commit comments

Comments
 (0)