diff mbox

[FFmpeg-devel,03/10] avformat/matroskadec: Don't make unnecessary assumptions

Message ID 20190308092604.3752-4-andreas.rheinhardt@googlemail.com
State Superseded
Headers show

Commit Message

Andreas Rheinhardt March 8, 2019, 9:25 a.m. UTC
regarding the length of a cluster's size field.

The earlier code relied on the length of clusters always being coded on
eight bytes (as is current Matroska muxer behaviour). But there is no
need to rely on this and this commit changes it.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@googlemail.com>
---
 libavformat/matroskadec.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
diff mbox

Patch

diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 8a14764d1a..88e80b2fda 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -3695,14 +3695,16 @@  static int webm_clusters_start_with_keyframe(AVFormatContext *s)
     before_pos = avio_tell(s->pb);
     while (1) {
         int64_t cluster_id = 0, cluster_length = 0;
+        int read;
         AVPacket *pkt;
         avio_seek(s->pb, cluster_pos, SEEK_SET);
         // read cluster id and length
-        ebml_read_num(matroska, matroska->ctx->pb, 4, &cluster_id);
-        ebml_read_length(matroska, matroska->ctx->pb, &cluster_length);
-        if (cluster_id != 0xF43B675) { // done with all clusters
+        read = ebml_read_num(matroska, matroska->ctx->pb, 4, &cluster_id);
+        if (read < 0 || cluster_id != 0xF43B675) // done with all clusters
+            break;
+        read = ebml_read_length(matroska, matroska->ctx->pb, &cluster_length);
+        if (read < 0)
             break;
-        }
         avio_seek(s->pb, cluster_pos, SEEK_SET);
         matroska->current_id = 0;
         matroska_clear_queue(matroska);
@@ -3711,7 +3713,8 @@  static int webm_clusters_start_with_keyframe(AVFormatContext *s)
             break;
         }
         pkt = &matroska->queue->pkt;
-        cluster_pos += cluster_length + 12; // 12 is the offset of the cluster id and length.
+        // 4 + read is the length of the cluster id and the cluster length field.
+        cluster_pos += 4 + read + cluster_length;
         if (!(pkt->flags & AV_PKT_FLAG_KEY)) {
             rv = 0;
             break;