extra: handle x = (u8)y; correctly
[smatch.git] / check_err_ptr.c
blobc3d0f7e9ae8e1c474368c463d191cc4f3c03caf6
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include "smatch.h"
19 #include "smatch_slist.h"
21 static int my_id;
23 static int err_ptr = 0;
24 static int returns_null = 0;
26 static void match_err_ptr(struct expression *expr)
28 expr = strip_expr(expr);
29 if (!expr)
30 return;
31 if (expr->type != EXPR_CALL)
32 return;
34 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
35 return;
36 if (!strcmp(expr->fn->symbol->ident->name, "ERR_PTR"))
37 err_ptr = 1;
40 extern int check_assigned_expr_id;
41 static void match_return(struct expression *ret_value)
43 struct state_list *slist;
44 struct sm_state *tmp;
45 sval_t sval;
47 if (__inline_fn)
48 return;
49 match_err_ptr(ret_value);
50 slist = get_possible_states_expr(check_assigned_expr_id, ret_value);
51 FOR_EACH_PTR(slist, tmp) {
52 if (tmp->state == &undefined || tmp->state == &merged)
53 continue;
54 match_err_ptr((struct expression *)tmp->state->data);
55 } END_FOR_EACH_PTR(tmp);
57 if (get_implied_value(ret_value, &sval)) {
58 if (sval.value == 0)
59 returns_null = 1;
63 static void match_end_func(struct symbol *sym)
65 if (__inline_fn)
66 return;
67 if (err_ptr)
68 sm_info("returns_err_ptr");
69 err_ptr = 0;
70 returns_null = 0;
73 void check_err_ptr(int id)
75 if (option_project != PROJ_KERNEL)
76 return;
77 if (!option_info)
78 return;
80 my_id = id;
81 add_hook(&match_return, RETURN_HOOK);
82 add_hook(&match_end_func, END_FUNC_HOOK);