2011-02-23 69 views
5

我有調用getline()的功能,其內容從一個標準的文本文件行。這些行以\r\n結尾,因爲這是規範要求的,因爲它是VCARD文件的「互聯網標準」。與函數getline Valgrind的捐贈錯誤()

不管怎樣,我通過運行Valgrind的在openSUSE 11.3這個寶貝了最新的GCC和Valgrind的版本;沒有錯誤。然而,對於沒有關聯到這個問題的原因,我切換到發行版備份軌道4(Ubuntu的基礎),現在我得到一噸Valgrind的錯誤:

==21536== Conditional jump or move depends on uninitialised value(s) 
==21536== at 0x40164A1: (within /lib/ld-2.11.1.so) 
==21536== by 0x4007C04: (within /lib/ld-2.11.1.so) 
==21536== by 0x4002A2C: (within /lib/ld-2.11.1.so) 
==21536== by 0x4014206: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000856: (within /lib/ld-2.11.1.so) 
==21536== 
==21536== Conditional jump or move depends on uninitialised value(s) 
==21536== at 0x40164AC: (within /lib/ld-2.11.1.so) 
==21536== by 0x4007C04: (within /lib/ld-2.11.1.so) 
==21536== by 0x4002A2C: (within /lib/ld-2.11.1.so) 
==21536== by 0x4014206: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000856: (within /lib/ld-2.11.1.so) 
--21536-- Warning: DWARF2 CFI reader: unhandled DW_OP_ opcode 0x55 
--21536-- Warning: DWARF2 CFI reader: unhandled DW_OP_ opcode 0x55 
--21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 
--21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 
--21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 
--21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 
--21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 
--21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 
--21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 
==21536== 
==21536== Conditional jump or move depends on uninitialised value(s) 
==21536== at 0x400B217: (within /lib/ld-2.11.1.so) 
==21536== by 0x40031D0: (within /lib/ld-2.11.1.so) 
==21536== by 0x4014206: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000856: (within /lib/ld-2.11.1.so) 
==21536== 
==21536== Conditional jump or move depends on uninitialised value(s) 
==21536== at 0x400AF0F: (within /lib/ld-2.11.1.so) 
==21536== by 0x40031D0: (within /lib/ld-2.11.1.so) 
==21536== by 0x4014206: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000856: (within /lib/ld-2.11.1.so) 
==21536== 
==21536== Conditional jump or move depends on uninitialised value(s) 
==21536== at 0x400B27A: (within /lib/ld-2.11.1.so) 
==21536== by 0x40031D0: (within /lib/ld-2.11.1.so) 
==21536== by 0x4014206: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000856: (within /lib/ld-2.11.1.so) 
==21536== 
==21536== Conditional jump or move depends on uninitialised value(s) 
==21536== at 0x400A5DE: (within /lib/ld-2.11.1.so) 
==21536== by 0x40030FE: (within /lib/ld-2.11.1.so) 
==21536== by 0x4014206: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000856: (within /lib/ld-2.11.1.so) 
==21536== 
==21536== Conditional jump or move depends on uninitialised value(s) 
==21536== at 0x400A5E6: (within /lib/ld-2.11.1.so) 
==21536== by 0x40030FE: (within /lib/ld-2.11.1.so) 
==21536== by 0x4014206: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000856: (within /lib/ld-2.11.1.so) 
==21536== 
==21536== Conditional jump or move depends on uninitialised value(s) 
==21536== at 0x400AF0F: (within /lib/ld-2.11.1.so) 
==21536== by 0x40030FE: (within /lib/ld-2.11.1.so) 
==21536== by 0x4014206: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) 
==21536== by 0x4000856: (within /lib/ld-2.11.1.so) 
==21536== 
==21536== Invalid read of size 8 
==21536== at 0x40B4785: (within /lib/tls/i686/cmov/libc-2.11.1.so) 
==21536== by 0x8049EFB: main (vcutil.c:496) 
==21536== Address 0x419d1b0 is 8 bytes before a block of size 120 alloc'd 
==21536== at 0x4025D2E: malloc (vg_replace_malloc.c:207) 
==21536== by 0x409DF9D: getdelim (in /lib/tls/i686/cmov/libc-2.11.1.so) 
==21536== by 0x408F3A2: getline (in /lib/tls/i686/cmov/libc-2.11.1.so) 
==21536== by 0x804947C: getUnfolded (vcutil.c:299) 
==21536== by 0x8049EFB: main (vcutil.c:496) 
==21536== 
==21536== Conditional jump or move depends on uninitialised value(s) 
==21536== at 0x40B47AD: (within /lib/tls/i686/cmov/libc-2.11.1.so) 
==21536== by 0x8049EFB: main (vcutil.c:496) 
==21536== 
==21536== Conditional jump or move depends on uninitialised value(s) 
==21536== at 0x8049524: getUnfolded (vcutil.c:307) 
==21536== by 0x8049EFB: main (vcutil.c:496) 

我特別不知道是什麼線涉及

--21536-- Warning: DWARF2 CFI reader: unhandled DW_OP_ opcode 0x55 

的意思。自從我改變發行版以來,這些都是新的。

無論如何,僅供參考,這裏是我的問題的功能:

