Is this (crash) a bug in VS2012 c++ compiler - c++

I was compiling the following code in c++, Visual Studio 2012 (Professional, Update 4)
class dum {
stringstream *ss;
~dum() {
delete ss;
}
public:
dum() : ss(NULL) {}
};
int main()
{
dum a;
return 0;
}
Now I know that the private destructor would force heap allocated objects only, but I would expect a compile error for that. Instead I get a window titled "Microsoft (R) C/C++ Optimizing compiler" saying
Microsoft (R) C/C++ Optimizing compiler has stopped working. Windows can check online for a solution to the problem
and then the usual prompts to go online where nothing happens (or gets resolved). Am I doing something wrong or have I stumbled upon a bug in the compiler?
EDIT
The code I'm posting is all that's present in a win32 console program (even the main() has this no arguments form) and the only header included is sstream.
If you move the destructor to the public section of the class we no longer have a crash, but as I mentioned above this should be a cause for compilation error (namelly cannot access private member declared in dum) and not for this pop-up. The question's targeted to people that can provide an intrinsic or two about what's the problem with the compiler here, I've seen similar problems before, but that's the smallest code segment that caused such a thing.

Trying to compile the fixed version:
#include <sstream>
using namespace std;
class dum {
stringstream *ss;
~dum() {
delete ss;
}
public:
dum() : ss(NULL) {}
};
int main()
{
dum a;
return 0;
}
gives following compile error for me.
Tried this with VS2012 Ultimate Version 11.061030.00 Update4.
1>------ Build started: Project: dum, Configuration: Debug Win32 ------
1> dum.cpp
1>c:\users\randmaniac\documents\visual studio 2012\projects\dum\dum\dum.cpp(19): error C2248: 'dum::~dum' : cannot access private member declared in class 'dum'
1> c:\users\randmaniac\documents\visual studio 2012\projects\dum\dum\dum.cpp(8) : see declaration of 'dum::~dum'
1> c:\users\randmaniac\documents\visual studio 2012\projects\dum\dum\dum.cpp(6) : see declaration of 'dum'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
No crash for me on a fairly recent installation of VS2012.

Related

__declspec(noinline) on lambda function inside template class throws syntax error in VS2017

While trying to debug an issue with an assert macro I came across this problem. Using __declspec(noinline) on a lambda function in a template class generates a syntax warning in Visual Studio 2017:
error C2760: syntax error: unexpected token '__declspec', expected '{'
This is the failing code:
template<class R>
class test
{
public:
void DoStuff()
{
[]() __declspec(noinline) { }; // syntax error
}
};
int WinMain(void)
{
return 0;
}
If I go to my project settings and switch my platform toolset from v141 (vs2017) to v140 (vs2015) in the general section of the project properties dialog the error goes away .
If I change the class to not be a template class it also compiles correctly:
class test
{
public:
void DoStuff()
{
[]() __declspec(noinline) { }; // compiles fine
}
};
int WinMain(void)
{
return 0;
}
I'm curious why this wouldn't succeed using the v141 platform toolset. Is there some other project setting that could be affecting this?
I was able to fix this by updating Visual Studio 2017 to the latest version (15.9.7). Previously I was running version 15.6.7. Thanks to everyone who looked in and commented! :)
It works in VC++ 2019, so may just be a regression in 2017?

c++ std::sort intel compiler error : access violation

Why does this simple c++ code snippet do not compile?
#include <algorithm>
#define SIZE (1000)
struct S {
int *vect;
};
int main() {
struct S* s = static_cast<struct S*>(malloc(sizeof(struct S)));
s->vect = static_cast<int*>(malloc(sizeof(int) * SIZE));
for(int i = 0; i < SIZE; i++) {
s->vect[i] = i;
}
std::sort(s->vect, s->vect + SIZE);
}
The compiler returns the following error related to the std::sort call
1>C:\Program Files (x86)\Microsoft Visual
Studio\2017\Enterprise\VC\Tools\MSVC\14.12.25827\include\algorithm(3138):
error : access violation
1> return (pair<_RanIt, _RanIt>(_Pfirst, _Plast));
1> ^
I'm using visual studio enterprise 2017 version 15.5.2 and the intel compiler 64 bit version 17.0.4.210 Build 20170411.
The code is successfully compiled using the default visual studio compiler.
Can't find out what I'm doing wrong.
I've found out that unfortunately visual studio update 15.5.x breaks Intel Compiler 2017 as can be seen in the intel forum where I asked this same question. Hope it will be useful to others too.

Why does const int change values

