From 2f2d41333b3089199005e6c4d10cbfbbd6fa219d Mon Sep 17 00:00:00 2001 From: Jorge Cisneros Date: Wed, 30 Sep 2026 22:16:57 -0500 Subject: [PATCH] Widen loop counters narrower than their bounds CodeQL cpp/comparison-with-wider-type flagged ten loops whose counter is narrower than the expression it is compared against. If the bound ever exceeds the counter's range the counter wraps and the loop never ends. Most bounds here are small in practice, but sendPronto() is reachable with a sequence ending at 65535, where i += 2 wraps a uint16_t to 0. Each counter now has the bound's width, or the comparison is rewritten so both sides are the same type (i + 1 < length rather than i < length - 1, which keeps the zero-length case false). No behaviour change for valid input; host tests unchanged. --- src/IRutils.cpp | 2 +- src/ir_Daikin.cpp | 2 +- src/ir_Kelvinator.cpp | 4 ++-- src/ir_Mitsubishi.cpp | 2 +- src/ir_Panasonic.cpp | 6 +++--- src/ir_Pronto.cpp | 6 ++++-- src/ir_Samsung.cpp | 4 ++-- 7 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/IRutils.cpp b/src/IRutils.cpp index f2cc030..b3020e7 100644 --- a/src/IRutils.cpp +++ b/src/IRutils.cpp @@ -144,7 +144,7 @@ String typeToString(const decode_type_t protocol, const bool isRepeat) { result = kUnknownStr; } else { auto *ptr = reinterpret_cast(kAllProtocolNamesStr); - for (uint16_t i = 0; i <= protocol && STRLEN(ptr); i++) { + for (int i = 0; i <= protocol && STRLEN(ptr); i++) { if (i == protocol) { result = FPSTR(ptr); break; diff --git a/src/ir_Daikin.cpp b/src/ir_Daikin.cpp index f4456f0..88dc52e 100644 --- a/src/ir_Daikin.cpp +++ b/src/ir_Daikin.cpp @@ -200,7 +200,7 @@ void IRDaikinESP::setRaw(const uint8_t new_code[], const uint16_t length) { offset = kDaikinStateLength - kDaikinStateLengthShort; stateReset(); } - for (uint8_t i = 0; i < length && i < kDaikinStateLength; i++) + for (uint16_t i = 0; i < length && i < kDaikinStateLength; i++) _.raw[i + offset] = new_code[i]; } diff --git a/src/ir_Kelvinator.cpp b/src/ir_Kelvinator.cpp index c44f7c6..19d95fa 100644 --- a/src/ir_Kelvinator.cpp +++ b/src/ir_Kelvinator.cpp @@ -164,10 +164,10 @@ uint8_t IRKelvinatorAC::calcBlockChecksum(const uint8_t *block, const uint16_t length) { uint8_t sum = kKelvinatorChecksumStart; // Sum the lower half of the first 4 bytes of this block. - for (uint8_t i = 0; i < 4 && i < length - 1; i++, block++) + for (uint16_t i = 0; i < 4 && i + 1 < length; i++, block++) sum += (*block & 0b1111); // then sum the upper half of the next 3 bytes. - for (uint8_t i = 4; i < length - 1; i++, block++) sum += (*block >> 4); + for (uint16_t i = 4; i + 1 < length; i++, block++) sum += (*block >> 4); // Trim it down to fit into the 4 bits allowed. i.e. Mod 16. return sum & 0b1111; } diff --git a/src/ir_Mitsubishi.cpp b/src/ir_Mitsubishi.cpp index 286072b..5064097 100644 --- a/src/ir_Mitsubishi.cpp +++ b/src/ir_Mitsubishi.cpp @@ -976,7 +976,7 @@ void IRMitsubishi136::checksum(void) { bool IRMitsubishi136::validChecksum(const uint8_t *data, const uint16_t len) { if (len < kMitsubishi136StateLength) return false; const uint16_t half = (len - kMitsubishi136PowerByte) / 2; - for (uint8_t i = 0; i < half; i++) { + for (uint16_t i = 0; i < half; i++) { // This variable is needed to avoid the warning: (known compiler issue) // warning: comparison of promoted ~unsigned with unsigned [-Wsign-compare] const uint8_t inverted = ~data[kMitsubishi136PowerByte + half + i]; diff --git a/src/ir_Panasonic.cpp b/src/ir_Panasonic.cpp index 3724157..2cc0666 100644 --- a/src/ir_Panasonic.cpp +++ b/src/ir_Panasonic.cpp @@ -1025,9 +1025,9 @@ bool IRrecv::decodePanasonicAC32(decode_results *results, uint16_t offset, uint32_t prev_section_data; // Match all the expected data blocks. - for (uint16_t block = 0; - block < sections * blocks_per_section; - block++) { + const uint32_t total_blocks = + static_cast(sections) * blocks_per_section; + for (uint32_t block = 0; block < total_blocks; block++) { prev_section_data = section_data; uint16_t used = matchGeneric(results->rawbuf + offset, §ion_data, results->rawlen - offset, bits_per_block * 2, diff --git a/src/ir_Pronto.cpp b/src/ir_Pronto.cpp index dcd513d..9b64745 100644 --- a/src/ir_Pronto.cpp +++ b/src/ir_Pronto.cpp @@ -81,7 +81,8 @@ void IRsend::sendPronto(uint16_t data[], uint16_t len, uint16_t repeat) { // Check we have enough data to send the complete first sequence. if (seq_1_len + seq_1_start > len) return; // Send the contents of the 1st sequence. - for (uint16_t i = seq_1_start; i < seq_1_start + seq_1_len; i += 2) { + const uint32_t seq_1_end = static_cast(seq_1_start) + seq_1_len; + for (uint32_t i = seq_1_start; i < seq_1_end; i += 2) { mark((data[i] * periodic_time_x10) / 10); space((data[i + 1] * periodic_time_x10) / 10); } @@ -98,8 +99,9 @@ void IRsend::sendPronto(uint16_t data[], uint16_t len, uint16_t repeat) { if (seq_2_len + seq_2_start > len) return; // Send the contents of the 2nd sequence. + const uint32_t seq_2_end = static_cast(seq_2_start) + seq_2_len; for (uint16_t r = 0; r < repeat; r++) - for (uint16_t i = seq_2_start; i < seq_2_start + seq_2_len; i += 2) { + for (uint32_t i = seq_2_start; i < seq_2_end; i += 2) { mark((data[i] * periodic_time_x10) / 10); space((data[i + 1] * periodic_time_x10) / 10); } diff --git a/src/ir_Samsung.cpp b/src/ir_Samsung.cpp index fc5cbfc..cace2dd 100644 --- a/src/ir_Samsung.cpp +++ b/src/ir_Samsung.cpp @@ -459,7 +459,7 @@ void IRSamsungAc::setRaw(const uint8_t new_code[], const uint16_t length) { _Sleep = _.Sleep5 && _.Sleep12; _OnTimer = _getOnTimer(); _OffTimer = _getOffTimer(); - for (uint8_t i = kSamsungAcStateLength; i < length; i++) + for (uint16_t i = kSamsungAcStateLength; i < length; i++) _.raw[i - kSamsungAcSectionLength] = _.raw[i]; } } @@ -962,7 +962,7 @@ bool IRrecv::decodeSamsungAC(decode_results *results, uint16_t offset, if (!matchMark(results->rawbuf[offset++], kSamsungAcBitMark)) return false; if (!matchSpace(results->rawbuf[offset++], kSamsungAcHdrSpace)) return false; // Section(s) - for (uint16_t pos = 0; pos <= (nbits / 8) - kSamsungAcSectionLength; + for (uint16_t pos = 0; pos + kSamsungAcSectionLength <= nbits / 8; pos += kSamsungAcSectionLength) { uint16_t used; // Section Header + Section Data (7 bytes) + Section Footer