Skip to content

Commit 38a6949

Browse files
Merge pull request #157 from gofr-dev/FIX/intconversion
Add check for uint16 bounds in Azure Filestore
2 parents 36e5742 + c7ca749 commit 38a6949

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

pkg/file/azure.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package file
33
import (
44
"context"
55
"fmt"
6+
"math"
67
"math/rand"
78
"net/url"
89
"os"
@@ -62,7 +63,7 @@ func newAzureFile(c *AzureConfig, filename string, mode Mode) (*azure, error) {
6263
return nil, err
6364
}
6465

65-
azFile.parallelism = uint16(pl)
66+
azFile.parallelism = validateAndConvertToUint16(pl)
6667
}
6768

6869
return azFile, nil
@@ -100,3 +101,11 @@ func randomString() string {
100101
r := rand.New(rand.NewSource(time.Now().UnixNano())) //nolint:gosec // Use of weak random number generator
101102
return strconv.Itoa(r.Int())
102103
}
104+
105+
func validateAndConvertToUint16(num int) uint16 {
106+
if num >= 0 && num <= math.MaxUint16 {
107+
return uint16(num)
108+
}
109+
110+
return uint16(0)
111+
}

pkg/file/azure_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/base64"
66
"errors"
7+
"math"
78
"net/url"
89
"strconv"
910
"testing"
@@ -102,6 +103,26 @@ func Test_azure_push(t *testing.T) {
102103
_ = localFile.Close()
103104
}
104105

106+
func TestValidateAndConvertToUint16(t *testing.T) {
107+
tests := []struct {
108+
desc string
109+
input int
110+
output uint16
111+
}{
112+
{"less than 0", -1, uint16(0)},
113+
{"is 0", 0, uint16(0)},
114+
{"is max uint16", math.MaxUint16, uint16(math.MaxUint16)},
115+
{"in uint16 range", 10, uint16(10)},
116+
{"greater than uint16 range", math.MaxUint16 + 1, uint16(0)},
117+
}
118+
119+
for _, tc := range tests {
120+
result := validateAndConvertToUint16(tc.input)
121+
122+
assert.Equal(t, tc.output, result)
123+
}
124+
}
125+
105126
func Test_azure_fetch(t *testing.T) {
106127
testPipelineMock := testPipeline{}
107128

0 commit comments

Comments
 (0)