Skip to content

Commit

Permalink
reader: tolerate strings without NULL terminator (#84)
Browse files Browse the repository at this point in the history
At least one major manufacturer is producing strings in
their FIT file without a NULL terminator.

This change makes it so that the entirety of the
contents of the data field can be used as the string,
instead of assuming the last part will be a NULL
terminator.

A testdata file has also been added which includes
strings without a NULL terminator.
  • Loading branch information
jcolp committed Nov 8, 2023
1 parent 8dc9409 commit 62de95b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
14 changes: 7 additions & 7 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,16 +740,16 @@ func (d *decoder) parseFitField(dm *defmsg, dfield fieldDef, fieldv reflect.Valu
f64 := math.Float64frombits(bits)
fieldv.SetFloat(f64)
case types.BaseString:
for j := 0; j < dsize; j++ {
var j int

for j = 0; j < dsize; j++ {
if d.tmp[j] == 0x00 {
if j > 0 {
fieldv.SetString(string(d.tmp[:j]))
}
break
}
if j == dsize-1 {
fieldv.SetString(string(d.tmp[:j]))
}
}

if j > 0 {
fieldv.SetString(string(d.tmp[:j]))
}
default:
return fmt.Errorf(
Expand Down
11 changes: 11 additions & 0 deletions reader_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,15 @@ var decodeTestFiles = [...]struct {
true,
"Contains developer data fields",
},
{
"misc",
"unterminated-strings.fit",
"https://github.com/tormoder/fit/pull/84",
false,
11593656398538188064,
true,
tdoNone,
true,
"Contains string fields without NULL terminator",
},
}
Binary file added testdata/misc/unterminated-strings.fit
Binary file not shown.
Binary file added testdata/misc/unterminated-strings.fit.golden.gz
Binary file not shown.

0 comments on commit 62de95b

Please sign in to comment.