Closed mewmew closed 6 years ago
https://github.com/lunixbochs/struc/blob/master/field.go#L102 - try changing to int64
Need a similar fix and corresponding test on unpack - https://github.com/lunixbochs/struc/blob/master/field.go#L225
Packing from int16 to int64 fails to sign extend, unpacking from int16 to int64 fails to sign extend?
case Int8: n = uint64(int8(buf[0]))
case Int16: n = uint64(int16(order.Uint16(buf)))
...etc
Thanks for the notes!
With https://github.com/mewpull/struc/commit/2b93aae5551fef7b1cb665966556c45141fb506f unpacking of 32-bit signed integers to 64-bit signed integer fields works.
P.S. I also added some pretty printing of values for failed test cases, so that it is easier to trouble shoot issues such as this.
For instance, if we do not convert the input to a singed 32-bit before converting it to the larger 64-bit signed integer,
diff --git a/field.go b/field.go
index 2c3ba29..64b059c 100644
--- a/field.go
+++ b/field.go
@@ -227,7 +227,7 @@ func (f *Field) unpackVal(buf []byte, val reflect.Value, length int, options *Op
case Int16:
n = int64(int16(order.Uint16(buf)))
case Int32:
- n = int64(int32(order.Uint32(buf)))
+ n = int64(order.Uint32(buf))
case Int64:
n = int64(order.Uint64(buf))
}
Then we get the following pretty printed test case failure.
u@x220 ~/g/s/g/l/struc> go test
[I32f2: 4294967295 != -1]
--- FAIL: TestCodec (0.00s)
struc_test.go:145: encode/decode failed
[I32f2: 4294967295 != -1]
--- FAIL: TestDecode (0.00s)
struc_test.go:168: decode failed
FAIL
exit status 1
FAIL github.com/lunixbochs/struc 0.004s
Pretty printing is implemented in https://github.com/mewpull/struc/commit/178c22d98083de3daa4e9b369ff64e709989395e
Since struc is currently a zero-dependency library, I'm kinda against adding a test-only dependency.
Since struc is currently a zero-dependency library, I'm kinda against adding a test-only dependency.
That's fair. I guess we could use the fmt
library to print expected and wanted values? Or just leave it as is, and instrument the test cases when trouble-shooting some code.
Using fmt.Printf("%#v")
or %+v
and requiring a manual diff is okay.
Using fmt.Printf("%#v") or %+v and requiring a manual diff is okay.
Done.
When converting signed 32-bit integers to 64-bit integers, the sign is lost, and negative values are converted to their unsigned counterpart.
E.g. converting -1 from int32 to int64 results in 4294967295, when the correct result should be -1.
The following test case demonstrates this:
Note, the unsigned test passes the test case successfully. However, the signed test fails.
To merge the test case, either apply this patch, or find it in the
signedconv
branch at https://github.com/mewpull/struc/tree/signedconv