Conversation
| return nil, fmt.Errorf("failed to parse shard from toncenter response: %w", err) | ||
| } | ||
|
|
||
| block, err := api.LookupBlock(ctx, res.BlockRef.Workchain, int64(shard), res.BlockRef.SeqNo) |
Check failure
Code scanning / CodeQL
Incorrect conversion between integer types High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 1 hour ago
In general, to fix this class of issues you must ensure that any integer parsed with a larger bit size is not converted to a smaller signed type without verifying that the value lies within the target type’s range. For strconv.ParseUint(..., 64) feeding into int64, you must ensure the parsed value is <= math.MaxInt64 before doing int64(value). If the value is out of range, you should treat it as an error (or clamp/ignore it, depending on your application’s semantics).
For this specific case in pkg/ton/codec/debug/explorer/explorer.go, the best fix with minimal behavior change is:
- Import the
mathpackage (it is not currently imported in this file). - After successfully parsing
shardwithstrconv.ParseUint, add a bounds check ensuringshard <= math.MaxInt64. - If the check fails, return an error indicating the shard value is out of range.
- Only then perform the cast
int64(shard)for the call toapi.LookupBlock.
Concretely:
- Update the import block at the top of
explorer.goto add"math". - In
findTxByToncenterMetadata, between thestrconv.ParseUintcall andapi.LookupBlock, insert a check:if shard > math.MaxInt64 { return nil, fmt.Errorf("shard value out of range for int64: %d", shard) }
- Leave the rest of the function as is, but now the conversion
int64(shard)is guaranteed safe.
| @@ -7,6 +7,7 @@ | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "math" | ||
| "net/http" | ||
| "net/url" | ||
| "os/exec" | ||
| @@ -698,6 +699,9 @@ | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse shard from toncenter response: %w", err) | ||
| } | ||
| if shard > math.MaxInt64 { | ||
| return nil, fmt.Errorf("shard value out of range for int64: %d", shard) | ||
| } | ||
|
|
||
| block, err := api.LookupBlock(ctx, res.BlockRef.Workchain, int64(shard), res.BlockRef.SeqNo) | ||
| if err != nil { |
Features
Bug Fixes
TODO