Fix missing bound checks, off-by-ones, typos

pull/5805/head
Fabian Reinartz 8 years ago
parent 5424a0cf75
commit 9ceed5378e

@ -90,9 +90,11 @@ func findPersistedBlocks(path string) ([]*persistedBlock, error) {
} }
for _, fi := range files { for _, fi := range files {
pb, err := newPersistedBlock(fi.Name()) p := filepath.Join(path, fi.Name())
pb, err := newPersistedBlock(p)
if err != nil { if err != nil {
return nil, fmt.Errorf("error initializing block %q: %s", fi.Name(), err) return nil, fmt.Errorf("error initializing block %q: %s", p, err)
} }
pbs = append(pbs, pb) pbs = append(pbs, pb)
} }

@ -129,8 +129,13 @@ func readHashmap(flag byte, b []byte, err error) (map[string]uint32, error) {
if n < 1 { if n < 1 {
return nil, errInvalidSize return nil, errInvalidSize
} }
s := string(b[n : n+int(l)]) b = b[n:]
b = b[n+int(l):]
if len(b) < int(l) {
return nil, errInvalidSize
}
s := string(b[:l])
b = b[l:]
o, n := binary.Uvarint(b) o, n := binary.Uvarint(b)
if n < 1 { if n < 1 {
@ -151,15 +156,16 @@ func (r *indexReader) section(o uint32) (byte, []byte, error) {
return 0, nil, errInvalidSize return 0, nil, errInvalidSize
} }
flag := r.b[0] flag := b[0]
l := binary.BigEndian.Uint32(b[1:5]) l := binary.BigEndian.Uint32(b[1:5])
b = b[5:] b = b[5:]
if len(b) < int(l) { // b must have the given length plus 4 bytes for the CRC32 checksum.
if len(b) < int(l)+4 {
return 0, nil, errInvalidSize return 0, nil, errInvalidSize
} }
return flag, b, nil return flag, b[:l], nil
} }
func (r *indexReader) lookupSymbol(o uint32) ([]byte, error) { func (r *indexReader) lookupSymbol(o uint32) ([]byte, error) {

@ -200,7 +200,6 @@ func newIndexWriter(w io.Writer) *indexWriter {
n: 0, n: 0,
symbols: make(map[string]uint32, 4096), symbols: make(map[string]uint32, 4096),
series: make(map[uint32]*indexWriterSeries, 4096), series: make(map[uint32]*indexWriterSeries, 4096),
labelIndexes: make([]hashEntry, 10),
} }
} }
@ -212,12 +211,10 @@ func (w *indexWriter) write(wr io.Writer, b []byte) error {
// section writes a CRC32 checksummed section of length l and guarded by flag. // section writes a CRC32 checksummed section of length l and guarded by flag.
func (w *indexWriter) section(l uint32, flag byte, f func(w io.Writer) error) error { func (w *indexWriter) section(l uint32, flag byte, f func(w io.Writer) error) error {
l++ // account for flag byte
h := crc32.NewIEEE() h := crc32.NewIEEE()
wr := io.MultiWriter(h, w.w) wr := io.MultiWriter(h, w.w)
b := [5]byte{flagStd, 0, 0, 0, 0} b := [5]byte{flag, 0, 0, 0, 0}
binary.BigEndian.PutUint32(b[1:], l) binary.BigEndian.PutUint32(b[1:], l)
if err := w.write(wr, b[:]); err != nil { if err := w.write(wr, b[:]); err != nil {
@ -357,7 +354,7 @@ func (w *indexWriter) WriteLabelIndex(names []string, values []string) error {
for _, v := range valt.s { for _, v := range valt.s {
binary.BigEndian.PutUint32(buf, w.symbols[v]) binary.BigEndian.PutUint32(buf, w.symbols[v])
if err := w.write(wr, buf); err != nil { if err := w.write(wr, buf[:4]); err != nil {
return err return err
} }
} }
@ -410,7 +407,7 @@ func (w *indexWriter) writeHashmap(h []hashEntry) error {
b = append(b, buf[:n]...) b = append(b, buf[:n]...)
} }
return w.section(uint32(len(buf)), flagStd, func(wr io.Writer) error { return w.section(uint32(len(b)), flagStd, func(wr io.Writer) error {
return w.write(wr, b) return w.write(wr, b)
}) })
} }

Loading…
Cancel
Save