From 74741f92685879072e0432f77bc292bb92b8fb2a Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Wed, 13 Jun 2012 11:16:00 +0300 Subject: [PATCH] buf_size: calculate bytes per element better I guess when I wrote this I didn't know about ->bit_size. Alignment doesn't work at all. It gives 4 byte alignment for 64 bit numbers. Signed-off-by: Dan Carpenter --- smatch_buf_size.c | 7 ++++--- validation/sm_buf_size2.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 validation/sm_buf_size2.c diff --git a/smatch_buf_size.c b/smatch_buf_size.c index 55590fd7..8310a3bf 100644 --- a/smatch_buf_size.c +++ b/smatch_buf_size.c @@ -192,9 +192,10 @@ static int get_stored_size(struct expression *expr) if (type->type == SYM_PTR) type = get_base_type(type); - if (!type->ctype.alignment) + if (bits_to_bytes(type->bit_size) == 0) return 0; - ret = max / type->ctype.alignment; + + ret = max / bits_to_bytes(type->bit_size); return ret * cast_ratio; } @@ -266,7 +267,7 @@ int get_array_size_bytes(struct expression *expr) element_size = tmp->bit_size / 8; } else if (tmp->type == SYM_PTR) { tmp = get_base_type(tmp); - element_size = tmp->ctype.alignment; + element_size = bits_to_bytes(tmp->bit_size); } else { return 0; } diff --git a/validation/sm_buf_size2.c b/validation/sm_buf_size2.c new file mode 100644 index 00000000..1f6a7cd8 --- /dev/null +++ b/validation/sm_buf_size2.c @@ -0,0 +1,28 @@ +#include "check_debug.h" + +void *malloc(int); + +void func(void) +{ + int *a; + short *b; + long long *c; + + a = malloc(sizeof(int) * 4); + b = a; + c = b; + __smatch_buf_size(a); + __smatch_buf_size(b); + __smatch_buf_size(c); +} + +/* + * check-name: smatch buf size #2 + * check-command: smatch -I.. sm_buf_size2.c + * + * check-output-start +sm_buf_size2.c:14 func() buf size: 'a' 4 elements, 16 bytes +sm_buf_size2.c:15 func() buf size: 'b' 8 elements, 16 bytes +sm_buf_size2.c:16 func() buf size: 'c' 2 elements, 16 bytes + * check-output-end + */ -- 2.11.4.GIT