Add -Wtype-limits to QUIC
This removes comparisons that the compiler believes are guaranteed to always have the same outcome. To ensure the compile-time property is always there I've added static_asserts().
gfe-relnote: n/a, compile-only
PiperOrigin-RevId: 253692720
Change-Id: I17602bcd9e20abb19f7e84109a67a40b559bbbf6
diff --git a/quic/core/quic_framer.cc b/quic/core/quic_framer.cc
index 1aec0bf..6446ef8 100644
--- a/quic/core/quic_framer.cc
+++ b/quic/core/quic_framer.cc
@@ -4937,8 +4937,12 @@
return false;
}
if (!no_stream_frame_length) {
- if ((frame.data_length > std::numeric_limits<uint16_t>::max()) ||
- !writer->WriteUInt16(static_cast<uint16_t>(frame.data_length))) {
+ static_assert(
+ std::numeric_limits<typeof(frame.data_length)>::max() <=
+ std::numeric_limits<uint16_t>::max(),
+ "If frame.data_length can hold more than a uint16_t than we need to "
+ "check that frame.data_length <= std::numeric_limits<uint16_t>::max()");
+ if (!writer->WriteUInt16(static_cast<uint16_t>(frame.data_length))) {
QUIC_BUG << "Writing stream frame length failed";
return false;
}
diff --git a/quic/platform/api/quic_socket_address.cc b/quic/platform/api/quic_socket_address.cc
index eefad84..089c91c 100644
--- a/quic/platform/api/quic_socket_address.cc
+++ b/quic/platform/api/quic_socket_address.cc
@@ -38,12 +38,14 @@
QuicSocketAddress::QuicSocketAddress(const sockaddr* saddr, socklen_t len) {
sockaddr_storage storage;
- if (len < 0 ||
+ static_assert(std::numeric_limits<socklen_t>::max() >= sizeof(storage),
+ "Cannot cast sizeof(storage) to socklen_t as it does not fit");
+ if (len < static_cast<socklen_t>(sizeof(sockaddr)) ||
(saddr->sa_family == AF_INET &&
- static_cast<size_t>(len) < sizeof(sockaddr_in)) ||
+ len < static_cast<socklen_t>(sizeof(sockaddr_in))) ||
(saddr->sa_family == AF_INET6 &&
- static_cast<size_t>(len) < sizeof(sockaddr_in6)) ||
- static_cast<size_t>(len) > sizeof(storage)) {
+ len < static_cast<socklen_t>(sizeof(sockaddr_in6))) ||
+ len > static_cast<socklen_t>(sizeof(storage))) {
QUIC_BUG << "Socket address of invalid length provided";
return;
}