Skip to content

Commit 9f8e191

Browse files
committed
fix bug
1 parent d68dc09 commit 9f8e191

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

storage/bbolt/dedup.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ package bbolt
2828
import (
2929
"context"
3030
"encoding/binary"
31-
"encoding/hex"
3231
"fmt"
3332

3433
"github.com/transparency-dev/static-ct/modules/dedup"
@@ -116,8 +115,9 @@ func (s *Storage) Add(_ context.Context, ldis []dedup.LeafDedupInfo) error {
116115
return fmt.Errorf("vtob(): %v", err)
117116
}
118117

118+
// old should always be 16 bytes long, but double check
119119
if old := db.Get(ldi.LeafID); len(old) == 16 && btoi(old[:8]) <= ldi.Idx {
120-
klog.V(3).Infof("Add(): bucket %q already contains a smaller index %d < %d for entry %q, not updating", dedupBucket, btoi(old[:8]), ldi.Idx, hex.EncodeToString(ldi.LeafID))
120+
klog.V(3).Infof("Add(): bucket %q already contains a smaller index %d < %d for entry \"%x\", not updating", dedupBucket, btoi(old[:8]), ldi.Idx, ldi.LeafID)
121121
} else if err := db.Put(ldi.LeafID, vB); err != nil {
122122
return err
123123
}
@@ -148,7 +148,7 @@ func (s *Storage) Get(_ context.Context, leafID []byte) (dedup.SCTDedupInfo, boo
148148
b := tx.Bucket([]byte(dedupBucket))
149149
vv := b.Get(leafID)
150150
if vv != nil {
151-
v = make([]byte, 16)
151+
v = make([]byte, len(vv))
152152
copy(v, vv)
153153
}
154154
return nil
@@ -196,7 +196,7 @@ func btoi(b []byte) uint64 {
196196

197197
// vtob concatenates an index and timestamp values into a byte array.
198198
func vtob(idx uint64, timestamp uint64) ([]byte, error) {
199-
b := make([]byte, 16)
199+
b := make([]byte, 0, 16)
200200
var err error
201201

202202
b, err = binary.Append(b, binary.BigEndian, idx)
@@ -214,6 +214,9 @@ func vtob(idx uint64, timestamp uint64) ([]byte, error) {
214214
// btov parses a byte array into an index and timestamp values.
215215
func btov(b []byte) (uint64, uint64, error) {
216216
var idx, timestamp uint64
217+
if l := len(b); l != 16 {
218+
return 0, 0, fmt.Errorf("input value is %d bytes long, expected %d", l, 16)
219+
}
217220
n, err := binary.Decode(b, binary.BigEndian, &idx)
218221
if err != nil {
219222
return 0, 0, fmt.Errorf("binary.Decode() could not decode idx: %v", err)

0 commit comments

Comments
 (0)