首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取RPC代码中的memcheck错误

获取RPC代码中的memcheck错误
EN

Stack Overflow用户
提问于 2015-03-16 00:23:01
回答 1查看 80关注 0票数 1

当我使用valgrind运行它时,我在客户端得到以下输出:

代码语言:javascript
复制
==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

在服务器终端中:

代码语言:javascript
复制
==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==

有人能帮我摆脱这个错误吗?什么意思?

EN

回答 1

Stack Overflow用户

发布于 2015-03-16 00:45:34

main()的客户端,您要确保dummy包含一个至少包含3个字符和一个空结束符的字符串(因为在每个argv2和argv3中至少有1个字符)。

然后调用vote_prog_1(),一些第一个语句是:

代码语言:javascript
复制
char * votefor_1_arg;         // <=====  !! uninitialized pointer 
strcpy(votefor_1_arg,dummy);  // <=====  !! copy the more than 4 bytes in dummy  

因此,您使用dummy[]中包含至少4个字节覆盖了某处内存(未初始化的指针),损坏了内存。

在使用指针之前,你必须先分配内存。例如,使用strdup() (linuxwindows):

代码语言:javascript
复制
 votefor_1_art = strdup(dummy);   // <== allocates memory and copy the string

votefor_1(&votefor_1_arg, clnt);还有一个潜在的问题,因为您向此函数传递的不是参数的地址,而是参数的poitner地址。这可能是正确的,但也可能是错误的,这取决于函数的签名。如果您对此感到困惑,也可以发布此函数的代码,以便我们可以查看。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29063052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档