FIX: FindIndex Works Incorrectly When nIndex Is Negative

ID: Q173244


The information in this article applies to:
  • The Microsoft Foundation Classes (MFC), included with:
    • Microsoft Visual C++, 32-bit Editions, versions 2.0, 2.1, 2.2, 4.0, 4.1, 4.2, 5.0


SYMPTOMS

If a value less that 0 is passed to the FindIndex function, the function will ASSERT in debug mode and enter an infinite loop in release mode.


CAUSE

The problem is that an ASSERT statement is used to test for a negative value in the FindIndex function instead of an if...then statement that would return NULL if the value is negative.


RESOLUTION

Check the value that is passed to FindIndex to make sure that it is not negative before calling the FindIndex function.


STATUS

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


MORE INFORMATION

The documentation for the FindIndex function in the CList, CStringList, CPtrList, and CObList classes states that the return value is NULL if nIndex is negative or too large.

In release mode, the program may also generate the following application error:


   The instruction at 0x... referenced memory at "0x00000000".
   
The memory could not be "read". 


REFERENCES

See AFXTEMPL.H for CList, LIST_S.cpp for CStringList, LIST_P.cpp for CPtrList, and LIST_O.cpp for CObList implementation details. The header file can be found in the in the ...\VC\mfc\include directory and the source files can be found in the ...\VC\mfc\src directory.

Additional query words:

Keywords : kbnokeyword kbMFC kbVC kbVC200bug kbVC210bug kbVC220bug kbVC400bug kbVC410bug kbVC420bug kbVC500bug kbVC600fix
Version : WINDOWS NT:2.0,2.1,2.2,4.0,4.1,4.2,5.0
Platform : NT WINDOWS
Issue type : kbbug


Last Reviewed: July 27, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.