Return-Path: Delivered-To: apmail-incubator-stdcxx-commits-archive@www.apache.org Received: (qmail 92726 invoked from network); 23 Oct 2007 17:35:46 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 Oct 2007 17:35:46 -0000 Received: (qmail 18311 invoked by uid 500); 23 Oct 2007 17:35:34 -0000 Delivered-To: apmail-incubator-stdcxx-commits-archive@incubator.apache.org Received: (qmail 18299 invoked by uid 500); 23 Oct 2007 17:35:34 -0000 Mailing-List: contact stdcxx-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: stdcxx-dev@incubator.apache.org Delivered-To: mailing list stdcxx-commits@incubator.apache.org Received: (qmail 18288 invoked by uid 99); 23 Oct 2007 17:35:34 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 23 Oct 2007 10:35:34 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 23 Oct 2007 17:35:46 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C57D71A9832; Tue, 23 Oct 2007 10:34:55 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r587563 - /incubator/stdcxx/branches/4.2.x/tests/src/locale.cpp Date: Tue, 23 Oct 2007 17:34:55 -0000 To: stdcxx-commits@incubator.apache.org From: faridz@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071023173455.C57D71A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 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