-
Notifications
You must be signed in to change notification settings - Fork 716
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: add testcast for PutInteger #1277
Conversation
02677f1
to
00f1100
Compare
Signed-off-by: atgane <hyper201286@gmail.com>
internal/kconfig/kconfig.go
Outdated
@@ -263,6 +263,21 @@ func PutInteger(data []byte, integer *btf.Int, n uint64) error { | |||
return fmt.Errorf("invalid boolean value: %d", n) | |||
} | |||
|
|||
if len(data) < int(integer.Size) { | |||
return fmt.Errorf("byte array size %d is less than the integer size %d", len(data), integer.Size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: technically, data is a byte slice, not an array. Also, it's always better to tell the user what the problem is. Sure the byte slice is smaller, but why is that an issue?
return fmt.Errorf("byte array size %d is less than the integer size %d", len(data), integer.Size) | |
return fmt.Errorf("can't fit an integer of size %d into a byte slice of length %d", integer.Size, len(data)) |
internal/kconfig/kconfig.go
Outdated
lowerBound = lowerBound - upperBound - 1 | ||
} | ||
|
||
if !(n <= upperBound || lowerBound < n) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is hard to read. Try:
if !(n <= upperBound || lowerBound < n) { | |
if n > upperBound || n < lowerBound { |
internal/kconfig/kconfig.go
Outdated
} | ||
|
||
upperBound := uint64(1<<(8*integer.Size) - 1) | ||
lowerBound := uint64(math.MaxUint64) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this called lowerBound
if the value used is math.MaxUint64
? Wouldn't this typically be used as an upper bound? Also, there's no need to perform this check on unsigned ints if we enforce some kind of minimum length of data
.
if !(n <= upperBound || lowerBound < n) { | ||
return fmt.Errorf("%d exceeded the given byte range %d bytes size", n, integer.Size) | ||
} | ||
|
||
switch integer.Size { | ||
case 1: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of the upperBound
/lowerBound
stuff from above, what about something like:
case 1: | |
case 1: | |
if integer.Encoding == btf.Signed && (n > math.MaxInt8 || n < math.MinInt8) { | |
return fmt.Errorf("...") | |
} |
This makes it stupidly obvious what the intention is and avoids the subtle math. Go defines all these constants, so we should just make use of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks good, but math.MinInt8 is not unsigned value, so condition can be
(n > math.MaxInt8 && n < math.MaxUint64 - math.MaxInt8)
And I'll change to return the error message only if it's signed conditions.
Signed-off-by: atgane <hyper201286@gmail.com>
00f1100
to
5161cb5
Compare
I've implemented three parts to catch errors.
For example, the red part below throws an error.
Fixes #1208