Skip to content

Commit 87d2d4b

Browse files
fix: address integer conversion issues and impossible uint64 comparisons
Fixed multiple issues identified by golangci-lint and CodeQL: 1. Removed impossible uint64 comparisons (SA4003): - Removed check for blockNum < 0 in tview_renderer.go since blockNum is uint64 - Removed check for result > math.MaxUint64 in rpctypes.go since result is already uint64 2. Fixed unsafe uint64 to int64 conversions in calculateBlockInterval: - Keep timestamp calculations as uint64 to avoid overflow - Check that blockTime >= parentTime before subtraction - Calculate interval and average interval using uint64 arithmetic - Removed unnecessary bounds checks for MaxInt64
1 parent 1c79b7e commit 87d2d4b

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

cmd/monitorv2/renderer/tview_renderer.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,18 +2037,18 @@ func (t *TviewRenderer) calculateBlockInterval(block rpctypes.PolyBlock, index i
20372037
blockTime := block.Time()
20382038
parentTime := parentBlock.Time()
20392039

2040-
// Check if times can be safely converted to int64
2041-
if blockTime > math.MaxInt64 || parentTime > math.MaxInt64 {
2042-
log.Error().
2043-
Uint64("block_time", blockTime).
2044-
Uint64("parent_time", parentTime).
2045-
Msg("Time values exceed int64 range")
2046-
return "N/A"
2040+
// Check if blockTime is after parentTime (normal case)
2041+
if blockTime >= parentTime {
2042+
interval := blockTime - parentTime
2043+
return fmt.Sprintf("%ds", interval)
20472044
}
20482045

2049-
// Safe conversion after bounds check
2050-
interval := int64(blockTime) - int64(parentTime)
2051-
return fmt.Sprintf("%ds", interval)
2046+
// Handle edge case where parent time is after block time
2047+
log.Warn().
2048+
Uint64("block_time", blockTime).
2049+
Uint64("parent_time", parentTime).
2050+
Msg("Parent block time is after child block time")
2051+
return "N/A"
20522052
}
20532053

20542054
// Parent not found by hash, use the next block in the slice
@@ -2065,27 +2065,22 @@ func (t *TviewRenderer) calculateBlockInterval(block rpctypes.PolyBlock, index i
20652065
blockTime := block.Time()
20662066
prevTime := prevBlock.Time()
20672067

2068-
// Check if times can be safely converted to int64
2069-
if blockTime > math.MaxInt64 || prevTime > math.MaxInt64 {
2070-
log.Error().
2068+
// Check if blockTime is after prevTime (normal case)
2069+
if blockTime < prevTime {
2070+
log.Warn().
20712071
Uint64("block_time", blockTime).
20722072
Uint64("prev_time", prevTime).
2073-
Msg("Time values exceed int64 range")
2073+
Msg("Previous block time is after current block time")
20742074
return "N/A"
20752075
}
20762076

2077-
// Safe conversion after bounds check
2078-
interval := int64(blockTime) - int64(prevTime)
2077+
// Calculate interval as uint64
2078+
interval := blockTime - prevTime
20792079

20802080
// For non-consecutive blocks, show average interval
20812081
if currentBlockNum-prevBlockNum > 1 {
20822082
blockDiff := currentBlockNum - prevBlockNum
2083-
// Check if block difference can be safely converted to int64
2084-
if blockDiff > math.MaxInt64 {
2085-
log.Error().Uint64("block_diff", blockDiff).Msg("Block difference exceeds int64 range")
2086-
return "N/A"
2087-
}
2088-
avgInterval := interval / int64(blockDiff)
2083+
avgInterval := interval / blockDiff
20892084
return fmt.Sprintf("~%ds", avgInterval)
20902085
}
20912086
return fmt.Sprintf("%ds", interval)

0 commit comments

Comments
 (0)