ref: b7db015a8cdbf4f265fc4d04f9fa9e56b283266c
parent: d767e216a8312907dd8c03f12f3e4741b603c852
author: Martin Storsjö <martin@martin.st>
date: Sun Mar 2 17:30:36 EST 2014
Avoid overflow when populating a struct timespec When adding the (dwMilliseconds % 1000) * 1000000 part to ts.tv_nsec, the ts.tv_nsec field can grow larger than one whole second. Therefore first add all of dwMilliseconds to the tv_nsec field and add all whole seconds to the tv_sec field instead - this way we make sure that the tv_nsec field actually is less than a second.
--- a/codec/common/WelsThreadLib.cpp
+++ b/codec/common/WelsThreadLib.cpp
@@ -330,8 +330,9 @@
gettimeofday (&tv, 0);
- ts.tv_sec = tv.tv_sec + dwMilliseconds / 1000;
- ts.tv_nsec = tv.tv_usec * 1000 + (dwMilliseconds % 1000) * 1000000;
+ ts.tv_nsec = tv.tv_usec * 1000 + dwMilliseconds * 1000000;
+ ts.tv_sec = tv.tv_sec + ts.tv_nsec / 1000000000;
+ ts.tv_nsec %= 1000000000;
return sem_timedwait (event, &ts);
#endif//__APPLE__