Skip to content

Commit f4b96b8

Browse files
committed
Address unsafe type conversions. Don't panic!
1 parent de5543b commit f4b96b8

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

sql/plan/insert.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"io"
2020
"math"
21+
"strconv"
2122
"strings"
2223

2324
"github.com/dolthub/vitess/go/vt/proto/query"
@@ -663,11 +664,22 @@ func toInt64(x interface{}) int64 {
663664
}
664665
return int64(x)
665666
case float32:
666-
return int64(x)
667+
// Use strconv to safely convert float32 to int64
668+
strVal := strconv.FormatFloat(float64(x), 'f', 0, 64)
669+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
670+
return intVal
671+
}
672+
return 0 // Safe default if conversion fails
667673
case float64:
668-
return int64(x)
674+
// Use strconv to safely convert float64 to int64
675+
strVal := strconv.FormatFloat(x, 'f', 0, 64)
676+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
677+
return intVal
678+
}
679+
return 0 // Safe default if conversion fails
669680
default:
670-
panic(fmt.Sprintf("Expected a numeric auto increment value, but got %T", x))
681+
// Return a safe default instead of panicking
682+
return 0
671683
}
672684
}
673685

0 commit comments

Comments
 (0)