2 * Copyright (C) 2010 Dan Carpenter.
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.
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.
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
20 static void match_snprintf(const char *fn
, struct expression
*expr
, void *unused
)
22 struct expression
*dest
;
23 struct expression
*dest_size_expr
;
24 struct expression
*format_string
;
25 struct expression
*data
;
26 char *data_name
= NULL
;
32 dest
= get_argument_from_call_expr(expr
->args
, 0);
33 dest_size_expr
= get_argument_from_call_expr(expr
->args
, 1);
34 format_string
= get_argument_from_call_expr(expr
->args
, 2);
35 data
= get_argument_from_call_expr(expr
->args
, 3);
37 dest_size
= get_array_size_bytes(dest
);
38 if (!get_implied_value(dest_size_expr
, &limit_size
))
40 if (dest_size
&& dest_size
< limit_size
.value
)
41 sm_msg("error: snprintf() is printing too much %s vs %d",
42 sval_to_str(limit_size
), dest_size
);
43 format
= expr_to_var(format_string
);
46 if (strcmp(format
, "\"%s\""))
48 data_name
= expr_to_str(data
);
49 data_size
= get_size_from_strlen(data
);
51 data_size
= get_array_size_bytes(data
);
52 if (limit_size
.value
< data_size
)
53 sm_msg("error: snprintf() chops off the last chars of '%s': %d vs %s",
54 data_name
, data_size
, sval_to_str(limit_size
));
56 free_string(data_name
);
60 void check_snprintf_overflow(int id
)
62 add_function_hook("snprintf", &match_snprintf
, NULL
);