FIX: Cl.exe throws a buffer overflow error in Visual C++ .NET (321653)



The information in this article applies to:

  • Microsoft Visual C++, 32-bit Editions 6.0
  • Microsoft Visual C++ .NET (2002)

This article was previously published under Q321653

SYMPTOMS

In Microsoft Visual C++ .NET, when you compile an application by using the C/C++ compiler (Cl.exe), you may receive the following error message:
---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Buffer overrun detected!

Program: ...es\Microsoft Visual Studio .NET\VC7\BIN\cl.exe
A buffer overrun has been detected which has corrupted the program's internal state. The program cannot safely continue execution and must now be terminated.

---------------------------
OK
---------------------------
In Visual C++ 6.0, you may receive a C1001 error message such as the following:
"fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1794)
Please choose the Technical Support command on the Visual C++ Help menu, or open the Technical Support help file for more information"

CAUSE

The compiler overflows a buffer that is intended to hold a precompiled header file name and, because Cl.exe is compiled with the Buffer Security Check flag (/GS), the buffer overflow warning dialog box appears and the process ends.

In Visual C++ .NET, the error occurs when you compile with the Automatic Use of Precompiled Headers switch (/YX) and you have a TMP environment variable that has a string length greater than or equal to 52 characters (for example, C:\Documents and Settings\Username\Local Settings\Temp).

In Visual C++ 6.0, the error occurs when you compile with /YX and the TMP environment variable's length is greater than or equal to 55 characters.

RESOLUTION

Make sure that the string length of the TMP environment variable is less than 52 characters.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section. This bug was corrected in Microsoft Visual C++ .NET 2003.

MORE INFORMATION

Steps to reproduce the behavior

  1. Create a new folder in your system with a path name that is longer than 52 characters. For example, C:\Thisisalongfoldernamethisisalongfoldernamethisisalongfoldername
  2. Open a Command Prompt window. At the command prompt, type the following to set the TMP environment variable:

    set TMP=C:\thisisalongfoldernamethisisalongfoldernamethisisalongfoldername
  3. Verify that the previous command succeeded by typing the set command. TMP should have the entry that you set in the previous step.
  4. Create a new .cpp file. In this file, paste the following sample code:
    // test.cpp
    #include <iostream>
    using namespace std;
    
    void main()
    {
    	cout << "Hello world!";
    }
    					
  5. Compile the .cpp file in the Command Prompt window where you set the TMP environment variable. Type the following command to compile the program:

    cl /YX test.cpp

    Note You may have to set the necessary environment variables before you run this command. To do this, run Vsvars32.bat, which is in \Program Files\Microsoft Visual Studio .NET\Common7\Tools.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MinorLast Reviewed:1/6/2006
Keywords:kbfix kbbug kbCompiler kbLangCPP kbpending KB321653