From 1bca301df2dbd8f5236baaff8615b6ef89d824c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Yhuel?= <fyhuel@viotech.net>
Date: Wed, 29 Feb 2012 14:23:55 +0100
Subject: [PATCH] DASH: prevent integer overflow

A integer overflow could happen in bandwidth computation, for example if
we have a DASH server and client on the same LAN.
---
 .../adaptationlogic/AbstractAdaptationLogic.cpp    |    8 ++++----
 .../dash/adaptationlogic/AbstractAdaptationLogic.h |    6 +++---
 .../dash/adaptationlogic/IAdaptationLogic.h        |    4 ++--
 .../dash/adaptationlogic/IDownloadRateObserver.h   |    4 +++-
 .../adaptationlogic/RateBasedAdaptationLogic.cpp   |    2 +-
 modules/stream_filter/dash/http/Chunk.cpp          |    2 +-
 modules/stream_filter/dash/http/Chunk.h            |    3 ++-
 .../dash/http/HTTPConnectionManager.cpp            |    6 +++---
 .../dash/http/HTTPConnectionManager.h              |    4 ++--
 modules/stream_filter/dash/mpd/BasicCMManager.cpp  |    4 ++--
 modules/stream_filter/dash/mpd/BasicCMManager.h    |    4 ++--
 modules/stream_filter/dash/mpd/IMPDManager.h       |    4 ++--
 .../stream_filter/dash/mpd/IsoffMainManager.cpp    |    4 ++--
 modules/stream_filter/dash/mpd/IsoffMainManager.h  |    4 ++--
 14 files changed, 31 insertions(+), 28 deletions(-)

diff --git a/modules/stream_filter/dash/adaptationlogic/AbstractAdaptationLogic.cpp b/modules/stream_filter/dash/adaptationlogic/AbstractAdaptationLogic.cpp
index da1b40e..dd15e87 100644
--- a/modules/stream_filter/dash/adaptationlogic/AbstractAdaptationLogic.cpp
+++ b/modules/stream_filter/dash/adaptationlogic/AbstractAdaptationLogic.cpp
@@ -33,7 +33,7 @@ using namespace dash::mpd;
 using namespace dash::exception;
 
 AbstractAdaptationLogic::AbstractAdaptationLogic    (IMPDManager *mpdManager, stream_t *stream) :
-                         bpsAvg                     (-1),
+                         bpsAvg                     (0),
                          bpsLastChunk               (0),
                          mpdManager                 (mpdManager),
                          stream                     (stream),
@@ -51,16 +51,16 @@ void AbstractAdaptationLogic::bufferLevelChanged     (mtime_t bufferedMicroSec,
     this->bufferedMicroSec = bufferedMicroSec;
     this->bufferedPercent  = bufferedPercent;
 }
-void AbstractAdaptationLogic::downloadRateChanged    (long bpsAvg, long bpsLastChunk)
+void AbstractAdaptationLogic::downloadRateChanged    (uint64_t bpsAvg, uint64_t bpsLastChunk)
 {
     this->bpsAvg        = bpsAvg;
     this->bpsLastChunk  = bpsLastChunk;
 }
-long AbstractAdaptationLogic::getBpsAvg              () const
+uint64_t AbstractAdaptationLogic::getBpsAvg          () const
 {
     return this->bpsAvg;
 }
-long AbstractAdaptationLogic::getBpsLastChunk        () const
+uint64_t AbstractAdaptationLogic::getBpsLastChunk    () const
 {
     return this->bpsLastChunk;
 }
diff --git a/modules/stream_filter/dash/adaptationlogic/AbstractAdaptationLogic.h b/modules/stream_filter/dash/adaptationlogic/AbstractAdaptationLogic.h
index 7f25387..0812cdd 100644
--- a/modules/stream_filter/dash/adaptationlogic/AbstractAdaptationLogic.h
+++ b/modules/stream_filter/dash/adaptationlogic/AbstractAdaptationLogic.h
@@ -47,11 +47,11 @@ namespace dash
                 AbstractAdaptationLogic             (dash::mpd::IMPDManager *mpdManager, stream_t *stream);
                 virtual ~AbstractAdaptationLogic    ();
 
