From 717758409e24b42dba8fbf1ac77c6fdf524efc12 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 26 Aug 2011 20:55:09 +0000 Subject: [PATCH] Fix segfault in BarMeterMode_draw() for small terminal widths --- ChangeLog | 2 ++ Meter.c | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index b6e19855..5643e356 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,8 @@ What's new in version 0.9.1 * Meters update in every screen (no longer halting while on Setup, etc.) * BUGFIX: Support larger numbers for process times. (thanks to Tristan Nakagawa for the report.) +* BUGFIX: Segfault in BarMeterMode_draw() for small terminal widths + (patch by Sebastian Pipping) What's new in version 0.9 diff --git a/Meter.c b/Meter.c index 6947d9b3..57862364 100644 --- a/Meter.c +++ b/Meter.c @@ -264,13 +264,19 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) { w--; x++; - char bar[w]; + + if (w < 1) { + attrset(CRT_colors[RESET_COLOR]); + return; + } + char bar[w + 1]; int blockSizes[10]; for (int i = 0; i < w; i++) bar[i] = ' '; - sprintf(bar + (w-strlen(buffer)), "%s", buffer); + const size_t barOffset = w - MIN(strlen(buffer), w); + snprintf(bar + barOffset, w - barOffset + 1, "%s", buffer); // First draw in the bar[] buffer... double total = 0.0;