Skip to content

Commit

Permalink
blob and namesapce constructor to forbid empty data
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Jun 26, 2023
1 parent 68f6f85 commit 47e95dc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
4 changes: 4 additions & 0 deletions blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blob
import (
"bytes"
"encoding/json"
"fmt"

tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

Expand Down Expand Up @@ -116,6 +117,9 @@ func NewBlobV0(namespace share.Namespace, data []byte) (*Blob, error) {

// NewBlob constructs a new blob from the provided Namespace, data and share version.
func NewBlob(shareVersion uint8, namespace share.Namespace, data []byte) (*Blob, error) {
if len(data) == 0 || len(data) > appconsts.DefaultMaxBytes {
return nil, fmt.Errorf("blob data must be > 0 && <= %d, but it was %d bytes", appconsts.DefaultMaxBytes, len(data))
}
if err := namespace.ValidateBlobNamespace(); err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions share/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ type Namespace []byte
// The byte slice must be <= 10 bytes.
// If it is less than 10 bytes, it will be left padded to size 10 with 0s.
func NewNamespaceV0(id []byte) (Namespace, error) {
if len(id) > appns.NamespaceVersionZeroIDSize {
return nil, fmt.Errorf("namespace id must be <= %v, but it was %v bytes", appns.NamespaceVersionZeroIDSize, len(id))
if len(id) == 0 || len(id) > appns.NamespaceVersionZeroIDSize {
return nil, fmt.Errorf(
"namespace id must be > 0 && <= %d, but it was %d bytes", appns.NamespaceVersionZeroIDSize, len(id))
}

n := make(Namespace, NamespaceSize)
Expand Down
12 changes: 9 additions & 3 deletions share/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestNewNamespaceV0(t *testing.T) {
}
testCases := []testCase{
{
name: "8 byte subNid, gets left padded",
name: "8 byte id, gets left padded",
subNid: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
expected: Namespace{
0x0,
Expand All @@ -37,7 +37,7 @@ func TestNewNamespaceV0(t *testing.T) {
wantErr: false,
},
{
name: "10 byte subNid, no padding",
name: "10 byte id, no padding",
subNid: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x9, 0x10},
expected: Namespace{
0x0, // version
Expand All @@ -46,11 +46,17 @@ func TestNewNamespaceV0(t *testing.T) {
wantErr: false,
},
{
name: "11 byte subNid",
name: "11 byte id",
subNid: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x9, 0x10, 0x11},
expected: []byte{},
wantErr: true,
},
{
name: "nil id",
subNid: nil,
expected: []byte{},
wantErr: true,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 47e95dc

Please sign in to comment.