Fix negate bug. (Dereferencing undefined false positive)
[smatch.git] / smatch.c
blob414308b998e1a74215293512193e6987351fb78e
1 /*
2 * sparse/smatch.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <libgen.h>
13 #include "smatch.h"
15 char *option_project_str = (char *)"";
16 enum project_type option_project = PROJ_NONE;
17 char *data_dir;
18 int option_no_data = 0;
19 int option_spammy = 0;
20 int option_full_path = 0;
22 typedef void (*reg_func) (int id);
23 void register_smatch_extra(int id);
24 void register_smatch_ignore(int id);
25 void register_implications(int id);
26 void register_function_hooks(int id);
27 void register_modification_hooks(int id);
28 void register_containers(int id);
29 void check_debug(int id);
30 void check_assigned_expr(int id);
31 void check_null_deref(int id);
32 void check_overflow(int id);
33 void check_locking(int id);
34 void check_memory(int id);
35 void check_frees_argument(int id);
36 void check_puts_argument(int id);
37 void check_leaks(int id);
38 void check_type(int id);
39 void check_allocation_funcs(int id);
40 void check_err_ptr(int id);
41 void check_err_ptr_deref(int id);
42 void check_balanced(int id);
43 void check_deref_check(int id);
44 void check_hold_dev(int id);
45 void check_redundant_null_check(int id);
46 /* <- your test goes here */
48 /* may as well put wine scripts all together */
49 void check_wine(int id);
50 void check_wine_filehandles(int id);
51 void check_wine_WtoA(int id);
52 void check_wine_locking(int id);
54 /* void register_template(int id); */
56 static const reg_func reg_funcs[] = {
57 &register_smatch_extra, /* smatch_extra always has to be first */
58 &register_smatch_ignore,
59 &check_debug,
60 &check_assigned_expr,
62 &check_null_deref,
63 &check_overflow,
64 &check_memory,
65 &check_type,
66 &check_allocation_funcs,
67 &check_leaks,
68 &check_frees_argument,
69 &check_balanced,
70 &check_deref_check,
71 &check_redundant_null_check,
73 /* <- your test goes here */
74 /* &register_template, */
76 /* kernel specific */
77 &check_locking,
78 &check_puts_argument,
79 &check_err_ptr,
80 &check_err_ptr_deref,
81 &check_hold_dev,
83 /* wine specific stuff */
84 &check_wine,
85 &check_wine_filehandles,
86 &check_wine_WtoA,
87 &check_wine_locking,
89 &register_modification_hooks,
90 &register_containers,
91 &register_implications, /* implications always has to be last */
92 NULL
95 struct smatch_state *default_state[sizeof(reg_funcs)/sizeof(reg_func)];
97 static void help(void)
99 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
100 printf("--project=<name> or -p=<name>: project specific tests\n");
101 printf("--spammy: print superfluous crap.\n");
102 printf("--debug: print lots of debug output.\n");
103 printf("--no-data: do not use the /smatch_data/ directory.\n");
104 printf("--full-path: print the full pathname.\n");
105 printf("--debug-implied: print debug output about implications.\n");
106 printf("--oom <num>: number of mB memory to use before giving up.\n");
107 printf("--no-implied: ignore implications.\n");
108 printf("--assume-loops: assume loops always go through at least once.\n");
109 printf("--known-conditions: don't branch for known conditions.\n");
110 printf("--two-passes: use a two pass system for each function.\n");
111 printf("--help: print this helpfull message.\n");
112 exit(1);
115 void parse_args(int *argcp, char ***argvp)
117 while(*argcp >= 2) {
118 if (!strncmp((*argvp)[1], "--project=", 10)) {
119 option_project_str = (*argvp)[1] + 10;
120 (*argvp)[1] = (*argvp)[0];
121 } else if (!strncmp((*argvp)[1], "-p=", 3)) {
122 option_project_str = (*argvp)[1] + 3;
123 (*argvp)[1] = (*argvp)[0];
124 } else if (!strcmp((*argvp)[1], "--debug")) {
125 debug_states = 1;
126 (*argvp)[1] = (*argvp)[0];
127 } else if (!strcmp((*argvp)[1], "--debug-implied")) {
128 debug_implied_states = 1;
129 (*argvp)[1] = (*argvp)[0];
130 } else if (!strcmp((*argvp)[1], "--oom")) {
131 option_oom_kb = atoi((*argvp)[2]) * 1000;
132 (*argvp)[2] = (*argvp)[0];
133 (*argcp)--;
134 (*argvp)++;
135 } else if (!strcmp((*argvp)[1], "--no-implied")) {
136 option_no_implied = 1;
137 (*argvp)[1] = (*argvp)[0];
138 } else if (!strcmp((*argvp)[1], "--assume-loops")) {
139 option_assume_loops = 1;
140 (*argvp)[1] = (*argvp)[0];
141 } else if (!strcmp((*argvp)[1], "--known-conditions")) {
142 option_known_conditions = 1;
143 (*argvp)[1] = (*argvp)[0];
144 } else if (!strcmp((*argvp)[1], "--no-data")) {
145 option_no_data = 1;
146 (*argvp)[1] = (*argvp)[0];
147 } else if (!strcmp((*argvp)[1], "--spammy")) {
148 option_spammy = 1;
149 (*argvp)[1] = (*argvp)[0];
150 } else if (!strcmp((*argvp)[1], "--two-passes")) {
151 option_two_passes = 1;
152 (*argvp)[1] = (*argvp)[0];
153 } else if (!strcmp((*argvp)[1], "--full-path")) {
154 option_full_path = 1;
155 (*argvp)[1] = (*argvp)[0];
156 } else if (!strcmp((*argvp)[1], "--help")) {
157 help();
158 } else {
159 break;
161 (*argcp)--;
162 (*argvp)++;
165 if (!strcmp(option_project_str, "kernel"))
166 option_project = PROJ_KERNEL;
167 if (!strcmp(option_project_str, "wine"))
168 option_project = PROJ_WINE;
171 static char *get_data_dir(char *arg0)
173 char *bin_dir;
174 char buf[256];
175 char *dir;
177 if (option_no_data) {
178 return NULL;
180 bin_dir = dirname(alloc_string(arg0));
181 strncpy(buf, bin_dir, 254);
182 buf[255] = '\0';
183 strncat(buf, "/smatch_data/", 254 - strlen(buf));
184 dir = alloc_string(buf);
185 if (!access(dir, R_OK))
186 return dir;
187 printf("Warning: %s is not accessible.\n", dir);
188 printf("Use --no-data to suppress this message.\n");
189 return NULL;
192 int main(int argc, char **argv)
194 int i;
195 reg_func func;
197 parse_args(&argc, &argv);
199 data_dir = get_data_dir(argv[0]);
201 /* The script IDs start at 1.
202 0 is used for internal stuff. */
203 create_function_hash();
204 for(i = 0; (func = reg_funcs[i]); i++){
205 func(i + 1);
207 register_function_hooks(-1);
209 smatch(argc, argv);
210 free_string(data_dir);
211 return 0;