Fix parse_csv() dropping the final CSV field - #181
Conversation
parse_csv() advances bptr and strdups into buf on every comma, but on the terminating '\0' it only sets fEnd and breaks out of the loop. The content accumulated in tmp since the last comma was never emitted, so for an N-field CSV the function returned N-1 strings followed by NULL even though count_fields() correctly reported N. In practice this meant buf[N-1] was NULL. Callers like _parseCSV() that read the last field (e.g. atof(fields[3]) for the elevation in a value,lat,lon,ele location CSV) dereferenced NULL and crashed with LoadProhibited on ESP32. The bug was masked on main by a second bug in _parseCSV() that incorrectly read fields[1] three times instead of fields[1..3], so the last slot was never actually accessed. Fix: emit the final field from inside the '\0' case before setting fEnd. Uses the same strdup + allocation-failure cleanup as the ',' case.
End-to-end verified on Feather ESP32 V2 and QT Py ESP32-S3Ran the stock Feather ESP32 V2: Note: on The two fixes
They mask each other: #180's index bug kept #180's readers away from |
Summary
parse_csv()insrc/AdafruitIO_Data.cppsilently drops the final field of every CSV it parses — for an N-field input it only populates slots0..N-2, leavingNULLatN-1.Root cause
The
'\0'case in the parse loop breaks out without emitting the field accumulated since the last comma, unlike the','case which correctlystrdups each field.Fix
Emit the final field from inside the
'\0'case before settingfEnd, using the samestrdup+ cleanup path as the','case.Relationship to #180
#180's index fix in
_parseCSV()is correct but depends on this PR — without it, correcting the indices causesatof(NULL)→ crash on every incoming location message. Suggest landing this first, then #180.Verification
Tested on Feather ESP32 V2 with both fixes applied:
Test plan
🤖 Generated with Claude Code