diff --git a/esphome/components/api/proto.cpp b/esphome/components/api/proto.cpp index 777fb35880..2a0ddf91db 100644 --- a/esphome/components/api/proto.cpp +++ b/esphome/components/api/proto.cpp @@ -48,15 +48,13 @@ uint32_t ProtoDecodableMessage::count_repeated_field(const uint8_t *buffer, size } uint32_t field_length = res->as_uint32(); ptr += consumed; - // Use subtraction to avoid integer overflow on 32-bit systems - if (field_length > end - ptr) { + if (field_length > static_cast(end - ptr)) { return count; // Out of bounds } ptr += field_length; break; } case WIRE_TYPE_FIXED32: { // 32-bit - skip 4 bytes - // Use subtraction to avoid integer overflow on 32-bit systems if (end - ptr < 4) { return count; } @@ -112,8 +110,7 @@ void ProtoDecodableMessage::decode(const uint8_t *buffer, size_t length) { } uint32_t field_length = res->as_uint32(); ptr += consumed; - // Use subtraction to avoid integer overflow on 32-bit systems - if (field_length > end - ptr) { + if (field_length > static_cast(end - ptr)) { ESP_LOGV(TAG, "Out-of-bounds Length Delimited at offset %ld", (long) (ptr - buffer)); return; } @@ -124,7 +121,6 @@ void ProtoDecodableMessage::decode(const uint8_t *buffer, size_t length) { break; } case WIRE_TYPE_FIXED32: { // 32-bit - // Use subtraction to avoid integer overflow on 32-bit systems if (end - ptr < 4) { ESP_LOGV(TAG, "Out-of-bounds Fixed32-bit at offset %ld", (long) (ptr - buffer)); return;