C++内存复制问题:(
我有一个问题,我的 memcpy src 指针指向错误。
unsigned char* lpBuffer
是一个包含我的字节的缓冲区,我与 olly 进行了检查。
代码:
IMAGE_DOS_HEADER iDOSh;
memcpy(&iDOSh,lpBuffer,sizeof(iDOSh));
问题是 lpBuffer
指向错误,调试器的输出是
dest = 002859E8 RIGHT
src = 000001D8 FALSE
src
指向无效:( 我不知道为什么
感谢您的阅读
I have a problem my src pointer of memcpy is pointing wrong.
unsigned char* lpBuffer
is a buffer that contains my bytes, i checked with olly.
The code:
IMAGE_DOS_HEADER iDOSh;
memcpy(&iDOSh,lpBuffer,sizeof(iDOSh));
The problem is that lpBuffer
points wrong, output from debugger is
dest = 002859E8 RIGHT
src = 000001D8 FALSE
src
is pointing invalid :( i have no idea why
Thanks for reading
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么你用 ollydbg 检查而不是更方便的东西?在 IDE 中跟踪您的步骤。当您将指针传递给 memcpy 函数时,指针的值无法更改为无效(因为它是按值传递的),因此这意味着它在调用 memcpy 之前就已经无效。
不幸的是,您的代码仅涵盖了明显的
memcpy
调用,其中“不会出错”。还要提一下,那个奇怪的
0x00000YY 值实际上表明出现了问题,并且可能您的代码中某处存在无效的类型转换(或类似的内容)。
Why do you check with ollydbg but not something more convenient?? Trace your steps in your IDE. A pointer's value can't change become invalid when you pass it to
memcpy
function (because it's passed by value), so it means it has been invalid right before thatmemcpy
call.Unfortunately your code covers only that obvious
memcpy
call where "nothing can go wrong".Also to mention, that strange
0x00000YY
value for your pointer actually signs that something went wrong and probably you have invalid type cast somewhere in your code (or something like that).我认为您正在调试调用 C 函数的程序集,并尝试使用 ollydbg 来跟踪它(我只是查了一下它是什么,并根据其功能列表得出了这一假设)。这是很难做到的。
我建议你这样做:
如果你实际上无法打印东西,那也没关系。只需将函数
extern
添加到具有相关memcpy
的文件中,它就会强制编译器将值加载到保存第一个参数的位置。您应该能够在调试器中观察到这一点。memcpy
(来自任何合理的 C 库)实际上做错事情的可能性非常非常低。如果我不得不猜测出了什么问题,那么
lpBuffer
实际上不应该是void *
,而是内存位置的链接器标签。在这种情况下,您可能应该尝试将其声明为:and do your memcpy as
或
and do your memcpy as
I think you are debugging in assembly calling C functions and trying to trace that with ollydbg (I just looked up what it is and based this assumption on their feature list). This is very difficult to do.
I suggest that you do:
If you aren't actually able to print things that will be ok. Just make the functions
extern
to the file with thememcpy
in question and it will force the compiler to load the value into the location which holds the first parameter. You should be able to observe this in your debugger.The likelihood the
memcpy
(from any reasonable C library) is actually doing something wrong is very very low.If I had to guess what is going wrong it would be that
lpBuffer
is not actually supposed to be avoid *
but a linker label for a memory location. In that case you might should try declaring it as:and do your memcpy as
or
and do your memcpy as
在调用 memcpy 之前检查 lpBuffer 的值,并在调用之后再次检查。它会改变吗?
如果它发生变化,唯一可能改变
lpBuffer
中的值的是memcpy
,这意味着您在调用中覆盖了它(即,它没有执行您想要的操作)认为它正在做...仔细检查你的参数)。不过,我的猜测是,查看您的代码后,对
memcpy
的调用可能不会发生变化。也就是说,如果在前后检查 lpBuffer 的值显示它没有变化,那么您在调用 memcpy 之前无意中更改了它。您需要跟踪该变化。Check the value of
lpBuffer
immediately before you callmemcpy
and again immediately afterwards. Does it change?If it changes, the only thing that could have changed the value in
lpBuffer
is thememcpy
, which means that you are overwriting it in the call (i.e. it's not doing what you think it's doing ... double check your parameters).My guess, though, looking at your code is that is probably not changing in the call to
memcpy
. That is, if checking the value oflpBuffer
immediately before and after shows it to be unchanged, you are inadvertantly changing it prior to callingmemcpy
. You'll need to track that change down.