flow: hide a bunch of the unreachable warnings under the --spammy flag
[smatch.git] / check_cast_assign.c
blobeea986696952a15ce8b4b8a0f5a097251cd93131
1 /*
2 * smatch/check_cast_assign.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
11 #include "smatch_extra.h"
12 #include "smatch_slist.h"
14 static int my_id;
16 static struct symbol *get_cast_type(struct expression *expr)
18 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
19 return NULL;
20 expr = strip_parens(expr->unop);
21 if (expr->type != EXPR_CAST)
22 return NULL;
23 return get_pointer_type(expr);
26 static void match_overflow(struct expression *expr)
28 struct expression *ptr;
29 struct symbol *type;
30 int cast_size;
31 int data_size;
33 type = get_cast_type(expr->left);
34 if (!type)
35 return;
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);
40 if (data_size <= 0)
41 return;
42 if (data_size >= cast_size)
43 return;
44 sm_msg("warn: potential memory corrupting cast %d vs %d bytes",
45 cast_size, data_size);
48 void check_cast_assign(int id)
50 my_id = id;
51 add_hook(&match_overflow, ASSIGNMENT_HOOK);