-                virtual void                downloadRateChanged     (long bpsAvg, long bpsLastChunk);
+                virtual void                downloadRateChanged     (uint64_t bpsAvg, uint64_t bpsLastChunk);
                 virtual void                bufferLevelChanged      (mtime_t bufferedMicroSec, int bufferedPercent);
 
-                long                        getBpsAvg               () const;
-                long                        getBpsLastChunk         () const;
+                uint64_t                    getBpsAvg               () const;
+                uint64_t                    getBpsLastChunk         () const;
                 int                         getBufferPercent        () const;
 
             private:
diff --git a/modules/stream_filter/dash/adaptationlogic/IAdaptationLogic.h b/modules/stream_filter/dash/adaptationlogic/IAdaptationLogic.h
index 92b96ed..9fc71d4 100644
--- a/modules/stream_filter/dash/adaptationlogic/IAdaptationLogic.h
+++ b/modules/stream_filter/dash/adaptationlogic/IAdaptationLogic.h
@@ -52,8 +52,8 @@ namespace dash
                 /**
                  *  \return     The average bitrate in bits per second.
                  */
-                virtual long                    getBpsAvg               () const = 0;
-                virtual long                    getBpsLastChunk         () const = 0;
+                virtual uint64_t                getBpsAvg               () const = 0;
+                virtual uint64_t                getBpsLastChunk         () const = 0;
         };
     }
 }
diff --git a/modules/stream_filter/dash/adaptationlogic/IDownloadRateObserver.h b/modules/stream_filter/dash/adaptationlogic/IDownloadRateObserver.h
index e4d6d41..e1f429a 100644
--- a/modules/stream_filter/dash/adaptationlogic/IDownloadRateObserver.h
+++ b/modules/stream_filter/dash/adaptationlogic/IDownloadRateObserver.h
@@ -25,6 +25,8 @@
 #ifndef IDOWNLOADRATEOBSERVER_H_
 #define IDOWNLOADRATEOBSERVER_H_
 
+#include <stdint.h>
+
 namespace dash
 {
     namespace logic
@@ -32,7 +34,7 @@ namespace dash
         class IDownloadRateObserver
         {
             public:
-                virtual void downloadRateChanged(long bpsAvg, long bpsLastChunk) = 0;
+                virtual void downloadRateChanged(uint64_t bpsAvg, uint64_t bpsLastChunk) = 0;
                 virtual ~IDownloadRateObserver(){}
         };
     }
diff --git a/modules/stream_filter/dash/adaptationlogic/RateBasedAdaptationLogic.cpp b/modules/stream_filter/dash/adaptationlogic/RateBasedAdaptationLogic.cpp
index b3fd174..5214777 100644
--- a/modules/stream_filter/dash/adaptationlogic/RateBasedAdaptationLogic.cpp
+++ b/modules/stream_filter/dash/adaptationlogic/RateBasedAdaptationLogic.cpp
@@ -53,7 +53,7 @@ Chunk*  RateBasedAdaptationLogic::getNextChunk() throw(EOFException)
     if(this->currentPeriod == NULL)
         throw EOFException();
 
-    long bitrate = this->getBpsAvg();
+    uint64_t bitrate = this->getBpsAvg();
 
     if(this->getBufferPercent() < MINBUFFER)
         bitrate = 0;
diff --git a/modules/stream_filter/dash/http/Chunk.cpp b/modules/stream_filter/dash/http/Chunk.cpp
index 445d703..430e855 100644
--- a/modules/stream_filter/dash/http/Chunk.cpp
+++ b/modules/stream_filter/dash/http/Chunk.cpp
@@ -72,7 +72,7 @@ void                Chunk::setUseByteRange  (bool value)
 {
     this->hasByteRange = value;
 }
-void                Chunk::setBitrate       (int bitrate)
+void                Chunk::setBitrate       (uint64_t bitrate)
 {
     this->bitrate = bitrate;
 }
diff --git a/modules/stream_filter/dash/http/Chunk.h b/modules/stream_filter/dash/http/Chunk.h
index efffa2c..ce589d7 100644
--- a/modules/stream_filter/dash/http/Chunk.h
+++ b/modules/stream_filter/dash/http/Chunk.h
@@ -27,6 +27,7 @@
 
 #include <vector>
 #include <string>
