Skip to content

Commit 12b5c75

Browse files
committed
fix conversion for int/uint in ReplaceVariables
There was an unsafe type conversion in the type switch statement in `cotnext.ReplaceVariables()` that caused a panic that looked like this: ``` panic: interface conversion: interface {} is int64, not int goroutine 147 [running]: github.com/gdt-dev/core/context.ReplaceVariables({0x105cbd6c8?, 0x140007a5030?}, {0x14000042480, 0xc}) /Users/abdul/go/pkg/mod/github.com/gdt-dev/core@v1.10.4/context/getter.go:132 +0x31c github.com/gdt-dev/kube.(*Action).get(0x140000b2a20, {0x105cbd6c8, 0x140007a5030}, 0x14000483530, {0x1400027aab8, 0x5}, 0x14000053d00) /Users/abdul/go/pkg/mod/github.com/gdt-dev/kube@v1.10.3/action.go:126 +0x60 github.com/gdt-dev/kube.(*Action).Do(0x105cbd6c8?, {0x105cbd6c8?, 0x140007a5030?}, 0x1400027aab8?, {0x1400027aab8?, 0x1046819ec?}, 0x14000567cc8?) /Users/abdul/go/pkg/mod/github.com/gdt-dev/kube@v1.10.3/action.go:104 +0xb8 github.com/gdt-dev/kube.(*Spec).Eval(0x140000c2d80, {0x105cbd6c8, 0x140007a5030}) /Users/abdul/go/pkg/mod/github.com/gdt-dev/kube@v1.10.3/eval.go:37 +0x1e8 github.com/gdt-dev/core/scenario.(*Scenario).execSpec(0x0?, {0x105cbd6c8, 0x140007a5030}, 0x140007a4fc0, 0x140002406c0, 0x0?, {0x105cbf320, 0x140000c2d80}) /Users/abdul/go/pkg/mod/github.com/gdt-dev/core@v1.10.4/scenario/run.go:369 +0x3ac1 ``` This patch fixes that unsafe type conversion and uses the recommended `strconv.FormatInt` and `strconv.FormatUint` functions for safe conversion of int64 and uint64 to string. Issue gdt-dev/gdt#72 Signed-off-by: Jay Pipes <jaypipes@gmail.com>
1 parent d0f1d21 commit 12b5c75

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

context/getter.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,30 @@ func ReplaceVariables(
128128
dataValStr = dataVal
129129
case []byte:
130130
dataValStr = string(dataVal)
131-
case int, uint, int8, int16, int32, int64:
132-
dataValStr = strconv.Itoa(dataVal.(int))
133-
case float32, float64:
134-
dataValStr = strconv.FormatFloat(dataVal.(float64), 'f', -1, 64)
131+
case int64:
132+
dataValStr = strconv.FormatInt(dataVal, 10)
133+
case int:
134+
dataValStr = strconv.FormatInt(int64(dataVal), 10)
135+
case int8:
136+
dataValStr = strconv.FormatInt(int64(dataVal), 10)
137+
case int16:
138+
dataValStr = strconv.FormatInt(int64(dataVal), 10)
139+
case int32:
140+
dataValStr = strconv.FormatInt(int64(dataVal), 10)
141+
case uint64:
142+
dataValStr = strconv.FormatUint(dataVal, 10)
143+
case uint:
144+
dataValStr = strconv.FormatUint(uint64(dataVal), 10)
145+
case uint8:
146+
dataValStr = strconv.FormatUint(uint64(dataVal), 10)
147+
case uint16:
148+
dataValStr = strconv.FormatUint(uint64(dataVal), 10)
149+
case uint32:
150+
dataValStr = strconv.FormatUint(uint64(dataVal), 10)
151+
case float32:
152+
dataValStr = strconv.FormatFloat(float64(dataVal), 'f', -1, 64)
153+
case float64:
154+
dataValStr = strconv.FormatFloat(dataVal, 'f', -1, 64)
135155
default:
136156
continue
137157
}

0 commit comments

Comments
 (0)