From 453595a8226c869ec4865198386111cc0c917f6f Mon Sep 17 00:00:00 2001 From: eater <=@eater.me> Date: Sun, 6 Jun 2021 15:38:33 +0200 Subject: [PATCH] lib/conf.c: strip whitespace after value in config Closes: #407 [via git-merge-pr] --- lib/conf.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/conf.c b/lib/conf.c index 72b17faf..9a84da18 100644 --- a/lib/conf.c +++ b/lib/conf.c @@ -196,7 +196,7 @@ static const struct key { static int parse_option(char *buf, const char **keyp, char **valp) { - size_t klen; + size_t klen, end; const char *key; char *value; int k = 0; @@ -224,8 +224,14 @@ parse_option(char *buf, const char **keyp, char **valp) while (isblank((unsigned char)*value)) value++; - /* eat final newline */ - value[strlen(value)-1] = '\0'; + end = strlen(value); + /* eat trailing spaces, end - 1 here because \0 should be set -after- the first non-space + * if end points at the actual current character, we can never make it an empty string + * because than end needs to be set to -1, but end is a unsigned type thus would result in underflow */ + while (end > 0 && isspace((unsigned char)value[end - 1])) + end--; + + value[end] = '\0'; /* option processed successfully */ *keyp = key;