From 47444266e917bcc873e671b97874ebf73dcea512 Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Wed, 17 May 2017 10:30:05 +0300 Subject: [PATCH] return_to_param: long to short should never return a longer version This is supposed to be looking for assignments like: struct foo *p = some->very->long_variable; Because we refer to p->xxx instead of some->very->long_variable->xxx in the code. But sometimes we do things like: p = alloc(); some->very->long_variable = p; We'll still refer to p->xxx in the function, so we don't want to return the long version of that variable. I did some related cleanup as well. Signed-off-by: Dan Carpenter --- smatch_return_to_param.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/smatch_return_to_param.c b/smatch_return_to_param.c index 5a6b9748..4e056937 100644 --- a/smatch_return_to_param.c +++ b/smatch_return_to_param.c @@ -87,14 +87,26 @@ static char *map_my_state_long_to_short(struct sm_state *sm, const char *name, s static char *map_assignment_long_to_short(struct sm_state *sm, const char *name, struct symbol *sym, struct symbol **new_sym, bool stack) { + struct expression *orig_expr; struct symbol *orig_sym; int len; char buf[256]; - if (!sm->state->data) + orig_expr = sm->state->data; + if (!orig_expr) return NULL; - orig_sym = expr_to_sym(sm->state->data); + /* + * Say we have an assignment like: + * foo->bar->my_ptr = my_ptr; + * We still expect the function to carry on using "my_ptr" as the + * shorter name. That's not a long to short mapping. + * + */ + if (orig_expr->type == EXPR_SYMBOL) + return NULL; + + orig_sym = expr_to_sym(orig_expr); if (!orig_sym) return NULL; if (sym != orig_sym) -- 2.11.4.GIT