读取大小为8的无效,写入大小为8的无效,C

时间:2013-09-15 17:07:21

标签: c valgrind

我有一个valgrind错误,我不知道如何摆脱它们:

==5685== Invalid read of size 8
==5685==    at 0x4008A1: main (in /home/mazix/Desktop/tests/filenames)
==5685==  Address 0x5207670 is 608 bytes inside a block of size 609 alloc'd
==5685==    at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5685==    by 0x40075E: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685==    by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685== 
==5685== Invalid read of size 8
==5685==    at 0x4008BB: main (in /home/mazix/Desktop/tests/filenames)
==5685==  Address 0x5207670 is 608 bytes inside a block of size 609 alloc'd
==5685==    at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5685==    by 0x40075E: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685==    by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685== 
==5685== 
==5685== FILE DESCRIPTORS: 3 open at exit.
==5685== Open file descriptor 2: /dev/pts/1
==5685==    <inherited from parent>
==5685== 
==5685== Open file descriptor 1: /dev/pts/1
==5685==    <inherited from parent>
==5685== 
==5685== Open file descriptor 0: /dev/pts/1
==5685==    <inherited from parent>
==5685== 
==5685== 
==5685== HEAP SUMMARY:
==5685==     in use at exit: 0 bytes in 0 blocks
==5685==   total heap usage: 231 allocs, 231 frees, 43,693 bytes allocated
==5685== 
==5685== All heap blocks were freed -- no leaks are possible
==5685== 
==5685== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 2 from 2)
==5685== 
==5685== 1 errors in context 1 of 3:
==5685== Invalid read of size 8
==5685==    at 0x4008BB: main (in /home/mazix/Desktop/tests/filenames)
==5685==  Address 0x5207670 is 608 bytes inside a block of size 609 alloc'd
==5685==    at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5685==    by 0x40075E: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685==    by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685== 
==5685== 
==5685== 1 errors in context 2 of 3:
==5685== Invalid read of size 8
==5685==    at 0x4008A1: main (in /home/mazix/Desktop/tests/filenames)
==5685==  Address 0x5207670 is 608 bytes inside a block of size 609 alloc'd
==5685==    at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5685==    by 0x40075E: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685==    by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685== 
==5685== 
==5685== 1 errors in context 3 of 3:
==5685== Invalid write of size 8
==5685==    at 0x400806: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685==    by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685==  Address 0x5207670 is 608 bytes inside a block of size 609 alloc'd
==5685==    at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5685==    by 0x40075E: getFilenames (in /home/mazix/Desktop/tests/filenames)
==5685==    by 0x40083C: main (in /home/mazix/Desktop/tests/filenames)
==5685== 
--5685-- 
--5685-- used_suppression:      2 dl-hack3-cond-1
==5685== 
==5685== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 2 from 2)

和我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glob.h>

char **getFilenames()
{
    char **filenames = NULL;
    glob_t data;
    unsigned int i;

    switch( glob("./*.*", 0, NULL, &data ) )
    {
    case 0:
        break;
    case GLOB_NOSPACE:
        printf( "Out of memory\n" );
        break;
    case GLOB_ABORTED:
        printf( "Reading error\n" );
        break;
    case GLOB_NOMATCH:
        printf( "No files found\n" );
        break;
    default:
        break;
    }

    filenames = malloc(sizeof(char*)*(data.gl_pathc)+1);
    for(i=0; i<data.gl_pathc; i++)
    {
        int len = strlen(data.gl_pathv[i]);
        filenames[i] = malloc(sizeof(char*)*len);
        strcpy(filenames[i], data.gl_pathv[i]);
    }
    filenames[i] = NULL;
    globfree( &data );
    return filenames;
}

int main( int argc, char *argv[] )
{
    char **filenames = getFilenames();
    unsigned int i = 0;
    for(i=0; filenames[i] != NULL; i++)
    {
        printf("%s\n", filenames[i]);
        free(filenames[i]);
    }

    free(filenames[i]);
    free(filenames);

    return 0;
}

1 个答案:

答案 0 :(得分:5)

您为data.gl_pathc指针+ 1字节分配空间,然后您使用data.gl_pathc + 1指针(最后一个指针由filenames[i] = NULL;

设置

换句话说,你的分配

filenames = malloc(sizeof(char*) * (data.gl_pathc) + 1);

可能应该分配data.gl_pathc + 1指针;

filenames = malloc(sizeof(char*) * (data.gl_pathc + 1));