From 0c913731ca6a1f65b52f47f8797bb6031cb7523b Mon Sep 17 00:00:00 2001
From: Andre Noll <maan@tuebingen.mpg.de>
Date: Sat, 28 Aug 2021 18:35:18 +0200
Subject: [PATCH] mp4: Reject files with zero time scale.

A value of zero indicates a corrupt mp4 file or a missing mdhd
atom. This is fatal because we need to divide by the time scale to
compute the duration of the audio track.

This patch modifies mp4_open_read() to check the value at open time
and fail the operation rather than allowing the open to succeed and
checking the value in mp4_get_duration(),

Only regular opens are affected since we don't look at the mdhd atom
for metadata opens.
---
 mp4.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/mp4.c b/mp4.c
index e60f18f5..9e33eb4c 100644
--- a/mp4.c
+++ b/mp4.c
@@ -629,6 +629,9 @@ int mp4_open_read(const struct mp4_callback *cb, struct mp4 **result)
 	ret = -E_MP4_BAD_SAMPLE_COUNT;
 	if (f->track.stsz_sample_count == 0)
 		goto fail;
+	ret = -E_MP4_CORRUPT;
+	if (f->track.time_scale == 0)
+		goto fail;
 	*result = f;
 	return 1;
 fail:
@@ -666,8 +669,6 @@ uint64_t mp4_get_duration(const struct mp4 *f)
 {
 	const struct mp4_track *t = &f->track;
 
-	if (t->time_scale == 0)
-		return 0;
 	return t->duration * 1000 / t->time_scale;
 }
 
-- 
2.39.5