2 * smatch/check_sizeof_pointer.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
14 static void check_sizeof(struct expression
*expr
, char *ptr_name
)
19 if (!expr
|| expr
->type
!= EXPR_SIZEOF
)
22 get_value(expr
, &val
);
24 expr
= strip_expr(expr
->cast_expression
);
25 name
= get_variable_from_expr_complex(expr
, NULL
);
29 if (strcmp(ptr_name
, name
) == 0)
30 sm_msg("warn: was 'sizeof(*%s)' intended?", ptr_name
);
34 static void match_call_assignment(struct expression
*expr
)
36 struct expression
*call
= strip_expr(expr
->right
);
37 struct expression
*arg
;
40 if (!is_pointer(expr
->left
))
43 ptr_name
= get_variable_from_expr_complex(expr
->left
, NULL
);
47 FOR_EACH_PTR(call
->args
, arg
) {
48 check_sizeof(arg
, ptr_name
);
49 } END_FOR_EACH_PTR(arg
);
52 static void check_passes_pointer(char *name
, struct expression
*call
)
54 struct expression
*arg
;
57 FOR_EACH_PTR(call
->args
, arg
) {
58 ptr_name
= get_variable_from_expr(arg
, NULL
);
61 if (strcmp(name
, ptr_name
) == 0)
62 sm_msg("warn: was 'sizeof(*%s)' intended?", name
);
63 free_string(ptr_name
);
64 } END_FOR_EACH_PTR(arg
);
67 static void match_check_params(struct expression
*call
)
69 struct expression
*arg
;
70 struct expression
*obj
;
73 FOR_EACH_PTR(call
->args
, arg
) {
74 if (arg
->type
!= EXPR_SIZEOF
)
76 obj
= strip_expr(arg
->cast_expression
);
79 name
= get_variable_from_expr(obj
, NULL
);
82 check_passes_pointer(name
, call
);
84 } END_FOR_EACH_PTR(arg
);
87 void check_sizeof_pointer(int id
)
91 add_hook(&match_call_assignment
, CALL_ASSIGNMENT_HOOK
);
92 add_hook(&match_check_params
, FUNCTION_CALL_HOOK
);