From 5604845a144d1448c9304d5be7c1bd26f24bae8b Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Wed, 13 Jun 2012 16:17:44 +0300 Subject: [PATCH] buf_size: fix bytes_per_element() for strings and arrays For string constants the call to get_type() returns NULL so that has to be handled specially. For arrays, it is still wrong to use ->ctype.alignment. We could be handling them the same as SYM_POINTER. Signed-off-by: Dan Carpenter --- smatch_buf_size.c | 16 +++++++--------- validation/sm_buf_size4.c | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 validation/sm_buf_size4.c diff --git a/smatch_buf_size.c b/smatch_buf_size.c index 5e7a66db..9550dc4e 100644 --- a/smatch_buf_size.c +++ b/smatch_buf_size.c @@ -60,20 +60,18 @@ static int bytes_per_element(struct expression *expr) struct symbol *type; int bpe; + if (expr->type == EXPR_STRING) + return 1; type = get_type(expr); if (!type) return 0; - if (type->type == SYM_PTR) { - type = get_base_type(type); - bpe = bits_to_bytes(type->bit_size); - } else if (type->type == SYM_ARRAY) { - bpe = type->ctype.alignment; - } else { - return 0; - } - if (bpe == 0) + if (type->type != SYM_PTR && type->type != SYM_ARRAY) return 0; + + type = get_base_type(type); + bpe = bits_to_bytes(type->bit_size); + if (bpe == -1) /* void pointer */ bpe = 1; diff --git a/validation/sm_buf_size4.c b/validation/sm_buf_size4.c new file mode 100644 index 00000000..0df4914d --- /dev/null +++ b/validation/sm_buf_size4.c @@ -0,0 +1,24 @@ +#include "check_debug.h" + +long long a[] = {1, 2}; +int b[] = {3, 4}; + +int main(char *arg0) +{ + short *s = a; + + __smatch_buf_size(a); + __smatch_buf_size(b); + __smatch_buf_size(s); + return 0; +} +/* + * check-name: smatch buf size #4 + * check-command: smatch -I.. sm_buf_size4.c + * + * check-output-start +sm_buf_size4.c:10 main() buf size: 'a' 2 elements, 16 bytes +sm_buf_size4.c:11 main() buf size: 'b' 2 elements, 8 bytes +sm_buf_size4.c:12 main() buf size: 's' 8 elements, 16 bytes + * check-output-end + */ -- 2.11.4.GIT