From 7a671b456416aaf9f62d4beacb65f2eed6f89ce8 Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Thu, 19 Dec 2013 19:56:44 +0300 Subject: [PATCH] *new* check_array_condition.c: arrays can't be NULL 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 --- check_array_condition.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ check_list.h | 1 + 2 files changed, 45 insertions(+) create mode 100644 check_array_condition.c diff --git a/check_array_condition.c b/check_array_condition.c new file mode 100644 index 00000000..5e9559af --- /dev/null +++ b/check_array_condition.c @@ -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); +} diff --git a/check_list.h b/check_list.h index b3cf8849..afa79a3b 100644 --- a/check_list.h +++ b/check_list.h @@ -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) -- 2.11.4.GIT