Skip to content

Commit bb64dc5

Browse files
committed
expand,syntax: parse brace sequence numbers as int64
strconv.Atoi parses into a platform-sized int, so on 32-bit platforms sequences like {0..9223372036854775807} silently fail to parse. In syntax.SplitBraces this caused the BraceExp to be marked broken and emitted as plain text; in expand it fell back to character-range mode. Use strconv.ParseInt with bit size 64 in both places so expansion behaves consistently across architectures.
1 parent cfd1ecf commit bb64dc5

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

expand/braces.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,22 @@ func bracesSeqRec(word *syntax.Word, yield func(*syntax.Word) bool) bool {
8181
zeros := max(extraLeadingZeros(fromLit), extraLeadingZeros(toLit))
8282

8383
chars := false
84-
from, err1 := strconv.Atoi(fromLit)
85-
to, err2 := strconv.Atoi(toLit)
84+
// ParseInt with bit size 64 to ensure consistent behavior on 32-bit platforms.
85+
from, err1 := strconv.ParseInt(fromLit, 10, 64)
86+
to, err2 := strconv.ParseInt(toLit, 10, 64)
8687
if err1 != nil || err2 != nil {
8788
chars = true
88-
from = int(fromLit[0])
89-
to = int(toLit[0])
89+
from = int64(fromLit[0])
90+
to = int64(toLit[0])
9091
}
9192
upward := from <= to
92-
incr := 1
93+
incr := int64(1)
9394
if !upward {
9495
incr = -1
9596
}
9697
if len(br.Elems) > 2 {
97-
n, _ := strconv.Atoi(br.Elems[2].Lit())
98+
// ParseInt with bit size 64 to ensure consistent behavior on 32-bit platforms.
99+
n, _ := strconv.ParseInt(br.Elems[2].Lit(), 10, 64)
98100
if n != 0 && n > 0 == upward {
99101
incr = n
100102
}
@@ -105,7 +107,7 @@ func bracesSeqRec(word *syntax.Word, yield func(*syntax.Word) bool) bool {
105107
if chars {
106108
lit.Value = string(rune(n))
107109
} else {
108-
lit.Value = strings.Repeat("0", zeros) + strconv.Itoa(n)
110+
lit.Value = strings.Repeat("0", zeros) + strconv.FormatInt(n, 10)
109111
}
110112
next.Parts = append([]syntax.WordPart{lit}, rest...)
111113
if !expand(&next) {

syntax/braces.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ func SplitBraces(word *Word) bool {
120120
broken := false
121121
for i, elem := range br.Elems[:2] {
122122
val := elem.Lit()
123-
if _, err := strconv.Atoi(val); err == nil {
123+
// ParseInt with bit size 64 to ensure consistent behavior on 32-bit platforms.
124+
if _, err := strconv.ParseInt(val, 10, 64); err == nil {
124125
} else if len(val) == 1 && asciiLetter(val[0]) {
125126
chars[i] = true
126127
} else {
@@ -130,7 +131,8 @@ func SplitBraces(word *Word) bool {
130131
if len(br.Elems) == 3 {
131132
// increment must be a number
132133
val := br.Elems[2].Lit()
133-
if _, err := strconv.Atoi(val); err != nil {
134+
// ParseInt with bit size 64 to ensure consistent behavior on 32-bit platforms.
135+
if _, err := strconv.ParseInt(val, 10, 64); err != nil {
134136
broken = true
135137
}
136138
}

0 commit comments

Comments
 (0)