Skip to content

Commit

Permalink
[go/cli] Fix uint64 bug in mcap cat (foxglove#1159)
Browse files Browse the repository at this point in the history
### Changelog

Fix: `mcap cat` reading `uint64` ros1 msgs properly

### Description

The `JSONTranscoder` for `ros1msg` was only reading 4 bytes for the `uint64` type.
  • Loading branch information
bradsquicciarini-coco authored Apr 22, 2024
1 parent b8a7389 commit 02cc054
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions go/cli/mcap/utils/ros/json_transcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,12 @@ func (t *JSONTranscoder) int64(w io.Writer, r io.Reader) error {
}

func (t *JSONTranscoder) uint64(w io.Writer, r io.Reader) error {
_, err := io.ReadFull(r, t.buf[:4])
_, err := io.ReadFull(r, t.buf[:8])
if err != nil {
return err
}
x := binary.LittleEndian.Uint64(t.buf[:8])
t.formattedNumber = strconv.AppendInt(t.formattedNumber, int64(x), 10)
t.formattedNumber = strconv.AppendUint(t.formattedNumber, x, 10)
_, err = w.Write(t.formattedNumber)
if err != nil {
return err
Expand Down
20 changes: 12 additions & 8 deletions go/cli/mcap/utils/ros/json_transcoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ func TestJSONTranscoding(t *testing.T) {
err = transcoder.Transcode(buf, bytes.NewReader(c.input))
require.NoError(t, err)
assert.Equal(t, c.expectedJSON, buf.String())
// ensure each test has a clean buffer
transcoder.buf = make([]byte, 8)
})
}
}
Expand Down Expand Up @@ -274,8 +276,8 @@ func TestSingleRecordConversion(t *testing.T) {
converter: transcoder.uint8,
},
},
[]byte{0x01},
`{"foo":1}`,
[]byte{0xff},
`{"foo":255}`,
},
{
"uint16",
Expand All @@ -286,8 +288,8 @@ func TestSingleRecordConversion(t *testing.T) {
converter: transcoder.uint16,
},
},
[]byte{0x07, 0x07},
`{"foo":1799}`,
[]byte{0xff, 0xff},
`{"foo":65535}`,
},
{
"uint32",
Expand All @@ -298,8 +300,8 @@ func TestSingleRecordConversion(t *testing.T) {
converter: transcoder.uint32,
},
},
[]byte{0x07, 0x07, 0x07, 0x07},
`{"foo":117901063}`,
[]byte{0xff, 0xff, 0xff, 0xff},
`{"foo":4294967295}`,
},
{
"uint64",
Expand All @@ -310,8 +312,8 @@ func TestSingleRecordConversion(t *testing.T) {
converter: transcoder.uint64,
},
},
[]byte{0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07},
`{"foo":506381209866536711}`,
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
`{"foo":18446744073709551615}`,
},
{
"float32",
Expand Down Expand Up @@ -530,6 +532,8 @@ func TestSingleRecordConversion(t *testing.T) {
err := converter(buf, bytes.NewBuffer(c.input))
require.NoError(t, err)
assert.Equal(t, c.output, buf.String())
// ensure each test has a clean buffer
transcoder.buf = make([]byte, 8)
})
}
}

0 comments on commit 02cc054

Please sign in to comment.