Skip to content

Commit c843e69

Browse files
authored
Merge pull request #621 from nginx-proxy/integer-conversion
fix: incorrect conversion between integer types
2 parents 6169bf3 + a3585d1 commit c843e69

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

internal/template/reflect.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
package template
22

33
import (
4+
"fmt"
45
"log"
56
"math"
67
"reflect"
78
"strconv"
89
"strings"
910
)
1011

12+
func parseAllocateInt(desired string) (int, error) {
13+
parsed, err := strconv.ParseInt(desired, 10, 32)
14+
if err != nil {
15+
return int(0), err
16+
}
17+
if parsed < 0 {
18+
return int(0), fmt.Errorf("non-negative decimal number required for array/slice index, got %#v", desired)
19+
}
20+
if parsed <= math.MaxInt32 {
21+
return int(parsed), nil
22+
}
23+
return math.MaxInt32, nil
24+
}
25+
1126
func deepGetImpl(v reflect.Value, path []string) interface{} {
1227
if !v.IsValid() {
1328
return nil
@@ -28,15 +43,11 @@ func deepGetImpl(v reflect.Value, path []string) interface{} {
2843
case reflect.Map:
2944
return deepGetImpl(v.MapIndex(reflect.ValueOf(path[0])), path[1:])
3045
case reflect.Slice, reflect.Array:
31-
iu64, err := strconv.ParseUint(path[0], 10, 64)
46+
i, err := parseAllocateInt(path[0])
3247
if err != nil {
33-
log.Printf("non-negative decimal number required for array/slice index, got %#v\n", path[0])
48+
log.Println(err.Error())
3449
return nil
3550
}
36-
if iu64 > math.MaxInt {
37-
iu64 = math.MaxInt
38-
}
39-
i := int(iu64)
4051
if i >= v.Len() {
4152
log.Printf("index %v out of bounds", i)
4253
return nil

0 commit comments

Comments
 (0)