From 17d258ff0a216dd87655c8e93d065e5e6c504f42 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 1 Aug 2011 02:51:24 +0200 Subject: [PATCH 3/3] Fix segfault in BarMeterMode_draw() for small terminal widths Segfault can be triggered by running "COLUMNS=1 ./htop" For me, COLUMNS=40 was the first that would not crash. --- ChangeLog | 2 ++ Meter.c | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Meter.c b/Meter.c index 6947d9b..408b981 100644 --- a/Meter.c +++ b/Meter.c @@ -264,13 +265,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 bar_offset = w - MIN(strlen(buffer), w); + snprintf(bar + bar_offset, w - bar_offset + 1, "%s", buffer); // First draw in the bar[] buffer... double total = 0.0; -- 1.7.6