Skip to content

Commit 12b3257

Browse files
authored
Merge pull request #2219 from cruessler/use-split-at-checked
Replace `split_at_pos` by `split_at_checked`
2 parents 4da2927 + 027b3ef commit 12b3257

File tree

8 files changed

+27
-45
lines changed

8 files changed

+27
-45
lines changed

gix-bitmap/src/ewah.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ pub fn decode(data: &[u8]) -> Result<(Vec, &[u8]), decode::Error> {
2525
// NOTE: git does this by copying all bytes first, and then it will change the endianness in a separate loop.
2626
// Maybe it's faster, but we can't do it without unsafe. Let's leave it to the optimizer and maybe
2727
// one day somebody will find out that it's worth it to use unsafe here.
28-
let (mut bits, data) = decode::split_at_pos(data, len * std::mem::size_of::<u64>()).ok_or(Error::Corrupt {
29-
message: "eof while reading bit data",
30-
})?;
28+
let (mut bits, data) = data
29+
.split_at_checked(len * std::mem::size_of::<u64>())
30+
.ok_or(Error::Corrupt {
31+
message: "eof while reading bit data",
32+
})?;
3133
let mut buf = std::vec::Vec::<u64>::with_capacity(len);
3234
for _ in 0..len {
3335
let (bit_num, rest) = bits.split_at(std::mem::size_of::<u64>());

gix-bitmap/src/lib.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@
77
pub mod ewah;
88

99
pub(crate) mod decode {
10-
#[inline]
11-
pub(crate) fn split_at_pos(data: &[u8], pos: usize) -> Option<(&[u8], &[u8])> {
12-
if data.len() < pos {
13-
return None;
14-
}
15-
data.split_at(pos).into()
16-
}
17-
1810
#[inline]
1911
pub(crate) fn u32(data: &[u8]) -> Option<(u32, &[u8])> {
20-
split_at_pos(data, 4).map(|(num, data)| (u32::from_be_bytes(num.try_into().unwrap()), data))
12+
data.split_at_checked(4)
13+
.map(|(num, data)| (u32::from_be_bytes(num.try_into().unwrap()), data))
2114
}
2215
}

gix-index/src/decode/entries.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::Range;
33
use crate::{
44
decode::{self, header},
55
entry,
6-
util::{read_u32, split_at_byte_exclusive, split_at_pos, var_int},
6+
util::{read_u32, split_at_byte_exclusive, var_int},
77
Entry, Version,
88
};
99

@@ -94,7 +94,7 @@ fn load_one<'a>(
9494
let (uid, data) = read_u32(data)?;
9595
let (gid, data) = read_u32(data)?;
9696
let (size, data) = read_u32(data)?;
97-
let (hash, data) = split_at_pos(data, hash_len)?;
97+
let (hash, data) = data.split_at_checked(hash_len)?;
9898
let (flags, data) = read_u16(data)?;
9999
let flags = entry::at_rest::Flags::from_bits_retain(flags);
100100
let (flags, data) = if flags.contains(entry::at_rest::Flags::EXTENDED) {
@@ -128,7 +128,7 @@ fn load_one<'a>(
128128
split_at_byte_exclusive(data, 0)?
129129
} else {
130130
let path_len = (flags.bits() & entry::Flags::PATH_LEN.bits()) as usize;
131-
let (path, data) = split_at_pos(data, path_len)?;
131+
let (path, data) = data.split_at_checked(path_len)?;
132132
(path, skip_padding(data, first_byte_of_entry))
133133
};
134134

@@ -177,5 +177,6 @@ fn skip_padding(data: &[u8], first_byte_of_entry: usize) -> &[u8] {
177177

178178
#[inline]
179179
fn read_u16(data: &[u8]) -> Option<(u16, &[u8])> {
180-
split_at_pos(data, 2).map(|(num, data)| (u16::from_be_bytes(num.try_into().unwrap()), data))
180+
data.split_at_checked(2)
181+
.map(|(num, data)| (u16::from_be_bytes(num.try_into().unwrap()), data))
181182
}

gix-index/src/extension/link.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::{
2-
extension::{Link, Signature},
3-
util::split_at_pos,
4-
};
1+
use crate::extension::{Link, Signature};
52

63
/// The signature of the link extension.
74
pub const SIGNATURE: Signature = *b"link";
@@ -39,7 +36,8 @@ pub mod decode {
3936
}
4037

4138
pub(crate) fn decode(data: &[u8], object_hash: gix_hash::Kind) -> Result<Link, decode::Error> {
42-
let (id, data) = split_at_pos(data, object_hash.len_in_bytes())
39+
let (id, data) = data
40+
.split_at_checked(object_hash.len_in_bytes())
4341
.ok_or(decode::Error::Corrupt(
4442
"link extension too short to read share index checksum",
4543
))

gix-index/src/extension/resolve_undo.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use bstr::BString;
22
use gix_hash::ObjectId;
33

4-
use crate::{
5-
extension::Signature,
6-
util::{split_at_byte_exclusive, split_at_pos},
7-
};
4+
use crate::{extension::Signature, util::split_at_byte_exclusive};
85

96
pub type Paths = Vec<ResolvePath>;
107

@@ -47,7 +44,7 @@ pub fn decode(mut data: &[u8], object_hash: gix_hash::Kind) -> Option<Paths> {
4744
if *mode == 0 {
4845
continue;
4946
}
50-
let (hash, rest) = split_at_pos(data, hash_len)?;
47+
let (hash, rest) = data.split_at_checked(hash_len)?;
5148
data = rest;
5249
*stage = Some(Stage {
5350
mode: *mode,

gix-index/src/extension/tree/decode.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use gix_hash::ObjectId;
22

3-
use crate::{
4-
extension::Tree,
5-
util::{split_at_byte_exclusive, split_at_pos},
6-
};
3+
use crate::{extension::Tree, util::split_at_byte_exclusive};
74

85
/// A recursive data structure
96
pub fn decode(data: &[u8], object_hash: gix_hash::Kind) -> Option<Tree> {
@@ -26,7 +23,7 @@ fn one_recursive(data: &[u8], hash_len: usize) -> Option<(Tree, &[u8])> {
2623
let subtree_count: usize = gix_utils::btoi::to_unsigned(subtree_count).ok()?;
2724

2825
let (id, mut data) = if num_entries >= 0 {
29-
let (hash, data) = split_at_pos(data, hash_len)?;
26+
let (hash, data) = data.split_at_checked(hash_len)?;
3027
(ObjectId::from_bytes_or_panic(hash), data)
3128
} else {
3229
(

gix-index/src/extension/untracked_cache.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use gix_hash::ObjectId;
44
use crate::{
55
entry,
66
extension::{Signature, UntrackedCache},
7-
util::{read_u32, split_at_byte_exclusive, split_at_pos, var_int},
7+
util::{read_u32, split_at_byte_exclusive, var_int},
88
};
99

1010
/// A structure to track filesystem stat information along with an object id, linking a worktree file with what's in our ODB.
@@ -44,7 +44,7 @@ pub fn decode(data: &[u8], object_hash: gix_hash::Kind) -> Option<UntrackedCache
4444
return None;
4545
}
4646
let (identifier_len, data) = var_int(data)?;
47-
let (identifier, data) = split_at_pos(data, identifier_len.try_into().ok()?)?;
47+
let (identifier, data) = data.split_at_checked(identifier_len.try_into().ok()?)?;
4848

4949
let hash_len = object_hash.len_in_bytes();
5050
let (info_exclude, data) = decode_oid_stat(data, hash_len)?;
@@ -96,7 +96,7 @@ pub fn decode(data: &[u8], object_hash: gix_hash::Kind) -> Option<UntrackedCache
9696
Some(())
9797
});
9898
hash_valid.for_each_set_bit(|index| {
99-
let (hash, rest) = split_at_pos(data, hash_len)?;
99+
let (hash, rest) = data.split_at_checked(hash_len)?;
100100
data = rest;
101101
directories[index].exclude_file_oid = ObjectId::from_bytes_or_panic(hash).into();
102102
Some(())
@@ -143,7 +143,7 @@ fn decode_directory_block<'a>(data: &'a [u8], directories: &mut Vec<Directory>)
143143

144144
fn decode_oid_stat(data: &[u8], hash_len: usize) -> Option<(OidStat, &[u8])> {
145145
let (stat, data) = crate::decode::stat(data)?;
146-
let (hash, data) = split_at_pos(data, hash_len)?;
146+
let (hash, data) = data.split_at_checked(hash_len)?;
147147
Some((
148148
OidStat {
149149
stat,

gix-index/src/lib.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,14 @@ pub(crate) mod util {
198198

199199
#[inline]
200200
pub fn read_u32(data: &[u8]) -> Option<(u32, &[u8])> {
201-
split_at_pos(data, 4).map(|(num, data)| (u32::from_be_bytes(num.try_into().unwrap()), data))
201+
data.split_at_checked(4)
202+
.map(|(num, data)| (u32::from_be_bytes(num.try_into().unwrap()), data))
202203
}
203204

204205
#[inline]
205206
pub fn read_u64(data: &[u8]) -> Option<(u64, &[u8])> {
206-
split_at_pos(data, 8).map(|(num, data)| (u64::from_be_bytes(num.try_into().unwrap()), data))
207+
data.split_at_checked(8)
208+
.map(|(num, data)| (u64::from_be_bytes(num.try_into().unwrap()), data))
207209
}
208210

209211
#[inline]
@@ -227,12 +229,4 @@ pub(crate) mod util {
227229
})
228230
})
229231
}
230-
231-
#[inline]
232-
pub fn split_at_pos(data: &[u8], pos: usize) -> Option<(&[u8], &[u8])> {
233-
if data.len() < pos {
234-
return None;
235-
}
236-
data.split_at(pos).into()
237-
}
238232
}

0 commit comments

Comments
 (0)