xref: /illumos-gate/usr/src/tools/smatch/src/smatch_annotate.c (revision cd61ae21816e53b94bc1673f3f1aa651fc3115e8)
1 /*
2  * Copyright (C) 2013 Oracle.
3  *
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.
8  *
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.
13  *
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
16  */
17 
18 /*
19  * A place to add function annotations for common functions.
20  *
21  */
22 
23 #include "smatch.h"
24 #include "smatch_extra.h"
25 
26 static int param_caps_return(struct expression *call, void *_arg, struct range_list **res)
27 {
28 	int arg = PTR_INT(_arg);
29 	struct expression *expr;
30 	struct range_list *rl;
31 
32 	expr = get_argument_from_call_expr(call->args, arg);
33 	if (get_implied_rl(expr, &rl) && rl_max(rl).value != 0)
34 		*res = alloc_rl(sval_type_val(rl_type(rl), 0), rl_max(rl));
35 	return 1;
36 }
37 
38 void register_annotate(int id)
39 {
40 	/*
41 	 * Technically snprintf() returns the number of bytes which *would* have
42 	 * been printed.  I do try caclulating that in check_snprintf().  But
43 	 * it probably works better to assume the limitter is accurate.
44 	 */
45 	add_implied_return_hook("snprintf", &param_caps_return, INT_PTR(1));
46 
47 }
48