changeset 499:8ede825a54f6

Rework canonicalisation so as to avoid theoretical possibility of integer overflow. (This is unit tested in svcore, not here - tests pass)
author Chris Cannam
date Mon, 11 Dec 2017 09:27:57 +0000
parents a87e1bcce7b0
children 4a86f866bb6b
files src/vamp-sdk/RealTime.cpp
diffstat 1 files changed, 5 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/vamp-sdk/RealTime.cpp	Tue Oct 10 13:53:48 2017 +0100
+++ b/src/vamp-sdk/RealTime.cpp	Mon Dec 11 09:27:57 2017 +0000
@@ -42,6 +42,7 @@
 */
 
 #include <iostream>
+#include <limits.h>
 
 #if (defined(__GNUC__)) && (__GNUC__ < 3)
 #include <strstream>
@@ -77,16 +78,10 @@
 RealTime::RealTime(int s, int n) :
     sec(s), nsec(n)
 {
-    if (sec == 0) {
-	while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; }
-	while (nsec >=  ONE_BILLION) { nsec -= ONE_BILLION; ++sec; }
-    } else if (sec < 0) {
-	while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; }
-	while (nsec > 0 && sec < 0)  { nsec -= ONE_BILLION; ++sec; }
-    } else { 
-	while (nsec >=  ONE_BILLION) { nsec -= ONE_BILLION; ++sec; }
-	while (nsec < 0 && sec > 0)  { nsec += ONE_BILLION; --sec; }
-    }
+    while (nsec <= -ONE_BILLION && sec > INT_MIN) { nsec += ONE_BILLION; --sec; }
+    while (nsec >=  ONE_BILLION && sec < INT_MAX) { nsec -= ONE_BILLION; ++sec; }
+    while (nsec > 0 && sec < 0) { nsec -= ONE_BILLION; ++sec; }
+    while (nsec < 0 && sec > 0) { nsec += ONE_BILLION; --sec; }
 }
 
 RealTime