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

[Bug] Fix crash when IRac::sendAc(state_t, *state_t) called with SAMSUNG_AC & NULL #1341

Merged
merged 2 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}