Skip to content

Commit b3f078d

Browse files
committed
fix: converstion issues found via gosec
1 parent 6d0b09d commit b3f078d

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

Diff for: converters.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ func (c *converters) parseAlphaField(r string, max uint) string {
3232
if ln > max {
3333
return r[ln-max:]
3434
}
35-
if count := int(max - ln); validSize(count) {
36-
r += strings.Repeat(" ", count)
35+
36+
rem := max - ln
37+
if !validSizeUint(rem) {
38+
return ""
39+
} else {
40+
r += strings.Repeat(" ", int(rem)) //nolint:gosec
3741
}
3842
return r
3943
}
@@ -44,8 +48,12 @@ func (c *converters) numericStringField(s string, max uint) string {
4448
if ln > max {
4549
return s[ln-max:]
4650
}
47-
if count := int(max - ln); validSize(count) {
48-
s = strings.Repeat("0", count) + s
51+
52+
rem := max - ln
53+
if !validSizeUint(rem) {
54+
return ""
55+
} else {
56+
s = strings.Repeat("0", int(rem)) + s //nolint:gosec
4957
}
5058
return s
5159
}
@@ -65,8 +73,11 @@ func (c *converters) formatAlphaField(s string, max uint, options FormatOptions)
6573
return s[:max]
6674
}
6775
if !options.VariableLengthFields {
68-
if count := int(max - ln); validSize(count) {
69-
s += strings.Repeat(" ", count)
76+
rem := max - ln
77+
if !validSizeUint(rem) {
78+
return ""
79+
} else {
80+
s += strings.Repeat(" ", int(rem)) //nolint:gosec
7081
}
7182
}
7283
return s

Diff for: unstructuredAddenda.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (ua *UnstructuredAddenda) String() string {
7474
buf.WriteString(ua.tag)
7575
buf.WriteString(ua.AddendaLengthField())
7676

77-
if size := ua.parseNumField(ua.AddendaLength); validSize(size) {
77+
if size := ua.parseNumField(ua.AddendaLength); validSizeInt(size) {
7878
buf.Grow(size)
7979
}
8080

@@ -123,5 +123,9 @@ func (ua *UnstructuredAddenda) AddendaLengthField() string {
123123

124124
// AddendaField gets a string of the Addenda field
125125
func (ua *UnstructuredAddenda) AddendaField() string {
126-
return ua.alphaField(ua.Addenda, uint(ua.parseNumField(ua.AddendaLength)))
126+
max := ua.parseNumField(ua.AddendaLength)
127+
if max < 0 || !validSizeInt(max) {
128+
return ""
129+
}
130+
return ua.alphaField(ua.Addenda, uint(max))
127131
}

Diff for: validators.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ const (
3131
maxBufferGrowth = 1e8
3232
)
3333

34-
func validSize(n int) bool {
34+
func validSizeInt(n int) bool {
3535
return n > 0 && n < maxBufferGrowth
3636
}
3737

38+
func validSizeUint(n uint) bool {
39+
return n < maxBufferGrowth
40+
}
41+
3842
// validator is common validation and formatting of golang types to WIRE type strings
3943
type validator struct{}
4044

Diff for: validators_test.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,31 @@ import (
1313
)
1414

1515
func TestValidSize(t *testing.T) {
16-
require.True(t, validSize(10))
17-
require.True(t, validSize(1e7))
16+
require.True(t, validSizeInt(10))
17+
require.True(t, validSizeInt(1e7))
1818

19-
require.False(t, validSize(1e8+1))
20-
require.False(t, validSize(1e9))
21-
require.False(t, validSize(math.MaxInt))
19+
require.False(t, validSizeInt(1e8+1))
20+
require.False(t, validSizeInt(1e9))
21+
require.False(t, validSizeInt(math.MaxInt))
2222

2323
t.Run("don't grow", func(t *testing.T) {
2424
ua := &UnstructuredAddenda{}
2525
ua.AddendaLength = fmt.Sprintf("%0.0f", 1e9)
2626
expected := "1000"
2727
require.Equal(t, expected, ua.String())
2828
})
29+
30+
t.Run("int", func(t *testing.T) {
31+
require.False(t, validSizeInt(int(1e9)))
32+
})
33+
34+
t.Run("uint", func(t *testing.T) {
35+
a := uint(100)
36+
b := uint(201)
37+
38+
require.False(t, validSizeUint(a-b))
39+
require.True(t, validSizeUint(b-a))
40+
})
2941
}
3042

3143
func TestValidators__validateOptionFName(t *testing.T) {

0 commit comments

Comments
 (0)