2 * smatch/check_cast_assign.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
11 #include "smatch_extra.h"
12 #include "smatch_slist.h"
16 static struct symbol
*get_cast_type(struct expression
*expr
)
18 if (!expr
|| expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
20 expr
= strip_parens(expr
->unop
);
21 if (expr
->type
!= EXPR_CAST
)
23 return get_pointer_type(expr
);
26 static void match_overflow(struct expression
*expr
)
28 struct expression
*ptr
;
33 type
= get_cast_type(expr
->left
);
36 cast_size
= bits_to_bytes(type
->bit_size
);
38 ptr
= strip_expr(expr
->left
->unop
);
39 data_size
= get_array_size_bytes_min(ptr
);
42 if (data_size
>= cast_size
)
44 sm_msg("warn: potential memory corrupting cast %d vs %d bytes",
45 cast_size
, data_size
);
48 void check_cast_assign(int id
)
51 add_hook(&match_overflow
, ASSIGNMENT_HOOK
);