Skip to content

Commit

Permalink
[x/serialize] Fix off-by-one error in encoder (#3815)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpranckaitis authored Oct 7, 2021
1 parent 1c72e40 commit 6da0a4a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
41 changes: 41 additions & 0 deletions src/x/serialize/encode_decode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package serialize

import (
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/m3db/m3/src/x/ident"
)

func TestRoundTripLiteralsOfMaximumLength(t *testing.T) {
name := strings.Repeat("n", int(DefaultMaxTagLiteralLength))
value := strings.Repeat("v", int(DefaultMaxTagLiteralLength))
tags := ident.NewTagsIterator(ident.NewTags(ident.StringTag(name, value)))
newTags, err := encodeAndDecode(tags)
require.NoError(t, err)
ok, err := tagItersAreEqual(tags, newTags)
require.NoError(t, err)
require.True(t, ok)
}
2 changes: 1 addition & 1 deletion src/x/serialize/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (e *encoder) encodeID(i ident.ID) error {
d := i.Bytes()

max := int(e.opts.TagSerializationLimits().MaxTagLiteralLength())
if len(d) >= max {
if len(d) > max {
return errTagLiteralTooLong
}

Expand Down
19 changes: 7 additions & 12 deletions src/x/serialize/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package serialize

import (
"bytes"
"strings"
"testing"

"github.com/m3db/m3/src/x/checked"
Expand Down Expand Up @@ -122,10 +122,10 @@ func TestSimpleEncode(t *testing.T) {
func TestTagEncoderErrorEncoding(t *testing.T) {
opts := NewTagEncoderOptions()
e := newTagEncoder(defaultNewCheckedBytesFn, opts, nil)
maxLiteralLen := opts.TagSerializationLimits().MaxTagLiteralLength()
maxLiteralLen := int(opts.TagSerializationLimits().MaxTagLiteralLength())
tags := ident.NewTagsIterator(ident.NewTags(
ident.StringTag("abc", "defg"),
ident.StringTag("x", nstring(1+int(maxLiteralLen))),
ident.StringTag("x", strings.Repeat("x", 1+maxLiteralLen)),
))

require.Error(t, e.Encode(tags))
Expand All @@ -134,7 +134,10 @@ func TestTagEncoderErrorEncoding(t *testing.T) {
require.False(t, ok)

e.Reset()
tags = ident.NewTagsIterator(ident.NewTags(ident.StringTag("abc", "defg")))
tags = ident.NewTagsIterator(ident.NewTags(
ident.StringTag("abc", "defg"),
ident.StringTag("x", strings.Repeat("x", maxLiteralLen)),
))
require.NoError(t, e.Encode(tags))
}

Expand Down Expand Up @@ -219,11 +222,3 @@ func TestSingleValueTagIterEncode(t *testing.T) {
mockBytes.EXPECT().DecRef()
enc.Reset()
}

func nstring(n int) string {
var buf bytes.Buffer
for i := 0; i < n; i++ {
buf.WriteByte(byte('a'))
}
return buf.String()
}

0 comments on commit 6da0a4a

Please sign in to comment.