Skip to content

Commit

Permalink
fix: change condition format & test msg
Browse files Browse the repository at this point in the history
Signed-off-by: atgane <[email protected]>
  • Loading branch information
atgane committed Feb 6, 2024
1 parent 94766ef commit 82fa5ac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
12 changes: 6 additions & 6 deletions internal/kconfig/kconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,18 @@ func PutInteger(data []byte, integer *btf.Int, n uint64) error {

switch integer.Size {
case 1:
if integer.Encoding == btf.Signed && (n > math.MaxInt8 && n < math.MaxUint64-math.MaxInt8) {
return fmt.Errorf("can't represent %d as a signed integer of size %d", n, integer.Size)
if integer.Encoding == btf.Signed && (int64(n) > math.MaxInt8 || int64(n) < math.MinInt8) {
return fmt.Errorf("can't represent %d as a signed integer of size %d", int64(n), integer.Size)
}
data[0] = byte(n)
case 2:
if integer.Encoding == btf.Signed && (n > math.MaxInt16 && n < math.MaxUint64-math.MaxInt16) {
return fmt.Errorf("can't represent %d as a signed integer of size %d", n, integer.Size)
if integer.Encoding == btf.Signed && (int64(n) > math.MaxInt16 || int64(n) < math.MinInt16) {
return fmt.Errorf("can't represent %d as a signed integer of size %d", int64(n), integer.Size)
}
internal.NativeEndian.PutUint16(data, uint16(n))
case 4:
if integer.Encoding == btf.Signed && (n > math.MaxInt32 && n < math.MaxUint64-math.MaxInt32) {
return fmt.Errorf("can't represent %d as a signed integer of size %d", n, integer.Size)
if integer.Encoding == btf.Signed && (int64(n) > math.MaxInt32 || int64(n) < math.MinInt32) {
return fmt.Errorf("can't represent %d as a signed integer of size %d", int64(n), integer.Size)
}
internal.NativeEndian.PutUint32(data, uint32(n))
case 8:
Expand Down
30 changes: 27 additions & 3 deletions internal/kconfig/kconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,11 @@ func TestPutInteger(t *testing.T) {
Size: 2,
Encoding: btf.Signed,
},
n: 0xffffffffffffffff - 0x010000,
n: 0xffffffffffffffff - 0x008000,
expected: expected{
err: "can't represent 18446744073709486079 as a signed integer of size 2",
err: "can't represent -32769 as a signed integer of size 2",
},
comment: "n means -32768 - 1 in signed value",
comment: "n means -32768(-MinInt16) - 1 in signed value",
},
{
data: make([]byte, 2),
Expand Down Expand Up @@ -594,6 +594,30 @@ func TestPutInteger(t *testing.T) {
data: []byte{0xff, 0xff, 0xff, 0xff},
},
},
{
data: make([]byte, 4),
integer: &btf.Int{
Size: 4,
Encoding: btf.Signed,
},
n: 0x0000000080000000,
expected: expected{
err: "can't represent 2147483648 as a signed integer of size 4",
},
comment: "outside of range int32 ~2147483648 ~ 2147483647",
},
{
data: make([]byte, 4),
integer: &btf.Int{
Size: 4,
Encoding: btf.Signed,
},
n: 0xffffffffffffffff - 0x0000000080000000,
expected: expected{
err: "can't represent -2147483649 as a signed integer of size 4",
},
comment: "outside of range int32 ~2147483648 ~ 2147483647",
},
{
data: make([]byte, 8),
integer: &btf.Int{
Expand Down

0 comments on commit 82fa5ac

Please sign in to comment.