Skip to content

Commit b5e672c

Browse files
authored
fix: guard against int overflows (#344)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent cf234f8 commit b5e672c

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

internal/storage/storage.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ import (
1818
"encoding/hex"
1919
"fmt"
2020
"log/slog"
21+
"math"
2122
"strconv"
2223
"strings"
2324
"sync"
@@ -194,6 +195,9 @@ func (s *Storage) AddUtxo(
194195
txOutBytes []byte,
195196
slot uint64,
196197
) error {
198+
if slot > math.MaxInt {
199+
return fmt.Errorf("slot number int overflow")
200+
}
197201
keyUtxo := fmt.Sprintf("utxo_%s_%s.%d", address, txId, txOutIdx)
198202
keyAdded := keyUtxo + `_added`
199203
err := s.db.Update(func(txn *badger.Txn) error {
@@ -225,7 +229,7 @@ func (s *Storage) AddUtxo(
225229
[]byte(keyAdded),
226230
[]byte(
227231
// Convert slot to string for storage
228-
strconv.Itoa(int(slot)),
232+
strconv.Itoa(int(slot)), // #nosec G115
229233
),
230234
); err != nil {
231235
return err
@@ -241,6 +245,9 @@ func (s *Storage) RemoveUtxo(
241245
utxoIdx uint32,
242246
slot uint64,
243247
) error {
248+
if slot > math.MaxInt {
249+
return fmt.Errorf("slot number int overflow")
250+
}
244251
keyUtxo := fmt.Sprintf("utxo_%s_%s.%d", address, txId, utxoIdx)
245252
keyDeleted := keyUtxo + `_deleted`
246253
err := s.db.Update(func(txn *badger.Txn) error {
@@ -253,7 +260,7 @@ func (s *Storage) RemoveUtxo(
253260
[]byte(keyDeleted),
254261
[]byte(
255262
// Convert slot to string for storage
256-
strconv.Itoa(int(slot)),
263+
strconv.Itoa(int(slot)), // #nosec G115
257264
),
258265
); err != nil {
259266
return err

internal/storage/trie.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package storage
1717
import (
1818
"encoding/hex"
1919
"fmt"
20+
"math"
2021
"strconv"
2122
"strings"
2223
"sync"
@@ -84,6 +85,9 @@ func (t *Trie) load() error {
8485
}
8586

8687
func (t *Trie) Update(key []byte, val []byte, slot uint64) error {
88+
if slot > math.MaxInt {
89+
return fmt.Errorf("slot number int overflow")
90+
}
8791
// Update trie
8892
t.trie.Set(key, val)
8993
// Update storage
@@ -98,7 +102,7 @@ func (t *Trie) Update(key []byte, val []byte, slot uint64) error {
98102
[]byte(keyAdded),
99103
[]byte(
100104
// Convert slot to string for storage
101-
strconv.Itoa(int(slot)),
105+
strconv.Itoa(int(slot)), // #nosec G115
102106
),
103107
); err != nil {
104108
return err
@@ -132,6 +136,9 @@ func (t *Trie) Delete(key []byte) error {
132136
}
133137

134138
func (t *Trie) Rollback(slot uint64) error {
139+
if slot > math.MaxInt64 {
140+
return fmt.Errorf("slot number int overflow")
141+
}
135142
dbKeyPrefix := t.dbKeyPrefix(nil)
136143
err := t.db.Update(func(txn *badger.Txn) error {
137144
it := txn.NewIterator(badger.DefaultIteratorOptions)
@@ -155,6 +162,7 @@ func (t *Trie) Rollback(slot uint64) error {
155162
if err != nil {
156163
return err
157164
}
165+
// #nosec G115
158166
if addSlot > int(slot) {
159167
// Delete rolled-back hashes from trie
160168
tmpKey := strings.TrimPrefix(

0 commit comments

Comments
 (0)