- #1
jk22
- 731
- 24
// Example of variable argument number function call and return adress deviation programm
// never program in a style like this
What it actually prints when runned is :
Hello
Bonjour
Salut
I come here // it is in the replace function for the exit
Return to main // it returns to main
Segmentation fault // when quitting the program error
I don't understand, maybe the stack is not treated in a good way ?
// never program in a style like this
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void (*ret)();
void replaceexit(void);void prtarg(char *arg, ...)
{
char **stack=&arg;
printf("Return adr : %x\n",(int)(*(stack-1)));
ret=(void (*)())*(stack-1);
*(stack-1)=(char *)(replaceexit);
while(*stack)
{
printf("%s",*stack);
stack++;
}
}
void replaceexit(void)
{
printf("I come here\n");
(*ret)();
}
int main(void)
{
char *arg1;
arg1=new char[10];
strcpy(arg1,"Hello\n");
prtarg(arg1,"Bonjour\n","Salut\n",0);
printf("Return to main\n");
}
What it actually prints when runned is :
Hello
Bonjour
Salut
I come here // it is in the replace function for the exit
Return to main // it returns to main
Segmentation fault // when quitting the program error
I don't understand, maybe the stack is not treated in a good way ?
Last edited by a moderator: