Skip to content

Commit ea17f9e

Browse files
authored
fix: Incorrect conversion between integer types (#69)
Add function `GetEnvInt32` using `strconv.ParseInt` when `int32` is expected --------- Signed-off-by: Rafael Vasquez <[email protected]>
1 parent 693291a commit ea17f9e

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

internal/envconfig/envconfig.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
@@ -46,6 +46,18 @@ func GetEnvInt(key string, defaultValue int, log logr.Logger) int {
4646
return defaultValue
4747
}
4848

49+
func GetEnvInt32(key string, defaultValue int32, log logr.Logger) int32 {
50+
if strVal, found := os.LookupEnv(key); found {
51+
val, err := strconv.ParseInt(strVal, 10, 32)
52+
if err != nil {
53+
log.Error(err, "Environment variable must be of type int32", "env_var", key, "value", strVal)
54+
os.Exit(1)
55+
}
56+
return int32(val)
57+
}
58+
return defaultValue
59+
}
60+
4961
func GetEnvFloat(key string, defaultValue float64, log logr.Logger) float64 {
5062
if strVal, found := os.LookupEnv(key); found {
5163
val, err := strconv.ParseFloat(strVal, 64)

model-mesh-torchserve-adapter/server/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
@@ -81,8 +81,8 @@ func GetAdapterConfigurationFromEnv(log logr.Logger) (*AdapterConfiguration, err
8181
return nil, fmt.Errorf("Could not construct torchserve model store path: %w", err)
8282
}
8383

84-
adapterConfig.RequestBatchSize = int32(GetEnvInt(requestBatchSize, defaultRequestBatchSize, log))
85-
adapterConfig.MaxBatchDelaySecs = int32(GetEnvInt(maxBatchDelaySecs, defaultMaxBatchDelaySecs, log))
84+
adapterConfig.RequestBatchSize = GetEnvInt32(requestBatchSize, defaultRequestBatchSize, log)
85+
adapterConfig.MaxBatchDelaySecs = GetEnvInt32(maxBatchDelaySecs, defaultMaxBatchDelaySecs, log)
8686

8787
if adapterConfig.TorchServeContainerMemReqBytes < 0 {
8888
return nil, fmt.Errorf("%s environment variable must be set to a positive integer, found value %v",

0 commit comments

Comments
 (0)