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

Marshal, unmarshal. For many int like cql types unmarshaling full data to the go type smaller than cql type, do not return an error. #253

Open
illia-li opened this issue Aug 29, 2024 · 0 comments
Assignees
Labels

Comments

@illia-li
Copy link

illia-li commented Aug 29, 2024

Currently the functions for Unmarshaling many int like cql types, do not return an error, then the data is full, but go type has a smaller length than the cql type. For example: Unmarshaling TypeBigInt into int8.
Type list: TypeSmallInt, TypeInt, TypeBigInt, TypeCounter, TypeVarint.

Test code:

func TestUnmarshalIntoSmallerTypes(t *testing.T) {
	type tCase struct {
		Type  Type
		Data  []byte
		Value interface{}
	}

	tCases := []tCase{
		{Type: TypeSmallInt, Data: []byte("\xff\xff"), Value: new(int8)},
		{Type: TypeSmallInt, Data: []byte("\xff\xff"), Value: new(uint8)},
		{Type: TypeInt, Data: []byte("\xff\xff\xff\xff"), Value: new(int8)},
		{Type: TypeInt, Data: []byte("\xff\xff\xff\xff"), Value: new(uint8)},
		{Type: TypeInt, Data: []byte("\xff\xff\xff\xff"), Value: new(int16)},
		{Type: TypeInt, Data: []byte("\xff\xff\xff\xff"), Value: new(uint16)},
		{Type: TypeBigInt, Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Value: new(int8)},
		{Type: TypeBigInt, Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Value: new(uint8)},
		{Type: TypeBigInt, Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Value: new(int16)},
		{Type: TypeBigInt, Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Value: new(uint16)},
		{Type: TypeBigInt, Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Value: new(int32)},
		{Type: TypeBigInt, Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Value: new(uint32)},
		{Type: TypeCounter, Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Value: new(int8)},
		{Type: TypeCounter, Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Value: new(uint8)},
		{Type: TypeCounter, Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Value: new(int16)},
		{Type: TypeCounter, Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Value: new(uint16)},
		{Type: TypeCounter, Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Value: new(int32)},
		{Type: TypeCounter, Data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), Value: new(uint32)},
		{Type: TypeVarint, Data: []byte{255, 255, 255, 255, 255, 255, 255, 252}, Value: new(int8)},
		{Type: TypeVarint, Data: []byte{255, 255, 255, 255, 255, 255, 255, 252}, Value: new(uint8)},
		{Type: TypeVarint, Data: []byte{255, 255, 255, 255, 255, 255, 255, 252}, Value: new(int16)},
		{Type: TypeVarint, Data: []byte{255, 255, 255, 255, 255, 255, 255, 252}, Value: new(uint16)},
		{Type: TypeVarint, Data: []byte{255, 255, 255, 255, 255, 255, 255, 252}, Value: new(int32)},
		{Type: TypeVarint, Data: []byte{255, 255, 255, 255, 255, 255, 255, 252}, Value: new(uint32)},
		{Type: TypeVarint, Data: []byte{255, 255, 255, 255, 255, 255, 255, 252}, Value: new(int64)},
	}

	out := make([]string, 0)
	for _, c := range tCases {
		tp := NativeType{proto: 2, typ: c.Type}
		if err := Unmarshal(tp, c.Data, c.Value); err == nil {
			rv := reflect.ValueOf(c.Value)
			if rv.Kind() == reflect.Ptr {
				rv = rv.Elem()
			}
			out = append(out, fmt.Sprintf("type:<%s><%T>\t-\tvalue: <%#v>", tp.Type(), c.Value, rv.Interface()))
		}
	}

	if len(out) != 0 {
		t.Errorf("\nOut of %d test cases, %d cases without errors::\n%s", len(tCases), len(out), strings.Join(out, "\n"))
	}
}

Output:

=== RUN   TestUnmarshalIntoSmallerTypes
    marshal_0_all_test.go:247: 
        Out of 25 test cases, 13 cases without errors::
        type:<smallint><*int8>	-	value: <-1>
        type:<int><*int8>	-	value: <-1>
        type:<int><*int16>	-	value: <-1>
        type:<bigint><*int8>	-	value: <-1>
        type:<bigint><*int16>	-	value: <-1>
        type:<bigint><*int32>	-	value: <-1>
        type:<counter><*int8>	-	value: <-1>
        type:<counter><*int16>	-	value: <-1>
        type:<counter><*int32>	-	value: <-1>
        type:<varint><*int8>	-	value: <-4>
        type:<varint><*int16>	-	value: <-4>
        type:<varint><*int32>	-	value: <-4>
        type:<varint><*int64>	-	value: <-4>
--- FAIL: TestUnmarshalIntoSmallerTypes (0.00s)
@dkropachev dkropachev self-assigned this Aug 31, 2024
@dkropachev dkropachev added the bug label Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants