From 62de95b39f9c6dfc66f71b6a3df2fa766726f85b Mon Sep 17 00:00:00 2001 From: "Joshua C. Colp" Date: Wed, 8 Nov 2023 08:49:53 -0400 Subject: [PATCH] reader: tolerate strings without NULL terminator (#84) 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. --- reader.go | 14 +++++++------- reader_files_test.go | 11 +++++++++++ testdata/misc/unterminated-strings.fit | Bin 0 -> 3894 bytes testdata/misc/unterminated-strings.fit.golden.gz | Bin 0 -> 7893 bytes 4 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 testdata/misc/unterminated-strings.fit create mode 100644 testdata/misc/unterminated-strings.fit.golden.gz diff --git a/reader.go b/reader.go index b5b3d6f..29df08c 100644 --- a/reader.go +++ b/reader.go @@ -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( diff --git a/reader_files_test.go b/reader_files_test.go index 4c7d2ee..8318496 100644 --- a/reader_files_test.go +++ b/reader_files_test.go @@ -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", + }, } diff --git a/testdata/misc/unterminated-strings.fit b/testdata/misc/unterminated-strings.fit new file mode 100644 index 0000000000000000000000000000000000000000..12ac52267bf95fe6ed1a50df3bcf1d3d8aaaeaba GIT binary patch literal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9NosT!K3Y4bL%^aT%cW*9(wnlCHW-;JcNo{iKdp@tC1L zc*9o*I4Rms>uB1~>YBrHeK+wV#F+2p8s-EUzAN=@9JdW;NBkU&JIGs~KM zO6@SfiFN~)Z<8(Jmtb$6Eg0BBa;YlS+s^_47I254-0t43u-z`nOPW%N_een&Xed#& zR7*QU_pK&B5H z;&(vSG@|!HIUqA*hWI^@FAJjC@FPHuj~n6|knMh=>)}}-C#DSX2Ot*%M28{^Ku#Vr z#H&F5fqNhxc@)Sa4;kV$AkT(~-gigMjvC^1AfLonMj?!%NweLCcmv3H8eutvw=l)G z8RAVKH=2k#FcOGrh_`_3!L9PS1{Wbi{1J$ayX9@n2M&-w0hz%KbFqT_8OX^G5#4~7 z9_Koc;YUjlyt`3Np6tweUYvTS-aCGgi$KM{JG zsSQ0Gr&2BIYSFJ2KdKgc)nbr4A?}2^)6AWydtgxr@jQqy-pF_p;|Alcj8GqUG3sWl zhp_?1FEHksci#}7fvb?4xGTRUPbhRoPwT}UPQiJQv&^-4C?(JrUUaJ@F`!K_A%=Vy9YsKrIeNi-%fepolW!M%luhHty`?&RyKO zn>+WqpOXk$WB5MAr(Y@VX2$y&`xs*;<6n_)JdP^ZeA~c3xZs9T_wNTMh%OjJAMiT3 zW|0_{36CE3JU&agPcD~St0$WG%bnZl)-l}W*jD-Ni#jN|TKd?f_0%8|$_I E2kzhBQ2+n{ literal 0 HcmV?d00001 diff --git a/testdata/misc/unterminated-strings.fit.golden.gz b/testdata/misc/unterminated-strings.fit.golden.gz new file mode 100644 index 0000000000000000000000000000000000000000..496130fbde9ef034713ff5d15ecccb89e8d4e5e0 GIT binary patch literal 7893 zcmb`MWmFtlw6>ApBoI6yK+qt;f;2$`p$RSt?hxFqX&e&VU4y$jK^h3|ZjE%~5P~%B z_A!~v%w6BVyVk8=RcG&W_IqmWRrS18RH2wizmNOfG)vnBzO#d+(E@yIL7;P04Q+pd z@wrY4gSF4>@QgXmkUstx!3JhDP$Vvu555P!_niuFQfyTY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