Skip to content

Commit 0bd50c0

Browse files
authored
Merge pull request #781 from marle3003/alert-autofix-53
Potential fix for code scanning alert no. 53: Incorrect conversion between integer types
2 parents 6c1a191 + 5bee462 commit 0bd50c0

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

schema/avro/schema/parser_json.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func parseType(v interface{}, s *Schema, typeName string) (interface{}, error) {
5050
if math.Trunc(val) != val {
5151
return 0, Errorf("type", "invalid type, expected %v but got %v", typeName, ToType(v))
5252
}
53+
// Ensure the value fits in a 32-bit signed integer
54+
if val < math.MinInt32 || val > math.MaxInt32 {
55+
return 0, Errorf("type", "integer value %v out of bounds for Avro int32", val)
56+
}
5357
return int(val), nil
5458
}
5559
case "long":

schema/avro/schema/parser_json_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package schema_test
22

33
import (
44
"encoding/json"
5-
"github.com/stretchr/testify/require"
5+
"fmt"
6+
"math"
67
"mokapi/config/dynamic"
78
"mokapi/config/dynamic/dynamictest"
89
"mokapi/schema/avro/schema"
910
"testing"
11+
12+
"github.com/stretchr/testify/require"
1013
)
1114

1215
func TestParser_Parse_Json(t *testing.T) {
@@ -50,6 +53,14 @@ func TestParser_Parse_Json(t *testing.T) {
5053
require.EqualError(t, err, "invalid type, expected int but got float\nschema path #/type")
5154
},
5255
},
56+
{
57+
name: "not int32",
58+
input: fmt.Sprintf("%v", int64(math.MaxInt32)+1),
59+
schema: &schema.Schema{Type: []interface{}{"int"}},
60+
test: func(t *testing.T, v interface{}, err error) {
61+
require.EqualError(t, err, "integer value 2.147483648e+09 out of bounds for Avro int32\nschema path #/type")
62+
},
63+
},
5364
{
5465
name: "long",
5566
input: `123`,

0 commit comments

Comments
 (0)