kernel: fix a type bug handling err_cast()
[smatch.git] / smatch.c
blob4c1e6af6125a952c3d583fe4318fc0376730df81
1 /*
2 * Copyright (C) 2006 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 <stdio.h>
19 #include <unistd.h>
20 #include <libgen.h>
21 #include "smatch.h"
22 #include "smatch_slist.h"
23 #include "check_list.h"
25 char *option_debug_check = (char *)"";
26 char *option_project_str = (char *)"smatch_generic";
27 static char *option_db_file = (char *)"smatch_db.sqlite";
28 enum project_type option_project = PROJ_NONE;
29 char *bin_dir;
30 char *data_dir;
31 int option_no_data = 0;
32 int option_spammy = 0;
33 int option_info = 0;
34 int option_full_path = 0;
35 int option_call_tree = 0;
36 int option_no_db = 0;
37 int option_enable = 0;
38 int option_disable = 0;
39 int option_file_output;
40 int option_time;
41 int option_mem;
42 char *option_datadir_str;
43 int option_fatal_checks;
44 int option_succeed;
46 FILE *sm_outfd;
47 FILE *sql_outfd;
48 FILE *caller_info_fd;
50 int sm_nr_errors;
51 int sm_nr_checks;
53 bool __silence_warnings_for_stmt;
55 typedef void (*reg_func) (int id);
56 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
57 static struct reg_func_info {
58 const char *name;
59 reg_func func;
60 int enabled;
61 } reg_funcs[] = {
62 {NULL, NULL},
63 #include "check_list.h"
65 #undef CK
66 int num_checks = ARRAY_SIZE(reg_funcs) - 1;
68 const char *check_name(unsigned short id)
70 if (id >= ARRAY_SIZE(reg_funcs))
71 return "internal";
73 return reg_funcs[id].name;
76 int id_from_name(const char *name)
78 int i;
80 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
81 if (!strcmp(name, reg_funcs[i].name))
82 return i;
84 return 0;
87 static void show_checks(void)
89 int i;
91 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
92 if (!strncmp(reg_funcs[i].name, "check_", 6))
93 printf("%3d. %s\n", i, reg_funcs[i].name);
95 exit(0);
98 static void enable_disable_checks(char *s, bool enable)
100 char buf[128];
101 char *next;
102 int i;
104 do {
105 next = strchr(s, ',');
106 if (next) {
107 *next = '\0';
108 next++;
110 if (*s == '\0')
111 return;
112 if (strncmp(s, "check_", 6) == 0)
113 snprintf(buf, sizeof(buf), "%s", s);
114 else
115 snprintf(buf, sizeof(buf), "check_%s", s);
118 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
119 if (strcmp(reg_funcs[i].name, buf) == 0) {
120 reg_funcs[i].enabled = (enable == true) ? 1 : -1;
121 break;
125 if (i == ARRAY_SIZE(reg_funcs))
126 sm_fatal("'%s' not found", s);
128 } while ((s = next));
131 static void help(void)
133 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
134 printf("--project=<name> or -p=<name>: project specific tests\n");
135 printf("--succeed: don't exit with an error\n");
136 printf("--spammy: print superfluous crap.\n");
137 printf("--info: print info used to fill smatch_data/.\n");
138 printf("--debug: print lots of debug output.\n");
139 printf("--no-data: do not use the /smatch_data/ directory.\n");
140 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
141 printf("--full-path: print the full pathname.\n");
142 printf("--debug-implied: print debug output about implications.\n");
143 printf("--assume-loops: assume loops always go through at least once.\n");
144 printf("--two-passes: use a two pass system for each function.\n");
145 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
146 printf("--fatal-checks: check output is treated as an error.\n");
147 printf("--help: print this helpful message.\n");
148 exit(1);
151 static int match_option(const char *arg, const char *option)
153 char *str;
154 char *tmp;
155 int ret = 0;
157 str = malloc(strlen(option) + 3);
158 snprintf(str, strlen(option) + 3, "--%s", option);
159 tmp = str;
160 while (*tmp) {
161 if (*tmp == '_')
162 *tmp = '-';
163 tmp++;
165 if (!strcmp(arg, str))
166 ret = 1;
167 free(str);
168 return ret;
171 #define OPTION(_x) do { \
172 if (!found && match_option((*argvp)[1], #_x)) { \
173 found = 1; \
174 option_##_x = 1; \
175 (*argvp)[1] = (*argvp)[0]; \
177 } while (0)
179 void parse_args(int *argcp, char ***argvp)
181 while (*argcp >= 2) {
182 int found = 0;
183 if (!strcmp((*argvp)[1], "--help"))
184 help();
186 if (!strcmp((*argvp)[1], "--show-checks"))
187 show_checks();
189 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
190 option_project_str = (*argvp)[1] + 10;
191 (*argvp)[1] = (*argvp)[0];
192 found = 1;
194 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
195 option_project_str = (*argvp)[1] + 3;
196 (*argvp)[1] = (*argvp)[0];
197 found = 1;
199 if (!found && !strncmp((*argvp)[1], "--db-file=", 10)) {
200 option_db_file = (*argvp)[1] + 10;
201 (*argvp)[1] = (*argvp)[0];
202 found = 1;
204 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
205 option_datadir_str = (*argvp)[1] + 7;
206 (*argvp)[1] = (*argvp)[0];
207 found = 1;
209 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
210 option_debug_check = (*argvp)[1] + 8;
211 (*argvp)[1] = (*argvp)[0];
212 found = 1;
214 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
215 trace_variable = (*argvp)[1] + 8;
216 (*argvp)[1] = (*argvp)[0];
217 found = 1;
219 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
220 enable_disable_checks((*argvp)[1] + 9, 1);
221 option_enable = 1;
222 (*argvp)[1] = (*argvp)[0];
223 found = 1;
225 if (!found && strncmp((*argvp)[1], "--disable=", 10) == 0) {
226 enable_disable_checks((*argvp)[1] + 10, 0);
227 option_enable = 1;
228 option_disable = 1;
229 (*argvp)[1] = (*argvp)[0];
230 found = 1;
233 OPTION(fatal_checks);
234 OPTION(spammy);
235 OPTION(info);
236 OPTION(debug);
237 OPTION(assume_loops);
238 OPTION(no_data);
239 OPTION(two_passes);
240 OPTION(full_path);
241 OPTION(call_tree);
242 OPTION(file_output);
243 OPTION(time);
244 OPTION(mem);
245 OPTION(no_db);
246 OPTION(succeed);
247 if (!found)
248 break;
249 (*argcp)--;
250 (*argvp)++;
253 if (strcmp(option_project_str, "smatch_generic") != 0)
254 option_project = PROJ_UNKNOWN;
256 if (strcmp(option_project_str, "kernel") == 0)
257 option_project = PROJ_KERNEL;
258 else if (strcmp(option_project_str, "wine") == 0)
259 option_project = PROJ_WINE;
260 else if (strcmp(option_project_str, "illumos_kernel") == 0)
261 option_project = PROJ_ILLUMOS_KERNEL;
262 else if (strcmp(option_project_str, "illumos_user") == 0)
263 option_project = PROJ_ILLUMOS_USER;
266 static char *read_bin_filename(void)
268 char filename[PATH_MAX] = {};
269 char proc[PATH_MAX];
271 pid_t pid = getpid();
272 sprintf(proc, "/proc/%d/exe", pid);
273 if (readlink(proc, filename, PATH_MAX) < 0)
274 return NULL;
275 return alloc_string(filename);
278 static char *get_bin_dir(char *arg0)
280 char *orig;
282 orig = read_bin_filename();
283 if (!orig)
284 orig = alloc_string(arg0);
285 return dirname(orig);
288 static char *get_data_dir(char *arg0)
290 char buf[256];
291 char *dir;
293 if (option_no_data)
294 return NULL;
296 if (option_datadir_str) {
297 if (access(option_datadir_str, R_OK))
298 sm_warning("%s is not accessible -- ignored.",
299 option_datadir_str);
300 else
301 return alloc_string(option_datadir_str);
304 strncpy(buf, "smatch_data/", sizeof(buf));
305 dir = alloc_string(buf);
306 if (!access(dir, R_OK))
307 return dir;
309 strncpy(buf, bin_dir, 254);
311 buf[255] = '\0';
312 strncat(buf, "/smatch_data/", 254 - strlen(buf));
313 dir = alloc_string(buf);
314 if (!access(dir, R_OK))
315 return dir;
316 free_string(dir);
317 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
318 dir = alloc_string(buf);
319 if (!access(dir, R_OK))
320 return dir;
322 sm_warning("%s is not accessible.", dir);
323 sm_warning("Use --no-data or --data to suppress this message.");
324 return NULL;
327 int main(int argc, char **argv)
329 struct string_list *filelist = NULL;
330 int i;
331 reg_func func;
333 sm_outfd = stdout;
334 sql_outfd = stdout;
335 caller_info_fd = stdout;
337 parse_args(&argc, &argv);
339 if (argc < 2)
340 help();
342 /* this gets set back to zero when we parse the first function */
343 final_pass = 1;
345 bin_dir = get_bin_dir(argv[0]);
346 data_dir = get_data_dir(argv[0]);
348 allocate_hook_memory();
349 allocate_dynamic_states_array(num_checks);
350 allocate_tracker_array(num_checks);
351 create_function_hook_hash();
352 open_smatch_db(option_db_file);
353 sparse_initialize(argc, argv, &filelist);
354 alloc_valid_ptr_rl();
356 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
357 func = reg_funcs[i].func;
358 /* The script IDs start at 1.
359 0 is used for internal stuff. */
360 if (!option_enable || reg_funcs[i].enabled == 1 ||
361 (option_disable && reg_funcs[i].enabled != -1) ||
362 strncmp(reg_funcs[i].name, "register_", 9) == 0)
363 func(i);
366 smatch(filelist);
367 free_string(data_dir);
369 if (option_succeed)
370 return 0;
371 if (sm_nr_errors > 0)
372 return 1;
373 if (sm_nr_checks > 0 && option_fatal_checks)
374 return 1;
375 return 0;