From ec62fab6330d356497514144a7284901b081247e Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Wed, 29 Aug 2018 22:59:57 -0400 Subject: [PATCH] wmcalc: Null-termitate display string when clearing. Often, the number currently being displayed has more than 10 digits. However, when clearing the display, only the first 10 digits were cleared. When a new number was entered, it would be prepended to any digits that weren't cleared. This behavior was invisible to the user, causing incorrect calculations, e.g., Debian bug #564173: 1814 / 720 * 300 -> 7568.41111 But the correct answer is 755.833333. The problem was that when the display was cleared, the string that stores this number was not null-terminated. Indeed, 1814 / 720 gives us 2.519444444 (6 4's), but only 2.51944444 (5 4's) is displayed. When we begin to multiply by 300, the final 4 remained, and so we really were multiplying by 3004. --- wmcalc/wmcalcfunc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wmcalc/wmcalcfunc.c b/wmcalc/wmcalcfunc.c index 8b98338..a8248ff 100644 --- a/wmcalc/wmcalcfunc.c +++ b/wmcalc/wmcalcfunc.c @@ -63,6 +63,7 @@ void clearcalc(void) { for (i=0; i < DISPSIZE; i++) { DispString[i] = ' '; } + DispString[DISPSIZE] = '\0'; OpFlag = ' '; } /***** End of function clearcalc() *****************************/ @@ -94,6 +95,7 @@ void clearnum(void) { for (i=0; i < DISPSIZE; i++) { DispString[i] = ' '; } + DispString[DISPSIZE] = '\0'; } /***** End of function clearnum() ******************************/