Skip to content

Commit 2f0120b

Browse files
committed
fix: vcsim: output signed byte in ByteSlice.MarshalXML
Fixes #3615 Signed-off-by: Doug MacEachern <[email protected]>
1 parent 0ded546 commit 2f0120b

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

vim25/types/byte_slice.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func (b ByteSlice) MarshalXML(e *xml.Encoder, field xml.StartElement) error {
3636
Name: field.Name,
3737
}
3838
for i := range b {
39-
if err := e.EncodeElement(b[i], start); err != nil {
39+
// Using int8() here to output a signed byte (issue #3615)
40+
if err := e.EncodeElement(int8(b[i]), start); err != nil {
4041
return err
4142
}
4243
}

vim25/types/byte_slice_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,33 @@ func TestByteSlice(t *testing.T) {
4343
t.Errorf("out=%#v", out.Byte)
4444
}
4545
}
46+
47+
func TestSignedByteSlice(t *testing.T) {
48+
// int8: <byte>4</byte><byte>-80</byte><byte>-79</byte><byte>-78</byte>
49+
// uint8: <byte>4</byte><byte>176</byte><byte>177</byte><byte>178</byte>
50+
in := &ArrayOfByte{
51+
Byte: []uint8{0x4, 0xb0, 0xb1, 0xb2},
52+
}
53+
54+
res, err := xml.Marshal(in)
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
var out struct {
60+
Byte []int8 `xml:"byte,omitempty" json:"_value"`
61+
}
62+
63+
// ByteSlice.MarshalXML must output signed byte, otherwise this fails with:
64+
// strconv.ParseInt: parsing "176": value out of range
65+
if err := xml.Unmarshal(res, &out); err != nil {
66+
t.Logf("%s", string(res))
67+
t.Fatal(err)
68+
}
69+
70+
for i := range in.Byte {
71+
if in.Byte[i] != byte(out.Byte[i]) {
72+
t.Errorf("out=%x", out.Byte[i])
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)