From 9239278cddbbc32ca59eb4b5cd21bddfdd6b586b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 14 Aug 2023 17:09:47 +0930 Subject: [PATCH] plugins: fix compile warning with some gcc versions and -Og MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Indeed, we can fall through this if it's not a valid enum value. gcc-12 (Ubuntu 12.2.0-17ubuntu1) 12.2.0 ``` In file included from plugins/commando.c:10: ccan/ccan/tal/str/str.h: In function ‘rune_altern_to_english’: ccan/ccan/tal/str/str.h:43:9: error: ‘cond_str’ may be used uninitialized [-Werror=maybe-uninitialized] 43 | tal_fmt_(ctx, TAL_LABEL(char, "[]"), __VA_ARGS__) | ^~~~~~~~ plugins/commando.c:97:21: note: ‘cond_str’ was declared here 97 | const char *cond_str; | ^~~~~~~~ cc1: all warnings being treated as errors ``` Signed-off-by: Rusty Russell --- lightningd/runes.c | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/lightningd/runes.c b/lightningd/runes.c index 6eaa5835d..a93d342ce 100644 --- a/lightningd/runes.c +++ b/lightningd/runes.c @@ -193,43 +193,41 @@ static bool is_unique_id(struct rune_restr **restrs, unsigned int index) return streq(restrs[index]->alterns[0]->fieldname, ""); } +static char *fmt_cond(const tal_t *ctx, + const struct rune_altern *alt, + const char *cond_str) +{ + return tal_fmt(ctx, "%s %s %s", alt->fieldname, cond_str, alt->value); +} + static char *rune_altern_to_english(const tal_t *ctx, const struct rune_altern *alt) { - const char *cond_str; switch (alt->condition) { case RUNE_COND_IF_MISSING: return tal_strcat(ctx, alt->fieldname, " is missing"); case RUNE_COND_EQUAL: - cond_str = "equal to"; - break; + return fmt_cond(ctx, alt, "equal to"); case RUNE_COND_NOT_EQUAL: - cond_str = "unequal to"; - break; + return fmt_cond(ctx, alt, "unequal to"); case RUNE_COND_BEGINS: - cond_str = "starts with"; - break; + return fmt_cond(ctx, alt, "starts with"); case RUNE_COND_ENDS: - cond_str = "ends with"; - break; + return fmt_cond(ctx, alt, "ends with"); case RUNE_COND_CONTAINS: - cond_str = "contains"; - break; + return fmt_cond(ctx, alt, "contains"); case RUNE_COND_INT_LESS: - cond_str = "<"; - break; + return fmt_cond(ctx, alt, "<"); case RUNE_COND_INT_GREATER: - cond_str = ">"; - break; + return fmt_cond(ctx, alt, ">"); case RUNE_COND_LEXO_BEFORE: - cond_str = "sorts before"; - break; + return fmt_cond(ctx, alt, "sorts before"); case RUNE_COND_LEXO_AFTER: - cond_str = "sorts after"; - break; + return fmt_cond(ctx, alt, "sorts after"); case RUNE_COND_COMMENT: return tal_fmt(ctx, "comment: %s %s", alt->fieldname, alt->value); } - return tal_fmt(ctx, "%s %s %s", alt->fieldname, cond_str, alt->value); + + abort(); } static char *json_add_alternative(const tal_t *ctx,