Valgrind 中的无效读取

use*_*165 1 c valgrind

我一直在 Valgrind 中看到这些错误。我的程序也有段错误,但我试图首先解决 Valgrind 中的错误。

81 char *ptr = NULL;
82      if (addition_is_safe(sz, TRAILER_SZ))
83          ptr = malloc(sz + TRAILER_SZ);
84 syslog(LOG_NOTICE, "Calling set trailer value, ptr=%p", ptr);
Run Code Online (Sandbox Code Playgroud)

将此打印到日志中:

9 月 17 日 00:05:50 设备 m61[18301]:调用设置拖尾值,ptr=0x96af158

瓦尔格林:

==18069== 2 errors in context 1 of 4:
==18069== Invalid read of size 1
==18069==    at 0x402BCCA: memcpy (mc_replace_strmem.c:882)
==18069==    by 0x8048819: main (test025.c:13)
==18069==  Address 0x4213709 is 137 bytes inside a block of size 368 free'd
==18069==    at 0x4028F0F: free (vg_replace_malloc.c:446)
==18069==    by 0x40AA00B: fclose@@GLIBC_2.1 (iofclose.c:88)
==18069==    by 0x4136003: __vsyslog_chk (syslog.c:228)
==18069==    by 0x4136446: syslog (syslog.c:119)
==18069==    by 0x8049823: attempt_create_allocation (m61_allocation_core.c:84)
==18069==    by 0x8048914: m61_malloc (m61.c:47)
==18069==    by 0x80487B4: main (test025.c:9)
==18069== 
Run Code Online (Sandbox Code Playgroud)

编辑:我运行的测试代码实际上是为了产生一个无效的自由,以便在 Valgrind 和我的内存调试代码中被捕获。但是,它不应该出现段错误,这就是正在发生的事情。关闭系统日志后,不再有段错误,并且 Valgrind 报告与测试代码的预期一致。我不知道为什么 syslog 会导致上述问题。

测试代码:

1 #include "m61.h"
2 #include <stdio.h>
3 #include <assert.h>
4 #include <string.h>


7 int main() {
8    char *a = (char *) malloc(200);
9    char *b = (char *) malloc(50);
10   char *c = (char *) malloc(200);
11   char *p = (char *) malloc(3000);
12   (void) a, (void) c;
13   memcpy(p, b - 200, 450);
14   free(p + 200);
Run Code Online (Sandbox Code Playgroud)

关闭 Valgrind 后的新 Valgrind 报告:

==5688== 18 errors in context 1 of 3:
==5688== Invalid read of size 4
==5688==    at 0x402BD00: memcpy (mc_replace_strmem.c:882)
==5688==    by 0x8048849: main (test025.c:13)
==5688==  Address 0x41f92c0 is 16 bytes before a block of size 208 alloc'd
==5688==    at 0x4029F6F: malloc (vg_replace_malloc.c:270)
==5688==    by 0x80497AD: attempt_create_allocation (m61_allocation_core.c:77)
==5688==    by 0x8048950: m61_malloc (m61.c:48)
==5688==    by 0x8048804: main (test025.c:10)
Run Code Online (Sandbox Code Playgroud)

Jon*_*ler 5

初步尝试

你得到的读取错误意味着你分配了一些空间然后被释放,但你的程序的某些部分设法保持一个指向内存块中间的指针,然后尝试从该块内部读取一个字节memcpy()inmain()在第 13 行。

不太清楚的是内存分配在哪里。看起来好像 free 是在对 的调用中完成的syslog(),这很奇怪。

您需要test025.c相当仔细地查看您的代码。如果m61.c是您的代码,您也应该查看它(并且大概m61_allocation_core.c也是)。中的代码vg_replace_malloc.c绝对是valgrind代码;我不确定中间的部分。

修订后的代码 - 修订后的答案

memcpy()在修改后的代码显然是100%错误的。同样,free()100% 是错误的。要了解原因,您必须记住,malloc()通常在分配的指针之前存储一些控制信息。您还需要知道,尝试free()指向未由 返回的指针malloc()calloc()或者realloc()是一个严重错误。

你的代码是:

 7 int main() {
 8     char *a = (char *) malloc(200);
 9     char *b = (char *) malloc(50);
10     char *c = (char *) malloc(200);
11     char *p = (char *) malloc(3000);
12     (void) a, (void) c;
13     memcpy(p, b - 200, 450);
14     free(p + 200);
Run Code Online (Sandbox Code Playgroud)

除了错误检查(或没有错误检查)之外,第 7-11 行也无一例外。有些人会因为演员阵容而责备你;我不是那种思想流派。我允许您忽略它们 — 只要您保证使用-Wmissing-prototypes -Wstrict-prototypes(和-Wall -Wextra) 进行编译并在运行代码之前修复报告的问题。如果你不这样做,你需要注意我(关于使用严格的编译器选项并修复编译器警告识别的问题)或它们(关于不转换 的结果malloc())。

第 12 行避免了“未使用的变量”警告;否则是空操作。

第 13 行memcpy(), 尝试从 开始之前的 200 个字节复制b到为 分配的空间末尾之后的 200 个字节b。在这 450 个字节中,访问其中的 400 个会导致未定义的行为。如果你只是在读取记忆,并且目的地——p足够大,你就“OK”。但是阅读两边的字节b是无效的,valgrind完全正确的抱怨。

第 14 行free(), 尝试释放 未返回的指针malloc()。因此,您正在调用未定义的行为。除了灾难之外,没有什么可以指望发生的。很有可能它free()本身不会失败——尽管调试malloc()会注意到你正在从分配的空间中间释放出来。但是任何后续的内存分配或释放都是非常成问题的。问题是free()会在(通常)指向的空间之前查看控制信息,而该信息将无效。