FIX: AfxIsValidString() Returns TRUE for NULL Near Pointers

ID: Q113535

1.00 WINDOWS kbprg kbfixlist kbbuglist

The information in this article applies to:

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:

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 kbnokeyword kbMFC kbVC kbbuglist kbfixlist
Version           : 1.00
Platform          : WINDOWS
Solution Type     : kbfix

Last Reviewed: September 21, 1997