1 /*
2  * Copyright (C) 2009 Dan Carpenter.
3  *
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.
8  *
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.
13  *
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
16  */
17 
18 /*
19  * Idea from Michael Stefaniuc and Vincent Béron's earlier WtoA
20  * check.
21  *
22  * Apparently when you are coding WINE, you are not allowed to call
23  * functions that end in capital 'A' from functions that end in
24  * capital 'W'
25  *
26  */
27 
28 #include "smatch.h"
29 
30 static int my_id;
31 
32 static int in_w = 0;
33 
match_function_def(struct symbol * sym)34 static void match_function_def(struct symbol *sym)
35 {
36 	char *func = get_function();
37 	int len;
38 
39 	if (!func) {
40 		in_w = 0;
41 		return;
42 	}
43 	len = strlen(func);
44 	if (func[len - 1] == 'W' && len > 2 && func[len - 2] != 'A' )
45 		in_w = 1;
46 	else
47 		in_w = 0;
48 }
49 
allowed_func(const char * fn)50 static int allowed_func(const char *fn)
51 {
52 	if (!strcmp("lstrcatA", fn))
53 		return 1;
54 	if (!strcmp("lstrcpyA", fn))
55 		return 1;
56 	if (!strcmp("lstrcpynA", fn))
57 		return 1;
58 	if (!strcmp("lstrlenA", fn))
59 		return 1;
60 	return 0;
61 }
62 
match_call(struct expression * expr)63 static void match_call(struct expression *expr)
64 {
65 	char *fn_name;
66 	int len;
67 
68 	if (!in_w)
69 		return;
70 
71 	fn_name = expr_to_var(expr->fn);
72 	if (!fn_name)
73 		goto free;
74 	len = strlen(fn_name);
75 	if (fn_name[len - 1] == 'A' && !allowed_func(fn_name)) {
76 		sm_warning("WtoA call '%s()'", fn_name);
77 	}
78 free:
79 	free_string(fn_name);
80 }
81 
check_wine_WtoA(int id)82 void check_wine_WtoA(int id)
83 {
84 	if (option_project != PROJ_WINE)
85 		return;
86 
87 	my_id = id;
88 	add_hook(&match_function_def, FUNC_DEF_HOOK);
89 	add_hook(&match_call, FUNCTION_CALL_HOOK);
90 }
91