From 5ae6c60d3934266a2ad1f5e7c8439bce2466400b Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Thu, 20 Sep 2018 10:33:52 +0200 Subject: [PATCH] Handle a bunch of unchecked errors (#365) As discovered by "gosec". Signed-off-by: Julius Volz --- block.go | 7 +++++-- cmd/tsdb/main.go | 23 +++++++++++++++++------ compact.go | 4 +++- head.go | 7 +++++-- index/index.go | 4 +++- tombstones.go | 9 ++++++--- 6 files changed, 39 insertions(+), 15 deletions(-) diff --git a/block.go b/block.go index 342a8d020..7c4ccf0dc 100644 --- a/block.go +++ b/block.go @@ -504,10 +504,13 @@ Outer: func (pb *Block) CleanTombstones(dest string, c Compactor) (*ulid.ULID, error) { 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) return nil - }) + }); err != nil { + // This should never happen, as the iteration function only returns nil. + panic(err) + } if numStones == 0 { return nil, nil } diff --git a/cmd/tsdb/main.go b/cmd/tsdb/main.go index 0a033e5a3..cd24ad1f0 100644 --- a/cmd/tsdb/main.go +++ b/cmd/tsdb/main.go @@ -145,7 +145,9 @@ func (b *writeBenchmark) run() { if err := b.storage.Close(); err != nil { exitWithError(err) } - b.stopProfiling() + if err := b.stopProfiling(); err != nil { + exitWithError(err) + } }) } @@ -248,7 +250,9 @@ func (b *writeBenchmark) startProfiling() { if err != nil { 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. b.memprof, err = os.Create(filepath.Join(b.outPath, "mem.prof")) @@ -271,29 +275,36 @@ func (b *writeBenchmark) startProfiling() { runtime.SetMutexProfileFraction(20) } -func (b *writeBenchmark) stopProfiling() { +func (b *writeBenchmark) stopProfiling() error { if b.cpuprof != nil { pprof.StopCPUProfile() b.cpuprof.Close() b.cpuprof = 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 = 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 = nil runtime.SetBlockProfileRate(0) } 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 = nil runtime.SetMutexProfileFraction(0) } + return nil } func measureTime(stage string, f func()) time.Duration { diff --git a/compact.go b/compact.go index 1b8f20fa5..6df33a4c8 100644 --- a/compact.go +++ b/compact.go @@ -626,7 +626,9 @@ func (c *LeveledCompactor) populateBlock(blocks []BlockReader, meta *BlockMeta, } 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 { diff --git a/head.go b/head.go index b342c8f57..bc8cdfbe4 100644 --- a/head.go +++ b/head.go @@ -793,7 +793,7 @@ func (h *Head) gc() { symbols := make(map[string]struct{}) 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.Value] = struct{}{} @@ -804,7 +804,10 @@ func (h *Head) gc() { } ss.set(t.Value) return nil - }) + }); err != nil { + // This should never happen, as the iteration function only returns nil. + panic(err) + } h.symMtx.Lock() diff --git a/index/index.go b/index/index.go index c58ff6ea8..c75796d73 100644 --- a/index/index.go +++ b/index/index.go @@ -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 // 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 { return errors.Errorf("series write not 16-byte aligned at %d", w.pos) diff --git a/tombstones.go b/tombstones.go index d4a3d0ef1..ad820a05f 100644 --- a/tombstones.go +++ b/tombstones.go @@ -16,12 +16,13 @@ package tsdb import ( "encoding/binary" "fmt" - "github.com/pkg/errors" "io" "io/ioutil" "os" "path/filepath" "sync" + + "github.com/pkg/errors" ) const tombstoneFilename = "tombstones" @@ -72,7 +73,7 @@ func writeTombstoneFile(dir string, tr TombstoneReader) error { 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 { buf.reset() @@ -86,7 +87,9 @@ func writeTombstoneFile(dir string, tr TombstoneReader) error { } } return nil - }) + }); err != nil { + return fmt.Errorf("error writing tombstones: %v", err) + } _, err = f.Write(hash.Sum(nil)) if err != nil {