slist: introduce __diff_stree() for debugging
[smatch.git] / check_returns_negative_error_code.c
blob032edfdfb23dec53d57b610f747273305e3adfa2
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 pre_merge_hook(struct sm_state *cur, struct sm_state *other)
28 struct sm_state *sm;
29 sval_t sval;
31 sm = get_sm_state(SMATCH_EXTRA, cur->name, cur->sym);
32 if (!sm || !estate_rl(sm->state))
33 return;
34 if (type_unsigned(estate_type(sm->state)))
35 return;
36 sval = estate_min(sm->state);
37 if (sval.value == 0)
38 set_state(my_id, cur->name, cur->sym, &undefined);
41 static bool is_error_macro(struct expression *expr)
43 char *macro;
44 sval_t sval;
46 if (!expr)
47 return false;
48 if (expr->type != EXPR_PREOP || expr->op != '-')
49 return false;
51 if (!get_value(expr, &sval))
52 return false;
53 if (sval.value < -4095 || sval.value >= 0)
54 return false;
56 expr = expr->unop;
57 macro = get_macro_name(expr->pos);
58 if (!macro || macro[0] != 'E')
59 return false;
61 return true;
64 static void match_assign(struct expression *expr)
66 if (expr->op != '=')
67 return;
69 if (!is_error_macro(expr->right))
70 return;
72 set_state_expr(my_id, expr->left, &error_code);
75 static void match_condition(struct expression *expr)
77 if (expr->type != EXPR_COMPARE)
78 return;
79 if (expr->op != '<' && expr->op != SPECIAL_LTE)
80 return;
81 if (!expr_is_zero(expr->right))
82 return;
84 set_true_false_states_expr(my_id, expr->left, NULL, &undefined);
87 static bool is_empty_state(struct expression *expr)
89 struct smatch_state *state;
91 state = get_extra_state(expr);
92 if (!state)
93 return false;
94 if (estate_rl(state))
95 return false;
96 return true;
99 bool holds_kernel_error_codes(struct expression *expr)
101 struct range_list *rl;
103 if (!expr)
104 return false;
106 if (is_error_macro(expr))
107 return true;
109 if (is_empty_state(expr))
110 return false;
112 if (get_implied_rl(expr, &rl)) {
113 if (rl_min(rl).value >= 0 &&
114 rl_max(rl).uvalue <= INT_MAX)
115 return false;
117 if (type_positive_bits(rl_type(rl)) >= 63 &&
118 !sval_is_negative(rl_min(rl)) &&
119 rl_max(rl).uvalue <= UINT_MAX)
120 return false;
123 return expr_has_possible_state(my_id, expr, &error_code);
126 static void match_return(int return_id, char *return_ranges, struct expression *expr)
128 if (!expr)
129 return;
131 if (!holds_kernel_error_codes(expr))
132 return;
134 sql_insert_return_states(return_id, return_ranges, NEGATIVE_ERROR,
135 -1, "$", "");
138 static void set_error_code(struct expression *expr, const char *name, struct symbol *sym, void *data)
140 set_state(my_id, name, sym, &error_code);
143 void check_returns_negative_error_code(int id)
145 my_id = id;
147 if (option_project != PROJ_KERNEL)
148 return;
150 add_hook(&match_assign, ASSIGNMENT_HOOK);
151 add_hook(&match_condition, CONDITION_HOOK);
152 add_modification_hook(my_id, &set_undefined);
153 add_pre_merge_hook(my_id, &pre_merge_hook);
154 add_split_return_callback(&match_return);
155 select_return_param_key(NEGATIVE_ERROR, &set_error_code);