tags:

views:

120

answers:

1

input .txt contents

start_point(0)
end_point(100)
turtle_speed(3)
hare_speed(6)
hare_sleep_time(5)
hare_turtle_dist(20)
process_delay(1)    

stack smashing error occurs if i put hare-TURTLE_DIST GREATER THAN 10

code

#include<stdio.h>
#include<stdlib.h>

void get(int *a,FILE *fp,int i,int j){
    char str[19];
    int input=0;
    fscanf(fp,"%s",str);
    while(str[i]!=')'){
        input=input*10+str[i]-48;
        i++;
    }
    a[j]=input;
    printf("%d\n",input);
}

main(){
    int a[7];
    FILE *fp;   
    int i,j=0;
    fp=fopen("input.txt","r");
    if (fp == NULL) {
        fprintf(stderr, "Can't open input file in.list!\n");
        exit(1);
    }
    i=12;

    get(a,fp,i,j);
    j++;
    i=10;
    get(a,fp,i,j);
    j++;
    i=13;
    get(a,fp,i,j);
    j++;
    i=11;
    get(a,fp,i,j);
    i=16;
    get(a,fp,i,j);
    j++;
    i=17;
    get(a,fp,i,j);
    j++;
    i=14;
    get(a,fp,i,j);
}
+2  A: 

In function get you have

char str[19];

which means you are assuming that every string read into it will have at max 18 char. But your input file has the line:

hare_turtle_dist(20)

which is of 20 char. This leads to crash.

To solve this increase the size of the char array to at least 21.

codaddict
thanks sir.......
sahil