【问题标题】:valgrind conditional jump or move depends on uninitialised valuevalgrind 条件跳转或移动取决于未初始化的值
【发布时间】:2011-04-18 21:03:36
【问题描述】:

我想修复 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)

我的相关代码部分是:

    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;
 }
    }

程序在 valgrind 下运行良好,但不是这样。该程序的想法是循环读取缓冲区并在 valise 和不同文件中写入文本

【问题讨论】:

  • 看来 buf 不是格式正确的字符串。如果插入语句 printf ("%s", buf); 会发生什么?就在 valgrind 抱怨的那一行之前?

标签: c valgrind


【解决方案1】:

程序在一个环境中崩溃,但在稍微不同的环境(在 Valgrind 下,在 gdb 中,不同的 -O)中没有崩溃,这是由错误引起的未定义行为的明显标志。问题是,实际的错误(例如,一次写入)可以位于您程序中的任何地方。堆栈跟踪仅告诉您错误在哪里检测到。您需要超越堆栈跟踪才能找到实际的错误。程序的哪一部分负责初始化 Valgrind 抱怨的值?

【讨论】:

    猜你喜欢
    • 2016-07-24
    • 2014-11-30
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 2016-09-03
    相关资源
    最近更新 更多