Overview
Features
Download
Documentation
Community
Add-Ons & Services

File::createFile : bug

Please post support and help requests here.

File::createFile : bug

Postby cdelmas » 24 Apr 2007, 14:43

Code: Select all

Path p("C:   mp   est1example.txt"); // assume test1 doesn't exists
File f(p);
bool b = f.createFile(); // returns true


In fact, CreateFile (used in CreateFileImpl) does not return 0 if it fails, but -1 (INVALID_HANDLE_VALUE) so the test
Code: Select all

// in FileImpl::createFileImpl
if(hFile) {
  //...
  return true;
}

will allways return true, consequently making the function allways succeed.

A patch would be :
[code]
// in FileImpl::createFileImpl
if(hFile != INVALID_HANDLE_VALUE) {
//...
return true;
}
cdelmas
 
Posts: 46
Joined: 22 Sep 2006, 10:04
Location: France

Re: File::createFile : bug

Postby guenter » 25 Apr 2007, 16:48

Thanks. I have fixed that. The fix is in the SVN trunk.
guenter
 
Posts: 1105
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: File::createFile : bug

Postby keith » 01 May 2007, 13:28

FYI,
I also found on WinCe that GetLastError() return ERROR_FILE_EXISTS in addition to ERROR_ALREADY_EXISTS. I want this to return(false) and not throw, so my version of FileImpl createFileImpl in WIN32/WIN32U/WINCE is now:

Code: Select all

bool FileImpl::createFileImpl()
{
   poco_assert (!_path.empty());

#ifdef WANTBUG
   HANDLE hFile = CreateFile(_path.c_str(), GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0);
   if (hFile)
   {
      CloseHandle(hFile);
      return true;
   }
   else if (GetLastError() == ERROR_ALREADY_EXISTS)
      return false;
   else
      handleError(_path);s
   return false;
#else
   HANDLE hFile = CreateFile(_path.c_str(), GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0);
   if (hFile != INVALID_HANDLE_VALUE)
   {
      CloseHandle(hFile);
      return true;
   }
   else
   {
      DWORD erc = GetLastError();
      if (( erc == ERROR_ALREADY_EXISTS) || (erc == ERROR_FILE_EXISTS))
         return false;
      else
         handleError(_path);
   }
   return false;
#endif


Cheers,
Keith.
keith
 
Posts: 4
Joined: 25 Apr 2007, 14:10


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron