1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2012 Oracle.
3*1f5207b7SJohn Levon  *
4*1f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
5*1f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
6*1f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
7*1f5207b7SJohn Levon  * of the License, or (at your option) any later version.
8*1f5207b7SJohn Levon  *
9*1f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
10*1f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*1f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*1f5207b7SJohn Levon  * GNU General Public License for more details.
13*1f5207b7SJohn Levon  *
14*1f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
15*1f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16*1f5207b7SJohn Levon  */
17*1f5207b7SJohn Levon 
18*1f5207b7SJohn Levon #include "smatch.h"
19*1f5207b7SJohn Levon 
20*1f5207b7SJohn Levon static int my_id;
21*1f5207b7SJohn Levon 
check_pointer(struct expression * expr,char * ptr_name)22*1f5207b7SJohn Levon static void check_pointer(struct expression *expr, char *ptr_name)
23*1f5207b7SJohn Levon {
24*1f5207b7SJohn Levon 	char *name;
25*1f5207b7SJohn Levon 	sval_t sval;
26*1f5207b7SJohn Levon 
27*1f5207b7SJohn Levon 	if (!expr || expr->type != EXPR_SIZEOF)
28*1f5207b7SJohn Levon 		return;
29*1f5207b7SJohn Levon 
30*1f5207b7SJohn Levon 	get_value(expr, &sval);
31*1f5207b7SJohn Levon 
32*1f5207b7SJohn Levon 	expr = strip_expr(expr->cast_expression);
33*1f5207b7SJohn Levon 	name = expr_to_str(expr);
34*1f5207b7SJohn Levon 	if (!name)
35*1f5207b7SJohn Levon 		return;
36*1f5207b7SJohn Levon 
37*1f5207b7SJohn Levon 	if (strcmp(ptr_name, name) == 0)
38*1f5207b7SJohn Levon 		sm_warning("was 'sizeof(*%s)' intended?", ptr_name);
39*1f5207b7SJohn Levon 	free_string(name);
40*1f5207b7SJohn Levon }
41*1f5207b7SJohn Levon 
match_call_assignment(struct expression * expr)42*1f5207b7SJohn Levon static void match_call_assignment(struct expression *expr)
43*1f5207b7SJohn Levon {
44*1f5207b7SJohn Levon 	struct expression *call = strip_expr(expr->right);
45*1f5207b7SJohn Levon 	struct expression *arg;
46*1f5207b7SJohn Levon 	char *ptr_name;
47*1f5207b7SJohn Levon 
48*1f5207b7SJohn Levon 	if (!is_pointer(expr->left))
49*1f5207b7SJohn Levon 		return;
50*1f5207b7SJohn Levon 
51*1f5207b7SJohn Levon 	ptr_name = expr_to_str(expr->left);
52*1f5207b7SJohn Levon 	if (!ptr_name)
53*1f5207b7SJohn Levon 		return;
54*1f5207b7SJohn Levon 
55*1f5207b7SJohn Levon 	FOR_EACH_PTR(call->args, arg) {
56*1f5207b7SJohn Levon 		check_pointer(arg, ptr_name);
57*1f5207b7SJohn Levon 	} END_FOR_EACH_PTR(arg);
58*1f5207b7SJohn Levon 
59*1f5207b7SJohn Levon 	free_string(ptr_name);
60*1f5207b7SJohn Levon }
61*1f5207b7SJohn Levon 
check_passes_pointer(char * name,struct expression * call)62*1f5207b7SJohn Levon static void check_passes_pointer(char *name, struct expression *call)
63*1f5207b7SJohn Levon {
64*1f5207b7SJohn Levon 	struct expression *arg;
65*1f5207b7SJohn Levon 	char *ptr_name;
66*1f5207b7SJohn Levon 
67*1f5207b7SJohn Levon 	FOR_EACH_PTR(call->args, arg) {
68*1f5207b7SJohn Levon 		ptr_name = expr_to_var(arg);
69*1f5207b7SJohn Levon 		if (!ptr_name)
70*1f5207b7SJohn Levon 			continue;
71*1f5207b7SJohn Levon 		if (strcmp(name, ptr_name) == 0)
72*1f5207b7SJohn Levon 			sm_warning("was 'sizeof(*%s)' intended?", name);
73*1f5207b7SJohn Levon 		free_string(ptr_name);
74*1f5207b7SJohn Levon 	} END_FOR_EACH_PTR(arg);
75*1f5207b7SJohn Levon }
76*1f5207b7SJohn Levon 
match_check_params(struct expression * call)77*1f5207b7SJohn Levon static void match_check_params(struct expression *call)
78*1f5207b7SJohn Levon {
79*1f5207b7SJohn Levon 	struct expression *arg;
80*1f5207b7SJohn Levon 	struct expression *obj;
81*1f5207b7SJohn Levon 	char *name;
82*1f5207b7SJohn Levon 
83*1f5207b7SJohn Levon 	FOR_EACH_PTR(call->args, arg) {
84*1f5207b7SJohn Levon 		if (arg->type != EXPR_SIZEOF)
85*1f5207b7SJohn Levon 			continue;
86*1f5207b7SJohn Levon 		obj = strip_expr(arg->cast_expression);
87*1f5207b7SJohn Levon 		if (!is_pointer(obj))
88*1f5207b7SJohn Levon 			continue;
89*1f5207b7SJohn Levon 		name = expr_to_var(obj);
90*1f5207b7SJohn Levon 		if (!name)
91*1f5207b7SJohn Levon 			continue;
92*1f5207b7SJohn Levon 		check_passes_pointer(name, call);
93*1f5207b7SJohn Levon 		free_string(name);
94*1f5207b7SJohn Levon 	} END_FOR_EACH_PTR(arg);
95*1f5207b7SJohn Levon }
96*1f5207b7SJohn Levon 
97*1f5207b7SJohn Levon static struct string_list *macro_takes_sizeof_argument;
check_sizeof_number(struct expression * expr)98*1f5207b7SJohn Levon static void check_sizeof_number(struct expression *expr)
99*1f5207b7SJohn Levon {
100*1f5207b7SJohn Levon 	char *macro, *tmp;
101*1f5207b7SJohn Levon 
102*1f5207b7SJohn Levon 	if (expr->type != EXPR_VALUE)
103*1f5207b7SJohn Levon 		return;
104*1f5207b7SJohn Levon 	macro = get_macro_name(expr->pos);
105*1f5207b7SJohn Levon 	FOR_EACH_PTR(macro_takes_sizeof_argument, tmp) {
106*1f5207b7SJohn Levon 		if (macro && strcmp(tmp, macro) == 0)
107*1f5207b7SJohn Levon 			return;
108*1f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
109*1f5207b7SJohn Levon 
110*1f5207b7SJohn Levon 	sm_warning("sizeof(NUMBER)?");
111*1f5207b7SJohn Levon }
112*1f5207b7SJohn Levon 
match_sizeof(struct expression * expr)113*1f5207b7SJohn Levon static void match_sizeof(struct expression *expr)
114*1f5207b7SJohn Levon {
115*1f5207b7SJohn Levon 	check_sizeof_number(expr);
116*1f5207b7SJohn Levon 	if (expr->type == EXPR_PREOP && expr->op == '&')
117*1f5207b7SJohn Levon 		sm_warning("sizeof(&pointer)?");
118*1f5207b7SJohn Levon 	if (expr->type == EXPR_SIZEOF)
119*1f5207b7SJohn Levon 		sm_warning("sizeof(sizeof())?");
120*1f5207b7SJohn Levon 	/* the ilog2() macro is a valid place to check the size of a binop */
121*1f5207b7SJohn Levon 	if (expr->type == EXPR_BINOP && !get_macro_name(expr->pos))
122*1f5207b7SJohn Levon 		sm_warning("taking sizeof binop");
123*1f5207b7SJohn Levon }
124*1f5207b7SJohn Levon 
register_macro_takes_sizeof_argument(void)125*1f5207b7SJohn Levon static void register_macro_takes_sizeof_argument(void)
126*1f5207b7SJohn Levon {
127*1f5207b7SJohn Levon 	struct token *token;
128*1f5207b7SJohn Levon 	char *macro;
129*1f5207b7SJohn Levon 	char name[256];
130*1f5207b7SJohn Levon 
131*1f5207b7SJohn Levon 	snprintf(name, 256, "%s.macro_takes_sizeof_argument", option_project_str);
132*1f5207b7SJohn Levon 
133*1f5207b7SJohn Levon 	token = get_tokens_file(name);
134*1f5207b7SJohn Levon 	if (!token)
135*1f5207b7SJohn Levon 		return;
136*1f5207b7SJohn Levon 	if (token_type(token) != TOKEN_STREAMBEGIN)
137*1f5207b7SJohn Levon 		return;
138*1f5207b7SJohn Levon 	token = token->next;
139*1f5207b7SJohn Levon 	while (token_type(token) != TOKEN_STREAMEND) {
140*1f5207b7SJohn Levon 		if (token_type(token) != TOKEN_IDENT)
141*1f5207b7SJohn Levon 			return;
142*1f5207b7SJohn Levon 		macro = alloc_string(show_ident(token->ident));
143*1f5207b7SJohn Levon 		add_ptr_list(&macro_takes_sizeof_argument, macro);
144*1f5207b7SJohn Levon 		token = token->next;
145*1f5207b7SJohn Levon 	}
146*1f5207b7SJohn Levon 	clear_token_alloc();
147*1f5207b7SJohn Levon }
148*1f5207b7SJohn Levon 
check_sizeof(int id)149*1f5207b7SJohn Levon void check_sizeof(int id)
150*1f5207b7SJohn Levon {
151*1f5207b7SJohn Levon 	my_id = id;
152*1f5207b7SJohn Levon 
153*1f5207b7SJohn Levon 	register_macro_takes_sizeof_argument();
154*1f5207b7SJohn Levon 	add_hook(&match_call_assignment, CALL_ASSIGNMENT_HOOK);
155*1f5207b7SJohn Levon 	add_hook(&match_check_params, FUNCTION_CALL_HOOK);
156*1f5207b7SJohn Levon 	add_hook(&match_sizeof, SIZEOF_HOOK);
157*1f5207b7SJohn Levon }
158