FIX: AfxIsValidString() Returns TRUE for NULL Near Pointers

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 kbnokeyword kbMFC kbVC
Issue type :
Technology : kbAudDeveloper kbMFC


Last Reviewed: May 5, 2001
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.