FIX: AfxIsValidString() Returns TRUE for NULL Near Pointers

Last reviewed: September 18, 1997
Article ID: Q113535
1.00 WINDOWS kbprg kbfixlist kbbuglist

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, version 1.0
    

SYMPTOMS

Calling AfxIsValidString() with a near NULL pointer results in a TRUE return value. The return value should be FALSE because a NULL pointer is invalid.

For example, consider the following code:

   char NEAR * npszEmpty = NULL;
   ASSERT(AfxIsValidString(npszEmpty));

This should cause the assertion to fail but doesn't.

CAUSE

AfxIsValidString() takes an LPCSTR, which is defined in WINDOWS.H as:

   typedef const char FAR* LPCSTR; // far pointer to a read-only string

Therefore, the "char NEAR *" parameter is first converted to a LPCSTR or FAR pointer, which changes it from an offset-only value of 0x0000 to a selector:offset value of DS:0x0000. This value will not evaluate to NULL because the data segment register will most likely contain a nonzero value.

RESOLUTION

To work around this problem, do one of the following:

  • Upgrade to Visual C++ version 1.5. The Microsoft Foundation Classes (MFC) version 2.5 supplied with Visual C++ version 1.5 for Windows provides two versions of AfxIsValidString(). The first is the original function that takes an LPCSTR. The second version takes a "const char *". The pointer in the "const char *" version is compared first with NULL, then passed to the LPCSTR version.
  • Manually compare the string to NULL along with calling AfxIsValidString(). This is the technique used by MFC version 2.5. For example:

          char NEAR * npszEmpty = NULL;
          ASSERT(npszEmpty != NULL && AfxIsValidString(npszEmpty));
    

STATUS

Microsoft has confirmed this to be a problem in the Microsoft Foundation Classes version 2.0. This problem was corrected in the Microsoft Foundation Classes version 2.5.


Additional reference words: 1.00 2.00 return valid pointer
KBCategory: kbprg kbfixlist kbbuglist
KBSubcategory: MfcMisc
Keywords : kb16bitonly MfcMisc kbbuglist kbfixlist kbprg
Technology : kbMfc
Version : 1.00
Platform : WINDOWS
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 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.