FIX: CInternet::File ReadString() Loses Data

Last reviewed: September 19, 1997
Article ID: Q154895
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++, 32-bit Edition, version 4.2

SYMPTOMS

Calling CInternetFile::ReadString() without calling SetReadBufferSize() first can result in lost data. If the file being read is less than 4096 bytes (the default read buffer size), a null string will be returned into the buffer specified by the first argument of ReadString(). The ReadString() function will also return NULL. If the file being read is greater than 4096 bytes, the first 4096 bytes will be lost and then subsequent ReadString() calls will return the rest of the data.

You can see this problem by running the TEAR MFC sample that is included on the Visual C++ 4.2 CD.

CAUSE

There is a bug in CInternetFile::ReadString(). It calls InternetReadFile() twice. The second call to InternetReadFile() removes the data retrieved by the first call to InternetReadFile().

RESOLUTION

To prevent ReadString from calling InternetReadFile() twice, call SetReadBufferSize() before making the first call to ReadString. This sets a read buffer, which means that the following code in ReadString is not executed:

   if (m_pbReadBuffer == NULL)
   {
       if (!SetReadBufferSize(4096))   // arbitrary but reasonable
           return NULL;
       if (!InternetReadFile(m_hFile, m_pbReadBuffer, m_nReadBufferSize,
               &dwRead))
           AfxThrowInternetException(m_dwContext);
        m_nReadBufferBytes = dwRead;
   }

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was fixed in Visual C++ version 5.0.

MORE INFORMATION

To fix the problem in the TEAR sample, before the ReadString() call, add a call to SetReadBufferSize(). For example:

   TCHAR sz[1024];
   pFile->SetReadBufferSize(4096);  // Add this line
   while (pFile->ReadString(sz, 1023))
   {
       if (bStripMode)
       StripTags(sz);
       cout << sz;
   }


Additional query words: WinInet Tear

Keywords : MfcInetSDK vcbuglist420 vcfixlist500 kbprg kbbuglist kbfixlist
Technology : kbMfc
Version : 4.20
Platform : NT WINDOWS
Issue type : kbbug
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 19, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.