db/fixup_kernel.sh: fix clear_user() handling
[smatch.git] / check_platform_get_irq_return.c
blob2faee4243ef6d4e3c7e066c9bbfa0bb295f7e9b2
1 /*
2 * Copyright (C) 2022 Oracle.
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"
20 #include "smatch_extra.h"
22 static int my_id;
24 STATE(get_irq);
26 static void match_platform_get_irq(struct expression *expr, const char *name, struct symbol *sym, void *data)
28 set_state(my_id, name, sym, &get_irq);
31 static bool assigned_zero(struct expression *expr)
33 struct sm_state *sm, *tmp;
34 struct expression *prev;
36 sm = get_assigned_sm(expr);
37 if (!sm)
38 return false;
39 FOR_EACH_PTR(sm->possible, tmp) {
40 prev = tmp->state->data;
41 if (expr_is_zero(prev))
42 return true;
43 } END_FOR_EACH_PTR(tmp);
45 return false;
48 static bool is_part_of_select(struct expression *expr)
50 struct expression *parent;
51 sval_t sval;
53 /* Code like "return irq ?: -EINVAL;" is wrong but harmless */
55 parent = expr_get_parent_expr(expr);
56 if (!parent)
57 return false;
58 if (parent->type != EXPR_CONDITIONAL &&
59 parent->type != EXPR_SELECT)
60 return false;
62 if (get_value(parent->cond_false, &sval) &&
63 sval.value >= -4095 &&
64 sval.value < 0)
65 return true;
67 return false;
70 static void match_condition(struct expression *expr)
72 struct range_list *rl;
74 if (!expr_has_possible_state(my_id, expr, &get_irq))
75 return;
77 if (get_implied_rl(expr, &rl) && rl_min(rl).value == 0)
78 return;
80 if (assigned_zero(expr))
81 return;
83 if (is_part_of_select(expr))
84 return;
86 sm_msg("warn: platform_get_irq() does not return zero");
89 void check_platform_get_irq_return(int id)
91 my_id = id;
93 add_function_param_key_hook_late("platform_get_irq", &match_platform_get_irq, -1, "$", NULL);
94 add_function_param_key_hook_late("platform_get_irq_optional", &match_platform_get_irq, -1, "$", NULL);
95 add_function_param_key_hook_late("platform_get_irq_byname", &match_platform_get_irq, -1, "$", NULL);
96 add_function_param_key_hook_late("platform_get_irq_byname_optional", &match_platform_get_irq, -1, "$", NULL);
97 add_modification_hook(my_id, &set_undefined);
98 add_hook(&match_condition, CONDITION_HOOK);