move my_id parameter [part 2 of 2]
[smatch.git] / check_err_ptr.c
blob403b992142c8c7cbd071819a18a19312b813df40
1 /*
2 * sparse/check_err_ptr.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 /*
11 * Functions should never return both NULL and ERR_PTR().
14 #include "smatch.h"
15 #include "smatch_slist.h"
17 static int my_id;
19 static int err_ptr = 0;
20 static int returns_null = 0;
22 static void match_err_ptr(struct expression *expr)
24 expr = strip_expr(expr);
25 if (!expr)
26 return;
27 if (expr->type != EXPR_CALL)
28 return;
30 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
31 return;
32 if (!strcmp(expr->fn->symbol->ident->name, "ERR_PTR"))
33 err_ptr = 1;
36 extern int check_assigned_expr_id;
37 static void match_return(struct statement *stmt)
39 struct state_list *slist;
40 struct sm_state *tmp;
42 match_err_ptr(stmt->ret_value);
43 slist = get_possible_states_expr(check_assigned_expr_id, stmt->ret_value);
44 FOR_EACH_PTR(slist, tmp) {
45 if (tmp->state == &undefined || tmp->state == &merged)
46 continue;
47 match_err_ptr((struct expression *)tmp->state->data);
48 } END_FOR_EACH_PTR(tmp);
50 if (get_implied_value(stmt->ret_value) == 0)
51 returns_null = 1;
54 static void match_end_func(struct symbol *sym)
56 if (returns_null && err_ptr)
57 smatch_msg("warn: returns both null and ERR_PTR.");
58 if (err_ptr)
59 smatch_msg("info: returns_err_ptr");
60 err_ptr = 0;
61 returns_null = 0;
64 void check_err_ptr(int id)
66 if (!option_spammy)
67 return;
69 my_id = id;
70 add_hook(&match_return, RETURN_HOOK);
71 add_hook(&match_end_func, END_FUNC_HOOK);