FIX: CFile::ReadHuge May Return Incorrect Value

ID: Q109849

1.00 WINDOWS kbprg kbfixlist kbbuglist

The information in this article applies to:

SYMPTOMS

The member function CFile::ReadHuge may return an incorrect value.

CAUSE

A miscalculation exists in the CFile::ReadHuge() function located in FILEX.CPP. FILEX.CPP may be found in the \MSVC\MFC\SRC directory.

RESOLUTION

To correct the problem, replace the code for CFile::ReadHuge() with the code below. You can modify the Microsoft Foundation Class (MFC) Library sources or you could make the function a new function of some CFile-derived class.

Sample Code

DWORD CFile::ReadHuge(void FAR* lpBuffer, DWORD dwCount) {

     ASSERT_VALID(this);

     DWORD dwToRead = dwCount;
     while (dwToRead > 0)
     {
          UINT nRead = _AfxCalcSize(dwToRead, lpBuffer);
          UINT nActuallyRead;
          if ((nActuallyRead = Read(lpBuffer, nRead)) < nRead)
               return ((dwCount - dwToRead) + nActuallyRead);
          ASSERT(nActuallyRead == nRead);
          dwToRead -= nRead;
          lpBuffer = ((BYTE _huge*)lpBuffer) + nRead;
     }
     return dwCount;
}

STATUS

Microsoft has confirmed this to be a bug in the products listed at the beginning of this article. This problem was corrected in MFC 2.5, included with Visual C++ for Windows, version 1.5.

MORE INFORMATION

The code below illustrates the problem. When the program is executed, the output line will be:

   Read returns 0, ReadHuge returns 512

Both reads should return zero for a zero-length file.

Sample Code

   // compile options required: none

   void Test()
   {
      CFile   file;
      CFileException exception;
      char *   buffer[1024];
      UINT    read1;
      DWORD   read2;

      ASSERT(file.Open( "blank.txt",
                 CFile::modeReadWrite
               | CFile::typeBinary
               | CFile::modeCreate,
                &exception ));

      read1 = file.Read( buffer, 512 );

      file.Seek( 0, CFile::begin );
      read2 = file.ReadHuge( buffer, 512 );

      TRACE( "Read returns %d, ReadHuge returns %ld\n",
               read1, read2 );

      file.Close();
   }

Additional reference words: 1.00 2.00 KBCategory: kbprg kbfixlist kbbuglist KBSubcategory: MfcFileIO
Keywords          : kb16bitonly kbFileIO kbMFC kbVC kbbuglist kbfixlist
Version           : 1.00
Platform          : WINDOWS
Solution Type     : kbfix

Last Reviewed: September 21, 1997