From 2212d4f475b675fc337839f03f1751b32f4eed7e Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Sun, 11 Feb 2018 13:02:24 +0000 Subject: [PATCH] cpstripped(): Replace malloc()s with statics based on MB_LEN_MAX. MB_CUR_MAX from stdlib.h isn't a compile-time constant so can't be used for the size of a couple of static char arrays so malloc() was used at run time, with the tiny claims never being freed. valgrind(1) noticed and David suggested MB_LEN_MAX from limits.h instead; that header's contents are suitable for #if expressions and thus compile-time constants. --- sbr/fmt_scan.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/sbr/fmt_scan.c b/sbr/fmt_scan.c index cb29b72c..b8e59e05 100644 --- a/sbr/fmt_scan.c +++ b/sbr/fmt_scan.c @@ -247,9 +247,9 @@ static void cpstripped (charstring_t dest, size_t max, char *str) { static bool deja_vu; - static char *oddchar; + static char oddchar[MB_LEN_MAX * 2]; static size_t oddlen; - static char *spacechar; + static char spacechar[MB_LEN_MAX * 2]; static size_t spacelen; char *end; bool squash; @@ -259,19 +259,12 @@ cpstripped (charstring_t dest, size_t max, char *str) int w; if (!deja_vu) { - size_t two; - deja_vu = true; - two = MB_CUR_MAX * 2; /* Varies at run-time. */ - - oddchar = mh_xmalloc(two); - oddlen = wcstombs(oddchar, L"?", two); + oddlen = wcstombs(oddchar, L"?", sizeof oddchar); assert(oddlen > 0); - assert(wcwidth(L' ') == 1); /* Need to pad in ones. */ - spacechar = mh_xmalloc(two); - spacelen = wcstombs(spacechar, L" ", two); + spacelen = wcstombs(spacechar, L" ", sizeof spacechar); assert(spacelen > 0); } -- 2.56.0