]> diplodocus.org Git - nmh/commitdiff
cpstripped(): Replace malloc()s with statics based on MB_LEN_MAX.
authorRalph Corderoy <ralph@inputplus.co.uk>
Sun, 11 Feb 2018 13:02:24 +0000 (13:02 +0000)
committerRalph Corderoy <ralph@inputplus.co.uk>
Sun, 11 Feb 2018 13:06:35 +0000 (13:06 +0000)
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

index cb29b72..b8e59e0 100644 (file)
@@ -247,9 +247,9 @@ static void
 cpstripped (charstring_t dest, size_t max, char *str)
 {
     static bool deja_vu;
 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 size_t oddlen;
-    static char *spacechar;
+    static char spacechar[MB_LEN_MAX * 2];
     static size_t spacelen;
     char *end;
     bool squash;
     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) {
     int w;
 
     if (!deja_vu) {
-        size_t two;
-
         deja_vu = true;
 
         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(oddlen > 0);
-
         assert(wcwidth(L' ') == 1); /* Need to pad in ones. */
         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);
     }
 
         assert(spacelen > 0);
     }