An auto_ptr bug in readString

Bostjan Lah <[email protected]> Sat, 04 May 2002 12:21:41 -0400
Newsgroups gmane.comp.gcc.cgicc.bugs
Message-ID <1020529301.6789.42.camel@bnb>
--Boundary_(ID_RcxW3Ruc609WTM4hyTQjXg)
Content-type: text/plain
Content-transfer-encoding: 7BIT

Hi.

First thanks for this excellent CGI library. I have been using it
extensively for the past several months. 
I recently also started using it with a memory leak detection tool
Valgrind (http://developer.kde.org/~sewardj/) and it kept reporting
unmatched new[] with delete / free (instead of delete []). The problem
function is readString which uses auto_ptr for allocating a char array
of readSize size, i.e.:
auto_ptr<char> temp(new char[readSize]);
auto_ptrs and arrays apparently don't mix so I'm attaching a patch for
CgiUtils.cpp which uses a simple char *temp instead.

Regards,
Bostjan

--Boundary_(ID_RcxW3Ruc609WTM4hyTQjXg)
Content-type: text/plain; name=readString.patch; charset=ISO-8859-1
Content-transfer-encoding: 7BIT
Content-disposition: attachment; filename=readString.patch

--- CgiUtils.cpp.old	Sat May  4 12:05:48 2002
+++ CgiUtils.cpp	Sat May  4 12:07:46 2002
@@ -179,18 +179,21 @@
   STDNS string::size_type dataSize = 0;
   
   in >> dataSize;
   in.get(); // skip ' '
 
-  STDNS auto_ptr<char> temp(new char[dataSize]);
+  char *temp = new char[dataSize];
 
-  in.read(temp.get(), dataSize);
+  in.read(temp, dataSize);
   if((STDNS string::size_type)in.gcount() != dataSize) {
+    delete [] temp;
     throw STDNS runtime_error("I/O error");
   }
 
-  return STDNS string(temp.get(), dataSize);
+  string res(temp, dataSize);
+  delete [] temp;
+  return res;
 }
 
 // read a long
 unsigned long
 CGICCNS readLong(STDNS istream& in)

--Boundary_(ID_RcxW3Ruc609WTM4hyTQjXg)--