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

frame: cqlvalue, added missing length checks in AsX methods #288

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
46 changes: 34 additions & 12 deletions frame/cqlvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ func (c CqlValue) AsFloat64() (float64, error) {
uint64(c.Value[7])), nil
}

func readString(raw []byte) (newRaw []byte, read string, err error) {
if len(raw) < 4 {
return nil, "", fmt.Errorf("expected at least 4 bytes, got %d", len(raw))
}
size := binary.BigEndian.Uint32(raw)
if len(raw) < int(size+4) {
return nil, "", fmt.Errorf("expected at least %d bytes, got %d", size+4, len(raw))
}
return raw[size+4:], string(raw[4 : size+4]), nil
}

func (c CqlValue) AsStringSlice() ([]string, error) {
if c.Type.ID != SetID && c.Type.ID != ListID {
return nil, fmt.Errorf("%v can't be interpreted as a slice", c)
Expand All @@ -224,12 +235,15 @@ func (c CqlValue) AsStringSlice() ([]string, error) {
raw = raw[4:]

for i := range res {
if len(raw) < 4 {
return nil, fmt.Errorf("expected at least 4 bytes, got %d", len(raw))
var err error
raw, res[i], err = readString(raw)
if err != nil {
return nil, err
}
size := binary.BigEndian.Uint32(raw)
res[i] = string(raw[4 : size+4])
raw = raw[size+4:]
}

if len(raw) > 0 {
return nil, fmt.Errorf("got %d unexpected trailing bytes", len(raw))
}

return res, nil
Expand All @@ -252,16 +266,24 @@ func (c CqlValue) AsStringMap() (map[string]string, error) {

res := make(map[string]string, n)
for i := 0; i < n; i++ {
keyN := binary.BigEndian.Uint32(raw)
key := string(raw[4 : keyN+4])
raw = raw[keyN+4:]

valueN := binary.BigEndian.Uint32(raw)
value := string(raw[4 : valueN+4])
raw = raw[valueN+4:]
var key, value string
var err error
raw, key, err = readString(raw)
if err != nil {
return nil, err
}
raw, value, err = readString(raw)
if err != nil {
return nil, err
}

res[key] = value
}

if len(raw) > 0 {
return nil, fmt.Errorf("got %d unexpected trailing bytes", len(raw))
}

return res, nil
}

Expand Down