VcStatus getUnfolded (FILE *const vcf, char **const buff) { 

    //int const buffSize = 30; 
    size_t lineSize = 0; 
    int done = 0; //Set to one if the next line checked does not entail unfolding 
    static char* queue = NULL; //A queue to store the next line read in 
    static int lineNum = 0; 
    char* buffer = NULL; //The dynamic memory that will be linked to buf 
    int valid_line = 0; //Set to one if the line contains characters other then whitespace 
    char* temp; //Used to point to the location of \r\n in the buffer 

    VcStatus error; //Initialize VcStatus with appropriate values 
    error.code = OK; 
    error.linefrom = lineNum; 
    error.lineto = 0; 

    //Special case for resetting getUnfolded() 
    if (vcf == NULL){  
     lineNum = 0; 
     if (queue != NULL) queue = NULL; 
     error.linefrom = 0; 
     error.lineto = 0; 
     return error; 
    } 

    //Copies any string contained in queue from last time into buffer 
    if (queue != NULL) { 
     buffer = malloc (strlen (queue)+1); 
     strcpy (buffer, queue); 
     strcpy (queue, ""); 
    } 

    //If the end of file has been reached, last line is returned. 
    if (feof (vcf) != 0) { 
     queue = NULL; 
     lineNum += 1; 
     if (buffer != NULL) { 
      if (strcmp (buffer, "") != 0)*buff = buffer; 
     } 
     else *buff = NULL; 
     return error; 
    } 

    while (done == 0) { 

     //Gets a new line from the vcf file, and returns null if there is no more data to be returned 
     getline (&queue, &lineSize, vcf); 

     if (ferror(vcf) != 0) { 
      error.code = IOERR; 
      return error; 
     } 

     //Sets valid_line to 1 if the line read in contains any characters other then whitespace 
     for (int i = 0; i < strlen (queue); i++){ 
      if (queue[i] != '\r' && queue[i] != '\n' && queue[i] != ' ' && queue[i] != '\t') valid_line = 1; 
     } 

     if (feof (vcf) && valid_line == 0) { 
      queue = NULL; 
      break; 
     }   

     if (valid_line == 1){ 
      //Finds the 'internet standard' newline and replaces it with a null terminator 
      temp = strstr (queue, "\r\n"); 
      if (temp != NULL) strcpy (temp, "\0"); 

      //Sets the buffer equal to contents of queue and resets queue if buffer is empty 
      if (buffer == NULL) { 

       buffer = malloc (strlen (queue)+1); 
       lineNum = lineNum + 1;   
       strcpy (buffer, queue); 
       strcpy (queue, ""); 

      //Checks for leading whitespace to indicate a folded line, and unfolds 
      }else if (queue [0] == ' ' || queue [0] == '\t') { 

       lineNum = lineNum + 1; 
       buffer = realloc (buffer, strlen (buffer)+strlen (queue)+2); 
       strcat (buffer, queue+1); 
       strcpy (queue, ""); 

      //If both conditions above are false, buffer contains the next valid line 
      }else{ 

       error.lineto = lineNum; 
       lineNum += 1; 
       done = 1; 

      } 
     } 
    } 

    *buff = buffer; 

    return error; 

} 

...和虛擬功能的測試數據發送給它:

int main() { 

    FILE* fp = fopen ("test.vcf", "r"); 

    char * buff; 

    getUnfolded (fp, &buff); 

} 

的test.vcf文件只是一個簡單的文本文件真的

任何幫助將是驚人的。

+0

這是非常有趣的關於谷歌的事情:P去告訴你我沒有如果這是第一個結果,那麼運氣會使用Google搜索這個問題。 是的,我在這個新的主機上重新編譯過多次,並且源代碼沒有改變... test.vcf文件使用了正確的EOL CRLF ...這是一個令人煩惱的問題! – araisbec 2011-02-23 02:55:31

+1

如果我沒有記錯的話,'dwarf2 opcode'錯誤可能只是你轉向比你使用的編譯器更早的valgrind版本,並且可以安全地忽略。關於你的錯誤:你調用'strlen(隊列)'。如果該行比'lineSize'更長,會發生什麼?如果你得出的結論是代碼是可以的,你可以生成一個抑制文件來繼續你的生活。 – Borealid 2011-02-23 03:52:47

+0

感謝您的回覆,很高興理解爲什麼valgrind在我身上噴出廢話,毫無意義!如果你知道如何抑制這些錯誤,我會很有興趣去學習。我不知道如何解決你所指的錯誤。在我的理解中,strlen(隊列)行被調用,如果從上次調用函數開始隊列中有某些內容。我一直收到類似「比較中使用的未初始化變量」這樣的錯誤,因爲我的變量已經在條件語句中初始化了(在任何情況下),我也不知道如何去除這些變量。 – araisbec 2011-02-23 04:04:15

回答

5

您正在使用的valgrind的版本是過時的,所以它不抑制正確的輸出。那些錯誤仍然存​​在,只是因爲它們在系統代碼中而被壓制。

您需要手動安裝Valgrind的最新版本

這是論壇上的帖子我用

http://ubuntuforums.org/showthread.php?t=1608001