*new* check_array_condition.c: arrays can't be NULL
authorDan Carpenter <dan.carpenter@oracle.com>
Thu, 19 Dec 2013 16:56:44 +0000 (19 19:56 +0300)
committerDan Carpenter <dan.carpenter@oracle.com>
Thu, 19 Dec 2013 16:56:44 +0000 (19 19:56 +0300)
These mostly are false positives in that they are just superfluous NULL
checks that the compiler removes.  But it does find some bugs.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
check_array_condition.c [new file with mode: 0644]
check_list.h

diff --git a/check_array_condition.c b/check_array_condition.c
new file mode 100644 (file)
index 0000000..5e9559a
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * smatch/check_array_condition.c
+ *
+ * Copyright (C) 2013 Oracle.
+ *
+ * Licensed under the Open Software License version 1.1
+ *
+ */
+
+/*
+ * struct foo { char buf[10]; };
+ *
+ * struct foo *p = something();
+ * if (p->buf) { ...
+ *
+ */
+
+#include "smatch.h"
+
+static int my_id;
+
+static void match_condition(struct expression *expr)
+{
+       struct symbol *type;
+       char *str;
+
+       if (expr->type != EXPR_DEREF)
+               return;
+       type = get_type(expr);
+       if (!type || type->type != SYM_ARRAY)
+               return;
+       if (get_macro_name(expr->pos))
+               return;
+
+       str = expr_to_str(expr);
+       sm_msg("warn: this array is probably non-NULL. '%s'", str);
+       free_string(str);
+}
+
+void check_array_condition(int id)
+{
+       my_id = id;
+       add_hook(&match_condition, CONDITION_HOOK);
+}
index b3cf884..afa79a3 100644 (file)
@@ -92,6 +92,7 @@ CK(check_or_vs_and)
 CK(check_passes_sizeof)
 CK(check_assign_vs_compare)
 CK(check_missing_break)
+CK(check_array_condition)
 CK(check_struct_type)
 CK(check_cast_assign)