Search

C++ compiler pointer arithmetic problem (wrong result with /Od optimization) by Ilias A

Active

1
0
Sign in
to vote
Type: Bug
ID: 778524
Opened: 2/5/2013 12:52:03 AM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
When trying to get a sum of 2 numeric value of addresses that belong to char buffer one of the terms is ignored depending on the presence of parentheses. This only happens with /Od optimization. With optimizations /O1 and /O2 the result of computation is always correct.
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling Version

Visual Studio 2012

Steps to reproduce

declare global char buffer, for example:

unsigned char tmp_arr[16];

and in main function local buffer:
unsigned char buffer[16];

try computing next 3 values

v1 = (unsigned long)buffer + 1 + (unsigned long)tmp_arr + 1;
v2 = (unsigned long)(buffer + 1) + (unsigned long)(tmp_arr +1);
v3 = (unsigned long)buffer + 1 + 1 ;

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

v1 = (unsigned long)buffer + 1 + (unsigned long)tmp_arr + 1;
v2 = (unsigned long)(buffer + 1) + (unsigned long)(tmp_arr +1);
v3 = (unsigned long)buffer + 1 + 1 ;

Here v2 == v3

Expected results

v1 = (unsigned long)buffer + 1 + (unsigned long)tmp_arr + 1;
v2 = (unsigned long)(buffer + 1) + (unsigned long)(tmp_arr +1);
v3 = (unsigned long)buffer + 1 + 1 ;

Expected v1 == v2
File Attachments
File Name Submitted By Submitted On File Size  
code.zip 2/5/2013 826 bytes
Sign in to post a comment.
Posted by Microsoft on 2/19/2013 at 1:25 PM
Hi, thanks for the bug report. I have confirmed that this is a compiler bug when optimizations are not enabled. Even without optimization, the compiler attempts to generate good code for x86 by combining elements into an efficient address mode (that can do base + index * scale addressing).

In this case, the compiler is failing to include the address of tmp_arr in the address mode correctly.

You can work around the bug by using a different code pattern (as in the assignment to v1) or by introducing a local variable for the second calculation ((unsigned long)(tmp_arr) + 1).

Thanks,
Mark Levine
Microsoft VC++ team
Posted by Microsoft on 2/5/2013 at 2:47 AM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 2/5/2013 at 1:50 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.