-1

The line of code causing the problem is

char command_tb_temp[][1000]={"gcc -Wall ","-o3 -ftree-ccp -fno-align-jumps "," Scripts/*.c -o output -lm && time -f \"%e\" -o TB.log ./output 1.dat"};

When the same code is written by giving only 1 optimizing option like below, It does not return any errors.

char command_tb_temp[][1000]={"gcc -Wall ","-o3 -ftree-ccp "," Scripts/*.c -o output -lm && time -f \"%e\" -o TB.log ./output 1.dat"};

Please help me resolve this issue. Any help is highly appreciated. Thankyou.

Heres the whole function..

int findtb(int flag)
{   
printf("debug -1-1-1");
char command_tb_temp[][1000]={"gcc -Wall ","-o3 -ftree-ccp -fno-align-jumps "," Scripts/*.c -o output -lm && time -f \"%e\" -o TB.log ./output 1.dat"};

char command_tb[]="",line[100];

if(var[initial].exetime>0.00&&flag==1)
{   
    if(var[initial].rip<0.00)
        strcat(finalop,var[initial].name);
    else
        return 0;
}

strcpy(command_tb_temp[1],finalop);
//strcat(command_tb_temp[1]," -ftree-ccp ");        
for(int i=0;i<3;i++)
    strcat(command_tb,command_tb_temp[i]);  
printf("***** %s ****",command_tb);

system(command_tb);     
fp=fopen("TB.log","r");
fscanf(fp,"%s",line);   
tb=atof(line);
printf("\nTb=%f\n",tb); 
fclose(fp);
return 1;

}

The error is...

*** stack smashing detected ***: ./3 terminated
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
clu3Less
  • 1,812
  • 3
  • 17
  • 20
  • 1
    `-o3` is not an optimization switch. `-O3` is. – Mat Jan 08 '12 at 21:22
  • "lyk"? What options are different? What errors do you get? How are you using this variable, when it returns errors? You need to add more information to make this question readable, as well as answerable. – Alex Reynolds Jan 08 '12 at 21:23
  • 1
    The problem is probably *not* with that line, but with some other bit of code. Changing the line above probably only compounds the problem that is elsewhere (by moving things back and forth in the compiled binary). – dreamlax Jan 08 '12 at 21:28
  • yea. I tried debuggin. I found the program running without any errors when i commented out this line strcat(command_tb,command_tb_temp[i]); – clu3Less Jan 08 '12 at 21:35

1 Answers1

3

char command_tb[] = "" defines a character array of size 1, containing just a terminating null character.
strcat(command_tb,command_tb_temp[i]); then writes data to it.
But it writes more data than it can hold, thus corrupting other parts of memory.

You should make it large enough.

Also, it's advisable not to use strcat, strcpy, as they can easily exceed the buffer. Better use strncpy at others, which get the buffer size and won't write more. It's still your responsibility to provide the right size.
But beware of strncat - the meaning of its size parameter is misleading, so read its documentation with care, or just avoid using it.

ugoren
  • 16,023
  • 3
  • 35
  • 65