在 RPC 代码中获取 memcheck 错误
Posted
技术标签:
【中文标题】在 RPC 代码中获取 memcheck 错误【英文标题】:Getting memcheck error in RPC code 【发布时间】:2015-03-15 16:23:01 【问题描述】:当我用 valgrind 运行它时,我在客户端得到以下输出:
==7374== Memcheck, a memory error detector
==7374== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==7374== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==7374== Command: ./rvotefor localhost bush 1
==7374==
==7374== Use of uninitialised value of size 8
==7374== at 0x4C2AD40: strcpy (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==7374== by 0x400A1C: vote_prog_1 (rvotefor.c:17)
==7374== by 0x400BF8: main (rvotefor.c:84)
==7374==
==7374== Invalid write of size 1
==7374== at 0x4C2AD40: strcpy (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==7374== by 0x400A1C: vote_prog_1 (rvotefor.c:17)
==7374== by 0x400BF8: main (rvotefor.c:84)
==7374== Address 0x2 is not stack'd, malloc'd or (recently) free'd
==7374==
==7374==
==7374== Process terminating with default action of signal 11 (SIGSEGV)
==7374== Access not within mapped region at address 0x2
==7374== at 0x4C2AD40: strcpy (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==7374== by 0x400A1C: vote_prog_1 (rvotefor.c:17)
==7374== by 0x400BF8: main (rvotefor.c:84)
==7374== If you believe this happened as a result of a stack
==7374== overflow in your program's main thread (unlikely but
==7374== possible), you can try to increase the size of the
==7374== main thread stack using the --main-stacksize= flag.
==7374== The main thread stack size used in this run was 8388608.
==7374==
==7374== HEAP SUMMARY:
==7374== in use at exit: 0 bytes in 0 blocks
==7374== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==7374==
==7374== All heap blocks were freed -- no leaks are possible
==7374==
==7374== For counts of detected and suppressed errors, rerun with: -v
==7374== Use --track-origins=yes to see where uninitialised values come from
==7374== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 2 from 2)
Segmentation fault
这在服务器端:
==6841== Memcheck, a memory error detector
==6841== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==6841== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==6841== Command: ./vote_server
==6841==
谁能帮我摆脱这个错误?什么意思?
【问题讨论】:
【参考方案1】:在main()
的客户端,确保dummy
包含一个至少有3 个字符和一个空终止符的字符串(因为每个argv2 和argv[3] 中至少有1 个字符) )。
当您调用vote_prog_1()
时,最初的一些语句是:
char * votefor_1_arg; // <===== !! uninitialized pointer
strcpy(votefor_1_arg,dummy); // <===== !! copy the more than 4 bytes in dummy
因此,您用dummy[]
中包含的至少4 个字节覆盖了某处内存(未初始化的指针),从而破坏了内存。
您必须在使用指针之前分配内存。例如strdup()
(linux 或windows):
votefor_1_art = strdup(dummy); // <== allocates memory and copy the string
votefor_1(&votefor_1_arg, clnt);
还存在一个潜在问题,因为您传递给此函数的不是参数的地址,而是参数指针的地址。这可能是正确的,但根据函数的签名可能是错误的。如果您对此感到困惑,请同时发布此功能的代码,以便我们检查。
【讨论】:
那么,我是否只需添加行 votefor_1_arg = strdup(dummy);在 char * votefor_1_arg 行之后; ? @MarthaPears 是的。事实上,该语句将取代您的 strcpy()。以上是关于在 RPC 代码中获取 memcheck 错误的主要内容,如果未能解决你的问题,请参考以下文章
是否有类似 Valgrind Memcheck 的工具,用于在出现免费错误后调试 Windows 使用? [关闭]
cudaGraph:多线程流捕获仅在 cuda-memcheck 中运行时才会导致错误
为啥 cuda-memcheck racecheck 报告 cufft 错误?