Author: faridz
Date: Tue Oct 23 10:34:55 2007
New Revision: 587563
URL: http://svn.apache.org/viewvc?rev=587563&view=rev
Log:
2007-10-23 Travis Vitek <[EMAIL PROTECTED]>
STDCXX-593
* locale.cpp (rw_locale): Update used array size to avoid
writing past the end of the allocated buffer. Use a growth
constant variable to avoid writing the same value in many
places. Use precalculated name length instead of calling
strlen() repeatedly.
[_WIN32]: Hide _malloc_dbg and _free_dbg behind macros
to clean up multiple conditional blocks and to avoid memory
block type mismatch.
Modified:
incubator/stdcxx/branches/4.2.x/tests/src/locale.cpp
Modified: incubator/stdcxx/branches/4.2.x/tests/src/locale.cpp
URL:
http://svn.apache.org/viewvc/incubator/stdcxx/branches/4.2.x/tests/src/locale.cpp?rev=587563&r1=587562&r2=587563&view=diff
==============================================================================
--- incubator/stdcxx/branches/4.2.x/tests/src/locale.cpp (original)
+++ incubator/stdcxx/branches/4.2.x/tests/src/locale.cpp Tue Oct 23 10:34:55
2007
@@ -339,22 +339,24 @@
static char deflocname [3] = "C\0";
static char* slocname = 0;
+ static const size_t grow_size = 5120;
static size_t size = 0; // the number of elements in the
array
- static size_t total_size = 5120; // the size of the array
+ static size_t total_size = grow_size; // the size of the array
static int last_cat = loc_cat; // last category
- // allocate first time through
- if (!slocname) {
-
#ifndef _MSC_VER
- slocname = _RWSTD_STATIC_CAST (char*, malloc (5120));
+# define _QUIET_MALLOC(n) malloc(n)
+# define _QUIET_FREE(p) free(p)
#else
- // prevent this leaked allocation from causing failures
- // in tests that keep track of storage allocated in
- // _NORMAL_BLOCKS
- slocname = _RWSTD_STATIC_CAST (char*,
- _malloc_dbg (5120, _CLIENT_BLOCK, 0, 0));
+ // prevent allocation from causing failures in tests that
+ // keep track of storage allocated in _NORMAL_BLOCKS
+# define _QUIET_MALLOC(n) _malloc_dbg (n, _CLIENT_BLOCK, 0, 0)
+# define _QUIET_FREE(p) _free_dbg (p, _CLIENT_BLOCK);
#endif
+
+ // allocate first time through
+ if (!slocname) {
+ slocname = _RWSTD_STATIC_CAST (char*, _QUIET_MALLOC (total_size));
*slocname = '\0';
}
@@ -412,7 +414,10 @@
// put the C locale at the front
if (prepend_c_loc) {
strcpy (locname, deflocname);
- locname += strlen (deflocname) + 1;
+
+ const size_t defnamelen = strlen (deflocname) + 1;
+ locname += defnamelen;
+ size += defnamelen;
}
// if successful, construct a char array with the locales
@@ -441,29 +446,26 @@
#endif // _RWSTD_OS_SUNOS
// if our buffer is full then dynamically allocate a new one
- if (total_size < (size += (strlen (linebuf) + 1))) {
- total_size += 5120;
+ size += linelen;
+ if (total_size < size) {
+ total_size += grow_size;
char* tmp =
- _RWSTD_STATIC_CAST (char*, malloc (total_size));
+ _RWSTD_STATIC_CAST (char*, _QUIET_MALLOC (total_size));
- memcpy (tmp, slocname, total_size - 5120);
+ memcpy (tmp, slocname, total_size - grow_size);
-#ifndef _MSC_VER
- free (slocname);
-#else
- _free_dbg (slocname, _CLIENT_BLOCK);
-#endif
+ _QUIET_FREE (slocname);
slocname = tmp;
- locname = slocname + size - strlen (linebuf) - 1;
+ locname = slocname + size - linelen;
}
#ifdef _WIN64
// prevent a hang (OS/libc bug?)
strcpy (locname, linebuf);
- locname += strlen (linebuf) + 1;
+ locname += linelen;
#else // if !defined (_WIN64)
if (loc_cat != _UNUSED_CAT) {
@@ -475,7 +477,7 @@
// from the last one, append it to the list
if (name && strcmp (last_name, name)) {
strcpy (locname, linebuf);
- locname += strlen (linebuf) + 1;
+ locname += linelen;
// save the last locale name
assert (strlen (name) < sizeof last_name);
@@ -484,7 +486,7 @@
}
else {
strcpy (locname, linebuf);
- locname += strlen (linebuf) + 1;
+ locname += linelen;
}
#endif // _WIN64