5

What is wrong with the following code (crashes):

#include <string>
#include <iostream>

void foo(std::string str, unsigned long long val)
{
    std::cout<< str<< " "<< val<< std::endl; // Crashes if commented as well
}

int main()
{
    double d = 30.0;
    foo("abc", d);
}

Visual Studio 2012/ Debug/ Win32

It works in x64 as well as if we change the unsigned long long to long long or even unsigned long

This code snippet crashes. In other scenarios the addresses of the function parameters are changed.

I suspect it has something to do with _stol2 that converts the double to unsigned long long

Xyand
  • 4,290
  • 3
  • 33
  • 59
  • Try it without the initial argument, the string. – unwind Dec 05 '12 at 13:50
  • 1
    Have you tested this MWE? There's nothing wrong with it visually, nothing wrong with it per the spec, nothing wrong with it with the multiple compilers that I have (but I don't have Visual Studio). – David Hammen Dec 05 '12 at 13:58
  • It works as expected on Visual studio 2010/debug/win32, I can't try it on 2012 at the moment but it should work on any compiler as far as I know. – jcoder Dec 05 '12 at 14:05
  • 2
    The code generator and optimizer parts in VS2012 that deal with floating point values were drastically rewritten to support the new auto-vectorizing feature. This has caused some breakage similar to this, the shoe fits. First check if disabling whole-program optimization solves the problem. Post your problem to connect.microsoft.com to get feedback from the people that can fix this bug. – Hans Passant Dec 05 '12 at 15:34
  • MWE = Minimal Working Example. In other words, *Did you test this specific code?* If you did, it's a very nice short and simple example to submit to Microsoft as a bug. (And if it does break, it most certainly is a bug.) – David Hammen Dec 05 '12 at 19:09
  • @DavidHammen, I did. I'll test it in a couple more build configurations and submit it. Thanks. – Xyand Dec 05 '12 at 19:37
  • @HansPassant. Whole-program optimization was off. Thanks. – Xyand Dec 05 '12 at 19:38
  • [Submitted to Microsoft](https://connect.microsoft.com/VisualStudio/feedback/details/773581/casting-double-to-unsigned-long-long-causes-memory-stack-corruption) – Xyand Dec 10 '12 at 08:12
  • @Albert - Please add your own answer saying "Submitted to Microsoft" and then select it as the best answer so this stops coming up in "unanswered questions". Thanks! – phonetagger Dec 24 '12 at 01:00

1 Answers1

1

Submitted to Microsoft as a bug (Accepted)

Xyand
  • 4,290
  • 3
  • 33
  • 59