Skip to content

Fix _parseCSV() array index bug: lon and ele always returned lat value#180

Closed
mikeysklar wants to merge 2 commits into
adafruit:masterfrom
mikeysklar:patch-1
Closed

Fix _parseCSV() array index bug: lon and ele always returned lat value#180
mikeysklar wants to merge 2 commits into
adafruit:masterfrom
mikeysklar:patch-1

Conversation

@mikeysklar
Copy link
Copy Markdown
Contributor

@mikeysklar mikeysklar commented Apr 12, 2026

Bug found and reported by gadgeteer on the forums.

In _parseCSV(), all three location fields hardcode fields[1], so lon and ele always return the latitude value.

Before (buggy):

_lat = atof(fields[1]);
_lon = atof(fields[1]);  // wrong — always returns lat
_ele = atof(fields[1]);  // wrong — always returns lat

After (fixed):

_lat = atof(fields[1]);
_lon = atof(fields[2]);
_ele = atof(fields[3]);

Also removes the unreachable return true; at the end of the function.

In _parseCSV(), all three location fields (lat, lon, ele) were reading from fields[1], meaning lon and ele always returned the latitude value instead of their own data. Also removes an unreachable return statement at the end of the function.
@mikeysklar
Copy link
Copy Markdown
Contributor Author

Been testing this and while the code fixes one issue it also crashes from another. So I'll be doing further testing.

@ladyada ladyada requested a review from brentru April 13, 2026 08:18
@brentru
Copy link
Copy Markdown
Member

brentru commented Apr 13, 2026

@mikeysklar To confirm, does the crash occur when the user tries to run the adafruitio_04_location.ino sketch? What board are you running this sketch on to repro. the crash?

@brentru
Copy link
Copy Markdown
Member

brentru commented Apr 13, 2026

Looks like Huzzah esp8266 feather

Been testing this and while the code fixes one issue it also crashes from another. So I'll be doing further testing.

I'll try to repro.

@ladyada
Copy link
Copy Markdown
Member

ladyada commented Apr 13, 2026

fyi, esp8266 need lots of wdt kicks with yield()!

@mikeysklar
Copy link
Copy Markdown
Contributor Author

mikeysklar commented Apr 13, 2026

I've seen the crash on two different boards Feather V2 and Qt Py ESP32-S3 using.

adafruitio_04_location.ino

I think it is a missing (but necessary) trailing comma with parse_csv that will resolve it. Testing and will update PR.

@brentru
Copy link
Copy Markdown
Member

brentru commented Apr 13, 2026

@mikeysklar If fixing it keeps causing another bug, please lmk and I can investigate.

@mikeysklar
Copy link
Copy Markdown
Contributor Author

mikeysklar commented Apr 13, 2026

End-to-end verified on Feather ESP32 V2 and QT Py ESP32-S3 (with companion PR #181)

Ran the stock examples/adafruitio_04_location sketch with this PR and #181 applied on both an Adafruit Feather ESP32 V2 and an Adafruit QT Py ESP32-S3. Consecutive round-trips through the real AIO broker, every field matching, value incrementing across cycles (no reboot loop), no crash on either board.

Feather ESP32 V2:

----- sending -----
value: 1
lat: 42.321427
lon: -83.025754
ele: 1.00
----- received -----
value: 1
lat: 42.321427
lon: -83.025754
ele: 1.00
----- sending -----
value: 2
lat: 42.311427
lon: -83.005754
ele: 2.00
----- received -----
value: 2
lat: 42.311427
lon: -83.005754
ele: 2.00
----- sending -----
value: 3
lat: 42.301427
lon: -82.985754
ele: 3.00
----- received -----
value: 3
lat: 42.301427
lon: -82.985754
ele: 3.00
----- sending -----
value: 4
lat: 42.291427
lon: -82.965754
ele: 4.00
----- received -----
value: 4
lat: 42.291427
lon: -82.965754
ele: 4.00

Note: on main the S3 was crashing in what looked like the publish path (right after the ele: 0.00 print), but it was actually the same atof(NULL) deref hit via the inbound echo on a tighter timing window — the save() call returns, the MQTT thread delivers the echoed message a few ms later, and _parseCSV() crashes before loop() gets back to its next print. The combined #180 + #181 patches fix both boards.

The two fixes

They mask each other: this PR's index bug kept readers away from fields[N-1], hiding #181's NULL. Landing this PR alone without #181 turns "wrong values" into "null-pointer deref on every receive" (LoadProhibited on ESP32). #181 should land first, then this one.

@mikeysklar
Copy link
Copy Markdown
Contributor Author

mikeysklar commented Apr 13, 2026

I can do further testing. I would appreciate if others would confirm these two pull work or if there are any other downstream concerns with these changes.

git clone -b combined-pr180-pr181 https://github.com/mikeysklar/Adafruit_IO_Arduino.git

@brentru
Copy link
Copy Markdown
Member

brentru commented Apr 14, 2026

In my queue to test this morning

@mikeysklar
Copy link
Copy Markdown
Contributor Author

Sounds good. Testing went well for myself and forum user gadgeteer yesterday.

@brentru
Copy link
Copy Markdown
Member

brentru commented Apr 14, 2026

@mikeysklar Can you please check out a PR for the combined PR branch instead of having two separate PRs open?

Plz checkout a PR for branch combined-pr180-pr181

Then tag me for review.

@brentru
Copy link
Copy Markdown
Member

brentru commented Apr 14, 2026

Works on ESP8266

@mikeysklar
Copy link
Copy Markdown
Contributor Author

Cool. Glad to hear it works. See PR #182 for combined PRs.

brentru added a commit that referenced this pull request Apr 14, 2026
Fix _parseCSV() array index bug: combined PR #180 and PR #181
@brentru
Copy link
Copy Markdown
Member

brentru commented Apr 14, 2026

Closing in favor of #182

@brentru brentru closed this Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants