stdcxx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vi...@apache.org
Subject svn commit: r648093 - in /stdcxx/trunk/tests/src: printf.cpp process.cpp
Date Tue, 15 Apr 2008 01:57:55 GMT
Author: vitek
Date: Mon Apr 14 18:57:44 2008
New Revision: 648093

URL: http://svn.apache.org/viewvc?rev=648093&view=rev
Log:

2008-04-14  Travis Vitek  <vitek@roguewave.com>

	* tests/src/printf.cpp (_rw_bufcat): Simplify logic to get
	new buffer size. Allocate minimum initial size to avoid a
	first allocation that only contains the guard bytes. Add
	comment about guard bytes so others might not get tricked.
	* tests/src/process.cpp (_rw_vsystem): Avoid using static
	buffer (might be freed by _rw_bufcat).
	(_rw_vprocess_create): Ditto.

Modified:
    stdcxx/trunk/tests/src/printf.cpp
    stdcxx/trunk/tests/src/process.cpp

Modified: stdcxx/trunk/tests/src/printf.cpp
URL: http://svn.apache.org/viewvc/stdcxx/trunk/tests/src/printf.cpp?rev=648093&r1=648092&r2=648093&view=diff
==============================================================================
--- stdcxx/trunk/tests/src/printf.cpp (original)
+++ stdcxx/trunk/tests/src/printf.cpp Mon Apr 14 18:57:44 2008
@@ -448,28 +448,28 @@
         // for guard block
         static const char guard[] = "\xde\xad\xbe\xef";
 
-        size_t guardsize = sizeof guard - 1;
+        const size_t guardsize = sizeof guard - 1;
 
-        size_t newbufsize = *buf.pbufsize * 2 + guardsize;
-
-        if (newbufsize <= buflen + len + guardsize)
-            newbufsize = 2 * (buflen + len + 1) + guardsize;
+        size_t newbufsize = *buf.pbufsize * 2;
+        if (newbufsize < 16)
+            newbufsize = 16;
+
+        const size_t requiredsize = buflen + len + 1;
+        if (newbufsize < requiredsize)
+            newbufsize = requiredsize * 2;
 
         // prevent buffer size from exceeding the maximum
         if (buf.maxsize < newbufsize)
             newbufsize = buf.maxsize;
 
-        if (newbufsize < buflen + len + guardsize)
-            guardsize = 0;
-
-        if (newbufsize < buflen + len + guardsize) {
+        if (newbufsize < requiredsize) {
 #ifdef ENOMEM
             errno = ENOMEM;
 #endif   // ENOMEM
             return 0;
         }
 
-        char* const newbuf = (char*)malloc (newbufsize);
+        char* const newbuf = (char*)malloc (newbufsize + guardsize);
 
         // return 0 on failure to allocate, let caller deal with it
         if (0 == newbuf)
@@ -478,17 +478,19 @@
         memcpy (newbuf, *buf.pbuf, buflen);
 
         // append a guard block to the end of the buffer
-        memcpy (newbuf + newbufsize - guardsize, guard, guardsize);
+        memcpy (newbuf + newbufsize, guard, guardsize);
 
         if (*buf.pbuf) {
-            // verify that we didn't write past the end of the buffer
+            // verify that we didn't write past the end of the buffer and
+            // that the user didn't pass a local or static buffer without
+            // a maxsize format specifier.
             RW_ASSERT (0 == memcmp (*buf.pbuf + *buf.pbufsize,
                                     guard, guardsize));
             free (*buf.pbuf);
         }
 
         *buf.pbuf     = newbuf;
-        *buf.pbufsize = newbufsize - guardsize;
+        *buf.pbufsize = newbufsize;
     }
 
     if (0 != str) {

Modified: stdcxx/trunk/tests/src/process.cpp
URL: http://svn.apache.org/viewvc/stdcxx/trunk/tests/src/process.cpp?rev=648093&r1=648092&r2=648093&view=diff
==============================================================================
--- stdcxx/trunk/tests/src/process.cpp (original)
+++ stdcxx/trunk/tests/src/process.cpp Mon Apr 14 18:57:44 2008
@@ -269,12 +269,9 @@
 {
     RW_ASSERT (0 != cmd);
 
-    char buffer [256];
-    char *buf = buffer;
+    char *buf = 0;
 
-    size_t bufsize = sizeof buffer;
-
-    rw_vasnprintf (&buf, &bufsize, cmd, va);
+    rw_vasnprintf (&buf, 0, cmd, va);
 
     rw_note (0, "file:" __FILE__, __LINE__, "executing \"%s\"", buf);
 
@@ -321,8 +318,7 @@
 
     }
 
-    if (buf != buffer)
-        free (buf);
+    free (buf);
 
     return ret;
 }
@@ -348,12 +344,9 @@
 {
     RW_ASSERT (0 != cmd);
 
-    char buffer [256];
-    char *buf = buffer;
-
-    size_t bufsize = sizeof (buffer);
+    char *buf = 0;
 
-    rw_vasnprintf (&buf, &bufsize, cmd, va);
+    rw_vasnprintf (&buf, 0, cmd, va);
 
     rw_pid_t ret = -1;
 
@@ -392,8 +385,7 @@
 
 #endif  // _WIN32
 
-    if (buf != buffer)
-        free (buf);
+    free (buf);
 
     return ret;
 }



Mime
View raw message