diff --git a/libavformat/rmdec.c b/libavformat/rmdec.c
index c46b53f3352eb2cca5da64e4aaec108897324cd0..19bd7a7a0b2375ea459abad233b946e2a2bbc0d6 100644
--- a/libavformat/rmdec.c
+++ b/libavformat/rmdec.c
@@ -312,6 +312,9 @@ ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb,
     int64_t codec_pos;
     int ret;
 
+    if (codec_data_size < 0)
+        return AVERROR_INVALIDDATA;
+
     avpriv_set_pts_info(st, 64, 1, 1000);
     codec_pos = avio_tell(pb);
     v = avio_rb32(pb);