Avoid implicit type truncations by inserting explicit casts or modifying prototypes to avoid needless up- and then down-casting.
BUG=chromium:82439
TEST=none
R=henrik.lundin@webrtc.org, mflodman@webrtc.org, pthatcher@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/40569004
Cr-Commit-Position: refs/heads/master@{#8229}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8229 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/talk/media/base/rtputils.cc b/talk/media/base/rtputils.cc
index 8c35983..aaa76c0 100644
--- a/talk/media/base/rtputils.cc
+++ b/talk/media/base/rtputils.cc
@@ -29,7 +29,7 @@
namespace cricket {
-static const int kRtpVersion = 2;
+static const uint8_t kRtpVersion = 2;
static const size_t kRtpFlagsOffset = 0;
static const size_t kRtpPayloadTypeOffset = 1;
static const size_t kRtpSeqNumOffset = 2;
@@ -62,7 +62,7 @@
return true;
}
-bool SetUint8(void* data, size_t offset, int value) {
+bool SetUint8(void* data, size_t offset, uint8_t value) {
if (!data) {
return false;
}
@@ -70,7 +70,7 @@
return true;
}
-bool SetUint16(void* data, size_t offset, int value) {
+bool SetUint16(void* data, size_t offset, uint16_t value) {
if (!data) {
return false;
}
@@ -142,16 +142,6 @@
return true;
}
-bool GetRtpVersion(const void* data, size_t len, int* version) {
- if (len == 0) {
- return false;
- }
-
- const uint8 first = static_cast<const uint8*>(data)[0];
- *version = static_cast<int>((first >> 6) & 0x3);
- return true;
-}
-
bool GetRtpHeader(const void* data, size_t len, RtpHeader* header) {
return (GetRtpPayloadType(data, len, &(header->payload_type)) &&
GetRtpSeqNum(data, len, &(header->seq_num)) &&
@@ -180,46 +170,20 @@
return true;
}
-bool SetRtpHeaderFlags(
- void* data, size_t len,
- bool padding, bool extension, int csrc_count) {
- if (csrc_count > 0x0F) {
- return false;
- }
- int flags = 0;
- flags |= (kRtpVersion << 6);
- flags |= ((padding ? 1 : 0) << 5);
- flags |= ((extension ? 1 : 0) << 4);
- flags |= csrc_count;
- return SetUint8(data, kRtpFlagsOffset, flags);
-}
-
-// Assumes marker bit is 0.
-bool SetRtpPayloadType(void* data, size_t len, int value) {
- if (value >= 0x7F) {
- return false;
- }
- return SetUint8(data, kRtpPayloadTypeOffset, value & 0x7F);
-}
-
-bool SetRtpSeqNum(void* data, size_t len, int value) {
- return SetUint16(data, kRtpSeqNumOffset, value);
-}
-
-bool SetRtpTimestamp(void* data, size_t len, uint32 value) {
- return SetUint32(data, kRtpTimestampOffset, value);
-}
-
bool SetRtpSsrc(void* data, size_t len, uint32 value) {
return SetUint32(data, kRtpSsrcOffset, value);
}
// Assumes version 2, no padding, no extensions, no csrcs.
bool SetRtpHeader(void* data, size_t len, const RtpHeader& header) {
- return (SetRtpHeaderFlags(data, len, false, false, 0) &&
- SetRtpPayloadType(data, len, header.payload_type) &&
- SetRtpSeqNum(data, len, header.seq_num) &&
- SetRtpTimestamp(data, len, header.timestamp) &&
+ if (header.payload_type >= 0x7F) {
+ return false;
+ }
+ return (SetUint8(data, kRtpFlagsOffset, kRtpVersion << 6) &&
+ SetUint8(data, kRtpPayloadTypeOffset, header.payload_type & 0x7F) &&
+ SetUint16(data, kRtpSeqNumOffset,
+ static_cast<uint16_t>(header.seq_num)) &&
+ SetUint32(data, kRtpTimestampOffset, header.timestamp) &&
SetRtpSsrc(data, len, header.ssrc));
}
@@ -227,11 +191,7 @@
if (len < kMinRtpPacketLen)
return false;
- int version = 0;
- if (!GetRtpVersion(data, len, &version))
- return false;
-
- return version == kRtpVersion;
+ return (static_cast<const uint8*>(data)[0] >> 6) == kRtpVersion;
}
} // namespace cricket
diff --git a/talk/media/base/rtputils.h b/talk/media/base/rtputils.h
index ca69ace..fa055fa 100644
--- a/talk/media/base/rtputils.h
+++ b/talk/media/base/rtputils.h
@@ -53,7 +53,6 @@
kRtcpTypePSFB = 206, // Payload-specific Feedback message payload type.
};
-bool GetRtpVersion(const void* data, size_t len, int* version);
bool GetRtpPayloadType(const void* data, size_t len, int* value);
bool GetRtpSeqNum(const void* data, size_t len, int* value);
bool GetRtpTimestamp(const void* data, size_t len, uint32* value);
@@ -63,13 +62,6 @@
bool GetRtcpSsrc(const void* data, size_t len, uint32* value);
bool GetRtpHeader(const void* data, size_t len, RtpHeader* header);
-// Assumes marker bit is 0.
-bool SetRtpHeaderFlags(
- void* data, size_t len,
- bool padding, bool extension, int csrc_count);
-bool SetRtpPayloadType(void* data, size_t len, int value);
-bool SetRtpSeqNum(void* data, size_t len, int value);
-bool SetRtpTimestamp(void* data, size_t len, uint32 value);
bool SetRtpSsrc(void* data, size_t len, uint32 value);
// Assumes version 2, no padding, no extensions, no csrcs.
bool SetRtpHeader(void* data, size_t len, const RtpHeader& header);
diff --git a/talk/media/base/rtputils_unittest.cc b/talk/media/base/rtputils_unittest.cc
index cec4f71..027f395 100644
--- a/talk/media/base/rtputils_unittest.cc
+++ b/talk/media/base/rtputils_unittest.cc
@@ -75,9 +75,7 @@
};
TEST(RtpUtilsTest, GetRtp) {
- int ver;
- EXPECT_TRUE(GetRtpVersion(kPcmuFrame, sizeof(kPcmuFrame), &ver));
- EXPECT_EQ(2, ver);
+ EXPECT_TRUE(IsRtpPacket(kPcmuFrame, sizeof(kPcmuFrame)));
int pt;
EXPECT_TRUE(GetRtpPayloadType(kPcmuFrame, sizeof(kPcmuFrame), &pt));
@@ -111,44 +109,24 @@
EXPECT_FALSE(GetRtpSsrc(kInvalidPacket, sizeof(kInvalidPacket), &ssrc));
}
-TEST(RtpUtilsTest, SetRtp) {
+TEST(RtpUtilsTest, SetRtpHeader) {
unsigned char packet[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
- EXPECT_TRUE(SetRtpHeaderFlags(packet, sizeof(packet), false, false, 0));
- EXPECT_TRUE(SetRtpPayloadType(packet, sizeof(packet), 9u));
- EXPECT_TRUE(SetRtpSeqNum(packet, sizeof(packet), 1111u));
- EXPECT_TRUE(SetRtpTimestamp(packet, sizeof(packet), 2222u));
- EXPECT_TRUE(SetRtpSsrc(packet, sizeof(packet), 3333u));
+ RtpHeader header = { 9, 1111, 2222u, 3333u };
+ EXPECT_TRUE(SetRtpHeader(packet, sizeof(packet), header));
// Bits: 10 0 0 0000
EXPECT_EQ(128u, packet[0]);
size_t len;
EXPECT_TRUE(GetRtpHeaderLen(packet, sizeof(packet), &len));
EXPECT_EQ(12U, len);
- RtpHeader header;
EXPECT_TRUE(GetRtpHeader(packet, sizeof(packet), &header));
EXPECT_EQ(9, header.payload_type);
EXPECT_EQ(1111, header.seq_num);
EXPECT_EQ(2222u, header.timestamp);
EXPECT_EQ(3333u, header.ssrc);
-
- unsigned char packet2[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
-
- EXPECT_TRUE(SetRtpHeader(packet2, sizeof(packet2), header));
-
- // Bits: 10 0 0 0000
- EXPECT_EQ(128u, packet2[0]);
- EXPECT_TRUE(GetRtpHeaderLen(packet2, sizeof(packet2), &len));
- EXPECT_EQ(12U, len);
- EXPECT_TRUE(GetRtpHeader(packet2, sizeof(packet2), &header));
- EXPECT_EQ(9, header.payload_type);
- EXPECT_EQ(1111, header.seq_num);
- EXPECT_EQ(2222u, header.timestamp);
- EXPECT_EQ(3333u, header.ssrc);
}
TEST(RtpUtilsTest, GetRtpHeaderLen) {
diff --git a/talk/session/media/channel_unittest.cc b/talk/session/media/channel_unittest.cc
index 0b8e1c1..578e2c9 100644
--- a/talk/session/media/channel_unittest.cc
+++ b/talk/session/media/channel_unittest.cc
@@ -423,7 +423,8 @@
rtc::SetBE32(const_cast<char*>(data.c_str()) + 8, ssrc);
rtc::SetBE16(const_cast<char*>(data.c_str()) + 2, sequence_number);
if (pl_type >= 0) {
- rtc::Set8(const_cast<char*>(data.c_str()), 1, pl_type);
+ rtc::Set8(const_cast<char*>(data.c_str()), 1,
+ static_cast<uint8_t>(pl_type));
}
return data;
}
diff --git a/webrtc/modules/audio_coding/main/acm2/acm_send_test.cc b/webrtc/modules/audio_coding/main/acm2/acm_send_test.cc
index d2ecb16..56830a4 100644
--- a/webrtc/modules/audio_coding/main/acm2/acm_send_test.cc
+++ b/webrtc/modules/audio_coding/main/acm2/acm_send_test.cc
@@ -61,7 +61,7 @@
Packet* AcmSendTest::NextPacket() {
assert(codec_registered_);
- if (filter_.test(payload_type_)) {
+ if (filter_.test(static_cast<size_t>(payload_type_))) {
// This payload type should be filtered out. Since the payload type is the
// same throughout the whole test run, no packet at all will be delivered.
// We can just as well signal that the test is over by returning NULL.
@@ -111,7 +111,7 @@
uint8_t* packet_memory = new uint8_t[allocated_bytes];
// Populate the header bytes.
packet_memory[0] = 0x80;
- packet_memory[1] = payload_type_;
+ packet_memory[1] = static_cast<uint8_t>(payload_type_);
packet_memory[2] = (sequence_number_ >> 8) & 0xFF;
packet_memory[3] = (sequence_number_) & 0xFF;
packet_memory[4] = (timestamp_ >> 24) & 0xFF;
diff --git a/webrtc/modules/audio_coding/main/acm2/acm_send_test_oldapi.cc b/webrtc/modules/audio_coding/main/acm2/acm_send_test_oldapi.cc
index 42dc628..6749e12 100644
--- a/webrtc/modules/audio_coding/main/acm2/acm_send_test_oldapi.cc
+++ b/webrtc/modules/audio_coding/main/acm2/acm_send_test_oldapi.cc
@@ -64,7 +64,7 @@
Packet* AcmSendTestOldApi::NextPacket() {
assert(codec_registered_);
- if (filter_.test(payload_type_)) {
+ if (filter_.test(static_cast<size_t>(payload_type_))) {
// This payload type should be filtered out. Since the payload type is the
// same throughout the whole test run, no packet at all will be delivered.
// We can just as well signal that the test is over by returning NULL.
@@ -115,7 +115,7 @@
uint8_t* packet_memory = new uint8_t[allocated_bytes];
// Populate the header bytes.
packet_memory[0] = 0x80;
- packet_memory[1] = payload_type_;
+ packet_memory[1] = static_cast<uint8_t>(payload_type_);
packet_memory[2] = (sequence_number_ >> 8) & 0xFF;
packet_memory[3] = (sequence_number_) & 0xFF;
packet_memory[4] = (timestamp_ >> 24) & 0xFF;
diff --git a/webrtc/modules/audio_coding/neteq/neteq_impl.cc b/webrtc/modules/audio_coding/neteq/neteq_impl.cc
index 537638d..a2f7a41 100644
--- a/webrtc/modules/audio_coding/neteq/neteq_impl.cc
+++ b/webrtc/modules/audio_coding/neteq/neteq_impl.cc
@@ -1129,12 +1129,12 @@
AudioDecoder* decoder = NULL;
if (!packet_list->empty()) {
const Packet* packet = packet_list->front();
- int payload_type = packet->header.payloadType;
+ uint8_t payload_type = packet->header.payloadType;
if (!decoder_database_->IsComfortNoise(payload_type)) {
decoder = decoder_database_->GetDecoder(payload_type);
assert(decoder);
if (!decoder) {
- LOG_FERR1(LS_WARNING, GetDecoder, payload_type);
+ LOG_FERR1(LS_WARNING, GetDecoder, static_cast<int>(payload_type));
PacketBuffer::DeleteAllPackets(packet_list);
return kDecoderNotFound;
}
@@ -1146,7 +1146,7 @@
->GetDecoderInfo(payload_type);
assert(decoder_info);
if (!decoder_info) {
- LOG_FERR1(LS_WARNING, GetDecoderInfo, payload_type);
+ LOG_FERR1(LS_WARNING, GetDecoderInfo, static_cast<int>(payload_type));
PacketBuffer::DeleteAllPackets(packet_list);
return kDecoderNotFound;
}
diff --git a/webrtc/modules/audio_coding/neteq/tools/neteq_rtpplay.cc b/webrtc/modules/audio_coding/neteq/tools/neteq_rtpplay.cc
index f9e379a..bc07063 100644
--- a/webrtc/modules/audio_coding/neteq/tools/neteq_rtpplay.cc
+++ b/webrtc/modules/audio_coding/neteq/tools/neteq_rtpplay.cc
@@ -187,57 +187,64 @@
void RegisterPayloadTypes(NetEq* neteq) {
assert(neteq);
int error;
- error = neteq->RegisterPayloadType(webrtc::kDecoderPCMu, FLAGS_pcmu);
+ error = neteq->RegisterPayloadType(webrtc::kDecoderPCMu,
+ static_cast<uint8_t>(FLAGS_pcmu));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_pcmu <<
" as " << CodecName(webrtc::kDecoderPCMu).c_str() << std::endl;
exit(1);
}
- error = neteq->RegisterPayloadType(webrtc::kDecoderPCMa, FLAGS_pcma);
+ error = neteq->RegisterPayloadType(webrtc::kDecoderPCMa,
+ static_cast<uint8_t>(FLAGS_pcma));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_pcma <<
" as " << CodecName(webrtc::kDecoderPCMa).c_str() << std::endl;
exit(1);
}
- error = neteq->RegisterPayloadType(webrtc::kDecoderILBC, FLAGS_ilbc);
+ error = neteq->RegisterPayloadType(webrtc::kDecoderILBC,
+ static_cast<uint8_t>(FLAGS_ilbc));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_ilbc <<
" as " << CodecName(webrtc::kDecoderILBC).c_str() << std::endl;
exit(1);
}
- error = neteq->RegisterPayloadType(webrtc::kDecoderISAC, FLAGS_isac);
+ error = neteq->RegisterPayloadType(webrtc::kDecoderISAC,
+ static_cast<uint8_t>(FLAGS_isac));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_isac <<
" as " << CodecName(webrtc::kDecoderISAC).c_str() << std::endl;
exit(1);
}
- error = neteq->RegisterPayloadType(webrtc::kDecoderISACswb, FLAGS_isac_swb);
+ error = neteq->RegisterPayloadType(webrtc::kDecoderISACswb,
+ static_cast<uint8_t>(FLAGS_isac_swb));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_isac_swb <<
" as " << CodecName(webrtc::kDecoderISACswb).c_str() << std::endl;
exit(1);
}
- error = neteq->RegisterPayloadType(webrtc::kDecoderOpus, FLAGS_opus);
+ error = neteq->RegisterPayloadType(webrtc::kDecoderOpus,
+ static_cast<uint8_t>(FLAGS_opus));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_opus << " as "
<< CodecName(webrtc::kDecoderOpus).c_str() << std::endl;
exit(1);
}
- error = neteq->RegisterPayloadType(webrtc::kDecoderPCM16B, FLAGS_pcm16b);
+ error = neteq->RegisterPayloadType(webrtc::kDecoderPCM16B,
+ static_cast<uint8_t>(FLAGS_pcm16b));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_pcm16b <<
" as " << CodecName(webrtc::kDecoderPCM16B).c_str() << std::endl;
exit(1);
}
error = neteq->RegisterPayloadType(webrtc::kDecoderPCM16Bwb,
- FLAGS_pcm16b_wb);
+ static_cast<uint8_t>(FLAGS_pcm16b_wb));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_pcm16b_wb <<
" as " << CodecName(webrtc::kDecoderPCM16Bwb).c_str() << std::endl;
exit(1);
}
error = neteq->RegisterPayloadType(webrtc::kDecoderPCM16Bswb32kHz,
- FLAGS_pcm16b_swb32);
+ static_cast<uint8_t>(FLAGS_pcm16b_swb32));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_pcm16b_swb32 <<
" as " << CodecName(webrtc::kDecoderPCM16Bswb32kHz).c_str() <<
@@ -245,52 +252,57 @@
exit(1);
}
error = neteq->RegisterPayloadType(webrtc::kDecoderPCM16Bswb48kHz,
- FLAGS_pcm16b_swb48);
+ static_cast<uint8_t>(FLAGS_pcm16b_swb48));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_pcm16b_swb48 <<
" as " << CodecName(webrtc::kDecoderPCM16Bswb48kHz).c_str() <<
std::endl;
exit(1);
}
- error = neteq->RegisterPayloadType(webrtc::kDecoderG722, FLAGS_g722);
+ error = neteq->RegisterPayloadType(webrtc::kDecoderG722,
+ static_cast<uint8_t>(FLAGS_g722));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_g722 <<
" as " << CodecName(webrtc::kDecoderG722).c_str() << std::endl;
exit(1);
}
- error = neteq->RegisterPayloadType(webrtc::kDecoderAVT, FLAGS_avt);
+ error = neteq->RegisterPayloadType(webrtc::kDecoderAVT,
+ static_cast<uint8_t>(FLAGS_avt));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_avt <<
" as " << CodecName(webrtc::kDecoderAVT).c_str() << std::endl;
exit(1);
}
- error = neteq->RegisterPayloadType(webrtc::kDecoderRED, FLAGS_red);
+ error = neteq->RegisterPayloadType(webrtc::kDecoderRED,
+ static_cast<uint8_t>(FLAGS_red));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_red <<
" as " << CodecName(webrtc::kDecoderRED).c_str() << std::endl;
exit(1);
}
- error = neteq->RegisterPayloadType(webrtc::kDecoderCNGnb, FLAGS_cn_nb);
+ error = neteq->RegisterPayloadType(webrtc::kDecoderCNGnb,
+ static_cast<uint8_t>(FLAGS_cn_nb));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_cn_nb <<
" as " << CodecName(webrtc::kDecoderCNGnb).c_str() << std::endl;
exit(1);
}
- error = neteq->RegisterPayloadType(webrtc::kDecoderCNGwb, FLAGS_cn_wb);
+ error = neteq->RegisterPayloadType(webrtc::kDecoderCNGwb,
+ static_cast<uint8_t>(FLAGS_cn_wb));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_cn_wb <<
" as " << CodecName(webrtc::kDecoderCNGwb).c_str() << std::endl;
exit(1);
}
error = neteq->RegisterPayloadType(webrtc::kDecoderCNGswb32kHz,
- FLAGS_cn_swb32);
+ static_cast<uint8_t>(FLAGS_cn_swb32));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_cn_swb32 <<
" as " << CodecName(webrtc::kDecoderCNGswb32kHz).c_str() << std::endl;
exit(1);
}
error = neteq->RegisterPayloadType(webrtc::kDecoderCNGswb48kHz,
- FLAGS_cn_swb48);
+ static_cast<uint8_t>(FLAGS_cn_swb48));
if (error) {
std::cerr << "Cannot register payload type " << FLAGS_cn_swb48 <<
" as " << CodecName(webrtc::kDecoderCNGswb48kHz).c_str() << std::endl;
@@ -442,16 +454,18 @@
// Change payload type to PCM16.
switch (CodecSampleRate(rtp_header->header.payloadType)) {
case 8000:
- rtp_header->header.payloadType = FLAGS_pcm16b;
+ rtp_header->header.payloadType = static_cast<uint8_t>(FLAGS_pcm16b);
break;
case 16000:
- rtp_header->header.payloadType = FLAGS_pcm16b_wb;
+ rtp_header->header.payloadType = static_cast<uint8_t>(FLAGS_pcm16b_wb);
break;
case 32000:
- rtp_header->header.payloadType = FLAGS_pcm16b_swb32;
+ rtp_header->header.payloadType =
+ static_cast<uint8_t>(FLAGS_pcm16b_swb32);
break;
case 48000:
- rtp_header->header.payloadType = FLAGS_pcm16b_swb48;
+ rtp_header->header.payloadType =
+ static_cast<uint8_t>(FLAGS_pcm16b_swb48);
break;
default:
std::cerr << "Payload type " <<
diff --git a/webrtc/modules/rtp_rtcp/source/producer_fec.cc b/webrtc/modules/rtp_rtcp/source/producer_fec.cc
index a571a01..4c62dad 100644
--- a/webrtc/modules/rtp_rtcp/source/producer_fec.cc
+++ b/webrtc/modules/rtp_rtcp/source/producer_fec.cc
@@ -55,7 +55,7 @@
data_[1] += red_pl_type;
// Add RED header
// f-bit always 0
- data_[header_length] = pl_type;
+ data_[header_length] = static_cast<uint8_t>(pl_type);
header_length_ = header_length + kREDForFECHeaderLength;
}
diff --git a/webrtc/modules/rtp_rtcp/source/producer_fec_unittest.cc b/webrtc/modules/rtp_rtcp/source/producer_fec_unittest.cc
index d8b67a7..f6d36d9 100644
--- a/webrtc/modules/rtp_rtcp/source/producer_fec_unittest.cc
+++ b/webrtc/modules/rtp_rtcp/source/producer_fec_unittest.cc
@@ -33,7 +33,7 @@
uint32_t parsed_timestamp = (data[4] << 24) + (data[5] << 16) +
(data[6] << 8) + data[7];
EXPECT_EQ(timestamp, parsed_timestamp);
- EXPECT_EQ(fec_pltype, data[kRtpHeaderSize]);
+ EXPECT_EQ(static_cast<uint8_t>(fec_pltype), data[kRtpHeaderSize]);
}
class ProducerFecTest : public ::testing::Test {