+#include <stdint.h>
 
 namespace dash
 {
@@ -46,7 +47,7 @@ namespace dash
                 void                addOptionalUrl  (const std::string& url);
                 bool                useByteRange    ();
                 void                setUseByteRange (bool value);
-                void                setBitrate      (int bitrate);
+                void                setBitrate      (uint64_t bitrate);
                 int                 getBitrate      ();
 
             private:
diff --git a/modules/stream_filter/dash/http/HTTPConnectionManager.cpp b/modules/stream_filter/dash/http/HTTPConnectionManager.cpp
index 1a5e7d0..dd4a57e 100644
--- a/modules/stream_filter/dash/http/HTTPConnectionManager.cpp
+++ b/modules/stream_filter/dash/http/HTTPConnectionManager.cpp
@@ -114,10 +114,10 @@ int                 HTTPConnectionManager::read( Chunk *chunk, void *p_buffer, s
         if(this->timeSecChunk > 0)
             this->bpsLastChunk = (this->bytesReadChunk / this->timeSecChunk) * 8;
 
-        if(this->bpsAvg < 0 || this->chunkCount < 2)
+        if(this->chunkCount < 2)
             this->bpsAvg = 0;
 
-        if(this->bpsLastChunk < 0 || this->chunkCount < 2)
+        if(this->chunkCount < 2)
             this->bpsLastChunk = 0;
 
         this->notify();
@@ -150,7 +150,7 @@ void                HTTPConnectionManager::attach                   (IDownloadRa
 }
 void                HTTPConnectionManager::notify                   ()
 {
-    if ( this->bpsAvg <= 0 )
+    if ( this->bpsAvg == 0 )
         return ;
     for(size_t i = 0; i < this->rateObservers.size(); i++)
         this->rateObservers.at(i)->downloadRateChanged(this->bpsAvg, this->bpsLastChunk);
diff --git a/modules/stream_filter/dash/http/HTTPConnectionManager.h b/modules/stream_filter/dash/http/HTTPConnectionManager.h
index 3a5366a..006ca12 100644
--- a/modules/stream_filter/dash/http/HTTPConnectionManager.h
+++ b/modules/stream_filter/dash/http/HTTPConnectionManager.h
@@ -59,8 +59,8 @@ namespace dash
                 std::map<Chunk *, HTTPConnection *>                 chunkMap;
                 std::map<std::string, HTTPConnection *>             urlMap;
                 std::vector<dash::logic::IDownloadRateObserver *>   rateObservers;
-                long                                                bpsAvg;
-                long                                                bpsLastChunk;
+                uint64_t                                            bpsAvg;
+                uint64_t                                            bpsLastChunk;
                 long                                                bytesReadSession;
                 double                                              timeSecSession;
                 long                                                bytesReadChunk;
diff --git a/modules/stream_filter/dash/mpd/BasicCMManager.cpp b/modules/stream_filter/dash/mpd/BasicCMManager.cpp
index 6994faf..b96d2fd 100644
--- a/modules/stream_filter/dash/mpd/BasicCMManager.cpp
+++ b/modules/stream_filter/dash/mpd/BasicCMManager.cpp
@@ -91,7 +91,7 @@ Period*                 BasicCMManager::getFirstPeriod          ()
     return periods.at(0);
 }
 
-Representation*         BasicCMManager::getRepresentation(Period *period, int bitrate ) const
+Representation*         BasicCMManager::getRepresentation(Period *period, uint64_t bitrate ) const
 {
     std::vector<Group *>    groups = period->getGroups();
 
@@ -134,7 +134,7 @@ const MPD*      BasicCMManager::getMPD() const
 {
     return this->mpd;
 }
-Representation*         BasicCMManager::getRepresentation (Period *period, int bitrate, int, int ) const
+Representation*         BasicCMManager::getRepresentation (Period *period, uint64_t bitrate, int, int ) const
 {
     return this->getRepresentation(period, bitrate);
 }
diff --git a/modules/stream_filter/dash/mpd/BasicCMManager.h b/modules/stream_filter/dash/mpd/BasicCMManager.h
index cea9387..a5c07b2 100644
--- a/modules/stream_filter/dash/mpd/BasicCMManager.h
+++ b/modules/stream_filter/dash/mpd/BasicCMManager.h
@@ -52,9 +52,9 @@ namespace dash
                 Period*                         getNextPeriod( Period *period );
                 Representation*                 getBestRepresentation( Period *period );
                 std::vector<Segment *>          getSegments( const Representation *rep );
-                Representation*                 getRepresentation( Period *period, int bitrate ) const;
+                Representation*                 getRepresentation( Period *period, uint64_t bitrate ) const;
                 const MPD*                      getMPD() const;
-                Representation*                 getRepresentation (Period *period, int bitrate,
+                Representation*                 getRepresentation (Period *period, uint64_t bitrate,
                                                                    int width, int height) const;
 
             private:
diff --git a/modules/stream_filter/dash/mpd/IMPDManager.h b/modules/stream_filter/dash/mpd/IMPDManager.h
index dc8ef12..d9072e3 100644
--- a/modules/stream_filter/dash/mpd/IMPDManager.h
+++ b/modules/stream_filter/dash/mpd/IMPDManager.h
@@ -52,9 +52,9 @@ namespace dash
                 virtual Period*                         getNextPeriod           (Period *period)                    = 0;
                 virtual Representation*                 getBestRepresentation   (Period *period)                    = 0;
                 virtual std::vector<Segment *>          getSegments             (const Representation *rep)         = 0;
-                virtual Representation*                 getRepresentation       (Period *period, int bitrate) const = 0;
+                virtual Representation*                 getRepresentation       (Period *period, uint64_t bitrate) const = 0;
                 virtual const MPD*                      getMPD                  () const                            = 0;
-                virtual Representation*                 getRepresentation       (Period *period, int bitrate,
+                virtual Representation*                 getRepresentation       (Period *period, uint64_t bitrate,
                                                                                  int width, int height) const       = 0;
         };
     }
diff --git a/modules/stream_filter/dash/mpd/IsoffMainManager.cpp b/modules/stream_filter/dash/mpd/IsoffMainManager.cpp
index 8ff2b3d..0494ea7 100644
--- a/modules/stream_filter/dash/mpd/IsoffMainManager.cpp
+++ b/modules/stream_filter/dash/mpd/IsoffMainManager.cpp
@@ -87,7 +87,7 @@ Period*                     IsoffMainManager::getFirstPeriod        ()
 
     return periods.at(0);
 }
-Representation*             IsoffMainManager::getRepresentation     (Period *period, int bitrate) const
+Representation*             IsoffMainManager::getRepresentation     (Period *period, uint64_t bitrate) const
 {
     if(period == NULL)
         return NULL;
@@ -129,7 +129,7 @@ const MPD*                  IsoffMainManager::getMPD                () const
 {
     return this->mpd;
 }
-Representation*             IsoffMainManager::getRepresentation     (Period *period, int bitrate, int width, int height) const
+Representation*             IsoffMainManager::getRepresentation     (Period *period, uint64_t bitrate, int width, int height) const
 {
     if(period == NULL)
         return NULL;
diff --git a/modules/stream_filter/dash/mpd/IsoffMainManager.h b/modules/stream_filter/dash/mpd/IsoffMainManager.h
index bdee2ef..7d6683f 100644
--- a/modules/stream_filter/dash/mpd/IsoffMainManager.h
+++ b/modules/stream_filter/dash/mpd/IsoffMainManager.h
@@ -52,9 +52,9 @@ namespace dash
                 Period*                         getNextPeriod           (Period *period);
                 Representation*                 getBestRepresentation   (Period *period);
                 std::vector<Segment *>          getSegments             (const Representation *rep);
-                Representation*                 getRepresentation       (Period *period, int bitrate) const;
+                Representation*                 getRepresentation       (Period *period, uint64_t bitrate) const;
                 const MPD*                      getMPD                  () const;
-                Representation*                 getRepresentation       (Period *period, int bitrate,
+                Representation*                 getRepresentation       (Period *period, uint64_t bitrate,
                                                                          int width, int height) const;
 
             private:
-- 
1.7.5.4

