无效读写valgrind
invalid read and write valgrind
以下几行给出了无效的读写错误。你能解释一下我错过了什么吗?我已经初始化了变量,但它仍然导致错误。
==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");
}
}
您发布的代码中未显示无效读取。
无效写入是由于 malloc(3)
将 space 作为参数以字节为单位进行分配。
您正在分配一个字节,并用 int *
指向该字节。因此,当您取消引用指针时,您正在访问 sizeof(int)
字节,这在您的平台上大于 1
。
尝试使用 malloc(sizeof (int))
或更好的 malloc(sizeof *new_sock)
。
您在 malloc
中使用了不正确的参数。您可以使用 sizeof(int)
获得 int 的正确大小,这通常会产生 4。尝试将 malloc(1)
替换为 malloc(sizeof(int))
。
new_sock = malloc(1)
分配一个字节的内存,并将该内存的地址赋值给变量new_sock
.
*new_sock = client_sock;
将一个 int 存储到该内存区域;将四个字节写入一个字节的内存区域会使分配溢出。
然后,当您尝试从分配的内存中读取一个 int 时(假设在另一个线程中),一个字节是从分配的区域读取的,但其他三个是从无效内存中读取的。
这个
if (NULL ==(new_sock = malloc(1)))
必须
new_sock = malloc(sizeof(int));
if(new_sock == NULL)
continue;
malloc 接受一个 size_t
参数,它是您要分配的内存量的 字节大小 。
在你的情况下,你想为 int
变量存储 space,然后 sizeof(int)
returns 分配正确的大小。
以下几行给出了无效的读写错误。你能解释一下我错过了什么吗?我已经初始化了变量,但它仍然导致错误。
==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");
}
}
您发布的代码中未显示无效读取。
无效写入是由于 malloc(3)
将 space 作为参数以字节为单位进行分配。
您正在分配一个字节,并用 int *
指向该字节。因此,当您取消引用指针时,您正在访问 sizeof(int)
字节,这在您的平台上大于 1
。
尝试使用 malloc(sizeof (int))
或更好的 malloc(sizeof *new_sock)
。
您在 malloc
中使用了不正确的参数。您可以使用 sizeof(int)
获得 int 的正确大小,这通常会产生 4。尝试将 malloc(1)
替换为 malloc(sizeof(int))
。
new_sock = malloc(1)
分配一个字节的内存,并将该内存的地址赋值给变量new_sock
.
*new_sock = client_sock;
将一个 int 存储到该内存区域;将四个字节写入一个字节的内存区域会使分配溢出。
然后,当您尝试从分配的内存中读取一个 int 时(假设在另一个线程中),一个字节是从分配的区域读取的,但其他三个是从无效内存中读取的。
这个
if (NULL ==(new_sock = malloc(1)))
必须
new_sock = malloc(sizeof(int));
if(new_sock == NULL)
continue;
malloc 接受一个 size_t
参数,它是您要分配的内存量的 字节大小 。
在你的情况下,你想为 int
变量存储 space,然后 sizeof(int)
returns 分配正确的大小。