From d7a93e250e96687fc20fc0cc99d518318450174d Mon Sep 17 00:00:00 2001 From: Christian Kadner Date: Thu, 5 Oct 2023 17:41:07 -0700 Subject: [PATCH] fix: Incorrect conversion between integer types To address code scanning alerts #1, #2, #3: - https://github.com/kserve/modelmesh-serving/security/code-scanning/1 - https://github.com/kserve/modelmesh-serving/security/code-scanning/2 - https://github.com/kserve/modelmesh-serving/security/code-scanning/3 Signed-off-by: Christian Kadner --- controllers/hpa/hpa_reconciler.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/controllers/hpa/hpa_reconciler.go b/controllers/hpa/hpa_reconciler.go index 6efd8427..0ce0b7ff 100644 --- a/controllers/hpa/hpa_reconciler.go +++ b/controllers/hpa/hpa_reconciler.go @@ -59,8 +59,11 @@ func getHPAMetrics(metadata metav1.ObjectMeta) []hpav2.MetricSpec { resourceName := corev1.ResourceCPU if value, ok := annotations[constants.TargetUtilizationPercentage]; ok { - utilizationInt, _ := strconv.Atoi(value) - utilization = int32(utilizationInt) + if valueInt, err := strconv.ParseInt(value, 10, 32); err != nil { + log.Error(err, "Could not parse TargetUtilizationPercentage", "value", value) + } else { + utilization = int32(valueInt) + } } if value, ok := annotations[constants.AutoscalerMetrics]; ok { @@ -90,13 +93,19 @@ func createHPA(runtimeMeta metav1.ObjectMeta, mmDeploymentName string, mmNamespa annotations := runtimeMeta.Annotations if value, ok := annotations[mmcontstant.MinScaleAnnotationKey]; ok { - minReplicasInt, _ := strconv.Atoi(value) - minReplicas = int32(minReplicasInt) - + if valueInt, err := strconv.ParseInt(value, 10, 32); err != nil { + log.Error(err, "Could not parse MinScaleAnnotationKey", "value", value) + } else { + minReplicas = int32(valueInt) + } } + if value, ok := annotations[mmcontstant.MaxScaleAnnotationKey]; ok { - maxReplicasInt, _ := strconv.Atoi(value) - maxReplicas = int32(maxReplicasInt) + if valueInt, err := strconv.ParseInt(value, 10, 32); err != nil { + log.Error(err, "Could not parse MaxScaleAnnotationKey", "value", value) + } else { + maxReplicas = int32(valueInt) + } } if maxReplicas < minReplicas {