Opened 9 years ago

Closed 9 years ago

#9248 closed Bugs (fixed)

os_file_functions.hpp: delete_subdirectories_recursive Win32 HANDLE leak

Reported by: tomas.kotal@… Owned by: Ion Gaztañaga
Milestone: To Be Determined Component: interprocess
Version: Boost 1.55.0 Severity: Problem
Keywords: Cc:

Description

I believe there is a memory leak in function delete_subdirectories_recursive in boost\interprocess\detail\os_file_functions.hpp:

252.inline bool delete_subdirectories_recursive
253.   (const std::string &refcstrRootDirectory, const char *dont_delete_this, unsigned int count)
254.{
255.   bool               bSubdirectory = false;       // Flag, indicating whether
256.                                                   // subdirectories have been found
257.   void *             hFile;                       // Handle to directory
258.   std::string        strFilePath;                 // Filepath
259.   std::string        strPattern;                  // Pattern
260.   winapi::win32_find_data_t  FileInformation;     // File information
261.
262.   //Find all files and directories
263.   strPattern = refcstrRootDirectory + "\\*.*";
264.   hFile = winapi::find_first_file(strPattern.c_str(), &FileInformation);
265.   if(hFile != winapi::invalid_handle_value){
266.      do{
267.         //If it's not "." or ".." or the pointed root_level dont_delete_this erase it
268.         if(FileInformation.cFileName[0] != '.' &&
269.            !(dont_delete_this && count == 0 && std::strcmp(dont_delete_this, FileInformation.cFileName) == 0)){
270.            strFilePath.erase();
271.            strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;
272.
273.            //If it's a directory, go recursive
274.            if(FileInformation.dwFileAttributes & winapi::file_attribute_directory){
275.               // Delete subdirectory
276.               if(!delete_subdirectories_recursive(strFilePath, dont_delete_this, count+1))
277.                  return false;
278.            }
279.            //If it's a file, just delete it
280.            else{
281.               // Set file attributes
282.               //if(::SetFileAttributes(strFilePath.c_str(), winapi::file_attribute_normal) == 0)
283.               //return winapi::get_last_error();
284.               // Delete file
285.               winapi::unlink_file(strFilePath.c_str());
286.            }
287.         }
288.      //Go to the next file
289.      } while(winapi::find_next_file(hFile, &FileInformation) == 1);
290.
291.      // Close handle
292.      winapi::find_close(hFile);
      ...

On line 277, if subsequent call to delete_subdirectories_recursive returns false, the calling function returns also false. But handle hFile (defined on 264) is never closed. I think line winapi::find_close(hFile); before return statement is missing here.

I noticed the bug in boost version 1.45.0 and it seems like it's still there in 1.55.0.

Attachments (1)

os_file_functions.patch (766 bytes ) - added by tomas.kotal@… 9 years ago.
Patch

Download all attachments as: .zip

Change History (4)

by tomas.kotal@…, 9 years ago

Attachment: os_file_functions.patch added

Patch

comment:1 by tomas.kotal@…, 9 years ago

Component: Noneinterprocess
Owner: set to Ion Gaztañaga
Summary: boost::interprocess - delete_subdirectories_recursive Win32 HANDLE leakos_file_functions.hpp: delete_subdirectories_recursive Win32 HANDLE leak

Please change the component of ticket to interprocess, I was not able to do it in my browser for some reason. Thank you.

comment:2 by Ion Gaztañaga, 9 years ago

You are right, thanks for the bug report. Fortunately the leak shouldn't be common as it's a rare error condition. winapi::find_close(hFile) should do the work.

comment:3 by Ion Gaztañaga, 9 years ago

Resolution: fixed
Status: newclosed

(In [86304]) Fixes #9248 ("os_file_functions.hpp: delete_subdirectories_recursive Win32 HANDLE leak")

Note: See TracTickets for help on using tickets.