From 671850a1c3e1aaac93a89fabc6d810be6a4a77fe Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 9 Jan 2015 14:21:09 +0100 Subject: [PATCH] bison: avoid warnings from static code analysis A static analysis tool reports that some callers of symbol_list_n_get might get NULL and not handle it properly. This is not the case, yet we can suppress this pattern. Reported by Mike Sullivan. * src/symlist.c (symbol_list_n_get): Actually it is never called to return 0. Enforce this postcondition via aver. (symbol_list_n_type_name_get): Simplify accordingly. In particular, discards a (translated) useless error message. * src/symlist.h: Adjust documentation. * src/scan-code.l: Style change. --- THANKS | 1 + po/POTFILES.in | 1 - src/scan-code.l | 2 +- src/symlist.c | 27 +++++++-------------------- src/symlist.h | 5 ++++- 5 files changed, 13 insertions(+), 23 deletions(-) diff --git a/THANKS b/THANKS index d5feb16e..4fc99849 100644 --- a/THANKS +++ b/THANKS @@ -91,6 +91,7 @@ Michel d'Hooge michel.dhooge@gmail.com Michiel De Wilde mdewilde.agilent@gmail.com Mickael Labau labau_m@epita.fr Mike Castle dalgoda@ix.netcom.com +Mike Sullivan Mike.sullivan@Oracle.COM Neil Booth NeilB@earthling.net Nelson H. F. Beebe beebe@math.utah.edu Nick Bowler nbowler@elliptictech.com diff --git a/po/POTFILES.in b/po/POTFILES.in index ee34879a..8086f556 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -15,7 +15,6 @@ src/reduce.c src/scan-code.l src/scan-gram.l src/scan-skel.l -src/symlist.c src/symtab.c djgpp/subpipe.c diff --git a/src/scan-code.l b/src/scan-code.l index f90916c1..308d1d0d 100644 --- a/src/scan-code.l +++ b/src/scan-code.l @@ -711,7 +711,7 @@ handle_action_dollar (symbol_list *rule, char *text, location dollar_loc) "]b4_rhs_value(%d, %d, ", effective_rule_length, n); obstack_quote (&obstack_for_string, type_name); obstack_sgrow (&obstack_for_string, ")["); - if (n > 0) + if (0 < n) symbol_list_n_get (effective_rule, n)->action_props.is_value_used = true; break; diff --git a/src/symlist.c b/src/symlist.c index b58cf5c7..d8514636 100644 --- a/src/symlist.c +++ b/src/symlist.c @@ -21,7 +21,6 @@ #include #include "system.h" -#include "complain.h" #include "symlist.h" /*--------------------------------------. @@ -174,22 +173,17 @@ symbol_list * symbol_list_n_get (symbol_list *l, int n) { int i; - - if (n < 0) - return NULL; - + aver (0 <= n); for (i = 0; i < n; ++i) { l = l->next; - if (l == NULL - || (l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL)) - return NULL; + aver (l); } - + aver (l->content_type == SYMLIST_SYMBOL); + aver (l->content.sym); return l; } - /*--------------------------------------------------------------. | Get the data type (alternative in the union) of the value for | | symbol N in symbol list L. | @@ -198,21 +192,14 @@ symbol_list_n_get (symbol_list *l, int n) uniqstr symbol_list_n_type_name_get (symbol_list *l, location loc, int n) { - l = symbol_list_n_get (l, n); - if (!l) - { - complain (&loc, complaint, _("invalid $ value: $%d"), n); - return NULL; - } - aver (l->content_type == SYMLIST_SYMBOL); - return l->content.sym->type_name; + return symbol_list_n_get (l, n)->content.sym->type_name; } bool symbol_list_null (symbol_list *node) { - return !node || - (node->content_type == SYMLIST_SYMBOL && !(node->content.sym)); + return (!node + || (node->content_type == SYMLIST_SYMBOL && !node->content.sym)); } void diff --git a/src/symlist.h b/src/symlist.h index 639b593b..aeb46229 100644 --- a/src/symlist.h +++ b/src/symlist.h @@ -116,7 +116,10 @@ void symbol_list_free (symbol_list *list); /** Return the length of \c l. */ int symbol_list_length (symbol_list const *l); -/** Get item \c n in symbol list \c l. */ +/** Get item \c n in symbol list \c l. + ** \pre 0 <= n + ** \post res != NULL + **/ symbol_list *symbol_list_n_get (symbol_list *l, int n); /* Get the data type (alternative in the union) of the value for -- 2.45.2