History

Work with split_at exclusively to make getting offsets wrong impossible

Sebastian Thiel committed 8 years ago

Commit 64c52d01 parent c0c26ef1 file · +25 −16Browse files

Unified · Split

lib/git-odb/src/pack/mod.rs+25−16
@@ -39,29 +39,38 @@ pub mod index { · 38 unmodified lines
3939 }
4040
4141 pub fn at(path: &Path) -> Result<File, Error> {
42− let d = FileBuffer::open(path)
42+ let data = FileBuffer::open(path)
4343 .with_context(|_| format!("Could not map file at '{}'", path.display()))?;
44− let idx_len = d.len();
44+ let idx_len = data.len();
4545 if idx_len < V2_SIGNATURE.len() + FOOTER_LEN {
4646 bail!("Pack index is truncated and not even empty");
4747 }
48− let kind = if &d[..V2_SIGNATURE.len()] == V2_SIGNATURE {
49− Kind::V2
50− } else {
51− Kind::V1
52− };
53− let version = {
54− let mut v = 1;
55− if let &Kind::V2 = &kind {
56− v = BigEndian::read_u32(&d[V2_SIGNATURE.len()..V2_SIGNATURE.len() + N32_SIZE]);
57− if v != 2 {
58− bail!("Unsupported index version: {}", v);
48+ let (kind, version) = {
49+ let (kind, d) = {
50+ let (sig, d) = data.split_at(V2_SIGNATURE.len());
51+ let kind = if sig == V2_SIGNATURE {
52+ Kind::V2
53+ } else {
54+ Kind::V1
55+ };
56+ (kind, d)
57+ };
58+ let (version, d) = {
59+ let (mut v, mut d) = (1, d);
60+ if let &Kind::V2 = &kind {
61+ let (vd, dr) = d.split_at(N32_SIZE);
62+ d = dr;
63+ v = BigEndian::read_u32(vd);
64+ if v != 2 {
65+ bail!("Unsupported index version: {}", v);
66+ }
5967 }
60− }
61− v
68+ (v, d)
69+ };
70+ (kind, version)
6271 };
6372 Ok(File {
64− _data: d,
73+ _data: data,
6574 kind,
6675 len: 0,
6776 version,