overflow: get rid of "buffer overflow calling %s. param %d." messages
[smatch.git] / smatch.c
blob8ee5e56727597e9d74c9b3c41dde807a6cb7ea93
2 /*
3 * sparse/smatch.c
5 * Copyright (C) 2006 Dan Carpenter.
7 * Licensed under the Open Software License version 1.1
9 */
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <libgen.h>
14 #include "smatch.h"
15 #include "check_list.h"
17 char *option_project_str = (char *)"";
18 enum project_type option_project = PROJ_NONE;
19 char *data_dir;
20 int option_no_data = 0;
21 int option_spammy = 0;
22 int option_info = 0;
23 int option_full_path = 0;
24 int option_param_mapper = 0;
25 int option_call_tree = 0;
26 int option_no_db = 0;
27 char *option_datadir_str;
29 typedef void (*reg_func) (int id);
30 #define CK(_x) {.name = #_x, .func = &_x},
31 static struct reg_func_info {
32 const char *name;
33 reg_func func;
34 } reg_funcs[] = {
35 #include "check_list.h"
37 #undef CK
38 int num_checks = ARRAY_SIZE(reg_funcs);
40 const char *check_name(unsigned short id)
42 if (id > ARRAY_SIZE(reg_funcs)) {
43 return "internal";
45 return reg_funcs[id - 1].name;
48 int id_from_name(const char *name)
50 int i;
52 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
53 if (!strcmp(name, reg_funcs[i].name))
54 return i + 1;
56 return 0;
59 static void help(void)
61 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
62 printf("--project=<name> or -p=<name>: project specific tests\n");
63 printf("--spammy: print superfluous crap.\n");
64 printf("--info: print info used to fill smatch_data/.\n");
65 printf("--debug: print lots of debug output.\n");
66 printf("--param-mapper: enable param_mapper output.\n");
67 printf("--no-data: do not use the /smatch_data/ directory.\n");
68 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
69 printf("--full-path: print the full pathname.\n");
70 printf("--debug-implied: print debug output about implications.\n");
71 printf("--no-implied: ignore implications.\n");
72 printf("--assume-loops: assume loops always go through at least once.\n");
73 printf("--known-conditions: don't branch for known conditions.\n");
74 printf("--two-passes: use a two pass system for each function.\n");
75 printf("--help: print this helpfull message.\n");
76 exit(1);
79 static int match_option(const char *arg, const char *option)
81 char *str;
82 char *tmp;
83 int ret = 0;
85 str = malloc(strlen(option) + 3);
86 snprintf(str, strlen(option) + 3, "--%s", option);
87 tmp = str;
88 while (*tmp) {
89 if (*tmp == '_')
90 *tmp = '-';
91 tmp++;
93 if (!strcmp(arg, str))
94 ret = 1;
95 free(str);
96 return ret;
99 #define OPTION(_x) do { \
100 if (!found && match_option((*argvp)[1], #_x)) { \
101 found = 1; \
102 option_##_x = 1; \
103 (*argvp)[1] = (*argvp)[0]; \
105 } while (0)
107 void parse_args(int *argcp, char ***argvp)
109 while(*argcp >= 2) {
110 int found = 0;
111 if (!strcmp((*argvp)[1], "--help")) {
112 help();
114 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
115 option_project_str = (*argvp)[1] + 10;
116 (*argvp)[1] = (*argvp)[0];
117 found = 1;
119 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
120 option_project_str = (*argvp)[1] + 3;
121 (*argvp)[1] = (*argvp)[0];
122 found = 1;
124 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
125 option_datadir_str = (*argvp)[1] + 7;
126 (*argvp)[1] = (*argvp)[0];
127 found = 1;
130 OPTION(spammy);
131 OPTION(info);
132 OPTION(debug);
133 OPTION(debug_implied);
134 OPTION(no_implied);
135 OPTION(assume_loops);
136 OPTION(known_conditions);
137 OPTION(no_data);
138 OPTION(two_passes);
139 OPTION(full_path);
140 OPTION(param_mapper);
141 OPTION(call_tree);
142 if (!found)
143 break;
144 (*argcp)--;
145 (*argvp)++;
148 if (!strcmp(option_project_str, "kernel"))
149 option_project = PROJ_KERNEL;
150 if (!strcmp(option_project_str, "wine"))
151 option_project = PROJ_WINE;
154 static char *get_data_dir(char *arg0)
156 char *bin_dir;
157 char *orig;
158 char buf[256];
159 char *dir;
161 if (option_no_data) {
162 return NULL;
165 if (option_datadir_str) {
166 if (access(option_datadir_str, R_OK))
167 printf("Warning: %s is not accessible -- ignore.\n",
168 option_datadir_str);
169 else
170 return alloc_string(option_datadir_str);
173 orig = alloc_string(arg0);
174 bin_dir = dirname(orig);
175 strncpy(buf, bin_dir, 254);
176 free_string(orig);
178 buf[255] = '\0';
179 strncat(buf, "/smatch_data/", 254 - strlen(buf));
180 dir = alloc_string(buf);
181 if (!access(dir, R_OK))
182 return dir;
183 free_string(dir);
184 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
185 dir = alloc_string(buf);
186 if (!access(dir, R_OK))
187 return dir;
189 printf("Warning: %s is not accessible.\n", dir);
190 printf("Use --no-data or --data to suppress this message.\n");
191 return NULL;
194 int main(int argc, char **argv)
196 int i;
197 reg_func func;
199 parse_args(&argc, &argv);
201 data_dir = get_data_dir(argv[0]);
203 create_function_hook_hash();
204 open_smatch_db();
205 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
206 func = reg_funcs[i].func;
207 /* The script IDs start at 1.
208 0 is used for internal stuff. */
209 func(i + 1);
211 register_function_hooks(-1);
213 smatch(argc, argv);
214 free_string(data_dir);
215 return 0;