From f9dad99db855a6891968abd71366ceb17b44a2c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 22 Apr 2021 11:44:09 +0200 Subject: [PATCH] particle/ramp: clamp min/max/value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make sure that: * min <= max * min <= value <= max Fixes a crash when the tag’s value was out-of-bounds. Closes #45 --- CHANGELOG.md | 4 +++- particles/ramp.c | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0a478a..1f41f9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ ### Changed * doc: split up **yambar-modules**(5) into multiple man pages, one for - each module (https://codeberg.org/dnkl/yambar/issues/15). + each module (https://codeberg.org/dnkl/yambar/issues/15). ### Deprecated ### Removed @@ -20,6 +20,8 @@ * Crash when merging non-dictionary anchors in the YAML configuration (https://codeberg.org/dnkl/yambar/issues/32). +* Crash in the `ramp` particle when the tag’s value was out-of-bounds + (https://codeberg.org/dnkl/yambar/issues/45). ### Security diff --git a/particles/ramp.c b/particles/ramp.c index 45cc277..9a7f06e 100644 --- a/particles/ramp.c +++ b/particles/ramp.c @@ -4,6 +4,9 @@ #include +#define LOG_MODULE "ramp" +#define LOG_ENABLE_DBG 0 +#include "../log.h" #include "../config.h" #include "../config-verify.h" #include "../particle.h" @@ -102,6 +105,26 @@ instantiate(const struct particle *particle, const struct tag_set *tags) long min = tag != NULL ? tag->min(tag) : 0; long max = tag != NULL ? tag->max(tag) : 0; + if (min > max) { + LOG_WARN( + "tag's minimum value is greater than its maximum: " + "tag=\"%s\", min=%ld, max=%ld", p->tag, min, max); + min = max; + } + + if (value < min) { + LOG_WARN( + "tag's value is less than its minimum value: " + "tag=\"%s\", min=%ld, value=%ld", p->tag, min, value); + value = min; + } + if (value > max) { + LOG_WARN( + "tag's value is greater than its maximum value: " + "tag=\"%s\", max=%ld, value=%ld", p->tag, max, value); + value = max; + } + assert(value >= min && value <= max); assert(max >= min);