Workaround 'bad address arithmetic' static analysis tool false positive
authorIvan Maidanski <ivmai@mail.ru>
Sat, 17 Dec 2016 14:21:40 +0000 (17:21 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Sat, 17 Dec 2016 14:21:40 +0000 (17:21 +0300)
The tool complains whether (alloc(size)+ofs) is intentional instead of
(alloc(size+ofs)).  In our case, it is a false alarm (the offset is
added to the result to align the allocation at HBLKSIZE boundary).

* os_dep.c [USE_WINALLOC && MSWIN32] (GC_win32_get_mem): Store result
of GlobalAlloc() to "result" local variable first (then, perform result
alignment in a standalone statement); add comment.

os_dep.c

index 8bceee5..ca0b989 100644 (file)
--- a/os_dep.c
+++ b/os_dep.c
@@ -2306,8 +2306,10 @@ void * os2_alloc(size_t bytes)
         /* VirtualAlloc doesn't like PAGE_EXECUTE_READWRITE.    */
         /* There are also unconfirmed rumors of other           */
         /* problems, so we dodge the issue.                     */
-        result = (ptr_t)(((word)GlobalAlloc(0, SIZET_SAT_ADD(bytes, HBLKSIZE))
-                            + HBLKSIZE - 1) & ~(word)(HBLKSIZE - 1));
+        result = (ptr_t)GlobalAlloc(0, SIZET_SAT_ADD(bytes, HBLKSIZE));
+        /* Align it at HBLKSIZE boundary.       */
+        result = (ptr_t)(((word)result + HBLKSIZE - 1)
+                         & ~(word)(HBLKSIZE - 1));
       } else
 #   endif
     /* else */ {