Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ua: ensure variant array dimensions are greater than zero #363

Merged
merged 2 commits into from
Aug 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ua/variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Variant struct {
// field.
arrayLength int32

// arrayDimensionsLength is the numer of dimensions.
// arrayDimensionsLength is the number of dimensions.
// This field is only present if the 'array dimensions' flag
// is set.
arrayDimensionsLength int32
Expand Down Expand Up @@ -147,7 +147,7 @@ func (m *Variant) Decode(b []byte) (int, error) {
m.arrayDimensions = make([]int32, m.arrayDimensionsLength)
for i := 0; i < int(m.arrayDimensionsLength); i++ {
m.arrayDimensions[i] = buf.ReadInt32()
if m.arrayDimensions[i] < 0 {
if m.arrayDimensions[i] < 1 {
return buf.Pos(), StatusBadEncodingLimitsExceeded
}
}
Expand Down
52 changes: 25 additions & 27 deletions ua/variant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func TestVariant(t *testing.T) {
},
},
{
Name: "ExtensionObjeject",
Name: "ExtensionObject",
Struct: MustVariant(NewExtensionObject(
&AnonymousIdentityToken{PolicyID: "anonymous"},
)),
Expand All @@ -277,7 +277,7 @@ func TestVariant(t *testing.T) {
},
},
{
Name: "ExtensionObjeject - ServerStatusDataType",
Name: "ExtensionObject - ServerStatusDataType",
Struct: MustVariant(NewExtensionObject(
&ServerStatusDataType{
StartTime: time.Date(2019, 3, 29, 19, 45, 3, 816525000, time.UTC), // Mar 29, 2019 20:45:03.816525000 CET
Expand Down Expand Up @@ -450,27 +450,6 @@ func TestVariant(t *testing.T) {
0x01, 0x00, 0x00, 0x00,
},
},
{
Name: "[0][0][0]uint32",
Struct: MustVariant([][][]uint32{
{{}, {}},
{{}, {}},
{{}, {}},
}),
Bytes: []byte{
// variant encoding mask
0xc7,
// array length
0x00, 0x00, 0x00, 0x00,
// array values
// array dimensions length
0x03, 0x00, 0x00, 0x00,
// array dimensions
0x03, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
},
},
}
RunCodecTest(t, cases)
}
Expand Down Expand Up @@ -574,9 +553,9 @@ func TestArray(t *testing.T) {
// array values
0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00,
// array dimesions length
// array dimensions length
0xff, 0xff, 0xff, 0xff, // -1
// array dimesions
// array dimensions
0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00,
}
Expand All @@ -595,9 +574,9 @@ func TestArray(t *testing.T) {
// array values
0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00,
// array dimesions length
// array dimensions length
0x02, 0x00, 0x00, 0x00,
// array dimesions
// array dimensions
0x01, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, // -1
}
Expand All @@ -607,6 +586,25 @@ func TestArray(t *testing.T) {
t.Fatalf("got error %#v want %#v", got, want)
}
})
t.Run("dimensions zero", func(t *testing.T) {
b := []byte{
// variant encoding mask
0xc7,
// array length
0x00, 0x00, 0x00, 0x00,
// array values
// array dimensions length
0x02, 0x00, 0x00, 0x00,
// array dimensions
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}

_, err := Decode(b, MustVariant([][]uint32{{}, {}}))
if got, want := err, StatusBadEncodingLimitsExceeded; !errors.Equal(got, want) {
t.Fatalf("got error %#v want %#v", got, want)
}
})
}

func TestSet(t *testing.T) {
Expand Down