I stumbled on a runtime crash in Visual Studio 2017 in minkernel\crts\ucrt\src\appcrt\string\i386\strlen.asm after I modified a function. Here's the situation as a minimal example in Compiler Explorer:
#include <string>
//void func(int i); // Original function signature
void func(const std::string& str); // New function signature: 'int' parameter changed to 'std::string'
int square(int num) {
func(0); // This implicit conversion of '0' to 'any pointer'
//func(1); // This doesn't compile anymore, which is expected
//func((void*) 0); // This doesn't compile, why?
}
Compiler Explorer version here.. I tried several compilers and several versions to make sure this wasn't a specific issue.
Explanations:
Originally, my function had an integer parameter. I refactored it to accept a std::string parameter, and used 'Compile' to have the compiler find all calls and report them as errors for me; So I went over each of them and fixed them. For example, func(1);
no longer compiled as was changed to func("1");
.
However something unexpected happened with calls to func(0);
: They didn't provoke any compile-time errors, but instead crash at runtime! Apparently in C++, there is still an invisible/automatic conversion of the integer constant 0 to a mysterious pointer type. It doesn't appear to be void *
since func((void*) 0);
doesn't compile.
Question: Is there a way to disable this automatic process? Or can you help me understand what is happening according to the standard and why?