c++ - Segmentation fault : Address out of bounds for a pointer in C -


i trying build , run complicated code written else, don't know , can't ask them help. code reads bpf (brain potential file) , converts readable ascii format. has 3 c files, , 2 corresponding header files. got build minor changes, crashes segmentation fault.

i narrowed problem down findsectionend() (in readbpfheader.c) , find error occurs when sscanfline() (in file sscanfline.c) called (code both below).

ui1 defined unsigned char. si1 defined char.

just before returning sscanfline(), address pointed dp 0x7e5191, or similar ending 191. however, on returning findsectionend(), dp points 0x20303035 , says 'address 0x20303035 out of bounds', causes fault @ strstr(). loop in findsectionend() runs without problem 14 iterations before fault occurs. have no idea going wrong. hope information have given here adequate.

ui1 *findsectionend(ui1 *dp) {     si1 line[256], string[256];     int cnt=0;     while (sscanfline(dp, line) != eof){         dp = (ui1 *)strstr(dp, line);         dp+= strlen(line);         sscanf(line,"%s",string);         if(sectionend(string))             return(dp);     }     return(null); }  si1 *sscanfline(ui1 *dp, si1 *s) {     int = 0;      *s = null;     int cnt = 0;     while (sscanf(dp, "%c", s + i) != eof){         cnt++;          dp++;         if(*(s + i) == '\n') {             *(s + + 1) = '\0';             return s;         }         ++i;     }     *(s + i) = '\0';     return s; } 

the sscanfline function doesn't respect size of buffer passed in, , if doesn't find '\n' within first 256 bytes, happily trashes stack next line array.

you may able work around making line bigger.

if you're going improve code, should pass buffer size sscanfline , make stop when count reached if newline wasn't found. while you're @ it, instead of returning s, caller has, make sscanfline return new value of dp, save caller needing use strstr , strlen.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -