Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

how do I find what is causing stack corruption?

void modify_material_file2(FILE *input_f,FILE *output_f,float *rho,int *mids,int num_ids)
{
int i=0,k=0,id=0,flag=0,change=0,num=0,format=0;
char dummy_str[200]="",dummy_str2[17]="",dummy_str3[200]="",ent[9]="",dummy_str4[10]="",dummy_str5[50]="";
char *ptr=NULL,*name=NULL,*ext=NULL,new_ext[20]="";
FILE *new_input=NULL,*new_output=NULL;
int adfdebug=0;
rewind(input_f);
do{
adfdebug++;
/*strcpy(dummy_str,"\n");*/
if (!fgets(dummy_str,200,input_f)) continue;
sscanf(dummy_str,"%s '%[^']s",dummy_str4,dummy_str5);
if (!strcmp(dummy_str4,"INCLUDE")){
do new_input=fopen(dummy_str5,"r");
...
}while(!feof(input_f));
// error occurs here....
}
Run-Time Check Failure #2 - Stack around the variable 'dummy_str4' was corrupted.
I get the above failure after modify_material_file2 is done and just before it returns to the calling line.
When compiled under VS6, this error does not occur. Under VS2003, it does.

From what I can gather, this error is caused by some type of overwriting neighbor memory? I'm guessing it's the sscanf that is causing this.

how do I figure out exactly what is causing the problem? the parts involving dummy_str4 are all listed above. It is not referenced anywhere else.

There are some here that say vs6 works so use it....but I'd rather not live with something that MAY cause problems later on.

Thanks!
[1577 byte] By [rssmps] at [2007-11-11 8:00:47]
# 1 Re: how do I find what is causing stack corruption?
Try to isolate the error and step with the debugger. If you can find the region the error is in, you can often simply spot the error by reading the code.

I suspect the feof function is trying to read past the end of file marker -- this is an off by one error. The deal is, you have to READ the eof, if you have not, you are not there. However, if its the only thing left in the file, and you try to read a record, its a foul up. Try rearranging the logic to a (while !eof) (you will want to read one record to prime the while, possible, depending on your needs) and see if that cures it.
jonnin at 2007-11-11 21:01:46 >
# 2 Re: how do I find what is causing stack corruption?
Thanks, I figured it out last night. As it turned out, it was caused by the size of dummy_str4 being only 10.

while all the keywords normally are withing 10 chars, some lines such as comment separators (eg. //***...***) are always longer then 10.
So VS6 must have allowed this overwriting of extra characters.
I solved this issue by changing the size of each string to 1000 and the problem went away.
rssmps at 2007-11-11 21:02:42 >
# 3 Re: how do I find what is causing stack corruption?
Firest fo all, I'm surprised that your code compiled! the initailizer of dummy_str4 is too long to fit into 10 chars. This error could have been detected at compile time.
As for the error itself: VC 6.0 didn't have the same runtime auxiliary facilties to detect stack corruption so it didn't report this error. This certainly doesn't mean that yoru code was OK.
Danny at 2007-11-11 21:03:47 >