2011-02-23 65 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