mirror of
https://github.com/vbatts/tar-split.git
synced 2024-11-16 13:28:37 +00:00
archive/tar: add missing error checks to Reader.Next
A recursive call to Reader.Next did not check the error before trying to use the result, leading to a nil pointer panic. This specific CL addresses the immediate issue, which is the panic, but does not solve the root issue, which is due to an integer overflow in the base-256 parser. Updates #12435 Change-Id: Ia908671f0f411a409a35e24f2ebf740d46734072 Reviewed-on: https://go-review.googlesource.com/15437 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
4ad443d166
commit
cb423795eb
2 changed files with 85 additions and 33 deletions
|
@ -820,40 +820,37 @@ func readGNUSparseMap1x0(r io.Reader) ([]sparseEntry, error) {
|
||||||
return sp, nil
|
return sp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// readGNUSparseMap0x1 reads the sparse map as stored in GNU's PAX sparse format version 0.1.
|
// readGNUSparseMap0x1 reads the sparse map as stored in GNU's PAX sparse format
|
||||||
// The sparse map is stored in the PAX headers.
|
// version 0.1. The sparse map is stored in the PAX headers.
|
||||||
func readGNUSparseMap0x1(headers map[string]string) ([]sparseEntry, error) {
|
func readGNUSparseMap0x1(extHdrs map[string]string) ([]sparseEntry, error) {
|
||||||
// Get number of entries
|
// Get number of entries.
|
||||||
numEntriesStr, ok := headers[paxGNUSparseNumBlocks]
|
// Use integer overflow resistant math to check this.
|
||||||
if !ok {
|
numEntriesStr := extHdrs[paxGNUSparseNumBlocks]
|
||||||
return nil, ErrHeader
|
numEntries, err := strconv.ParseInt(numEntriesStr, 10, 0) // Intentionally parse as native int
|
||||||
}
|
if err != nil || numEntries < 0 || int(2*numEntries) < int(numEntries) {
|
||||||
numEntries, err := strconv.ParseInt(numEntriesStr, 10, 0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, ErrHeader
|
return nil, ErrHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
sparseMap := strings.Split(headers[paxGNUSparseMap], ",")
|
// There should be two numbers in sparseMap for each entry.
|
||||||
|
sparseMap := strings.Split(extHdrs[paxGNUSparseMap], ",")
|
||||||
// There should be two numbers in sparseMap for each entry
|
|
||||||
if int64(len(sparseMap)) != 2*numEntries {
|
if int64(len(sparseMap)) != 2*numEntries {
|
||||||
return nil, ErrHeader
|
return nil, ErrHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop through the entries in the sparse map
|
// Loop through the entries in the sparse map.
|
||||||
|
// numEntries is trusted now.
|
||||||
sp := make([]sparseEntry, 0, numEntries)
|
sp := make([]sparseEntry, 0, numEntries)
|
||||||
for i := int64(0); i < numEntries; i++ {
|
for i := int64(0); i < numEntries; i++ {
|
||||||
offset, err := strconv.ParseInt(sparseMap[2*i], 10, 0)
|
offset, err := strconv.ParseInt(sparseMap[2*i], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ErrHeader
|
return nil, ErrHeader
|
||||||
}
|
}
|
||||||
numBytes, err := strconv.ParseInt(sparseMap[2*i+1], 10, 0)
|
numBytes, err := strconv.ParseInt(sparseMap[2*i+1], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ErrHeader
|
return nil, ErrHeader
|
||||||
}
|
}
|
||||||
sp = append(sp, sparseEntry{offset: offset, numBytes: numBytes})
|
sp = append(sp, sparseEntry{offset: offset, numBytes: numBytes})
|
||||||
}
|
}
|
||||||
|
|
||||||
return sp, nil
|
return sp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -672,23 +672,78 @@ func TestSparseFileReader(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadGNUSparseMap0x1(t *testing.T) {
|
func TestReadGNUSparseMap0x1(t *testing.T) {
|
||||||
headers := map[string]string{
|
const (
|
||||||
|
maxUint = ^uint(0)
|
||||||
|
maxInt = int(maxUint >> 1)
|
||||||
|
)
|
||||||
|
var (
|
||||||
|
big1 = fmt.Sprintf("%d", int64(maxInt))
|
||||||
|
big2 = fmt.Sprintf("%d", (int64(maxInt)/2)+1)
|
||||||
|
big3 = fmt.Sprintf("%d", (int64(maxInt) / 3))
|
||||||
|
)
|
||||||
|
|
||||||
|
var vectors = []struct {
|
||||||
|
extHdrs map[string]string // Input data
|
||||||
|
sparseMap []sparseEntry // Expected sparse entries to be outputted
|
||||||
|
err error // Expected errors that may be raised
|
||||||
|
}{{
|
||||||
|
extHdrs: map[string]string{paxGNUSparseNumBlocks: "-4"},
|
||||||
|
err: ErrHeader,
|
||||||
|
}, {
|
||||||
|
extHdrs: map[string]string{paxGNUSparseNumBlocks: "fee "},
|
||||||
|
err: ErrHeader,
|
||||||
|
}, {
|
||||||
|
extHdrs: map[string]string{
|
||||||
|
paxGNUSparseNumBlocks: big1,
|
||||||
|
paxGNUSparseMap: "0,5,10,5,20,5,30,5",
|
||||||
|
},
|
||||||
|
err: ErrHeader,
|
||||||
|
}, {
|
||||||
|
extHdrs: map[string]string{
|
||||||
|
paxGNUSparseNumBlocks: big2,
|
||||||
|
paxGNUSparseMap: "0,5,10,5,20,5,30,5",
|
||||||
|
},
|
||||||
|
err: ErrHeader,
|
||||||
|
}, {
|
||||||
|
extHdrs: map[string]string{
|
||||||
|
paxGNUSparseNumBlocks: big3,
|
||||||
|
paxGNUSparseMap: "0,5,10,5,20,5,30,5",
|
||||||
|
},
|
||||||
|
err: ErrHeader,
|
||||||
|
}, {
|
||||||
|
extHdrs: map[string]string{
|
||||||
|
paxGNUSparseNumBlocks: "4",
|
||||||
|
paxGNUSparseMap: "0.5,5,10,5,20,5,30,5",
|
||||||
|
},
|
||||||
|
err: ErrHeader,
|
||||||
|
}, {
|
||||||
|
extHdrs: map[string]string{
|
||||||
|
paxGNUSparseNumBlocks: "4",
|
||||||
|
paxGNUSparseMap: "0,5.5,10,5,20,5,30,5",
|
||||||
|
},
|
||||||
|
err: ErrHeader,
|
||||||
|
}, {
|
||||||
|
extHdrs: map[string]string{
|
||||||
|
paxGNUSparseNumBlocks: "4",
|
||||||
|
paxGNUSparseMap: "0,fewafewa.5,fewafw,5,20,5,30,5",
|
||||||
|
},
|
||||||
|
err: ErrHeader,
|
||||||
|
}, {
|
||||||
|
extHdrs: map[string]string{
|
||||||
paxGNUSparseNumBlocks: "4",
|
paxGNUSparseNumBlocks: "4",
|
||||||
paxGNUSparseMap: "0,5,10,5,20,5,30,5",
|
paxGNUSparseMap: "0,5,10,5,20,5,30,5",
|
||||||
}
|
},
|
||||||
expected := []sparseEntry{
|
sparseMap: []sparseEntry{{0, 5}, {10, 5}, {20, 5}, {30, 5}},
|
||||||
{offset: 0, numBytes: 5},
|
}}
|
||||||
{offset: 10, numBytes: 5},
|
|
||||||
{offset: 20, numBytes: 5},
|
|
||||||
{offset: 30, numBytes: 5},
|
|
||||||
}
|
|
||||||
|
|
||||||
sp, err := readGNUSparseMap0x1(headers)
|
for i, v := range vectors {
|
||||||
if err != nil {
|
sp, err := readGNUSparseMap0x1(v.extHdrs)
|
||||||
t.Errorf("Unexpected error: %v", err)
|
if !reflect.DeepEqual(sp, v.sparseMap) && !(len(sp) == 0 && len(v.sparseMap) == 0) {
|
||||||
|
t.Errorf("test %d, readGNUSparseMap0x1(...): got %v, want %v", i, sp, v.sparseMap)
|
||||||
|
}
|
||||||
|
if err != v.err {
|
||||||
|
t.Errorf("test %d, unexpected error: got %v, want %v", i, err, v.err)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(sp, expected) {
|
|
||||||
t.Errorf("Incorrect sparse map: got %v, wanted %v", sp, expected)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue