Skip to content

Commit 527e0cc

Browse files
Copilotgaby
andauthored
refactor: share uint64 overflow cutoff constants
Agent-Logs-Url: https://github.com/gofiber/utils/sessions/f1341fc1-b18d-4310-ab22-63cc07523d84 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
1 parent 530f871 commit 527e0cc

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

parse.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import (
55
"strconv"
66
)
77

8-
const maxFracDigits = 16
8+
const (
9+
maxFracDigits = 16
10+
maxUint64Cutoff = math.MaxUint64 / 10
11+
maxUint64Cutlim = math.MaxUint64 % 10
12+
)
913

1014
type Signed interface {
1115
~int | ~int8 | ~int16 | ~int32 | ~int64
@@ -136,10 +140,6 @@ func ParseUint8[S byteSeq](s S) (uint8, error) {
136140
// It returns an error if any non-digit is encountered or overflow happens.
137141
func parseDigits[S byteSeq](s S, i int) (uint64, error) {
138142
var n uint64
139-
const (
140-
cutoff = math.MaxUint64 / 10
141-
cutlim = math.MaxUint64 % 10
142-
)
143143
digits := 0
144144
for ; i < len(s); i++ {
145145
c := s[i] - '0'
@@ -148,7 +148,7 @@ func parseDigits[S byteSeq](s S, i int) (uint64, error) {
148148
}
149149
d := uint64(c)
150150
// Any value with <= 19 digits is guaranteed to fit in uint64.
151-
if digits >= 19 && (n > cutoff || (n == cutoff && d > cutlim)) {
151+
if digits >= 19 && (n > maxUint64Cutoff || (n == maxUint64Cutoff && d > maxUint64Cutlim)) {
152152
return 0, strconv.ErrRange
153153
}
154154
n = n*10 + d
@@ -240,14 +240,12 @@ func parseFloat[S byteSeq](fn string, s S) (float64, error) {
240240
}
241241

242242
var intPart uint64
243-
const maxUint64Div10 = ^uint64(0) / 10
244-
const maxUint64Mod10 = ^uint64(0) % 10
245243
for i < len(s) {
246244
c := s[i] - '0'
247245
if c > 9 {
248246
break
249247
}
250-
if intPart > maxUint64Div10 || (intPart == maxUint64Div10 && uint64(c) > maxUint64Mod10) {
248+
if intPart > maxUint64Cutoff || (intPart == maxUint64Cutoff && uint64(c) > maxUint64Cutlim) {
251249
return 0, &strconv.NumError{Func: fn, Num: string(s), Err: strconv.ErrRange}
252250
}
253251
intPart = intPart*10 + uint64(c)

0 commit comments

Comments
 (0)