Skip to content

Commit

Permalink
all: fix int64 and uint64 casting to int (#409)
Browse files Browse the repository at this point in the history
* fixed int64 and uint64 casting to int

* don't create more vulns in the process :-)

* review fixes
  • Loading branch information
AL-CT authored Feb 28, 2025
1 parent 27164a1 commit 1713162
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
7 changes: 5 additions & 2 deletions cmd/btctool/blockstream/blockstream.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 Hemi Labs, Inc.
// Copyright (c) 2024-2025 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.

Expand Down Expand Up @@ -40,10 +40,13 @@ func Tip(ctx context.Context) (int, error) {
if err != nil {
return 0, fmt.Errorf("request: %w", err)
}
height, err := strconv.ParseInt(string(b), 10, 64)
height, err := strconv.ParseInt(string(b), 10, 0)
if err != nil {
return 0, fmt.Errorf("parse uint: %w", err)
}
if height < 0 {
return 0, fmt.Errorf("parse uint: unexpected negative value")
}

return int(height), nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/btctool/btctool.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func _main() error {
blockCount := int(1024)
count := args["count"]
if count != "" {
bc, err := strconv.ParseInt(count, 10, 64)
bc, err := strconv.ParseInt(count, 10, 0)
if err != nil {
return fmt.Errorf("count: %w", err)
}
Expand All @@ -661,7 +661,7 @@ func _main() error {
return fmt.Errorf("tip: %w", err)
}
} else {
e, err := strconv.ParseInt(end, 10, 64)
e, err := strconv.ParseInt(end, 10, 0)
if err != nil {
return fmt.Errorf("end: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/hemictl/hemictl.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ func tbcdb(pctx context.Context) error {
}

maxCache := args["maxcache"]
var mc uint64
if maxCache != "" {
if mc, err = strconv.ParseUint(maxCache, 10, 64); err != nil {
mc, err := strconv.ParseInt(maxCache, 10, 0)
if err != nil {
return fmt.Errorf("maxCache: %w", err)
}
cfg.MaxCachedTxs = int(mc)
Expand All @@ -380,9 +380,9 @@ func tbcdb(pctx context.Context) error {
}

maxCache := args["maxcache"]
var mc uint64
if maxCache != "" {
if mc, err = strconv.ParseUint(maxCache, 10, 64); err != nil {
mc, err := strconv.ParseInt(maxCache, 10, 0)
if err != nil {
return fmt.Errorf("maxCache: %w", err)
}
cfg.MaxCachedTxs = int(mc)
Expand Down
2 changes: 1 addition & 1 deletion database/tbcd/level/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,7 @@ func (l *ldb) UtxosByScriptHash(ctx context.Context, sh tbcd.ScriptHash, start u
copy(txId[:], it.Key()[33:65])
utxos = append(utxos, tbcd.NewUtxo(txId, value, index))

if len(utxos) >= int(count) {
if uint64(len(utxos)) >= count {
break
}
}
Expand Down

0 comments on commit 1713162

Please sign in to comment.