Im starting to learn c++ and was under the impression that by putting const is means that the value wont change but i wrote the following code:
#include<iostream>
int main()
{
const int a = 1;
a += 1;
std::cout << a << std::endl;
return 0;
}
and it prints out 2 while i thought it would have given me an error for changing a const int value.
I am using MSVS as my compiler
EDIT: I get a compiler warning saying C4530: C++ exception handler used, but unwind semantics are not enabled, specify /EHsc
It works now and gives me the correct error but does anyone know what this means
This program cannot be compiled using GNU GCC 4.8:
alioth% g++ x.cpp
x.cpp: In function ‘int main()’:
x.cpp:6:7: error: assignment of read-only variable ‘a’
a += 1;
Either your compiler is broken or you are doing something wrong (like compiling different project).
This program cannot be compiled on VS2013:
1>------ Build started: Project: SOTesting, Configuration: Release Win32 ------
1> Source.cpp
1>Source.cpp(6): error C3892: 'a' : you cannot assign to a variable that is const
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========`
The posted code:
#include<iostream>
int main()
{
const int a = 1;
a += 1;
std::cout << a << std::endl;
return 0;
}
The claim that this code compiled and produced "2" as output, is incorrect.
You can easily get the impression of something like that by inadvertently compiling a different program, or not noticing that a compilation failed and then running an existing executable.

Compiler Bug in Visual C++ 10.0 SP1 - cl.exe version 16.0.40219.1 Access violation [confirmed]

I have ran into a problem compiling some template code with Visual Stuido 2010 SP1, cl.exe version 16.0.40219.1
The following code will cause the compiler to access violate:
template<typename T>
class A
{
A(){}
};
template<typename T>
class B : public A<T>
{
using A::A(); // Compiler access violates
// **EDIT**
//using A<T>::A<T>; // Compiler succeeds
//using A<T>::A(); // Compiler reports error
};
int main(int argc, char* argv[])
{
return 0;
}
It generates the following error (in addition to the "cl.exe has stopped working, C0000005 exception):
1>d:\projects\cpptest\cpptest\cpptest.cpp(11): fatal error C1001: An internal error has occurred in the compiler.
1> (compiler file 'msc1.cpp', line 1420)
1> To work around this problem, try simplifying or changing the program near the locations listed above.
The code compiles fine (well, that is, it emits a proper error message and doesn't crash the compiler) in Dev-C++ with g++.
main.cpp:11: error: `template<class T> class A' used without template parameters
main.cpp:11: error: expected nested-name-specifier before "A"
main.cpp:11: error: using-declaration for non-member at class scope
main.cpp:11: error: expected `;' before '(' token
main.cpp:11: error: expected unqualified-id before ')' token
make.exe: *** [main.o] Error 1
EDIT
The following, however, compiles fine, without access violation, so it seems this is related to templates:
class A
{
A(){}
};
class B : public A
{
using A::A;
};
int main(int argc, char* argv[])
{
return 0;
}
Do you think this is worth reporting to Microsoft? Can anyone else reproduce this?
Maybe try in Visual Studio 2013 to see if it still occurs?
Since this is reproducible by others on Visual C++ platforms, I have opened a bug report on Microsoft Connect as "answer".
Also, as workaround, the following syntax works:
using A<T>::A<T>;
Update 2013-12-06: Microsoft has confirmed the issue and the issue will be fixed in the Visual Studio 2013 C++ compiler.

running shellcode + vs2010

I just tried the following code snippet for shellcode testing purposes:-
#include<iostream>
using namespace std;
char sc[] = ""; #i've removed the shellcode
int main() {
int (*func)();
func = (int(*)())sc;
(int)(*func)();
}
I get a build error on compilation :-
------ Build started: Project: shellcoderunner, Configuration: Debug Win32 ------
Build started 10/15/2011 12:51:16 PM.
InitializeBuildStatus:
Touching "Debug\shellcoderunner.unsuccessfulbuild".
ClCompile:
blah.cpp
c:\users\reverser\documents\visual studio 2010\projects\shellcoderunner\shellcoderunner\blah.cpp(7): error C2440: 'type cast' : cannot convert from 'char [149]' to 'int (__cdecl *)(void)'
There is no context in which this conversion is possible
Build FAILED.
Time Elapsed 00:00:01.99
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Something obvious that I'm doing wrong?
To execute a shellcode in your C/C++ program with VS, the simplest way is embedding an Assembly code like this example below:
char* buffer="blah blah blah";
int main() {
__asm{
lea eax, buffer
call eax
}
}
Hope this help!
[
At the time I am answering the question is about why compilation fails for …
#include<iostream>
using namespace std;
char sc[] = ""; #i've removed the shellcode
int main() {
int (*func)();
func = (int(*)())sc;
(int)(*func)();
}
This code is an attempt to execute data bytes as machine code. However, the OP calls this a “code snippet for shellcode testing purposes”, which is unrelated. And so I am including this original context.
]
You may have success using a void* as intermediary.
In the formal even that should not compile, because in the formal a data pointer cannot be converted to a function pointer or vice versa.
However, reportedly Posix requires the ability to do that conversion, and it's old existing practice, so I believe most if not all compilers support it.
Note that you are in UB-land as regarding effects.
Also, note that anti-virus software and page level execute permission checking may disagree a bit with trying to execute the bytes in a string as machine code, so at that higher level yes you're doing something obviously wrong. ;-)
By the way, if what you are trying to achieve is to execute a shell script, then look into the system function.
What command to pass in the system call would depend on your system, so if you change your question be sure to include information about that.
Cheers & hth.,
I think the following should work:
char sc[] = ""; // i've removed the shellcode
int main()
{
int (*func)() = (int(*)())sc; // C++
int (*func)() = sc; /* C */
func();
}
It's technically undefined behaviour, but then again that's the whole point of shellcode.
You cannot cast an array to a function pointer. You have to first acquire a pointer to the array, which then can be cast:
func = (int(*)())&sc;