From 62f531a1f2db2873533808327f0a8fce99bc738c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 9 Aug 2024 10:37:37 +0930 Subject: [PATCH] lightningd: configvar style fixes 1) We can't simply cast away const to manipulate a string, the compiler can assume we don't. The type must be made non-const. 2) cisspace() is nicer to use than isspace() (no cast required!) 3) Simply place a NUL terminator instead of using memmove to set it. 4) Use cast_const to add const to char **, where necessary. 5) Add Changelog line, for CHANGELOG.md Changelog-Fixed: Config: whitespace at the end of (most) options is now ignored, not complained about. Signed-off-by: Rusty Russell --- common/configvar.c | 13 ++++--------- common/configvar.h | 4 ++-- lightningd/options.c | 6 +++--- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/common/configvar.c b/common/configvar.c index d696ab2d0e7e..4a3dff39df39 100644 --- a/common/configvar.c +++ b/common/configvar.c @@ -31,7 +31,7 @@ const struct opt_table *configvar_unparsed(struct configvar *cv) ot = opt_find_short(cv->configline[0]); cv->optarg = NULL; } else { - ot = opt_find_long(cv->configline, &cv->optarg); + ot = opt_find_long(cv->configline, cast_const2(const char **, &cv->optarg)); } if (!ot) return NULL; @@ -51,18 +51,13 @@ const struct opt_table *configvar_unparsed(struct configvar *cv) return ot; } -static void trim_whitespace(const char *s) +static void trim_whitespace(char *s) { size_t len = strlen(s); - /* Cast away const to allow modifications */ - char *mutable_s = (char *)s; - - while (len > 0 && isspace((unsigned char)mutable_s[len - 1])) + while (len > 0 && cisspace(s[len - 1])) len--; - - /* Move null terminator to the end of the trimmed string */ - memmove(mutable_s + len, mutable_s + strlen(s), 1); + s[len] = '\0'; } const char *configvar_parse(struct configvar *cv, diff --git a/common/configvar.h b/common/configvar.h index 158704ab07fa..4453f455ffd2 100644 --- a/common/configvar.h +++ b/common/configvar.h @@ -35,13 +35,13 @@ struct configvar { /* Where did we get this from? */ enum configvar_src src; /* Never NULL, the whole line */ - const char *configline; + char *configline; /* These are filled in by configvar_parse */ /* The variable name (without any =) */ const char *optvar; /* NULL for no-arg options, otherwise points after =. */ - const char *optarg; + char *optarg; /* Was this overridden by a following option? */ bool overridden; }; diff --git a/lightningd/options.c b/lightningd/options.c index cd6680584a1a..8c62e23e9c16 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -1887,9 +1887,9 @@ void handle_early_opts(struct lightningd *ld, int argc, char *argv[]) } /* Free *str, set *str to copy with `cln` prepended */ -static void prefix_cln(const char **str STEALS) +static void prefix_cln(char **str STEALS) { - const char *newstr = tal_fmt(tal_parent(*str), "cln%s", *str); + char *newstr = tal_fmt(tal_parent(*str), "cln%s", *str); tal_free(*str); *str = newstr; } @@ -1910,7 +1910,7 @@ static void fixup_clnrest_options(struct lightningd *ld) && !strstarts(cv->configline, "rest-certs=")) continue; /* Did some (plugin) claim it? */ - if (opt_find_long(cv->configline, &cv->optarg)) + if (opt_find_long(cv->configline, cast_const2(const char **, &cv->optarg))) continue; if (!opt_deprecated_ok(ld, tal_strndup(tmpctx, cv->configline,