[api] Remove as_uint16/as_uint32 accessors from ProtoVarIntResult, use .value directly

Address code review feedback:
- Remove as_uint16() and as_uint32() accessors from ProtoVarIntResult
- Use .value directly with static_cast where narrowing is needed
- Fix ESP_LOGV truncation: use PRIu64 with static_cast<uint64_t> for BLE builds

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
J. Nick Koston
2026-03-08 19:52:03 -10:00
co-authored by Claude Opus 4.6
parent fab59c86a7
commit ce70c955c4
3 changed files with 12 additions and 14 deletions
@@ -136,13 +136,12 @@ APIError APIPlaintextFrameHelper::try_read_frame_() {
continue;
}
if (msg_size_varint.as_uint32() > MAX_MESSAGE_SIZE) {
if (msg_size_varint.value > MAX_MESSAGE_SIZE) {
state_ = State::FAILED;
HELPER_LOG("Bad packet: message size %" PRIu32 " exceeds maximum %u", msg_size_varint.as_uint32(),
MAX_MESSAGE_SIZE);
HELPER_LOG("Bad packet: message size %" PRIu32 " exceeds maximum %u", msg_size_varint.value, MAX_MESSAGE_SIZE);
return APIError::BAD_DATA_PACKET;
}
rx_header_parsed_len_ = msg_size_varint.as_uint16();
rx_header_parsed_len_ = static_cast<uint16_t>(msg_size_varint.value);
// Move to next varint position
varint_pos += msg_size_varint.consumed;
@@ -152,13 +151,13 @@ APIError APIPlaintextFrameHelper::try_read_frame_() {
// not enough data there yet
continue;
}
if (msg_type_varint.as_uint32() > std::numeric_limits<uint16_t>::max()) {
if (msg_type_varint.value > std::numeric_limits<uint16_t>::max()) {
state_ = State::FAILED;
HELPER_LOG("Bad packet: message type %" PRIu32 " exceeds maximum %u", msg_type_varint.as_uint32(),
HELPER_LOG("Bad packet: message type %" PRIu32 " exceeds maximum %u", msg_type_varint.value,
std::numeric_limits<uint16_t>::max());
return APIError::BAD_DATA_PACKET;
}
rx_header_parsed_type_ = msg_type_varint.as_uint16();
rx_header_parsed_type_ = static_cast<uint16_t>(msg_type_varint.value);
rx_header_parsed_ = true;
}
// header reading done
+6 -5
View File
@@ -69,7 +69,7 @@ uint32_t ProtoDecodableMessage::count_repeated_field(const uint8_t *buffer, size
break; // Invalid data, stop counting
}
uint32_t tag = res.as_uint32();
uint32_t tag = res.value;
uint32_t field_type = tag & WIRE_TYPE_MASK;
uint32_t field_id = tag >> 3;
ptr += res.consumed;
@@ -94,7 +94,7 @@ uint32_t ProtoDecodableMessage::count_repeated_field(const uint8_t *buffer, size
if (!res.has_value()) {
return count;
}
uint32_t field_length = res.as_uint32();
uint32_t field_length = res.value;
ptr += res.consumed;
if (field_length > static_cast<size_t>(end - ptr)) {
return count; // Out of bounds
@@ -215,7 +215,7 @@ void ProtoDecodableMessage::decode(const uint8_t *buffer, size_t length) {
return;
}
uint32_t tag = res.as_uint32();
uint32_t tag = res.value;
uint32_t field_type = tag & WIRE_TYPE_MASK;
uint32_t field_id = tag >> 3;
ptr += res.consumed;
@@ -228,7 +228,8 @@ void ProtoDecodableMessage::decode(const uint8_t *buffer, size_t length) {
return;
}
if (!this->decode_varint(field_id, res.value)) {
ESP_LOGV(TAG, "Cannot decode VarInt field %" PRIu32 " with value %" PRIu32 "!", field_id, res.as_uint32());
ESP_LOGV(TAG, "Cannot decode VarInt field %" PRIu32 " with value %" PRIu64 "!", field_id,
static_cast<uint64_t>(res.value));
}
ptr += res.consumed;
break;
@@ -239,7 +240,7 @@ void ProtoDecodableMessage::decode(const uint8_t *buffer, size_t length) {
ESP_LOGV(TAG, "Invalid Length Delimited at offset %ld", (long) (ptr - buffer));
return;
}
uint32_t field_length = res.as_uint32();
uint32_t field_length = res.value;
ptr += res.consumed;
if (field_length > static_cast<size_t>(end - ptr)) {
ESP_LOGV(TAG, "Out-of-bounds Length Delimited at offset %ld", (long) (ptr - buffer));
-2
View File
@@ -115,8 +115,6 @@ struct ProtoVarIntResult {
uint32_t consumed; // PROTO_VARINT_PARSE_FAILED = parse failed
constexpr bool has_value() const { return this->consumed != PROTO_VARINT_PARSE_FAILED; }
constexpr uint16_t as_uint16() const { return this->value; }
constexpr uint32_t as_uint32() const { return this->value; }
};
/// Static varint parsing methods for the protobuf wire format.