Re: Stack overflow..



On Jun 1, 12:29 am, Tauno Voipio <tauno.voi...@xxxxxxxxxxxxx> wrote:
praveenraj1...@xxxxxxxxx wrote:
On May 31, 1:14 am, Nate Eldredge <n...@xxxxxxxxxx> wrote:
praveenraj1...@xxxxxxxxx writes:
does stack issue a segmentation fault signal in unix/linux??
It does on all the machines I tried.  Why not try it on yours and see?

#include<iostream>

int main()
{
int b=50;
int a[2]={10,100};
a[-1]=200;
a[-2]=250;
a[-1000]=300;

std::cout<<"b = "<<b<<" "<<&b;
std::cout
<<std::endl<<"a0 = "<<a[0]<<" "<<&a[0]
<<std::endl<<"a1 = "<<a[1]<<" "<<&a[1]
<<std::endl<<"a2 = "<<a[2]<<" "<<&a[2]
<<std::endl<<"a3 = "<<a[3]<<" "<<&a[3]
/*b=a[3] beyond that stack starts writing on memory that is beyond its
stack frame..
return address and other things.. do not to write above that..
even if u overwrite it and get segmentation fault..
that is because u have overwritten the return address and jump to that
memory after functin ends, gives u SEG FAULT..
or your code is trying to access memory that is not on stack and is on
different PAGE.. as by giving a very high aray bound..
which makes it point to past the stack area....
so it is valid to write any where on STACK..*/
<<std::endl<<"a-1 = "<<a[-1]<<" "<<&a[-1]
<<std::endl<<"a-2 = "<<a[-2]<<" "<<&a[-2]
<<std::endl<<"a-3 = "<<a[-1000]<<" "<<&a[-1000];
}

compiled withs gcc...
stack is growing downwards so i have used -ve subscript..

You are messing with the stack area outside the
allocated array boundaries.

yes, but can writing to stack area may lead to segmentation fault.
getting down to assembly.. as the ESP is pointed to some segment.. if
we write on to the stack then can it generate me SEGMENTATION FAULT
as if we try to access to stack area by some pointer and write it..

if we modify the return address then it may get me a SEGMENTATION
FAULT as jumping to the modified return address may get me to some
page that is wrong or not accessible..

The compiled behaviour is *undefined* in this case.

yes, i know thats undefined by C++ standrds.
but i posted this to know what really goes behind the scene. its
undefined because it may not be portable.. to all the systems..
but if i ask it to be specifically.. on GNU compiler and UNIX
systems..


There are no guarantees whatsoever which data you
clobbered. Please understand that the compilers will
do some operations with functions in compiler support
library, and they may use the stack are written to.

i agree, return address may be pushed with base pointer...
but writing to these memory should not get me SEGMENTATION FAULT

SIGSEGV signal should only be signaled by the OS to the application if
it accesses a wrong memory..

and i think even if i overwrite the return address then OS should not
signal SIGSEGV
but it should if after making the jump to the memory specified by
modified return address is in wrong segment..

SIGSEGV should not be issued for just writing to return address..
if it were to be issued on modifying return address then stack
overflow problem should not be there


--

Tauno Voipio
tauno voipio (at) iki fi

.