From ccc8c6c31f14d4167a8eab0a374d2c1ba9147af5 Mon Sep 17 00:00:00 2001 From: "Tim J. Robbins" Date: Thu, 22 Apr 2004 11:35:12 +0000 Subject: [PATCH] Use the correct size to allocate, copy and clear argument type tables after their change from an array of char to an array of enum. This fixes problems that occurred when using positional arguments in format strings, particularly with more than STATIC_ARG_TBL_SIZE (8) of them. PR: 65841 Submitted by: Steven Smith (mostly) --- lib/libc/stdio/vfprintf.c | 15 +++++++++------ lib/libc/stdio/vfwprintf.c | 15 +++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/libc/stdio/vfprintf.c b/lib/libc/stdio/vfprintf.c index 3285c374ca93..cd147b85335b 100644 --- a/lib/libc/stdio/vfprintf.c +++ b/lib/libc/stdio/vfprintf.c @@ -1321,7 +1321,8 @@ __find_arguments (const char *fmt0, va_list ap, union arg **argtable) tablesize = STATIC_ARG_TBL_SIZE; tablemax = 0; nextarg = 1; - memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE); + for (n = 0; n < STATIC_ARG_TBL_SIZE; n++) + typetable[n] = T_UNUSED; /* * Scan the format for conversions (`%' character). @@ -1590,19 +1591,21 @@ __grow_type_table (int nextarg, enum typeid **typetable, int *tablesize) enum typeid *const oldtable = *typetable; const int oldsize = *tablesize; enum typeid *newtable; - int newsize = oldsize * 2; + int n, newsize = oldsize * 2; if (newsize < nextarg + 1) newsize = nextarg + 1; if (oldsize == STATIC_ARG_TBL_SIZE) { - if ((newtable = malloc(newsize)) == NULL) + if ((newtable = malloc(newsize * sizeof(enum typeid))) == NULL) abort(); /* XXX handle better */ - bcopy(oldtable, newtable, oldsize); + bcopy(oldtable, newtable, oldsize * sizeof(enum typeid)); } else { - if ((newtable = reallocf(oldtable, newsize)) == NULL) + newtable = reallocf(oldtable, newsize * sizeof(enum typeid)); + if (newtable == NULL) abort(); /* XXX handle better */ } - memset(&newtable[oldsize], T_UNUSED, newsize - oldsize); + for (n = oldsize; n < newsize; n++) + newtable[n] = T_UNUSED; *typetable = newtable; *tablesize = newsize; diff --git a/lib/libc/stdio/vfwprintf.c b/lib/libc/stdio/vfwprintf.c index 9a34ff069e98..afecc971f9a0 100644 --- a/lib/libc/stdio/vfwprintf.c +++ b/lib/libc/stdio/vfwprintf.c @@ -1317,7 +1317,8 @@ __find_arguments (const wchar_t *fmt0, va_list ap, union arg **argtable) tablesize = STATIC_ARG_TBL_SIZE; tablemax = 0; nextarg = 1; - memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE); + for (n = 0; n < STATIC_ARG_TBL_SIZE; n++) + typetable[n] = T_UNUSED; /* * Scan the format for conversions (`%' character). @@ -1586,19 +1587,21 @@ __grow_type_table (int nextarg, enum typeid **typetable, int *tablesize) enum typeid *const oldtable = *typetable; const int oldsize = *tablesize; enum typeid *newtable; - int newsize = oldsize * 2; + int n, newsize = oldsize * 2; if (newsize < nextarg + 1) newsize = nextarg + 1; if (oldsize == STATIC_ARG_TBL_SIZE) { - if ((newtable = malloc(newsize)) == NULL) + if ((newtable = malloc(newsize * sizeof(enum typeid))) == NULL) abort(); /* XXX handle better */ - bcopy(oldtable, newtable, oldsize); + bcopy(oldtable, newtable, oldsize * sizeof(enum typeid)); } else { - if ((newtable = reallocf(oldtable, newsize)) == NULL) + newtable = reallocf(oldtable, newsize * sizeof(enum typeid)); + if (newtable == NULL) abort(); /* XXX handle better */ } - memset(&newtable[oldsize], T_UNUSED, newsize - oldsize); + for (n = oldsize; n < newsize; n++) + newtable[n] = T_UNUSED; *typetable = newtable; *tablesize = newsize;