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

Fix int like cql types with negative range, add error when processing uint like go types #306

Closed

Conversation

illia-li
Copy link

@illia-li illia-li commented Oct 11, 2024

All int like cql types have negative range and positive range, but uint like go types have only positive range.
For example, if cast -1 tinyint []byte("\xff") into uint like go types we got 255.

In the old marhsal/unmarshal functions (before the starts redesign), this problem was solved in different ways.
Now marhsal/unmarshal functions (before the starts redesign), allow the processing with a negative range of the int like cql types for all uint like go types.
But this can lead to problems due to the different DB and GO representations of the same values .

So, my suggest to leave only one way - stop the processing with a negative range of the int like cql types for all uint like go types.

@illia-li illia-li force-pushed the il/fix/marshal/uints_overflow branch from fa2b2a4 to 43a25cd Compare October 11, 2024 18:31
Comment on lines +183 to +188
case reflect.Uint, reflect.Uint64:
val := v.Uint()
if val > math.MaxInt64 {
return nil, fmt.Errorf("failed to marshal bigint: value (%T)(%[1]v) out of range", v.Interface())
}
return encUint64(val), nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just call EncUint64() here? there is the MaxInt64 check there so no need to duplicate

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To track exactly where the error occurred, some differences are needed in the errors.
In this case, these differences - the type of value (%T)(%[1]v).
If you do not do this, it will be more difficult to track the location of the error.

For me, if the content of all errors included the name of the function and something like a stack trace , this would be an ideal option.

Comment on lines +184 to +188
val := v.Uint()
if val > math.MaxInt64 {
return nil, fmt.Errorf("failed to marshal counter: value (%T)(%[1]v) out of range", v.Interface())
}
return encUint64(val), nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not call EncUint64?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answer here #306 (comment)

@sylwiaszunejko
Copy link
Collaborator

Please add the information about what are you fixing in the commit message

@dkropachev
Copy link
Collaborator

dkropachev commented Oct 12, 2024

@illia-li , @sylwiaszunejko , on some systems int/uint is 32 bytes, on some 64, we need considering this on marshal/unmarshal.
And I think this PR is good place to start doing that.

@@ -398,7 +407,10 @@ func DecUintR(p []byte, v **uint) error {
*v = new(uint)
}
case 8:
val := uint(p[0])<<56 | uint(p[1])<<48 | uint(p[2])<<40 | uint(p[3])<<32 | uint(p[4])<<24 | uint(p[5])<<16 | uint(p[6])<<8 | uint(p[7])
if p[0] > math.MaxInt8 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you consider only first byte ? It looks like mistake

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The p[0] > math.MaxInt8 condition is are like a question is a data value in the negative range?

@dkropachev
Copy link
Collaborator

I think we need first to come up with what is the "correct" behavior and then align code to it.

@illia-li
Copy link
Author

Issue was created: #307
This PR will be closed until a decision is made on the #307 issue.

@illia-li illia-li closed this Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants