Use MB_LEN_MAX instead of MB_CUR_MAX to avoid VLA (#70)

MB_CUR_MAX is the maximum number of bytes in a multibyte character
for the current locale, and might not be a constant expression.
MB_LEN_MAX is the maximum number of bytes in a multibyte character
for any locale, and always expands to a constant-expression.
This commit is contained in:
Michael Forney 2020-01-30 22:23:34 -08:00 committed by GitHub
parent 4d9b12969e
commit 69325710b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

5
lib.c
View File

@ -29,6 +29,7 @@ THIS SOFTWARE.
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <limits.h>
#include "awk.h" #include "awk.h"
#include "ytab.h" #include "ytab.h"
@ -343,14 +344,14 @@ void fldbld(void) /* create fields from current record */
*fr = 0; *fr = 0;
} else if ((sep = *inputFS) == 0) { /* new: FS="" => 1 char/field */ } else if ((sep = *inputFS) == 0) { /* new: FS="" => 1 char/field */
for (i = 0; *r != '\0'; r += n) { for (i = 0; *r != '\0'; r += n) {
char buf[MB_CUR_MAX + 1]; char buf[MB_LEN_MAX + 1];
i++; i++;
if (i > nfields) if (i > nfields)
growfldtab(i); growfldtab(i);
if (freeable(fldtab[i])) if (freeable(fldtab[i]))
xfree(fldtab[i]->sval); xfree(fldtab[i]->sval);
n = mblen(r, MB_CUR_MAX); n = mblen(r, MB_LEN_MAX);
if (n < 0) if (n < 0)
n = 1; n = 1;
memcpy(buf, r, n); memcpy(buf, r, n);