param_key: fix container of when no struct member is referenced
[smatch.git] / check_returns_negative_error_code.c
blob6b73c311ab4cd28b204f4d5a34dc01d0dc8f6abd
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_extra.h"
20 #include "smatch_slist.h"
22 static int my_id;
24 STATE(error_code);
26 static void clear_state(struct sm_state *sm, struct expression *mod_expr)
28 set_state(my_id, sm->name, sm->sym, &undefined);
31 static bool is_error_macro(struct expression *expr)
33 char *macro;
34 sval_t sval;
36 if (!expr)
37 return false;
38 if (expr->type != EXPR_PREOP || expr->op != '-')
39 return false;
41 if (!get_value(expr, &sval))
42 return false;
43 if (sval.value < -4095 || sval.value >= 0)
44 return false;
46 expr = expr->unop;
47 macro = get_macro_name(expr->pos);
48 if (!macro || macro[0] != 'E')
49 return false;
51 return true;
54 static void match_assign(struct expression *expr)
56 if (expr->op != '=')
57 return;
59 if (!is_error_macro(expr->right))
60 return;
62 set_state_expr(my_id, expr->left, &error_code);
65 bool holds_kernel_error_codes(struct expression *expr)
67 if (!expr)
68 return false;
70 if (is_error_macro(expr))
71 return true;
73 return expr_has_possible_state(my_id, expr, &error_code);
76 static void match_return(int return_id, char *return_ranges, struct expression *expr)
78 struct range_list *rl;
80 if (!expr)
81 return;
83 if (!get_implied_rl(expr, &rl) || !sval_is_negative(rl_min(rl)))
84 return;
86 if (!holds_kernel_error_codes(expr))
87 return;
89 sql_insert_return_states(return_id, return_ranges, NEGATIVE_ERROR,
90 -1, "$", "");
93 static void set_error_code(struct expression *expr, const char *name, struct symbol *sym, void *data)
95 set_state(my_id, name, sym, &error_code);
98 void check_returns_negative_error_code(int id)
100 my_id = id;
102 if (option_project != PROJ_KERNEL)
103 return;
105 add_hook(&match_assign, ASSIGNMENT_HOOK);
106 add_modification_hook(my_id, &clear_state);
107 add_split_return_callback(&match_return);
108 select_return_param_key(NEGATIVE_ERROR, &set_error_code);