From d669790560446e366ee4e3d3b0d8ad98dd579098 Mon Sep 17 00:00:00 2001
From: "Timothy B. Terriberry" <tterribe@xiph.org>
Date: Mon, 2 Sep 2013 17:23:58 -0700
Subject: [PATCH] Fix a granpos calculation we assumed couldn't fail.

It was only guaranteed to work in the seekable case.
Thanks to Radio Stadtfilter 96.3Mhz for sending a stream which
 triggered this failure.
---
 src/opusfile.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/opusfile.c b/src/opusfile.c
index 9ec6b32..392ddb2 100644
--- a/src/opusfile.c
+++ b/src/opusfile.c
@@ -2519,9 +2519,17 @@ static ogg_int64_t op_get_pcm_offset(const OggOpusFile *_of,
     _gp=links[_li].pcm_end;
   }
   if(OP_LIKELY(op_granpos_cmp(_gp,links[_li].pcm_start)>0)){
-    OP_ALWAYS_TRUE(!op_granpos_diff(&delta,_gp,links[_li].pcm_start));
+    if(OP_UNLIKELY(op_granpos_diff(&delta,_gp,links[_li].pcm_start)<0)){
+      /*This means an unseekable stream claimed to have a page from more than
+         2 billion days after we joined.*/
+      OP_ASSERT(!_of->seekable);
+      return OP_INT64_MAX;
+    }
     if(delta<links[_li].head.pre_skip)delta=0;
     else delta-=links[_li].head.pre_skip;
+    /*In the seekable case, _gp was limited by pcm_end.
+      In the unseekable case, pcm_offset should be 0.*/
+    OP_ASSERT(pcm_offset<=OP_INT64_MAX-delta);
     pcm_offset+=delta;
   }
   return pcm_offset;
-- 
GitLab