analyzer: fix wording for assignment from NULL
authorDavid Malcolm <dmalcolm@redhat.com>
Wed, 12 Feb 2020 15:56:28 +0000 (10:56 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Mon, 17 Feb 2020 07:15:00 +0000 (02:15 -0500)
This patch improves the wording of the state-transition event (1) in
the -Wanalyzer-null-dereference diagnostic for:

void test (void)
{
  int *p = NULL;
  *p = 1;
}

taking the path description from:

  ‘test’: events 1-2
    |
    |    5 |   int *p = NULL;
    |      |        ^
    |      |        |
    |      |        (1) assuming ‘p’ is NULL
    |    6 |   *p = 1;
    |      |   ~~~~~~
    |      |      |
    |      |      (2) dereference of NULL ‘p’
    |

to:

  ‘test’: events 1-2
    |
    |    5 |   int *p = NULL;
    |      |        ^
    |      |        |
    |      |        (1) ‘p’ is NULL
    |    6 |   *p = 1;
    |      |   ~~~~~~
    |      |      |
    |      |      (2) dereference of NULL ‘p’
    |

since the "assuming" at (1) only makes sense for state transitions
due to comparisons, not for assignments.

gcc/analyzer/ChangeLog:
* sm-malloc.cc (malloc_diagnostic::describe_state_change): For
transition to the "null" state, only say "assuming" when
transitioning from the "unchecked" state.

gcc/testsuite/ChangeLog:
* gcc.dg/analyzer/malloc-1.c (test_48): New.

gcc/analyzer/ChangeLog
gcc/analyzer/sm-malloc.cc
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/analyzer/malloc-1.c

index 9f1e25d1e90d91a6f8580d333939a9a36ce5d9c4..5945abc04ee98acaa24e208e9ed5a693384892cb 100644 (file)
@@ -1,3 +1,9 @@
+2020-02-17  David Malcolm  <dmalcolm@redhat.com>
+
+       * sm-malloc.cc (malloc_diagnostic::describe_state_change): For
+       transition to the "null" state, only say "assuming" when
+       transitioning from the "unchecked" state.
+
 2020-02-17  David Malcolm  <dmalcolm@redhat.com>
 
        * diagnostic-manager.h (diagnostic_manager::get_saved_diagnostic):
index bdd0731b5d1e300550d3674b2ac79df3e93497c2..46225b6f70056db31ee9d3a2c3fbc7b673176dbe 100644 (file)
@@ -130,8 +130,15 @@ public:
       return change.formatted_print ("assuming %qE is non-NULL",
                                     change.m_expr);
     if (change.m_new_state == m_sm.m_null)
-      return change.formatted_print ("assuming %qE is NULL",
-                                    change.m_expr);
+      {
+       if (change.m_old_state == m_sm.m_unchecked)
+         return change.formatted_print ("assuming %qE is NULL",
+                                        change.m_expr);
+       else
+         return change.formatted_print ("%qE is NULL",
+                                        change.m_expr);
+      }
+
     return label_text ();
   }
 
index 83c581c4e0c7592757adeaec1a62a4f70bac58b1..a08ad2e71755586b37562a9927ca3dff310d784b 100644 (file)
@@ -1,3 +1,7 @@
+2020-02-17  David Malcolm  <dmalcolm@redhat.com>
+
+       * gcc.dg/analyzer/malloc-1.c (test_48): New.
+
 2020-02-17  Jiufu Guo  <guojiufu@linux.ibm.com>
 
        PR target/93047
index c13170560afc83ff9433b7696cfd7df60b1f62b5..3024e54613763175658f03344a8da26d8933a1fb 100644 (file)
@@ -583,3 +583,9 @@ int test_47 (void)
   }
   return p_size;
 }
+
+void test_48 (void)
+{
+  int *p = NULL; /* { dg-message "'p' is NULL" } */
+  *p = 1; /* { dg-warning "dereference of NULL 'p'" } */
+}