From c9558e560eaead8ef3d73c8a3e4cdb5c4ffbfef4 Mon Sep 17 00:00:00 2001
From: Ben Boeckel <mathstuf@gmail.com>
Date: Sun, 25 Jan 2015 19:35:36 -0500
Subject: [PATCH] tl_matroska: only check the chapter count when adding
 chapters

Fixes #1445 without failing in the case where file outside.mkv with N
chapters references inside.mkv with N+1 chapters. Since we don't care
about the number of chapters in inner files (yet; nested chapters would
change this, but also make chapters dynamic anyways), don't check there.
---
 player/timeline/tl_matroska.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/player/timeline/tl_matroska.c b/player/timeline/tl_matroska.c
index 8727c8c72a..17734d0363 100644
--- a/player/timeline/tl_matroska.c
+++ b/player/timeline/tl_matroska.c
@@ -396,10 +396,10 @@ static void build_timeline_loop(struct MPContext *mpctx,
             if (!demux_matroska_uid_cmp(&c->uid, &linked_m->uid))
                 continue;
 
-            if (i >= ctx->num_chapters)
-                break; // probably needed only for broken sources
-
             if (!info->limit) {
+                if (i >= ctx->num_chapters)
+                    break; // malformed files can cause this to happen.
+
                 chapters[i].pts = ctx->start_time / 1e9;
                 chapters[i].name = talloc_strdup(chapters, c->name);
             }