Skip to content

Commit 1c79b7e

Browse files
fix: remove impossible uint64 comparisons in tview_renderer and rpctypes
- Remove check for blockNum < 0 in tview_renderer.go since blockNum is uint64 - Remove check for result > math.MaxUint64 in rpctypes.go since result is already uint64 - Fixes SA4003 staticcheck warnings
1 parent 70427fe commit 1c79b7e

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

cmd/monitorv2/renderer/tview_renderer.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,21 +1984,21 @@ func weiToGwei(wei *big.Int) string {
19841984
// formatRelativeTime converts Unix timestamp to human-readable relative time
19851985
func formatRelativeTime(timestamp uint64) string {
19861986
now := time.Now().Unix()
1987-
1987+
19881988
// Check if timestamp can be safely converted to int64
19891989
if timestamp > math.MaxInt64 {
19901990
log.Error().Uint64("timestamp", timestamp).Msg("Timestamp exceeds int64 range")
19911991
return "invalid"
19921992
}
1993-
1993+
19941994
// Safe conversion after bounds check
19951995
timestampInt64 := int64(timestamp)
1996-
1996+
19971997
// Handle case where timestamp is in the future
19981998
if timestampInt64 > now {
19991999
return "future"
20002000
}
2001-
2001+
20022002
diff := now - timestampInt64
20032003

20042004
if diff < 60 {
@@ -2020,7 +2020,7 @@ func formatBlockTime(timestamp uint64) string {
20202020
// Return a special format for invalid timestamps
20212021
return "invalid timestamp - invalid"
20222022
}
2023-
2023+
20242024
// Safe conversion after bounds check
20252025
t := time.Unix(int64(timestamp), 0).UTC()
20262026
absolute := t.Format("2006-01-02T15:04:05Z")
@@ -2036,7 +2036,7 @@ func (t *TviewRenderer) calculateBlockInterval(block rpctypes.PolyBlock, index i
20362036
// Calculate interval in seconds
20372037
blockTime := block.Time()
20382038
parentTime := parentBlock.Time()
2039-
2039+
20402040
// Check if times can be safely converted to int64
20412041
if blockTime > math.MaxInt64 || parentTime > math.MaxInt64 {
20422042
log.Error().
@@ -2045,7 +2045,7 @@ func (t *TviewRenderer) calculateBlockInterval(block rpctypes.PolyBlock, index i
20452045
Msg("Time values exceed int64 range")
20462046
return "N/A"
20472047
}
2048-
2048+
20492049
// Safe conversion after bounds check
20502050
interval := int64(blockTime) - int64(parentTime)
20512051
return fmt.Sprintf("%ds", interval)
@@ -2064,7 +2064,7 @@ func (t *TviewRenderer) calculateBlockInterval(block rpctypes.PolyBlock, index i
20642064
if currentBlockNum > prevBlockNum && currentBlockNum-prevBlockNum <= 100 {
20652065
blockTime := block.Time()
20662066
prevTime := prevBlock.Time()
2067-
2067+
20682068
// Check if times can be safely converted to int64
20692069
if blockTime > math.MaxInt64 || prevTime > math.MaxInt64 {
20702070
log.Error().
@@ -2073,10 +2073,10 @@ func (t *TviewRenderer) calculateBlockInterval(block rpctypes.PolyBlock, index i
20732073
Msg("Time values exceed int64 range")
20742074
return "N/A"
20752075
}
2076-
2076+
20772077
// Safe conversion after bounds check
20782078
interval := int64(blockTime) - int64(prevTime)
2079-
2079+
20802080
// For non-consecutive blocks, show average interval
20812081
if currentBlockNum-prevBlockNum > 1 {
20822082
blockDiff := currentBlockNum - prevBlockNum
@@ -2720,9 +2720,6 @@ func (t *TviewRenderer) searchBlockByNumber(blockNum uint64) {
27202720
if blockNum > math.MaxInt64 {
27212721
log.Error().Uint64("block_num", blockNum).Msg("Block number exceeds int64 range, using MaxInt64")
27222722
constrainedBlockNum = math.MaxInt64
2723-
} else if blockNum < 0 {
2724-
log.Error().Uint64("block_num", blockNum).Msg("Block number is negative, using 0")
2725-
constrainedBlockNum = 0
27262723
} else {
27272724
constrainedBlockNum = int64(blockNum)
27282725
}

rpctypes/rpctypes.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,11 +665,7 @@ func (r RawQuantityResponse) ToUint64() uint64 {
665665
if err != nil {
666666
return 0
667667
}
668-
if result > math.MaxUint64 {
669-
log.Error().Uint64("value", result).Msg("Value exceeds uint64 range, clamping to MaxUint64")
670-
result = math.MaxUint64
671-
}
672-
return uint64(result)
668+
return result
673669
}
674670
func (r RawQuantityResponse) ToFloat64() float64 {
675671
return float64(r.ToInt64())

0 commit comments

Comments
 (0)