I want to fix problem reported by valgrind:
==7182== Conditional jump or move depends on uninitialised value(s)
==7182== at 0x40EC75C: strstr (in /lib/libc-2.9.so)
==7182== by 0x804A977: search_graph_begin (compression.c:462)
==7182== by 0x804AB60: search_graph_end (compression.c:497)
==7182== by 0x804AA97: search_graph_begin (compression.c:477)
==7182== by 0x804B59A: do_g_decompress (compression.c:767)
==7182== by 0x804996C: main (server.c:302)
my relevant part of code is:
void search_graph_begin(char* buf, FILE *dest,int* graph_count,int extension,
char* graphs,char* directory,int have)
{
char* begingraph = NULL;
begingraph = strstr(buf,"<GRAPH>");
if (begingraph != NULL)
{
if ( (int)(begingraph - buf) > 1)
{
printf("(int)(begingraph-buf) %d\n",(int)(begingraph-buf));
xwrite(dest,buf,(int)(begingraph-buf));
}
(*graph_count)++;
sprintf(graphs,"%s/tmp/graphs%d/graph%d",directory,extension,(*graph_count));
/*open file to save received graph data*/
FILE* graphfile = fopen(graphs,"wb");
if (graphfile == NULL)
fprintf(stderr,"could not create graph file\n");
search_graph_end(begingraph+strlen("<GRAPH>")+1,graphfile,dest,graph_count,extension,graphs,directory,
have-(begingraph+strlen("<GRAPH>")+1-buf));
}
else
{
if (have > 1)
xwrite(dest,buf,have);
buf = NULL;
}
}
void search_graph_end(char* buf, FILE* graphfile, FILE *dest,int* graph_count,int extension,
char* graphs,char* directory,int have)
{
char* endgraph = NULL;
endgraph = strstr(buf,"<GRAPH/>");
if (endgraph != NULL)
{
xwrite(graphfile,buf,sizeof(char)*(endgraph-buf));
fclose(graphfile);
search_graph_begin(endgraph+strlen("<GRAPH/>")+1,dest,graph_count,extension,graphs,directory,
have-(endgraph+strlen("<GRAPH/>")+1-buf));
}
else
{
if (have > 1)
xwrite(graphfile,buf,have);
buf = NULL;
}
}
the program runs fine under valgrind but its not the case when not. The idea of the program is to read in loop a buffer and write text between valise and in different files