2010-09-29 58 views
1

我想修復由Valgrind的報告的問題: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但它運行良好不是時不時。該程序的想法是讀入循環緩衝區並在不同文件之間寫入文本

+1

這似乎是buf是不正確形成的字符串。如果插入語句printf(「%s」,buf),會發生什麼情況;就在valgrind抱怨的那條線之前? – 2010-10-01 16:58:33

回答

1

在一個環境中崩潰的程序,但不是在稍微不同的環境中(在Valgrind中,在gdb中,不同於-O )是一個由錯誤引起的未定義行爲的跡象。問題是,在你的程序中,實際的錯誤(例如,一次寫入)可以在的任何位置找到。堆棧跟蹤僅告訴你錯誤是檢測到的地方。您需要查看堆棧跟蹤以查找實際的錯誤。你的程序的哪個部分負責初始化Valgrind抱怨的價值?

相關問題