【问题标题】:invalid read and write valgrind无效的读写valgrind
【发布时间】:2016-02-11 06:46:10
【问题描述】:

以下行给出了无效的读写错误。你能解释一下我错过了什么吗?我已经初始化了变量,但它仍然导致错误。

==26319==   Invalid read of size 4

==26319==    at 0x4035CC: connection_handler (thread.c:26)

==26319==    by 0x4E36A50: start_thread (in /lib64/libpthread-2.12.so)

==26319==    by 0x61E06FF: ???

==26319==   Address 0x53e02c0 is 0 bytes inside a block of size 1 alloc'd

==26319==    at 0x4C27A2E: malloc (vg_replace_malloc.c:270)

==26319==    by 0x40335C: main (send_server.c:154)


==26319==   1 errors in context 3 of 3:

==26319==   Thread 1:

==26319==   Invalid write of size 4

==26319==    at 0x4033C3: main (send_server.c:157)

==26319==  Address 0x53e02c0 is 0 bytes inside a block of size 1 alloc'd

==26319==    at 0x4C27A2E: malloc (vg_replace_malloc.c:270)

==26319==    by 0x40335C: main (send_server.c:154)

代码

int *new_sock = NULL;

while (1) 
{
    client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c); 

    if (client_sock < 0)
    {
        fprintf(stderr,"accept failed\n");
        LOGGER("Accept failed\n");
        continue;
    }

    else
    {   
        LOGGER("\nConnection accepted\n");
        pthread_t sniffer_thread;       //assign thread for each client
        if (NULL ==(new_sock = malloc(1))) //invalid read
            continue;       
        printf("VAlue of new sock %p \n",new_sock);
        *new_sock = client_sock; // invalid write of size 4

        if ( pthread_create( &sniffer_thread , NULL ,  connection_handler , (void*) new_sock) < 0)  //Serving each thread
        {
            fprintf(stderr,"could not create thread\n");
            LOGGER("ERROR could not create thread\n");
            free(new_sock);

        }
        pthread_detach(sniffer_thread);
        LOGGER("Handler assigned\n");
    }

}

【问题讨论】:

    标签: c sockets valgrind


    【解决方案1】:

    您对malloc 使用了不正确的参数。您可以使用 sizeof(int) 获得正确大小的 int,这通常产生 4。尝试将 malloc(1) 替换为 malloc(sizeof(int))

    new_sock = malloc(1) 分配一个字节的内存,并将该内存的地址分配给变量new_sock

    *new_sock = client_sock; 将一个 int 存储到该内存区域;将 4 个字节写入 1 个字节的内存区域会导致分配溢出。

    然后,当您尝试从分配的内存中读取一个 int(假设在另一个线程中)时,会从分配的区域中读取一个字节,但从无效内存中读取其他三个字节。

    【讨论】:

      【解决方案2】:

      您发布的代码中未显示无效读取。

      无效写入是由于malloc(3) 将要分配的空间作为参数(以字节为单位)。

      您正在分配一个字节,并使用int * 指向该字节。因此,当您取消引用您的指针时,您访问的 sizeof(int) 字节大于您平台上的 1

      尝试使用malloc(sizeof (int)) 或更好的malloc(sizeof *new_sock)

      【讨论】:

        【解决方案3】:

        这个

         if (NULL ==(new_sock = malloc(1)))
        

        必须是

         new_sock = malloc(sizeof(int));
         if(new_sock == NULL)
            continue;
        

        malloc 采用size_t 参数,即您要分配的内存量的大小(以字节为单位)。 在您的情况下,您想为 int 变量存储空间,然后 sizeof(int) 将正确的大小返回给分配。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-11
          • 1970-01-01
          • 1970-01-01
          • 2014-08-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多