Skip to content

Commit 1a60d22

Browse files
authored
Check for all returned errors (#396)
* linter: add errcheck Signed-off-by: Ignacio Hagopian <[email protected]> * tests: do not ignore errors Signed-off-by: Ignacio Hagopian <[email protected]> * lint: fix warnings Signed-off-by: Ignacio Hagopian <[email protected]> * avoid ignoring errors in main code Signed-off-by: Ignacio Hagopian <[email protected]> * rebase fix Signed-off-by: Ignacio Hagopian <[email protected]> * make deepsource linter happy Signed-off-by: Ignacio Hagopian <[email protected]> * add more missing checks after rebase Signed-off-by: Ignacio Hagopian <[email protected]> --------- Signed-off-by: Ignacio Hagopian <[email protected]>
1 parent 6a6b1f7 commit 1a60d22

10 files changed

+417
-152
lines changed

.golangci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ run:
1212
linters:
1313
disable-all: true
1414
enable:
15+
- errcheck
1516
- gofmt
1617
- staticcheck
17-
- deadcode
1818
- goconst
1919
- goimports
2020
- gosimple
2121
- govet
2222
- ineffassign
2323
- misspell
2424
- unconvert
25-
- varcheck
25+
- unused
2626

2727
linters-settings:
2828
gofmt:

benchs/main.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ func main() {
1717
func benchmarkInsertInExisting() {
1818
f, _ := os.Create("cpu.prof")
1919
g, _ := os.Create("mem.prof")
20-
pprof.StartCPUProfile(f)
20+
_ = pprof.StartCPUProfile(f)
2121
defer pprof.StopCPUProfile()
22-
defer pprof.WriteHeapProfile(g)
22+
defer func() { _ = pprof.WriteHeapProfile(g) }()
2323
// Number of existing leaves in tree
2424
n := 1000000
2525
// Leaves to be inserted afterwards
@@ -34,7 +34,9 @@ func benchmarkInsertInExisting() {
3434
// Generate set of keys once
3535
for i := 0; i < total; i++ {
3636
key := make([]byte, 32)
37-
rand.Read(key)
37+
if _, err := rand.Read(key); err != nil {
38+
panic(err)
39+
}
3840
if i < n {
3941
keys[i] = key
4042
} else {

conversion.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func BatchNewLeafNode(nodesValues []BatchNewLeafNodeData) ([]LeafNode, error) {
5555
c1c2frs[2*i], c1c2frs[2*i+1] = new(Fr), new(Fr)
5656
}
5757

58-
banderwagon.BatchMapToScalarField(c1c2frs, c1c2points)
58+
if err := banderwagon.BatchMapToScalarField(c1c2frs, c1c2points); err != nil {
59+
return fmt.Errorf("mapping to scalar field: %s", err)
60+
}
5961

6062
var poly [NodeWidth]Fr
6163
poly[0].SetUint64(1)
@@ -145,7 +147,7 @@ func (n *InternalNode) InsertMigratedLeaves(leaves []LeafNode, resolver NodeReso
145147
return nil
146148
}
147149

148-
func (n *InternalNode) insertMigratedLeavesSubtree(leaves []LeafNode, resolver NodeResolverFn) error {
150+
func (n *InternalNode) insertMigratedLeavesSubtree(leaves []LeafNode, resolver NodeResolverFn) error { // skipcq: GO-R1005
149151
for i := range leaves {
150152
ln := leaves[i]
151153
parent := n

debug_test.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,18 @@ func TestJSON(t *testing.T) {
3333
t.Parallel()
3434

3535
root := New()
36-
root.Insert(zeroKeyTest, fourtyKeyTest, nil)
37-
root.Insert(oneKeyTest, zeroKeyTest, nil)
38-
root.Insert(forkOneKeyTest, zeroKeyTest, nil) // Force an internal node in the first layer.
39-
root.Insert(fourtyKeyTest, oneKeyTest, nil)
36+
if err := root.Insert(zeroKeyTest, fourtyKeyTest, nil); err != nil {
37+
t.Fatal(err)
38+
}
39+
if err := root.Insert(oneKeyTest, zeroKeyTest, nil); err != nil {
40+
t.Fatal(err)
41+
}
42+
if err := root.Insert(forkOneKeyTest, zeroKeyTest, nil); err != nil { // Force an internal node in the first layer.
43+
t.Fatal(err)
44+
}
45+
if err := root.Insert(fourtyKeyTest, oneKeyTest, nil); err != nil {
46+
t.Fatal(err)
47+
}
4048
root.(*InternalNode).children[152] = HashedNode{}
4149
root.Commit()
4250

encoding.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,17 @@ func parseLeafNode(serialized []byte, depth byte) (VerkleNode, error) {
109109
return nil, fmt.Errorf("leaf node commitments are not the correct size, expected at least %d, got %d", 3*banderwagon.UncompressedSize, len(serialized[leafC1CommitmentOffset:]))
110110
}
111111

112-
ln.c1.SetBytesUncompressed(serialized[leafC1CommitmentOffset:leafC1CommitmentOffset+banderwagon.UncompressedSize], true)
112+
if err := ln.c1.SetBytesUncompressed(serialized[leafC1CommitmentOffset:leafC1CommitmentOffset+banderwagon.UncompressedSize], true); err != nil {
113+
return nil, fmt.Errorf("setting c1 commitment: %w", err)
114+
}
113115
ln.c2 = new(Point)
114-
ln.c2.SetBytesUncompressed(serialized[leafC2CommitmentOffset:leafC2CommitmentOffset+banderwagon.UncompressedSize], true)
116+
if err := ln.c2.SetBytesUncompressed(serialized[leafC2CommitmentOffset:leafC2CommitmentOffset+banderwagon.UncompressedSize], true); err != nil {
117+
return nil, fmt.Errorf("setting c2 commitment: %w", err)
118+
}
115119
ln.commitment = new(Point)
116-
ln.commitment.SetBytesUncompressed(serialized[leafCommitmentOffset:leafC1CommitmentOffset], true)
120+
if err := ln.commitment.SetBytesUncompressed(serialized[leafCommitmentOffset:leafC1CommitmentOffset], true); err != nil {
121+
return nil, fmt.Errorf("setting commitment: %w", err)
122+
}
117123
return ln, nil
118124
}
119125

@@ -145,6 +151,8 @@ func CreateInternalNode(bitlist []byte, raw []byte, depth byte) (*InternalNode,
145151
}
146152

147153
node.commitment = new(Point)
148-
node.commitment.SetBytesUncompressed(raw, true)
154+
if err := node.commitment.SetBytesUncompressed(raw, true); err != nil {
155+
return nil, fmt.Errorf("setting commitment: %w", err)
156+
}
149157
return node, nil
150158
}

proof_ipa.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,21 @@ func DeserializeProof(vp *VerkleProof, statediff StateDiff) (*Proof, error) {
350350
commitments[i] = &commitment
351351
}
352352

353-
multipoint.D.SetBytes(vp.D[:])
353+
if err := multipoint.D.SetBytes(vp.D[:]); err != nil {
354+
return nil, fmt.Errorf("setting D: %w", err)
355+
}
354356
multipoint.IPA.A_scalar.SetBytes(vp.IPAProof.FinalEvaluation[:])
355357
multipoint.IPA.L = make([]Point, IPA_PROOF_DEPTH)
356358
for i, b := range vp.IPAProof.CL {
357-
multipoint.IPA.L[i].SetBytes(b[:])
359+
if err := multipoint.IPA.L[i].SetBytes(b[:]); err != nil {
360+
return nil, fmt.Errorf("setting L[%d]: %w", i, err)
361+
}
358362
}
359363
multipoint.IPA.R = make([]Point, IPA_PROOF_DEPTH)
360364
for i, b := range vp.IPAProof.CR {
361-
multipoint.IPA.R[i].SetBytes(b[:])
365+
if err := multipoint.IPA.R[i].SetBytes(b[:]); err != nil {
366+
return nil, fmt.Errorf("setting R[%d]: %w", i, err)
367+
}
362368
}
363369

364370
// turn statediff into keys and values

0 commit comments

Comments
 (0)