11f5207b7SJohn Levon /*
21f5207b7SJohn Levon * Copyright (C) 2011 Dan Carpenter.
31f5207b7SJohn Levon *
41f5207b7SJohn Levon * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon * of the License, or (at your option) any later version.
81f5207b7SJohn Levon *
91f5207b7SJohn Levon * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
121f5207b7SJohn Levon * GNU General Public License for more details.
131f5207b7SJohn Levon *
141f5207b7SJohn Levon * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207b7SJohn Levon */
171f5207b7SJohn Levon
181f5207b7SJohn Levon #include "smatch.h"
191f5207b7SJohn Levon
201f5207b7SJohn Levon static int my_id;
211f5207b7SJohn Levon
check_size_not_zero(struct expression * expr)22*c85f09ccSJohn Levon static void check_size_not_zero(struct expression *expr)
231f5207b7SJohn Levon {
241f5207b7SJohn Levon sval_t sval;
251f5207b7SJohn Levon
26*c85f09ccSJohn Levon if (expr->type != EXPR_VALUE)
271f5207b7SJohn Levon return;
28*c85f09ccSJohn Levon if (!get_value(expr, &sval))
291f5207b7SJohn Levon return;
301f5207b7SJohn Levon if (sval.value != 0)
311f5207b7SJohn Levon return;
321f5207b7SJohn Levon sm_error("calling memset(x, y, 0);");
331f5207b7SJohn Levon }
341f5207b7SJohn Levon
check_size_not_ARRAY_SIZE(struct expression * expr)35*c85f09ccSJohn Levon static void check_size_not_ARRAY_SIZE(struct expression *expr)
36*c85f09ccSJohn Levon {
37*c85f09ccSJohn Levon char *name;
38*c85f09ccSJohn Levon
39*c85f09ccSJohn Levon name = get_macro_name(expr->pos);
40*c85f09ccSJohn Levon if (name && strcmp(name, "ARRAY_SIZE") == 0)
41*c85f09ccSJohn Levon sm_warning("calling memset(x, y, ARRAY_SIZE());");
42*c85f09ccSJohn Levon }
43*c85f09ccSJohn Levon
match_memset(const char * fn,struct expression * expr,void * data)44*c85f09ccSJohn Levon static void match_memset(const char *fn, struct expression *expr, void *data)
45*c85f09ccSJohn Levon {
46*c85f09ccSJohn Levon struct expression *arg_expr;
47*c85f09ccSJohn Levon
48*c85f09ccSJohn Levon arg_expr = get_argument_from_call_expr(expr->args, 2);
49*c85f09ccSJohn Levon if (!arg_expr)
50*c85f09ccSJohn Levon return;
51*c85f09ccSJohn Levon check_size_not_zero(arg_expr);
52*c85f09ccSJohn Levon check_size_not_ARRAY_SIZE(arg_expr);
53*c85f09ccSJohn Levon }
54*c85f09ccSJohn Levon
check_memset(int id)551f5207b7SJohn Levon void check_memset(int id)
561f5207b7SJohn Levon {
571f5207b7SJohn Levon my_id = id;
581f5207b7SJohn Levon add_function_hook("memset", &match_memset, NULL);
591f5207b7SJohn Levon add_function_hook("__builtin_memset", &match_memset, NULL);
601f5207b7SJohn Levon }
61