From 9568952519ae6886bfb24e0b09bdb5754dd53ff7 Mon Sep 17 00:00:00 2001 From: Patrick Cowland Date: Wed, 15 Jan 2025 18:52:54 +0000 Subject: [PATCH] Fix gosec errors --- dvgenerate/generate.go | 1 + internal/service/davinci/resource_variable.go | 16 +++++++++++++--- internal/utils/int.go | 13 +++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 internal/utils/int.go diff --git a/dvgenerate/generate.go b/dvgenerate/generate.go index f4419968..2eaa6b7b 100644 --- a/dvgenerate/generate.go +++ b/dvgenerate/generate.go @@ -105,6 +105,7 @@ func writeTemplateFile(t *template.Template, fileName string, overwrite bool, da return fmt.Errorf("failed to check if file exists: %v", err) } + fileName = filepath.Clean(fileName) outputFile, err := os.Create(fileName) if err != nil { return err diff --git a/internal/service/davinci/resource_variable.go b/internal/service/davinci/resource_variable.go index eddbe3df..a2534118 100644 --- a/internal/service/davinci/resource_variable.go +++ b/internal/service/davinci/resource_variable.go @@ -754,20 +754,30 @@ func (p *VariableResourceModel) toState(apiObject map[string]davinci.Variable) d } value = string(bytes) } - + p.ValueService = framework.StringToTF(value) } else { p.ValueService = types.StringNull() } if v := variableObject.Min; v != nil { - p.Min = framework.Int32ToTF(int32(*v)) + safeInt, err := utils.SafeIntToInt32(*v) + if err != nil { + diags.AddError("Error converting min value", err.Error()) + } else { + p.Min = framework.Int32ToTF(safeInt) + } } else { p.Min = types.Int64Null() } if v := variableObject.Max; v != nil { - p.Max = framework.Int32ToTF(int32(*v)) + safeInt, err := utils.SafeIntToInt32(*v) + if err != nil { + diags.AddError("Error converting max value", err.Error()) + } else { + p.Max = framework.Int32ToTF(safeInt) + } } else { p.Max = types.Int64Null() } diff --git a/internal/utils/int.go b/internal/utils/int.go new file mode 100644 index 00000000..c0144c24 --- /dev/null +++ b/internal/utils/int.go @@ -0,0 +1,13 @@ +package utils + +import ( + "errors" + "math" +) + +func SafeIntToInt32(value int) (int32, error) { + if value > math.MaxInt32 || value < math.MinInt32 { + return 0, errors.New("value out of range for int32") + } + return int32(value), nil +}