Eliminate 'value shift followed by expansion' false code defect warning
authorIvan Maidanski <ivmai@mail.ru>
Thu, 11 May 2017 08:21:01 +0000 (11:21 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Thu, 11 May 2017 08:21:01 +0000 (11:21 +0300)
* src/atomic_ops.c [AO_USE_NANOSLEEP] (AO_pause): Use "L" suffix for
shifted immediate value (1) to match the type of ts.tv_nsec.
* src/atomic_ops.c [!AO_USE_NANOSLEEP && AO_USE_WIN32_PTHREADS]
(AO_pause): Cast shifted immediate value (1) to DWORD to match Sleep
argument type.
* src/atomic_ops.c [!AO_USE_NANOSLEEP && !AO_USE_WIN32_PTHREADS]
(AO_pause): Store computed usec value to an intermediate int variable
(before storing it to tv.tv_usec) so that to avoid widening conversion
of shifted immediate int value (1); add comment.

src/atomic_ops.c

index a659621..4be37b6 100644 (file)
@@ -243,15 +243,20 @@ void AO_pause(int n)
 #     ifdef AO_USE_NANOSLEEP
         struct timespec ts;
         ts.tv_sec = 0;
-        ts.tv_nsec = (n > 28 ? 100000 * 1000 : 1 << (n - 2));
+        ts.tv_nsec = n > 28 ? 100000L * 1000 : 1L << (n - 2);
         nanosleep(&ts, 0);
 #     elif defined(AO_USE_WIN32_PTHREADS)
-        Sleep(n > 28 ? 100 : n < 22 ? 1 : 1 << (n - 22)); /* in millis */
+        Sleep(n > 28 ? 100 /* millis */
+                     : n < 22 ? 1 : (DWORD)1 << (n - 22));
 #     else
         struct timeval tv;
         /* Short async-signal-safe sleep. */
+        int usec = n > 28 ? 100000 : 1 << (n - 12);
+                /* Use an intermediate variable (of int type) to avoid  */
+                /* "shift followed by widening conversion" warning.     */
+
         tv.tv_sec = 0;
-        tv.tv_usec = n > 28 ? 100000 : 1 << (n - 12);
+        tv.tv_usec = usec;
         select(0, 0, 0, 0, &tv);
 #     endif
     }