From bfba1860cf1243e0099e211d0843ff37c1592490 Mon Sep 17 00:00:00 2001 From: Stefan Gehn Date: Mon, 15 Sep 2025 20:54:49 +0200 Subject: [PATCH] krell: Avoid int overflow in scaling calculation clamp both the value difference as well as the calculated ema to int values but only *after* calculating them using an intermediate ulong. This increases the range for the difference/scale calculation without a possible overflow causing negative values. Related to #75 --- src/krell.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/krell.c b/src/krell.c index 19ca8fb..d947fea 100644 --- a/src/krell.c +++ b/src/krell.c @@ -34,6 +34,8 @@ #include "gkrellm.h" #include "gkrellm-private.h" +#include + static GkrellmBorder zero_border; static GkrellmMargin zero_margin; @@ -85,7 +87,10 @@ gkrellm_update_krell(GkrellmPanel *p, GkrellmKrell *k, gulong value) } if (!k->monotonic) k->previous = 0; - k->reading = (gint) (value - k->previous) * k->full_scale_expand; + + assert(value >= k->previous); + const unsigned long value_diff = value - k->previous; + k->reading = (value_diff > INT_MAX ? INT_MAX : (int)value_diff) * k->full_scale_expand; if (k->reading > k->full_scale) k->reading = k->full_scale; @@ -94,7 +99,8 @@ gkrellm_update_krell(GkrellmPanel *p, GkrellmKrell *k, gulong value) k->previous = value; k->last_reading = k->reading; - xnew = k->x0 + k->ema * k->w_scale / k->full_scale; + const unsigned long scaled_ema = (unsigned long)k->ema * (unsigned long)k->w_scale / (unsigned long)k->full_scale; + xnew = k->x0 + (scaled_ema > INT_MAX ? INT_MAX : (int)scaled_ema); if (xnew == k->x0 && k->reading) {