Skip to content

Commit

Permalink
lightningd: configvar style fixes
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell committed Aug 10, 2024
1 parent 3e65ef4 commit 62f531a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
13 changes: 4 additions & 9 deletions common/configvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions common/configvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
6 changes: 3 additions & 3 deletions lightningd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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,
Expand Down

0 comments on commit 62f531a

Please sign in to comment.