Workaround 'possible null pointer dereference' cppcheck warnings
authorIvan Maidanski <ivmai@mail.ru>
Tue, 18 Oct 2016 09:04:52 +0000 (12:04 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Tue, 18 Oct 2016 09:17:25 +0000 (12:17 +0300)
* include/private/gc_priv.h (GC_on_abort): Move definition upper (to be
before ABORT definition); define it to empty if PCR (this is needed for
CPPCHECK).
* include/private/gc_priv.h (ABORT): Specially define to
{GC_on_abort(msg);abort();} block statement if CPPCHECK.
* tests/test.c (chktree): Check for n is zero only once; add comment.

include/private/gc_priv.h
tests/test.c

index e4c4cc4..a23a3e8 100644 (file)
@@ -482,7 +482,14 @@ typedef char * ptr_t;   /* A generic pointer to which we can add        */
 #endif
 
 /* Abandon ship */
-# ifdef PCR
+# if defined(SMALL_CONFIG) || defined(PCR)
+#   define GC_on_abort(msg) (void)0 /* be silent on abort */
+# else
+    GC_API_PRIV GC_abort_func GC_on_abort;
+# endif
+# if defined(CPPCHECK)
+#   define ABORT(msg) { GC_on_abort(msg); abort(); }
+# elif defined(PCR)
 #   define ABORT(s) PCR_Base_Panic(s)
 # else
 #   if defined(MSWINCE) && !defined(DebugBreak) \
@@ -494,11 +501,6 @@ typedef char * ptr_t;   /* A generic pointer to which we can add        */
       /* instead of defining it as a macro).                            */
 #     define DebugBreak() _exit(-1) /* there is no abort() in WinCE */
 #   endif
-#   ifdef SMALL_CONFIG
-#     define GC_on_abort(msg) (void)0 /* be silent on abort */
-#   else
-      GC_API_PRIV GC_abort_func GC_on_abort;
-#   endif /* !SMALL_CONFIG */
 #   if defined(MSWIN32) && (defined(NO_DEBUGGING) || defined(LINT2))
       /* A more user-friendly abort after showing fatal message.        */
 #     define ABORT(msg) (GC_on_abort(msg), _exit(-1))
index d9c8997..6c9bef4 100644 (file)
@@ -942,11 +942,12 @@ tn * mktree(int n)
 
 void chktree(tn *t, int n)
 {
-    if (n == 0 && t != 0) {
+    if (0 == n) {
+        if (NULL == t) /* is a leaf? */
+            return;
         GC_printf("Clobbered a leaf - collector is broken\n");
         FAIL;
     }
-    if (n == 0) return;
     if (t -> level != n) {
         GC_printf("Lost a node at level %d - collector is broken\n", n);
         FAIL;