]> git.sesse.net Git - vlc/commitdiff
rtp sout: fix integer overflow
authorPierre Ynard <linkfanel@yahoo.fr>
Sun, 5 Feb 2012 02:29:47 +0000 (03:29 +0100)
committerPierre Ynard <linkfanel@yahoo.fr>
Sun, 5 Feb 2012 02:29:47 +0000 (03:29 +0100)
This became likely to happen using VoD. Thanks to Denis Charmet for
pointing out this issue.

modules/stream_out/rtp.c

index d2b6500de5b10df7d1071b80eac15b316a29bb52..5900172cc4c12611ddbc959d0b1cc20069556851 100644 (file)
@@ -946,9 +946,14 @@ rtp_set_ptime (sout_stream_id_t *id, unsigned ptime_ms, size_t bytes)
 
 uint32_t rtp_compute_ts( unsigned i_clock_rate, int64_t i_pts )
 {
-    /* NOTE: this plays nice with offsets because the calculations are
-     * linear. */
-    return i_pts * (int64_t)i_clock_rate / CLOCK_FREQ;
+    /* This is an overflow-proof way of doing:
+     * return i_pts * (int64_t)i_clock_rate / CLOCK_FREQ;
+     *
+     * NOTE: this plays nice with offsets because the (equivalent)
+     * calculations are linear. */
+    lldiv_t q = lldiv(i_pts, CLOCK_FREQ);
+    return q.quot * (int64_t)i_clock_rate
+          + q.rem * (int64_t)i_clock_rate / CLOCK_FREQ;
 }
 
 /** Add an ES as a new RTP stream */