Fix more mismatched new[] / delete[] (eliminate invalid auto_ptr use) to correct valgrind reported memory issues.

This commit is contained in:
David Cole 2009-10-30 10:31:54 -04:00
parent 5c594b6f3a
commit 43d07f6620
3 changed files with 20 additions and 15 deletions

View File

@ -218,7 +218,7 @@ int cmCPackTGZGenerator::CompressFiles(const char* outFileName,
&mydata &mydata
}; };
// Ok, this libtar is not const safe. Make a non-const copy of outFileName // This libtar is not const safe. Make a non-const copy of outFileName
char* realName = new char[ strlen(outFileName) + 1 ]; char* realName = new char[ strlen(outFileName) + 1 ];
strcpy(realName, outFileName); strcpy(realName, outFileName);
int flags = O_WRONLY | O_CREAT; int flags = O_WRONLY | O_CREAT;
@ -241,6 +241,8 @@ int cmCPackTGZGenerator::CompressFiles(const char* outFileName,
return 0; return 0;
} }
delete [] realName;
std::vector<std::string>::const_iterator fileIt; std::vector<std::string>::const_iterator fileIt;
for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt ) for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
{ {
@ -256,7 +258,6 @@ int cmCPackTGZGenerator::CompressFiles(const char* outFileName,
<< pathname << "\"): " << pathname << "\"): "
<< strerror(errno) << std::endl); << strerror(errno) << std::endl);
tar_close(t); tar_close(t);
delete [] realName;
return 0; return 0;
} }
} }
@ -265,7 +266,6 @@ int cmCPackTGZGenerator::CompressFiles(const char* outFileName,
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_append_eof(): " cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_append_eof(): "
<< strerror(errno) << std::endl); << strerror(errno) << std::endl);
tar_close(t); tar_close(t);
delete [] realName;
return 0; return 0;
} }
@ -273,10 +273,8 @@ int cmCPackTGZGenerator::CompressFiles(const char* outFileName,
{ {
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_close(): " cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_close(): "
<< strerror(errno) << std::endl); << strerror(errno) << std::endl);
delete [] realName;
return 0; return 0;
} }
delete [] realName;
return 1; return 1;
} }

View File

