Check RtcTransport payload isn't too short before checking it's an RTP packet

Bug: chromium:488803429
Change-Id: Iaf6204c1f3bce1a8786c00ecfa7faabe78bb9021
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/454280
Auto-Submit: Tony Herre <herre@google.com>
Reviewed-by: Stefan Holmer <stefan@webrtc.org>
Commit-Queue: Stefan Holmer <stefan@webrtc.org>
Reviewed-by: Philip Eliasson <philipel@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#47101}
diff --git a/pc/datagram_connection_internal.cc b/pc/datagram_connection_internal.cc
index be25bba..38c2e43 100644
--- a/pc/datagram_connection_internal.cc
+++ b/pc/datagram_connection_internal.cc
@@ -58,6 +58,7 @@
 
 const size_t kMaxRtpPacketLen = 2048;
 const size_t kIceUfragLength = 16;
+const size_t kMinPayloadLengthForRtpCheck = 2;
 
 // Helper function to create IceTransportInit
 IceTransportInit CreateIceTransportInit(const Environment& env,
@@ -291,7 +292,8 @@
     return;
   }
 
-  if (IsRtpOrRtcpPacket(packet.payload[0])) {
+  if (packet.payload.size() >= kMinPayloadLengthForRtpCheck &&
+      IsRtpOrRtcpPacket(packet.payload[0])) {
     // Copy the payload into a buffer with some extra capacity to allow space
     // for the SRTP encryption tag to be added.
     CopyOnWriteBuffer buffer(packet.payload.data(), packet.payload.size(),
diff --git a/pc/datagram_connection_unittest.cc b/pc/datagram_connection_unittest.cc
index fae8c8b..a352816 100644
--- a/pc/datagram_connection_unittest.cc
+++ b/pc/datagram_connection_unittest.cc
@@ -517,5 +517,31 @@
   EXPECT_TRUE(callbacks.send_outcome);
 }
 
+TEST_F(DatagramConnectionTest, SingleBytePacketsAreSent) {
+  CreateConnections(WireProtocol::kDtls);
+  Connect();
+  ASSERT_TRUE(
+      WaitUntil([&]() { return conn1_->Writable() && conn2_->Writable(); }));
+
+  std::vector<uint8_t> data = {1};
+  bool callback_called = false;
+  EXPECT_CALL(*observer1_ptr_, OnSendOutcome(_))
+      .WillOnce([&](const SendOutcome& outcome) {
+        EXPECT_EQ(outcome.id, 1u);
+        EXPECT_EQ(outcome.status, SendOutcome::Status::kSuccess);
+        EXPECT_NE(outcome.send_time, Timestamp::MinusInfinity());
+        callback_called = true;
+        loop_.Quit();
+      });
+  std::vector<PacketSendParameters> packets = {
+      PacketSendParameters{.id = 1, .payload = data}};
+  conn1_->SendPackets(packets);
+  // For direct DTLS, the sent packet should be larger than the data due to
+  // DTLS overhead.
+  EXPECT_GT(ice1_->last_sent_packet().size(), data.size());
+  loop_.Run();
+  EXPECT_TRUE(callback_called);
+}
+
 }  // namespace
 }  // namespace webrtc