*** stack smashing detected ***
error occurs when as the name suggests, you smash the stack, meaning that you have a buffer overflow and the canary gets overwritten by a different value.
This is a security mechanism implemented by gcc/g++ to prevent buffer overflow exploits using -fstack-protector
.
To avoid this error, disable fstack-protector
in gcc while compiling the code using
g++ myProgram.c -o myProgram -fno-stack-protector
Edit 1
However, disabling stack-protection will remove this error but you might get segmentation fault
as a result of overwriting stack.
If it is a computer security assignment where you are working on a buffer overflow exploit then you need to figure out to bybass these security mechanism, if you are not familiar with it, then somehow you are overflowing buffer and without looking at code I can't comment much where exactly the problem is.
to see if a data from vector 1 appears in vector 2 this indeed seems like buffer overflow kind of assignment where you are required to overwrite contents of arrays from one another.
The fact this error is not consistent because sometimes the canary does not get overwritten (fine run of program) or overwritten by the same exact value and sometimes the canary gets overwritten with a different value leading to this error.
You need to configure your compiler to make it easier for buffer overflow.
Edit 2
Your program behaviour is random because probably you have not disabled ASLR (Address space layout randomization). When you compile your program your compiler gcc/g++ optimize your executable for security mechanisms to prevent buffer overflow exploits.
Address space layout randomization (ASLR) is a computer security technique involved in preventing exploitation of memory corruption vulnerabilities. In order to prevent an attacker from reliably jumping to, for example, a particular exploited function in memory, ASLR randomly arranges the address space positions of key data areas of a process, including the base of the executable and the positions of the stack, heap and libraries.
It means the address space allocation is random and the bytes between your vectors is different every time. Sometimes the overflown buffer does not reaches to canary and sometimes it does.
To get the consistent behaviour every time, you need to disable ASLR as well. ASLR support is provided by your OS. To disable ASLR, on linux it is disabled by setting randomize_va_space
to 0. It can be achieved by
echo 0 > /proc/sys/kernel/randomize_va_space