@ -23,7 +23,6 @@
#include <cmsys/SystemTools.hxx> #include <cmsys/SystemTools.hxx>
#include <cmcompress/cmcompress.h> #include <cmcompress/cmcompress.h>
#include <libtar/libtar.h> #include <libtar/libtar.h>
#include <memory> // auto_ptr
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
@ -165,9 +164,8 @@ int cmCPackTarCompressGenerator::CompressFiles(const char* outFileName,
&mydata &mydata
}; };
// Ok, this libtar is not const safe. for now use auto_ptr hack // This libtar is not const safe. Make a non-const copy of outFileName
char* realName = new char[ strlen(outFileName) + 1 ]; char* realName = new char[ strlen(outFileName) + 1 ];
std::auto_ptr<char> realNamePtr(realName);
strcpy(realName, outFileName); strcpy(realName, outFileName);
int flags = O_WRONLY | O_CREAT; int flags = O_WRONLY | O_CREAT;
int options = 0; int options = 0;
@ -185,9 +183,12 @@ int cmCPackTarCompressGenerator::CompressFiles(const char* outFileName,
{ {
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_open(): " cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_open(): "
<< strerror(errno) << std::endl); << strerror(errno) << std::endl);
delete [] realName;
return 0; return 0;
} }
delete [] realName;
std::vector<std::string>::const_iterator fileIt; std::vector<std::string>::const_iterator fileIt;
for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt ) for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
{ {

View File

@ -46,7 +46,6 @@
#if defined(CMAKE_BUILD_WITH_CMAKE) #if defined(CMAKE_BUILD_WITH_CMAKE)
# include <libtar/libtar.h> # include <libtar/libtar.h>
# include <memory> // auto_ptr
# include <fcntl.h> # include <fcntl.h>
# include <cm_zlib.h> # include <cm_zlib.h>
# include <cmsys/MD5.h> # include <cmsys/MD5.h>
@ -1807,9 +1806,8 @@ bool cmSystemTools::CreateTar(const char* outFileName,
&gzs &gzs
}; };
// Ok, this libtar is not const safe. for now use auto_ptr hack // This libtar is not const safe. Make a non-const copy of outFileName
char* realName = new char[ strlen(outFileName) + 1 ]; char* realName = new char[ strlen(outFileName) + 1 ];
std::auto_ptr<char> realNamePtr(realName);
strcpy(realName, outFileName); strcpy(realName, outFileName);
int options = 0; int options = 0;
if(verbose) if(verbose)
@ -1825,9 +1823,12 @@ bool cmSystemTools::CreateTar(const char* outFileName,
options) == -1) options) == -1)
{ {
cmSystemTools::Error("Problem with tar_open(): ", strerror(errno)); cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
delete [] realName;
return false; return false;
} }
delete [] realName;
std::vector<cmStdString>::const_iterator it; std::vector<cmStdString>::const_iterator it;
for (it = files.begin(); it != files.end(); ++ it ) for (it = files.begin(); it != files.end(); ++ it )
{ {
@ -1859,6 +1860,7 @@ bool cmSystemTools::CreateTar(const char* outFileName,
cmSystemTools::Error("Problem with tar_close(): ", strerror(errno)); cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
return false; return false;
} }
return true; return true;
#else #else
(void)outFileName; (void)outFileName;
@ -1886,9 +1888,8 @@ bool cmSystemTools::ExtractTar(const char* outFileName,
&gzs &gzs
}; };
// Ok, this libtar is not const safe. for now use auto_ptr hack // This libtar is not const safe. Make a non-const copy of outFileName
char* realName = new char[ strlen(outFileName) + 1 ]; char* realName = new char[ strlen(outFileName) + 1 ];
std::auto_ptr<char> realNamePtr(realName);
strcpy(realName, outFileName); strcpy(realName, outFileName);
if (tar_open(&t, realName, if (tar_open(&t, realName,
(gzip? &gztype : NULL), (gzip? &gztype : NULL),
@ -1901,9 +1902,12 @@ bool cmSystemTools::ExtractTar(const char* outFileName,
| 0) == -1) | 0) == -1)
{ {
cmSystemTools::Error("Problem with tar_open(): ", strerror(errno)); cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
delete [] realName;
return false; return false;
} }
delete [] realName;
if (tar_extract_all(t, 0) != 0) if (tar_extract_all(t, 0) != 0)
{ {
cmSystemTools::Error("Problem with tar_extract_all(): ", strerror(errno)); cmSystemTools::Error("Problem with tar_extract_all(): ", strerror(errno));
@ -1940,9 +1944,8 @@ bool cmSystemTools::ListTar(const char* outFileName,
&gzs &gzs
}; };
// Ok, this libtar is not const safe. for now use auto_ptr hack // This libtar is not const safe. Make a non-const copy of outFileName
char* realName = new char[ strlen(outFileName) + 1 ]; char* realName = new char[ strlen(outFileName) + 1 ];
std::auto_ptr<char> realNamePtr(realName);
strcpy(realName, outFileName); strcpy(realName, outFileName);
if (tar_open(&t, realName, if (tar_open(&t, realName,
(gzip? &gztype : NULL), (gzip? &gztype : NULL),
@ -1955,9 +1958,12 @@ bool cmSystemTools::ListTar(const char* outFileName,
| 0) == -1) | 0) == -1)
{ {
cmSystemTools::Error("Problem with tar_open(): ", strerror(errno)); cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
delete [] realName;
return false; return false;
} }
delete [] realName;
while ((th_read(t)) == 0) while ((th_read(t)) == 0)
{ {
const char* filename = th_get_pathname(t); const char* filename = th_get_pathname(t);