diff --git a/libavformat/rtpproto.c b/libavformat/rtpproto.c
index b109ac84020ec647bbc282ed32716b43dcd9e282..ba699b570889b281263bc9f725bdf528c2b73fa4 100644
--- a/libavformat/rtpproto.c
+++ b/libavformat/rtpproto.c
@@ -369,6 +369,9 @@ static int rtp_write(URLContext *h, const uint8_t *buf, int size)
     int ret;
     URLContext *hd;
 
+    if (size < 2)
+        return AVERROR(EINVAL);
+
     if (RTP_PT_IS_RTCP(buf[1])) {
         /* RTCP payload type */
         hd = s->rtcp_hd;