开发者

valgrind conditional jump or move depends on uninitialised value

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 0x8开发者_如何学编程04A977: 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


A program that crashes in one environment, but not in a slightly different environment (under Valgrind, in gdb, different -O) is a telltale sign of undefined behaviour caused by a bug. The thing is though that the actual bug (eg. an off-by-one write) can be located anywhere in your program. The stack trace only tells you where the bug was detected. You need to look beyond the stack trace to find the actual bug. What part of your program is responsible for initializing the value that Valgrind complains about?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