简体   繁体   English

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

[英]Invalid read of size 8, Invalid write of size 8, C

I have a valgrind errors, and I dont know how to get rid of them: 我有一个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)

and my code : 和我的代码:

#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;
}

You're allocating space for data.gl_pathc pointers + 1 byte, then you're using data.gl_pathc + 1 pointers (the last pointer being set by filenames[i] = NULL; 您正在为data.gl_pathc指针+ 1字节分配空间,然后在使用data.gl_pathc + 1指针(最后一个指针由filenames[i] = NULL;

In other words, your allocation 换句话说,您的分配

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

should probably be allocating data.gl_pathc + 1 pointers instead; 应该应该分配data.gl_pathc + 1指针;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM