Handle a bunch of unchecked errors (#365)

As discovered by "gosec".

Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
Julius Volz 2018-09-20 10:33:52 +02:00 committed by Krasi Georgiev
parent dfcb7d0d50
commit 5ae6c60d39
6 changed files with 39 additions and 15 deletions

View file

@ -504,10 +504,13 @@ Outer:
func (pb *Block) CleanTombstones(dest string, c Compactor) (*ulid.ULID, error) { func (pb *Block) CleanTombstones(dest string, c Compactor) (*ulid.ULID, error) {
numStones := 0 numStones := 0
pb.tombstones.Iter(func(id uint64, ivs Intervals) error { if err := pb.tombstones.Iter(func(id uint64, ivs Intervals) error {
numStones += len(ivs) numStones += len(ivs)
return nil return nil
}) }); err != nil {
// This should never happen, as the iteration function only returns nil.
panic(err)
}
if numStones == 0 { if numStones == 0 {
return nil, nil return nil, nil
} }

View file

@ -145,7 +145,9 @@ func (b *writeBenchmark) run() {
if err := b.storage.Close(); err != nil { if err := b.storage.Close(); err != nil {
exitWithError(err) exitWithError(err)
} }
b.stopProfiling() if err := b.stopProfiling(); err != nil {
exitWithError(err)
}
}) })
} }
@ -248,7 +250,9 @@ func (b *writeBenchmark) startProfiling() {
if err != nil { if err != nil {
exitWithError(fmt.Errorf("bench: could not create cpu profile: %v", err)) exitWithError(fmt.Errorf("bench: could not create cpu profile: %v", err))
} }
pprof.StartCPUProfile(b.cpuprof) if err := pprof.StartCPUProfile(b.cpuprof); err != nil {
exitWithError(fmt.Errorf("bench: could not start CPU profile: %v", err))
}
// Start memory profiling. // Start memory profiling.
b.memprof, err = os.Create(filepath.Join(b.outPath, "mem.prof")) b.memprof, err = os.Create(filepath.Join(b.outPath, "mem.prof"))
@ -271,29 +275,36 @@ func (b *writeBenchmark) startProfiling() {
runtime.SetMutexProfileFraction(20) runtime.SetMutexProfileFraction(20)
} }
func (b *writeBenchmark) stopProfiling() { func (b *writeBenchmark) stopProfiling() error {
if b.cpuprof != nil { if b.cpuprof != nil {
pprof.StopCPUProfile() pprof.StopCPUProfile()
b.cpuprof.Close() b.cpuprof.Close()
b.cpuprof = nil b.cpuprof = nil
} }
if b.memprof != nil { if b.memprof != nil {
pprof.Lookup("heap").WriteTo(b.memprof, 0) if err := pprof.Lookup("heap").WriteTo(b.memprof, 0); err != nil {
return fmt.Errorf("error writing mem profile: %v", err)
}
b.memprof.Close() b.memprof.Close()
b.memprof = nil b.memprof = nil
} }
if b.blockprof != nil { if b.blockprof != nil {
pprof.Lookup("block").WriteTo(b.blockprof, 0) if err := pprof.Lookup("block").WriteTo(b.blockprof, 0); err != nil {
return fmt.Errorf("error writing block profile: %v", err)
}
b.blockprof.Close() b.blockprof.Close()
b.blockprof = nil b.blockprof = nil
runtime.SetBlockProfileRate(0) runtime.SetBlockProfileRate(0)
} }
if b.mtxprof != nil { if b.mtxprof != nil {
pprof.Lookup("mutex").WriteTo(b.mtxprof, 0) if err := pprof.Lookup("mutex").WriteTo(b.mtxprof, 0); err != nil {
return fmt.Errorf("error writing mutex profile: %v", err)
}
b.mtxprof.Close() b.mtxprof.Close()
b.mtxprof = nil b.mtxprof = nil
runtime.SetMutexProfileFraction(0) runtime.SetMutexProfileFraction(0)
} }
return nil
} }
func measureTime(stage string, f func()) time.Duration { func measureTime(stage string, f func()) time.Duration {

View file

@ -626,7 +626,9 @@ func (c *LeveledCompactor) populateBlock(blocks []BlockReader, meta *BlockMeta,
} }
for _, chk := range chks { for _, chk := range chks {
c.chunkPool.Put(chk.Chunk) if err := c.chunkPool.Put(chk.Chunk); err != nil {
return errors.Wrap(err, "put chunk")
}
} }
for _, l := range lset { for _, l := range lset {

View file

@ -793,7 +793,7 @@ func (h *Head) gc() {
symbols := make(map[string]struct{}) symbols := make(map[string]struct{})
values := make(map[string]stringset, len(h.values)) values := make(map[string]stringset, len(h.values))
h.postings.Iter(func(t labels.Label, _ index.Postings) error { if err := h.postings.Iter(func(t labels.Label, _ index.Postings) error {
symbols[t.Name] = struct{}{} symbols[t.Name] = struct{}{}
symbols[t.Value] = struct{}{} symbols[t.Value] = struct{}{}
@ -804,7 +804,10 @@ func (h *Head) gc() {
} }
ss.set(t.Value) ss.set(t.Value)
return nil return nil
}) }); err != nil {
// This should never happen, as the iteration function only returns nil.
panic(err)
}
h.symMtx.Lock() h.symMtx.Lock()

View file

@ -271,7 +271,9 @@ func (w *Writer) AddSeries(ref uint64, lset labels.Labels, chunks ...chunks.Meta
} }
// We add padding to 16 bytes to increase the addressable space we get through 4 byte // We add padding to 16 bytes to increase the addressable space we get through 4 byte
// series references. // series references.
w.addPadding(16) if err := w.addPadding(16); err != nil {
return errors.Errorf("failed to write padding bytes: %v", err)
}
if w.pos%16 != 0 { if w.pos%16 != 0 {
return errors.Errorf("series write not 16-byte aligned at %d", w.pos) return errors.Errorf("series write not 16-byte aligned at %d", w.pos)

View file

@ -16,12 +16,13 @@ package tsdb
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"github.com/pkg/errors"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"github.com/pkg/errors"
) )
const tombstoneFilename = "tombstones" const tombstoneFilename = "tombstones"
@ -72,7 +73,7 @@ func writeTombstoneFile(dir string, tr TombstoneReader) error {
mw := io.MultiWriter(f, hash) mw := io.MultiWriter(f, hash)
tr.Iter(func(ref uint64, ivs Intervals) error { if err := tr.Iter(func(ref uint64, ivs Intervals) error {
for _, iv := range ivs { for _, iv := range ivs {
buf.reset() buf.reset()
@ -86,7 +87,9 @@ func writeTombstoneFile(dir string, tr TombstoneReader) error {
} }
} }
return nil return nil
}) }); err != nil {
return fmt.Errorf("error writing tombstones: %v", err)
}
_, err = f.Write(hash.Sum(nil)) _, err = f.Write(hash.Sum(nil))
if err != nil { if err != nil {