Rename smatch_extra_helper.c to smatch_ranges.c
[smatch.git] / check_err_ptr.c
blob3368472e3a73ad08807b2d2364804a896ef0b1fc
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 struct symbol *this_func;
20 static int err_ptr = 0;
21 static int returns_null = 0;
23 static void match_function_def(struct symbol *sym)
25 this_func = sym;
28 static void match_err_ptr(const char *fn, struct expression *expr, void *info)
30 struct expression *arg;
31 int value;
33 arg = get_argument_from_call_expr(expr->args, 0);
34 value = get_implied_value(arg);
35 if (value != UNDEFINED && value < -4095)
36 smatch_msg("error: the error code is too large for ERR_PTR");
37 if (!err_ptr)
38 smatch_msg("info: returns_err_ptr");
39 err_ptr = 1;
42 static void match_return(struct statement *stmt)
44 if (get_implied_value(stmt->ret_value) != 0)
45 return;
46 if (!returns_null)
47 smatch_msg("info: returns_null");
48 returns_null = 1;
51 static void match_end_func(struct symbol *sym)
53 err_ptr = 0;
54 returns_null = 0;
57 void check_err_ptr(int id)
59 my_id = id;
60 add_hook(&match_function_def, FUNC_DEF_HOOK);
61 add_function_hook("ERR_PTR", &match_err_ptr, NULL);
62 add_hook(&match_return, RETURN_HOOK);
63 add_hook(&match_end_func, END_FUNC_HOOK);