当程序完美无缺时,Valgrind大喊大叫(C)

时间:2014-11-17 07:21:10

标签: c valgrind

我有一个程序(在其初级阶段)应该从stdin获取文件,对其进行编码并将其打印到stdout。无论我读什么类型的标准输入,它似乎工作得很好。但是,Valgrind告诉我有一个问题:

==6508== Invalid read of size 4
==6508==    at 0x4009EC: insertObject (lzw.c:106)
==6508==    by 0x400B72: lzw_encode (lzw.c:228)
==6508==    by 0x40091D: main (lzw.c:48)
==6508==  Address 0x5203044 is 0 bytes after a block of size 65,540 alloc'd
==6508==    at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==6508==    by 0x400D51: initialize (lzw.c:305)
==6508==    by 0x400AB3: lzw_encode (lzw.c:196)
==6508==    by 0x40091D: main (lzw.c:48)
==6508== 

以下是代码的相关摘要:

int lzw_encode ()
{
    table* hashtable;
    int counter=0;
    hashtable = initialize(); //line 196
    int code = -1;
    char k;
    if ((k=getc(stdin))==EOF)
    {
        return 0;
    }
    code = k;
    while ((k=getc(stdin))!=EOF)
    {
        if (HashSearch(hashtable, code, k)!=-1)
        {   
            code=HashSearch(hashtable, code, k);
        }
        else
        {  
            putBits(12, code);

            if (hash(code, k, hashtable->size)>4340)
            {
                counter=1;
            }
            if (counter==0)
            {
                hashtable = insertObject(hashtable, code, k); //line 228
            }

            code = HashSearch(hashtable, -1, k);

        }
    }
    if (code != 0)
    {
        printf ("%d\n", code);
    }
    /*if (!fp)
    {
        printf("????\n");
    }*/

    return 0;
}

InsertObject:

table *insertObject (table *h, int pref, char ch)
{
    struct node x;
    int i;
    if (ch < 0)
    {
        ch=4096-ch;
    }
    x.chr=ch;
    x.pref=pref;
    i = hash(pref, ch, h->size);
    while (h->hash[i].pref!=0)
    {
        i++;
    }
    if (i>4340)
    {
        return h;
    }
    h->hash[i]=x; //line 106
    return h;
}

初​​始化:

table *initialize ()
{
    table *hashtable = malloc(sizeof(table)); //Line 305
    //hashtable->hash = malloc (sizeof(struct node) * 4096);
    memset(hashtable, 0, sizeof(*hashtable));
    hashtable->size=4096;
    //hashtable->hash=malloc(sizeof(table));
    for (int i=0; i<4096; i++)
    {
        hashtable->hash[i].pref=-1;
        hashtable->hash[i].before=-1;
        hashtable->hash[i].after=-1;
        hashtable->hash[i].chr=0;
    }
    for (int i=0; i<256; i++)
    {
        hashtable->hash[i].chr=i+128;
    }
    //printf("Initialized\n");
    return hashtable;
}

我已经标记了valgrind中提到的行。

编辑:

以下是数据结构:

typedef struct hash_t table;

struct node {
    int pref;
    int before;
    int after;
    char chr;
};

struct hash_t {
    int size;
    struct node hash[4096];
};

EDIT2:确保我永远不会超过4096似乎已修复它。但是,我发现了一个我以前见过的valgrind错误:

==20137== Syscall param write(buf) points to uninitialised byte(s)
==20137==    at 0x4F198B0: __write_nocancel (in /usr/lib64/libc-2.17.so)
==20137==    by 0x4EA8E32: _IO_file_write@@GLIBC_2.2.5 (in /usr/lib64/libc-2.17.so)
==20137==    by 0x4EAA29B: _IO_do_write@@GLIBC_2.2.5 (in /usr/lib64/libc-2.17.so)
==20137==    by 0x4EABDE6: _IO_flush_all_lockp (in /usr/lib64/libc-2.17.so)
==20137==    by 0x4EABF39: _IO_cleanup (in /usr/lib64/libc-2.17.so)
==20137==    by 0x4E6AE0A: __run_exit_handlers (in /usr/lib64/libc-2.17.so)
==20137==    by 0x4E6AEA4: exit (in /usr/lib64/libc-2.17.so)
==20137==    by 0x4E53AFB: (below main) (in /usr/lib64/libc-2.17.so)
==20137==  Address 0x4023002 is not stack'd, malloc'd or (recently) free'd

基于此网站上的其他问题,这似乎是在结构未初始化时发生的。但是,我没有看到任何单元化的结构。这段代码有这样的问题,还是应该在程序的其他地方查找?

1 个答案:

答案 0 :(得分:2)

insertObject的第106行,您正试图从地址hash[i]读取。 hash的大小为4096,但您只检查了i > 4340上面的四行。在执行程序期间,i可能在4096到4340之间取值。

Valgrind输出的第一个块通知您无法从地址hash + i * sizeof(struct node)读取请求的四个字节。

第二个块告诉您正在尝试读取的地址超出了分配的大小为65,540的数组的末尾。实际上,您的h变量已传递给insertObject

参见例如http://valgrind.org/gallery/linux_mag.html解释常见的Valgrind错误报告。