Skip to content

Commit

Permalink
Stop panicking when trying to parse numbers bigger than 2^64.
Browse files Browse the repository at this point in the history
Before: panic: mixedNumberPattern yielded an unparseable int: strconv.ParseInt: parsing "99999999999999999999999999999999999999999999999": value out of range
  • Loading branch information
JeffFaer committed Dec 15, 2023
1 parent 8890132 commit d5c1ab7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
keep-sorted

.idea/
*.iml
1 change: 1 addition & 0 deletions goldens/numeric.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ FOO_3
BAR_1
BAR_2
BAR_10
BAR_99999999999999999999999999999999999999999999999
// keep-sorted-test end


Expand Down
1 change: 1 addition & 0 deletions goldens/numeric.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Numeric sorting:
BAR_1
BAR_2
BAR_10
BAR_99999999999999999999999999999999999999999999999
FOO_2
FOO_3
FOO_100
Expand Down
4 changes: 2 additions & 2 deletions keepsorted/keep_sorted.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (f *Fixer) errorMissingEnd() string {
return fmt.Sprintf("This instruction doesn't have matching '%s' line", f.endDirective)
}

// Fix all of the findings on contents to make go/keep-sorted happy.
// Fix all of the findings on contents to make keep-sorted happy.
func (f *Fixer) Fix(contents string, modifiedLines []LineRange) (fixed string, alreadyCorrect bool) {
lines := strings.Split(contents, "\n")
fs := f.findings("unused-filename", lines, modifiedLines, false)
Expand All @@ -78,7 +78,7 @@ func (f *Fixer) Fix(contents string, modifiedLines []LineRange) (fixed string, a
}

// Findings returns a slice of things that need to be addressed in the file to
// make go/keep-sorted happy.
// make keep-sorted happy.
//
// If modifiedLines is non-nil, we only report findings for issues within the
// modified lines. Otherwise, we report all findings.
Expand Down
16 changes: 7 additions & 9 deletions keepsorted/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"unicode"

"go.uber.org/multierr"
"golang.org/x/exp/slices"
"math/big"
)

var (
Expand Down Expand Up @@ -83,7 +83,7 @@ type blockOptions struct {

// LINT.ThenChange(//depot/google3/devtools/keep_sorted/README.md)

// Syntax used to start a comment for go/keep-sorted annotation, e.g. "//".
// Syntax used to start a comment for keep-sorted annotation, e.g. "//".
commentMarker string
}

Expand Down Expand Up @@ -242,8 +242,8 @@ func (opts blockOptions) maybeParseNumeric(s string) numericTokens {
// See the comment on numericTokens for more details.
t.s = append(t.s, "")
}
i, err := strconv.ParseInt(sm[1], 10, 64)
if err != nil {
i := new(big.Int)
if err := i.UnmarshalText([]byte(sm[1])); err != nil {
panic(fmt.Errorf("mixedNumberPattern yielded an unparseable int: %w", err))
}
t.i = append(t.i, i)
Expand All @@ -269,7 +269,7 @@ func (opts blockOptions) maybeParseNumeric(s string) numericTokens {
// i: []int64{123},
type numericTokens struct {
s []string
i []int64
i []*big.Int
}

func (t numericTokens) len() int {
Expand All @@ -287,10 +287,8 @@ func (t numericTokens) compare(o numericTokens) int {
return c
}
} else { // Alternate by comparing with numbers.
if c := t.i[i/2] - o.i[i/2]; c < 0 {
return -1
} else if c > 0 {
return 1
if c := t.i[i/2].Cmp(o.i[i/2]); c != 0 {
return c
}
}
}
Expand Down

0 comments on commit d5c1ab7

Please sign in to comment.