【发布时间】:2012-12-05 10:32:27
【问题描述】:
我是 c 编程新手,我正在编写一个简单的客户端服务器应用程序。我收到这条消息:
Source and destination overlap in memcpy(0x41f0beb, 0x41f0258, 69141077)
==9522== at 0x402D9A9: memcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==9522== by 0x8049C13: message_to_buffer (message.c:92)
具体代码如下:
case CT_ENTRY://100
{
int c=(2+2+4+strlen(msg->content.entry->key)-1+4+4+strlen(msg->content.entry->value->data));
char *offset=malloc(c);
*msg_buf=offset;
memcpy(offset,&opcode,2);
offset+=2;
memcpy(offset,&ctype,2 );
offset+=2;
int ks=strlen(msg->content.entry->key);
int ksc=host_to_net(ks);
memcpy(offset,&ksc,4);
offset+=4;
memcpy(offset, msg->content.entry->key, ks);
offset+=ks;
int l=host_to_net(get_time());
memcpy(offset,&l,4);
offset+=4;
int ds=host_to_net(msg->content.entry->value->datasize);
memcpy(offset,&ds,4);
offset+=4;
// this line here!
memcpy(offset,msg->content.entry->value->data, msg->content.value->datasize);
return c;
break;
违规行是
memcpy(offset,msg->content.entry->value->data, msg->content.value->datasize);
谁能解释一下为什么会这样?谢谢
【问题讨论】:
-
您确定
69141077的datasize值正确吗? -
您正在复制将近 70 兆字节的数据,这真的正确吗?
-
是的,这似乎是问题所在。它应该只有几个字节。感谢您为我指明正确的方向。