Skip to content

Commit

Permalink
Fix CLN service startup failure by trimming spaces in config parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Max Rantil <[email protected]>
  • Loading branch information
maxrantil committed May 23, 2024
1 parent 2d0778e commit 3c829f0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
28 changes: 28 additions & 0 deletions ccan/ccan/tal/str/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <unistd.h>
#include <stdio.h>
#include <ccan/str/str.h>
#include <stdbool.h>

char *tal_strdup_(const tal_t *ctx, const char *p, const char *label)
{
Expand All @@ -35,6 +36,33 @@ char *tal_strndup_(const tal_t *ctx, const char *p, size_t n, const char *label)
return ret;
}

bool trim_resize_str(char **ctxp) {
if (ctxp == NULL || *ctxp == NULL) {
return false;
}

char *str = *ctxp;
size_t len = strlen(str);

size_t start = 0;
while (start < len && isspace((unsigned char)str[start])) {
start++;
}

size_t end = len;
while (end > start && isspace((unsigned char)str[end - 1])) {
end--;
}

if (start > 0 || end < len) {
memmove(str, str + start, end - start);
str[end - start] = '\0';
return tal_resize(ctxp, end - start + 1);
}

return true;
}

char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt, ...)
{
va_list ap;
Expand Down
10 changes: 10 additions & 0 deletions ccan/ccan/tal/str/str.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ char *tal_strdup_(const tal_t *ctx, const char *p TAKES, const char *label);
char *tal_strndup_(const tal_t *ctx, const char *p TAKES, size_t n,
const char *label);

/**
* trim_resize_str - Trim leading and trailing whitespace from a string and resize using tal.
* @ctxp: Pointer to a pointer of a tal allocated string.
*
* Returns:
* - true if the string was modified and resized successfully or if no modification was needed.
* - false if the input was NULL or if an error occurred during the resizing process.
*/
bool trim_resize_str(char **ctxp);

/**
* tal_fmt - allocate a formatted string
* @ctx: NULL, or tal allocated object to be parent.
Expand Down
5 changes: 5 additions & 0 deletions common/configdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ static struct configvar **gather_file_configvars(const tal_t *ctx,
/* Break into lines. */
lines = tal_strsplit(contents, contents, "\r\n", STR_EMPTY_OK);
for (size_t i = 0; i < tal_count(lines) - 1; i++) {
/* Trim whitespaces from start and end of line */
if (!trim_resize_str(&lines[i])) {
err(1, "Failed to trim and resize line %zu: \"%s\" in file %s", i, lines[i], filename);
}

/* Comments & blank lines*/
if (strstarts(lines[i], "#") || streq(lines[i], ""))
continue;
Expand Down
4 changes: 2 additions & 2 deletions contrib/startup_regtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ start_nodes() {
funder-min-their-funding=10000
funder-per-channel-max=100000
funder-fuzz-percent=0
funder-lease-requests-only=false
lease-fee-base-sat=2sat
funder-lease-requests-only=false
lease-fee-base-sat=2sat
lease-fee-basis=50
invoices-onchain-fallback
EOF
Expand Down

0 comments on commit 3c829f0

Please sign in to comment.