BUG: /Oc Optimization Can Corrupt Stack With Inline Assembly

Last reviewed: July 22, 1997
Article ID: Q115702
6.00 6.00a 7.00 | 1.00 1.50
MS-DOS           | WINDOWS
kbtool kbbuglist

The information in this article applies to:

  • The Microsoft C/C++ Compiler (CL.EXE), included with:

        - Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax
        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
    

SYMPTOMS

The use of the /Oc optimization (Enable Block-Common Subexpression Optimization) on inline assembly code which uses the stack may corrupt the stack. The source code below can be used to illustrate this problem.

RESOLUTION

There are two workarounds to this problem:

  1. Use the fast compiler option /f if you are using the C/C++ version 7.0 or later, or else use the /qc option if using earlier versions.

    -or-

  2. Disable optimization during the function by using the optimize pragma:

          #pragma optimize("",off)
    

          void bad_function(void)
          {
          /* ... */
          }
    

          #pragma optimize("",on)
    

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed above. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

This is not a problem in Visual C++, 32-bit Edition.

MORE INFORMATION

The following code can be used to demonstrate this problem.

Sample Code

/* Compile options needed: /c /Oc
*/

void _cdecl b(int c,int d);

void    a(void)
{
_asm pushf _asm cli

       b(1,2);

_asm popf
        return;
}


Additional reference words: 6.00 6.00a 7.00 8.00 8.00c 1.00 1.50
KBCategory: kbtool kbbuglist
KBSubcategory: CLIss
Keywords : kb16bitonly


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: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.