Valgrind 使用 getline() 给出错误

发布于 2024-10-18 16:37:37 字数 8321 浏览 3 评论 0原文

我有一个调用 getline() 的函数,它从标准文本文件中读取行。这些行以 \r\n 结尾,因为这是规范所要求的,因为它是 VCARD 文件的“互联网标准”。

无论如何,我通过 OpenSUSE 11.3 上的 Valgrind 运行这个宝贝,并使用最新的 GCC 和 Valgrind 版本;没有错误。然而,由于与这个问题无关的原因,我将发行版切换到 Back Track 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 文件实际上只是一个简单的文本文件

任何帮助都会很棒。

I have a function which calls getline(), which reads lines from a standard text file. These lines end in \r\n, as this is what the spec calls for as it is the "internet standard" for VCARD files.

Anyway, I run this baby through Valgrind on OpenSUSE 11.3 with the latest GCC and Valgrind versions; no errors. However, for reasons of no relevance to this problem, I switched distros to Back Track 4 (Ubuntu based), and now I am getting a metric ton of Valgrind errors:

    ==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)

I especially don't know what the lines refering to

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

mean. These are new since I changed distros.

Anyway, for reference, here is my function in question:

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;

}

...And the dummy function to send test data to it:

int main () {

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

    char * buff;

    getUnfolded (fp, &buff);

}

The test.vcf file is just a simple text file really

Any help would be amazing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女猫性小仙女 2024-10-25 16:37:37

您使用的 valgrind 版本已过时,因此它不会抑制正确的输出。这些错误在 suse 上仍然存在,只是因为它们在系统代码中而被抑制。

您需要手动安装最新版本的 valgrind

这是我使用的论坛帖子

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

The version of valgrind you are using is outdated and so it is not suppressing the correct output. Those errors are still there on suse they are just suppressed because they are in system code.

You need to manually install the newest version of valgrind

This is the forum post I used

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文