-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_buffer_too_small_for_struct.c
113 lines (94 loc) · 2.86 KB
/
check_buffer_too_small_for_struct.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (C) 2014 Oracle.
*
* 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 int my_id;
STATE(too_small);
static void match_assign(struct expression *expr)
{
struct symbol *left_type, *right_type;
struct expression *size_expr;
sval_t min_size;
int limit_type;
int bytes;
left_type = get_type(expr->left);
if (!left_type || left_type->type != SYM_PTR)
return;
left_type = get_real_base_type(left_type);
if (!left_type || left_type->type != SYM_STRUCT)
return;
right_type = get_type(expr->right);
if (!right_type || right_type->type != SYM_PTR)
return;
right_type = get_real_base_type(right_type);
if (!right_type)
return;
if (right_type != &void_ctype && type_bits(right_type) != 8)
return;
bytes = get_array_size_bytes(expr->right);
if (bytes >= type_bytes(left_type))
return;
size_expr = get_size_variable(expr->right, &limit_type);
if (!size_expr)
return;
if (limit_type != ELEM_COUNT)
return;
get_absolute_min(size_expr, &min_size);
if (min_size.value >= type_bytes(left_type))
return;
set_state_expr(my_id, expr->left, &too_small);
}
static void match_dereferences(struct expression *expr)
{
struct symbol *left_type;
struct expression *right;
struct smatch_state *state;
char *name;
struct expression *size_expr;
sval_t min_size;
int limit_type;
if (expr->type != EXPR_PREOP)
return;
expr = strip_expr(expr->unop);
state = get_state_expr(my_id, expr);
if (state != &too_small)
return;
left_type = get_type(expr);
if (!left_type || left_type->type != SYM_PTR)
return;
left_type = get_real_base_type(left_type);
if (!left_type || left_type->type != SYM_STRUCT)
return;
right = get_assigned_expr(expr);
size_expr = get_size_variable(right, &limit_type);
if (!size_expr)
return;
if (limit_type != ELEM_COUNT)
return;
get_absolute_min(size_expr, &min_size);
if (min_size.value >= type_bytes(left_type))
return;
name = expr_to_str(right);
sm_warning("is '%s' large enough for 'struct %s'? %s", name, left_type->ident ? left_type->ident->name : "<anon>", sval_to_str(min_size));
free_string(name);
set_state_expr(my_id, expr, &undefined);
}
void check_buffer_too_small_for_struct(int id)
{
my_id = id;
add_hook(&match_assign, ASSIGNMENT_HOOK);
add_hook(&match_dereferences, DEREF_HOOK);
}