From a6524ba1ac8cecd67529cf88570edbc245cf65a8 Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Fri, 10 Feb 2017 16:44:43 +0300 Subject: [PATCH] buf_size: fix type_bytes() Ugh... I'm not totally sure this is correct. It seems to work but I can't swear that I haven't missed something. Anyway, this patch fixes commit 1ce2e2a68fe9 ("type: fix type_bits() for arrays. The problem is if you pass an array to a function then you want to say that the array is a pointer in the valid range. But if you want to get the size of the array then maybe you want the whole size... I've introduced a array_size() to give you that. Signed-off-by: Dan Carpenter --- smatch.h | 1 + smatch_buf_size.c | 4 ++-- smatch_type.c | 13 ++++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/smatch.h b/smatch.h index a82fe1e6..b56f74cf 100644 --- a/smatch.h +++ b/smatch.h @@ -352,6 +352,7 @@ int pop_int(struct int_stack **stack); /* smatch_type.c */ struct symbol *get_real_base_type(struct symbol *sym); int type_bytes(struct symbol *type); +int array_bytes(struct symbol *type); struct symbol *get_pointer_type(struct expression *expr); struct symbol *get_type(struct expression *expr); int type_signed(struct symbol *base_type); diff --git a/smatch_buf_size.c b/smatch_buf_size.c index 274f7290..a0e635bd 100644 --- a/smatch_buf_size.c +++ b/smatch_buf_size.c @@ -314,7 +314,7 @@ static int get_bytes_from_address(struct expression *expr) if (type->type == SYM_PTR) type = get_base_type(type); - ret = type_bytes(type); + ret = array_bytes(type); if (ret == 1) return 0; /* ignore char pointers */ @@ -427,7 +427,7 @@ static int get_stored_size_end_struct_bytes(struct expression *expr) if (!estate_to_size(state)) return 0; - return estate_to_size(state) - type_bytes(base_sym) + type_bytes(get_type(expr)); + return estate_to_size(state) - type_bytes(base_sym) + array_bytes(get_type(expr)); } static struct range_list *alloc_int_rl(int value) diff --git a/smatch_type.c b/smatch_type.c index 5417bf02..7da17b8b 100644 --- a/smatch_type.c +++ b/smatch_type.c @@ -39,13 +39,24 @@ struct symbol *get_real_base_type(struct symbol *sym) int type_bytes(struct symbol *type) { - int bits = type_bits(type); + int bits; + if (type && type->type == SYM_ARRAY) + return array_bytes(type); + + bits = type_bits(type); if (bits < 0) return 0; return bits_to_bytes(bits); } +int array_bytes(struct symbol *type) +{ + if (!type || type->type != SYM_ARRAY) + return 0; + return bits_to_bytes(type->bit_size); +} + static struct symbol *get_binop_type(struct expression *expr) { struct symbol *left, *right; -- 2.11.4.GIT