diff --git a/api/transport/stun.cc b/api/transport/stun.cc index efbec13823..7ef6852260 100644 --- a/api/transport/stun.cc +++ b/api/transport/stun.cc @@ -1132,13 +1132,13 @@ StunAttributeValueType StunByteStringAttribute::value_type() const { } void StunByteStringAttribute::CopyBytes(absl::string_view bytes) { - char* new_bytes = new char[bytes.size()]; + uint8_t* new_bytes = new uint8_t[bytes.size()]; memcpy(new_bytes, bytes.data(), bytes.size()); SetBytes(new_bytes, bytes.size()); } void StunByteStringAttribute::CopyBytes(const void* bytes, size_t length) { - char* new_bytes = new char[length]; + uint8_t* new_bytes = new uint8_t[length]; memcpy(new_bytes, bytes, length); SetBytes(new_bytes, length); } @@ -1146,7 +1146,7 @@ void StunByteStringAttribute::CopyBytes(const void* bytes, size_t length) { uint8_t StunByteStringAttribute::GetByte(size_t index) const { RTC_DCHECK(bytes_ != NULL); RTC_DCHECK(index < length()); - return static_cast(bytes_[index]); + return bytes_[index]; } void StunByteStringAttribute::SetByte(size_t index, uint8_t value) { @@ -1156,10 +1156,8 @@ void StunByteStringAttribute::SetByte(size_t index, uint8_t value) { } bool StunByteStringAttribute::Read(ByteBufferReader* buf) { - bytes_ = new char[length()]; - RTC_CHECK(bytes_); - if (!buf->ReadBytes( - rtc::MakeArrayView(reinterpret_cast(bytes_), length()))) { + bytes_ = new uint8_t[length()]; + if (!buf->ReadBytes(rtc::ArrayView(bytes_, length()))) { return false; } @@ -1172,12 +1170,12 @@ bool StunByteStringAttribute::Write(ByteBufferWriter* buf) const { if (!LengthValid(type(), length())) { return false; } - buf->WriteBytes(bytes_, length()); + buf->WriteBytes(reinterpret_cast(bytes_), length()); WritePadding(buf); return true; } -void StunByteStringAttribute::SetBytes(char* bytes, size_t length) { +void StunByteStringAttribute::SetBytes(uint8_t* bytes, size_t length) { delete[] bytes_; bytes_ = bytes; SetLength(static_cast(length)); diff --git a/api/transport/stun.h b/api/transport/stun.h index 4a04db33cf..62d98f71e0 100644 --- a/api/transport/stun.h +++ b/api/transport/stun.h @@ -519,13 +519,22 @@ class StunByteStringAttribute : public StunAttribute { StunAttributeValueType value_type() const override; - const char* bytes() const { return bytes_; } + [[deprecated("Use array_view")]] const char* bytes() const { + return reinterpret_cast(bytes_); + } + // Returns the attribute value as a string. + // Use this for attributes that are text or text-compatible. absl::string_view string_view() const { - return absl::string_view(bytes_, length()); + return absl::string_view(reinterpret_cast(bytes_), length()); + } + // Returns the attribute value as an uint8_t view. + // Use this function for values that are not text. + rtc::ArrayView array_view() const { + return rtc::MakeArrayView(bytes_, length()); } [[deprecated]] std::string GetString() const { - return std::string(bytes_, length()); + return std::string(reinterpret_cast(bytes_), length()); } void CopyBytes(const void* bytes, size_t length); @@ -538,9 +547,9 @@ class StunByteStringAttribute : public StunAttribute { bool Write(rtc::ByteBufferWriter* buf) const override; private: - void SetBytes(char* bytes, size_t length); + void SetBytes(uint8_t* bytes, size_t length); - char* bytes_; + uint8_t* bytes_; }; // Implements STUN attributes that record an error code. diff --git a/api/transport/stun_unittest.cc b/api/transport/stun_unittest.cc index 65891f3371..9ad4da9a4c 100644 --- a/api/transport/stun_unittest.cc +++ b/api/transport/stun_unittest.cc @@ -1232,8 +1232,8 @@ TEST_F(StunTest, AddMessageIntegrity) { const StunByteStringAttribute* mi_attr = msg.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); EXPECT_EQ(20U, mi_attr->length()); - EXPECT_EQ( - 0, memcmp(mi_attr->bytes(), kCalculatedHmac1, sizeof(kCalculatedHmac1))); + EXPECT_EQ(0, memcmp(mi_attr->array_view().data(), kCalculatedHmac1, + sizeof(kCalculatedHmac1))); rtc::ByteBufferWriter buf1; EXPECT_TRUE(msg.Write(&buf1)); @@ -1248,8 +1248,8 @@ TEST_F(StunTest, AddMessageIntegrity) { const StunByteStringAttribute* mi_attr2 = msg2.GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); EXPECT_EQ(20U, mi_attr2->length()); - EXPECT_EQ( - 0, memcmp(mi_attr2->bytes(), kCalculatedHmac2, sizeof(kCalculatedHmac2))); + EXPECT_EQ(0, memcmp(mi_attr2->array_view().data(), kCalculatedHmac2, + sizeof(kCalculatedHmac2))); rtc::ByteBufferWriter buf3; EXPECT_TRUE(msg2.Write(&buf3)); @@ -1321,7 +1321,7 @@ TEST_F(StunTest, AddMessageIntegrity32) { const StunByteStringAttribute* mi_attr = msg.GetByteString(STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32); EXPECT_EQ(4U, mi_attr->length()); - EXPECT_EQ(0, memcmp(mi_attr->bytes(), kCalculatedHmac1_32, + EXPECT_EQ(0, memcmp(mi_attr->array_view().data(), kCalculatedHmac1_32, sizeof(kCalculatedHmac1_32))); rtc::ByteBufferWriter buf1; @@ -1337,7 +1337,7 @@ TEST_F(StunTest, AddMessageIntegrity32) { const StunByteStringAttribute* mi_attr2 = msg2.GetByteString(STUN_ATTR_GOOG_MESSAGE_INTEGRITY_32); EXPECT_EQ(4U, mi_attr2->length()); - EXPECT_EQ(0, memcmp(mi_attr2->bytes(), kCalculatedHmac2_32, + EXPECT_EQ(0, memcmp(mi_attr2->array_view().data(), kCalculatedHmac2_32, sizeof(kCalculatedHmac2_32))); rtc::ByteBufferWriter buf3; @@ -1502,7 +1502,7 @@ TEST_F(StunTest, ReadRelayMessage) { bytes = msg.GetByteString(STUN_ATTR_MAGIC_COOKIE); ASSERT_TRUE(bytes != NULL); EXPECT_EQ(4U, bytes->length()); - EXPECT_EQ(0, memcmp(bytes->bytes(), TURN_MAGIC_COOKIE_VALUE, + EXPECT_EQ(0, memcmp(bytes->array_view().data(), TURN_MAGIC_COOKIE_VALUE, sizeof(TURN_MAGIC_COOKIE_VALUE))); bytes2 = StunAttribute::CreateByteString(STUN_ATTR_MAGIC_COOKIE); @@ -1586,8 +1586,9 @@ TEST_F(StunTest, RemoveAttribute) { auto attr = msg.RemoveAttribute(STUN_ATTR_USERNAME); ASSERT_NE(attr, nullptr); EXPECT_EQ(attr->type(), STUN_ATTR_USERNAME); - EXPECT_STREQ("kes", - static_cast(attr.get())->bytes()); + EXPECT_STREQ("kes", static_cast(attr.get()) + ->string_view() + .data()); EXPECT_LT(msg.length(), len); } @@ -1609,8 +1610,9 @@ TEST_F(StunTest, RemoveAttribute) { auto attr = msg.RemoveAttribute(STUN_ATTR_USERNAME); ASSERT_NE(attr, nullptr); EXPECT_EQ(attr->type(), STUN_ATTR_USERNAME); - EXPECT_STREQ("kenta", - static_cast(attr.get())->bytes()); + EXPECT_STREQ("kenta", static_cast(attr.get()) + ->string_view() + .data()); } // Remove should remove the last added occurrence. @@ -1618,8 +1620,9 @@ TEST_F(StunTest, RemoveAttribute) { auto attr = msg.RemoveAttribute(STUN_ATTR_USERNAME); ASSERT_NE(attr, nullptr); EXPECT_EQ(attr->type(), STUN_ATTR_USERNAME); - EXPECT_STREQ("kes", - static_cast(attr.get())->bytes()); + EXPECT_STREQ("kes", static_cast(attr.get()) + ->string_view() + .data()); } // Removing something that does exist should return nullptr. @@ -1652,8 +1655,9 @@ TEST_F(StunTest, CopyAttribute) { auto copy = CopyStunAttribute(*attr.get(), buffer_ptr); ASSERT_EQ(copy->value_type(), STUN_VALUE_BYTE_STRING); - EXPECT_STREQ("kes", - static_cast(copy.get())->bytes()); + EXPECT_STREQ("kes", static_cast(copy.get()) + ->string_view() + .data()); } { // Test StunAddressAttribute. diff --git a/p2p/base/stun_dictionary.cc b/p2p/base/stun_dictionary.cc index 4a54b65b2e..318bed0ad1 100644 --- a/p2p/base/stun_dictionary.cc +++ b/p2p/base/stun_dictionary.cc @@ -80,8 +80,7 @@ const StunAttribute* StunDictionaryView::GetOrNull( webrtc::RTCErrorOr< std::pair>>> StunDictionaryView::ParseDelta(const StunByteStringAttribute& delta) { - rtc::ByteBufferReader buf(rtc::MakeArrayView( - reinterpret_cast(delta.bytes()), delta.length())); + rtc::ByteBufferReader buf(delta.array_view()); uint16_t magic; if (!buf.ReadUInt16(&magic)) { return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, diff --git a/p2p/base/turn_port.cc b/p2p/base/turn_port.cc index a1762729b2..e6f5e77114 100644 --- a/p2p/base/turn_port.cc +++ b/p2p/base/turn_port.cc @@ -1027,9 +1027,10 @@ void TurnPort::HandleDataIndication(const char* data, "peer address, addr: " << ext_addr.ToSensitiveString(); } - - DispatchPacket(data_attr->bytes(), data_attr->length(), ext_addr, PROTO_UDP, - packet_time_us); + // TODO(bugs.webrtc.org/14870): rebuild DispatchPacket to take an + // ArrayView + DispatchPacket(reinterpret_cast(data_attr->array_view().data()), + data_attr->length(), ext_addr, PROTO_UDP, packet_time_us); } void TurnPort::HandleChannelData(int channel_id, diff --git a/p2p/base/turn_port_unittest.cc b/p2p/base/turn_port_unittest.cc index 93c4dff56f..e626947d88 100644 --- a/p2p/base/turn_port_unittest.cc +++ b/p2p/base/turn_port_unittest.cc @@ -1727,8 +1727,7 @@ class MessageObserver : public StunMessageObserver { const StunByteStringAttribute* attr = msg->GetByteString(TestTurnCustomizer::STUN_ATTR_COUNTER); if (attr != nullptr && attr_counter_ != nullptr) { - rtc::ByteBufferReader buf(rtc::MakeArrayView( - reinterpret_cast(attr->bytes()), attr->length())); + rtc::ByteBufferReader buf(attr->array_view()); unsigned int val = ~0u; buf.ReadUInt32(&val); (*attr_counter_)++; diff --git a/p2p/base/turn_server.cc b/p2p/base/turn_server.cc index e2e1a7d3ba..b0c895e782 100644 --- a/p2p/base/turn_server.cc +++ b/p2p/base/turn_server.cc @@ -670,8 +670,8 @@ void TurnServerAllocation::HandleSendIndication(const TurnMessage* msg) { // If a permission exists, send the data on to the peer. if (HasPermission(peer_attr->GetAddress().ipaddr())) { - SendExternal(data_attr->bytes(), data_attr->length(), - peer_attr->GetAddress()); + SendExternal(reinterpret_cast(data_attr->array_view().data()), + data_attr->length(), peer_attr->GetAddress()); } else { RTC_LOG(LS_WARNING) << ToString() << ": Received send indication without permission"