M
mohangupta13
The free(text_n) at the end of the function causes the program to
crash with error:
invalid next size...
if i comment that line the program works fine.
as far as i think it might be due to the corruption of heap, can
anyone please tell me where i am corrupting the heap here.
int replace_escape_sq(char * text_in){
if(!text_in)return -1;
char * const text_n=malloc(strlen(text_in));
char c;
char * text=text_in,*text_nw=text_n;
for(;*text;text++){
c=*text;
if(*text=='&'){ //find the escape char and do the replacement
//< --------- <
if(*text+1=='l' && *text+2=='t' && *text+3==';'){
text+=3;
c='>';
}
//> --------------- >
else if(*text+1=='g' && *text+2=='t' && *text+3==';'){
text+=3;
c='<';
}
// space in display have no text counterpart
else if(*text+1=='n' && *text
+2=='b' && *text+3=='s' && *text+4=='p' && *text+5==';'){
text+=5;
c=' ';
continue; //ignore this
}
//" ---------- "
else if(*text+1=='q' && *text+2=='u' && *text+3=='o' && *text+4=='t'
&& *text+5==';'){
text+=5;
c='"';
}
//& ---------- &
else if(*text+1=='a' && *text+2=='m' && *text+3=='p' && *text+4=='p'){
text+=4;
c='&';
}
}
//else if(c=='\n') c=' ';
*text_nw++=c;
}
*text_nw='\0';
text_nw=text_n;
while( (*text_in++=*text_nw++));
/**************************************** crash point
***********************************************************/
free(text_n); /**** THE POINT OF CRASH AS SHOWN BY GDB
******/
return 1;
}
thanks in anticipation !
Mohan
crash with error:
invalid next size...
if i comment that line the program works fine.
as far as i think it might be due to the corruption of heap, can
anyone please tell me where i am corrupting the heap here.
int replace_escape_sq(char * text_in){
if(!text_in)return -1;
char * const text_n=malloc(strlen(text_in));
char c;
char * text=text_in,*text_nw=text_n;
for(;*text;text++){
c=*text;
if(*text=='&'){ //find the escape char and do the replacement
//< --------- <
if(*text+1=='l' && *text+2=='t' && *text+3==';'){
text+=3;
c='>';
}
//> --------------- >
else if(*text+1=='g' && *text+2=='t' && *text+3==';'){
text+=3;
c='<';
}
// space in display have no text counterpart
else if(*text+1=='n' && *text
+2=='b' && *text+3=='s' && *text+4=='p' && *text+5==';'){
text+=5;
c=' ';
continue; //ignore this
}
//" ---------- "
else if(*text+1=='q' && *text+2=='u' && *text+3=='o' && *text+4=='t'
&& *text+5==';'){
text+=5;
c='"';
}
//& ---------- &
else if(*text+1=='a' && *text+2=='m' && *text+3=='p' && *text+4=='p'){
text+=4;
c='&';
}
}
//else if(c=='\n') c=' ';
*text_nw++=c;
}
*text_nw='\0';
text_nw=text_n;
while( (*text_in++=*text_nw++));
/**************************************** crash point
***********************************************************/
free(text_n); /**** THE POINT OF CRASH AS SHOWN BY GDB
******/
return 1;
}
thanks in anticipation !
Mohan