From f0a34d00553379611bd2eee8da09b347cd1474ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Fri, 30 Oct 2020 16:28:48 +0100 Subject: [PATCH] module/script: parse booleans correctly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit User is expected to send ‘false’ or ‘true’. But we were parsing the value using `strtol()`. This caused all bools to be false, since `strtol()` would always return 0. --- modules/script.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/script.c b/modules/script.c index b4e165b..ebb55df 100644 --- a/modules/script.c +++ b/modules/script.c @@ -67,6 +67,16 @@ content(struct module *mod) return e; } + +static bool +str_to_bool(const char *s) +{ + return strcasecmp(s, "on") == 0 || + strcasecmp(s, "true") == 0 || + strcasecmp(s, "yes") == 0 || + strtoul(s, NULL, 0) > 0; +} + static struct tag * process_line(struct module *mod, const char *line, size_t len) { @@ -112,7 +122,7 @@ process_line(struct module *mod, const char *line, size_t len) tag = tag_new_int(mod, name, strtol(value, NULL, 0)); else if (type_len == 4 && memcmp(type, "bool", 4) == 0) - tag = tag_new_bool(mod, name, strtol(value, NULL, 0)); + tag = tag_new_bool(mod, name, str_to_bool(value)); else if (type_len == 5 && memcmp(type, "float", 5) == 0) tag = tag_new_float(mod, name, strtod(value, NULL));