diff options
author | David Cole <david.cole@kitware.com> | 2006-02-02 20:53:31 (GMT) |
---|---|---|
committer | David Cole <david.cole@kitware.com> | 2006-02-02 20:53:31 (GMT) |
commit | 4c0668b7daf8e3a53e65eeeb58757eea769bc482 (patch) | |
tree | 08e95b64335beefa0a8e6c5e2d18c2d1fbf1ebb5 /Utilities | |
parent | 9dd1633a9852590a7e03f04dd700273414e05f69 (diff) | |
download | CMake-4c0668b7daf8e3a53e65eeeb58757eea769bc482.zip CMake-4c0668b7daf8e3a53e65eeeb58757eea769bc482.tar.gz CMake-4c0668b7daf8e3a53e65eeeb58757eea769bc482.tar.bz2 |
BUG: Fix memory leak in libtar's kwReadDir. Use a static buffer like readdir (probably) does rather than malloc-ing a block which never gets free-d.
Diffstat (limited to 'Utilities')
-rw-r--r-- | Utilities/cmtar/filesystem.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/Utilities/cmtar/filesystem.c b/Utilities/cmtar/filesystem.c index 1ddd5a5..496f81c 100644 --- a/Utilities/cmtar/filesystem.c +++ b/Utilities/cmtar/filesystem.c @@ -54,19 +54,26 @@ kwDirectory * kwOpenDir(const char* name) kwDirEntry * kwReadDir(kwDirectory * dir) { - kwDirEntry * entry; + static kwDirEntry entry; if(!dir || dir->EOD ==1) { return NULL; } - entry = (kwDirEntry*)malloc(sizeof(kwDirEntry)); - strncpy(entry->d_name,dir->Entry.name,TAR_MAXPATHLEN-1); + strncpy(entry.d_name,dir->Entry.name,TAR_MAXPATHLEN-1); if(_findnext(dir->SrchHandle, &dir->Entry) == -1) { dir->EOD=1; } - return entry; + + // It is both stupid and dangerous to return a pointer to a static like this. + // This can only be called by one caller at a time: i.e., it's not thread safe. + // On the other hand, it mimics the documented behavior of "readdir" which is + // what it's implemented to replace for platforms that do not have readdir. + // Memory leaks are also stupid and dangerous... perhaps this is less so. + // + return &entry; } + int kwCloseDir(kwDirectory * dir) { int r=-1; |