SDOCM00115143 Improper timeout handling in MessageQ_get (Linux)
authorRamsey Harris <ramsey@ti.com>
Fri, 13 Mar 2015 00:44:09 +0000 (17:44 -0700)
committerRobert Tivy <rtivy@ti.com>
Fri, 13 Mar 2015 23:34:17 +0000 (16:34 -0700)
Split the given timeout value into seconds and microseconds.
Add these to the current time of day. Check for rollover on
the microseconds.

linux/src/api/MessageQ.c

index 53185fcdabfce7d44d74f0765cb577befcbe210d..b229e1b26193c28038929feeb2068fae1ec0ebdd 100644 (file)
@@ -930,15 +930,28 @@ Int MessageQ_get(MessageQ_Handle handle, MessageQ_Msg *msg, UInt timeout)
         sem_wait(&obj->synchronizer);
     }
     else {
+        /* add timeout (microseconds) to current time of day */
         gettimeofday(&tv, NULL);
+        tv.tv_sec += timeout / 1000000;
+        tv.tv_usec += timeout % 1000000;
+
+        if (tv.tv_usec >= 1000000) {
+              tv.tv_sec++;
+              tv.tv_usec -= 1000000;
+        }
+
+        /* set absolute timeout value */
         ts.tv_sec = tv.tv_sec;
-        ts.tv_nsec = (tv.tv_usec + timeout) * 1000;
+        ts.tv_nsec = tv.tv_usec * 1000; /* convert to nanoseconds */
 
         if (sem_timedwait(&obj->synchronizer, &ts) < 0) {
             if (errno == ETIMEDOUT) {
                 PRINTVERBOSE0("MessageQ_get: operation timed out\n")
-
-                return MessageQ_E_TIMEOUT;
+                return (MessageQ_E_TIMEOUT);
+            }
+            else {
+                PRINTVERBOSE0("MessageQ_get: sem_timedwait error\n")
+                return (MessageQ_E_FAIL);
             }
         }
     }