-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheck_strcpy_overflow.c
62 lines (53 loc) · 1.8 KB
/
check_strcpy_overflow.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Copyright (C) 2010 Dan Carpenter.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include "smatch.h"
static void match_strcpy(const char *fn, struct expression *expr, void *unused)
{
struct expression *dest;
struct expression *data;
char *dest_name = NULL;
char *data_name = NULL;
int dest_size;
int data_size;
dest = get_argument_from_call_expr(expr->args, 0);
data = get_argument_from_call_expr(expr->args, 1);
dest_size = get_array_size_bytes(dest);
if (!dest_size)
return;
data_size = get_size_from_strlen(data);
if (!data_size)
data_size = get_array_size_bytes(data);
/* If the size of both arrays is known and the destination
* buffer is larger than the source buffer, we're okay.
*/
if (data_size && dest_size >= data_size)
return;
dest_name = expr_to_str(dest);
data_name = expr_to_str(data);
if (data_size)
sm_error("%s() '%s' too large for '%s' (%d vs %d)",
fn, data_name, dest_name, data_size, dest_size);
else if (option_spammy)
sm_warning("%s() '%s' of unknown size might be too large for '%s'",
fn, data_name, dest_name);
free_string(dest_name);
free_string(data_name);
}
void check_strcpy_overflow(int id)
{
add_function_hook("strcpy", &match_strcpy, NULL);
}