Skip to content

Commit

Permalink
Fix uint64 bug in JSON transcoder
Browse files Browse the repository at this point in the history
This ports the bugfix from
foxglove/mcap#1159
  • Loading branch information
wkalt committed Apr 22, 2024
1 parent 6da1307 commit afe5195
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ros1msg/json_transcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ func (t *JSONTranscoder) int64(w io.Writer, r io.Reader) error {
}

func (t *JSONTranscoder) uint64(w io.Writer, r io.Reader) error {
if _, err := io.ReadFull(r, t.buf[:4]); err != nil {
if _, err := io.ReadFull(r, t.buf[:8]); err != nil {
return fmt.Errorf("failed to read uint64: %w", 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)
if _, err := w.Write(t.formattedNumber); err != nil {
return fmt.Errorf("failed to write formatted uint64: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions ros1msg/json_transcoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func TestJSONTranscoding(t *testing.T) {
err = transcoder.Transcode(buf, bytes.NewReader(c.input))
assert.Nil(t, err)
assert.Equal(t, c.expectedJSON, buf.String())
transcoder.buf = make([]byte, 8)
})
}
}
Expand Down Expand Up @@ -309,8 +310,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

0 comments on commit afe5195

Please sign in to comment.