Skip to content

Commit

Permalink
[Bug] Fix crash when IRac::sendAc(state_t, *state_t) called with SAMS…
Browse files Browse the repository at this point in the history
…UNG_AC & NULL (#1341)

* Ensure protocols that use the`prev` pointer's power state are okay when it's NULL.

Fixes #1339
  • Loading branch information
crankyoldgit authored Nov 25, 2020
1 parent 34c283d commit 71f42c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/IRac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,11 @@ bool IRac::sendAc(const stdAc::state_t desired, const stdAc::state_t *prev) {
desired.celsius ? desired.degrees : fahrenheitToCelsius(desired.degrees);
// special `state_t` that is required to be sent based on that.
stdAc::state_t send = this->handleToggles(this->cleanState(desired), prev);
// Some protocols expect a previous state for power.
// Construct a pointer-safe previous power state incase prev is NULL/NULLPTR.
#if (SEND_HITACHI_AC1 || SEND_SAMSUNG_AC || SEND_SHARP_AC)
const bool prev_power = (prev != NULL) ? prev->power : !send.power;
#endif
// Per vendor settings & setup.
switch (send.protocol) {
#if SEND_AIRWELL
Expand Down Expand Up @@ -2534,7 +2539,7 @@ bool IRac::sendAc(const stdAc::state_t desired, const stdAc::state_t *prev) {
IRSamsungAc ac(_pin, _inverted, _modulation);
samsung(&ac, send.power, send.mode, degC, send.fanspeed, send.swingv,
send.quiet, send.turbo, send.light, send.filter, send.clean,
send.beep, prev->power);
send.beep, prev_power);
break;
}
#endif // SEND_SAMSUNG_AC
Expand All @@ -2551,8 +2556,6 @@ bool IRac::sendAc(const stdAc::state_t desired, const stdAc::state_t *prev) {
case SHARP_AC:
{
IRSharpAc ac(_pin, _inverted, _modulation);
bool prev_power = !send.power;
if (prev != NULL) prev_power = prev->power;
sharp(&ac, (sharp_ac_remote_model_t)send.model, send.power, prev_power,
send.mode, degC, send.fanspeed, send.swingv, send.turbo, send.light,
send.filter, send.clean);
Expand Down
16 changes: 16 additions & 0 deletions test/IRac_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2296,3 +2296,19 @@ TEST(TestIRac, Issue1250) {
// Confirm nothing in the state changed with the send.
ASSERT_FALSE(IRac::cmpStates(irac.next, copy_of_next_pre_send));
}

// Ensure Protocols that expect the IRac::sendAC() call to have a prev value set
// still works when it is NULL. i.e. It doesn't crash.
// Ref: https://github.com/crankyoldgit/IRremoteESP8266/issues/1339
TEST(TestIRac, Issue1339) {
IRac irac(kGpioUnused);
stdAc::state_t to_send;
IRac::initState(&to_send);

to_send.protocol = decode_type_t::SAMSUNG_AC;
ASSERT_TRUE(irac.sendAc(to_send, NULL));
to_send.protocol = decode_type_t::SHARP_AC;
ASSERT_TRUE(irac.sendAc(to_send, NULL));
to_send.protocol = decode_type_t::HITACHI_AC1;
ASSERT_TRUE(irac.sendAc(to_send, NULL));
}

0 comments on commit 71f42c6

Please sign in to comment.