Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #36 Command buffer overflow with SHT2x #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

vcolella
Copy link

@vcolella vcolella commented Oct 16, 2024

This fixes #36. Problem was a buffer overflow when splitting the T and RH commands for SHT2x.

@vcolella vcolella changed the title Fixes issue #34 Fixes Sensirion/arduino-sht#34 Oct 16, 2024
@vcolella vcolella changed the title Fixes Sensirion/arduino-sht#34 Fixes #34 Oct 16, 2024
@vcolella vcolella changed the title Fixes #34 fixes #34 Oct 16, 2024
@vcolella vcolella changed the title fixes #34 Fixes #34 Command buffer overflow with SHT2x Oct 16, 2024
@vcolella vcolella changed the title Fixes #34 Command buffer overflow with SHT2x Fixes #36 Command buffer overflow with SHT2x Oct 16, 2024
@hmueller01
Copy link

Ok, this works on ESP8266 as there is a 4 byte alignment and if cmd is 1,2,3 or 4 does not overwrite other used memory.
Thanks for the finding. But I would implement this in another way ...

@hmueller01
Copy link

+    // read T from SHT2x Sensor
     // Upper byte is T for SHT2x Sensors
     cmd[0] = mI2cCommand >> 8;
-    // Lower byte is RH for SHT2x Sensors
-    cmd[1] = mI2cCommand & 0xff;
-
-    // read T from SHT2x Sensor
     if (!readFromI2c(mWire, mI2cAddress, cmd, mCmd_Size, data,
                      EXPECTED_DATA_SIZE / 2, mDuration)) {
       DEBUG_SHT("SHT2x readFromI2c(T) false\n");
       return false;
+
     }
+
     // read RH from SHT2x Sensor
-    if (!readFromI2c(mWire, mI2cAddress, &cmd[1], mCmd_Size, &data[3],
+    // Lower byte is RH for SHT2x Sensors
+    cmd[0] = mI2cCommand & 0xff;
+    if (!readFromI2c(mWire, mI2cAddress, cmd, mCmd_Size, &data[3],
                      EXPECTED_DATA_SIZE / 2, mDuration)) {

@vcolella
Copy link
Author

vcolella commented Oct 17, 2024

Ok, this works on ESP8266 as there is a 4 byte alignment and if cmd is 1,2,3 or 4 does not overwrite other used memory.

Ah, now it makes sense why it failed for me, I was testing with an Atmega328p.

Regarding you implementation, I guess it does make more sense to just reuse cmd[0]. I'll update the PR.

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.

SHT20 sensor reading fails
2 participants