C++内存复制问题:(

发布于 2024-09-03 17:42:46 字数 388 浏览 1 评论 0原文

我有一个问题,我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无声情话 2024-09-10 17:42:46

为什么你用 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 that memcpy 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).

别想她 2024-09-10 17:42:46

我认为您正在调试调用 C 函数的程序集,并尝试使用 ollydbg 来跟踪它(我只是查了一下它是什么,并根据其功能列表得出了这一假设)。这是很难做到的。

我建议你这样做:

...
void print_ptr(void * p) {
     fprintf(stderr, "%p\n", p);
}
...
    IMAGE_DOS_HEADER iDOSh;

    print_ptr(lpBuffer);
    memcpy(&iDOSh,lpBuffer,sizeof(iDOSh));
    print_ptr(lpBuffer);

如果你实际上无法打印东西,那也没关系。只需将函数 extern 添加到具有相关 memcpy 的文件中,它就会强制编译器将值加载到保存第一个参数的位置。您应该能够在调试器中观察到这一点。

memcpy(来自任何合理的 C 库)实际上做错事情的可能性非常非常低。

如果我不得不猜测出了什么问题,那么 lpBuffer 实际上不应该是 void *,而是内存位置的链接器标签。在这种情况下,您可能应该尝试将其声明为:

extern char lpBuffer[];

and do your memcpy as

memcpy(&iDOSh,lpBuffer,sizeof(iDOSh));

extern IMAGE_DOS_HEADER lpBuffer;

and do your memcpy as

memcpy(&iDOSh,&lpBuffer,sizeof(iDOSh));

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:

...
void print_ptr(void * p) {
     fprintf(stderr, "%p\n", p);
}
...
    IMAGE_DOS_HEADER iDOSh;

    print_ptr(lpBuffer);
    memcpy(&iDOSh,lpBuffer,sizeof(iDOSh));
    print_ptr(lpBuffer);

If you aren't actually able to print things that will be ok. Just make the functions extern to the file with the memcpy 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 a void * but a linker label for a memory location. In that case you might should try declaring it as:

extern char lpBuffer[];

and do your memcpy as

memcpy(&iDOSh,lpBuffer,sizeof(iDOSh));

or

extern IMAGE_DOS_HEADER lpBuffer;

and do your memcpy as

memcpy(&iDOSh,&lpBuffer,sizeof(iDOSh));
尛丟丟 2024-09-10 17:42:46

在调用 memcpy 之前检查 lpBuffer 的值,并在调用之后再次检查。它会改变吗?

如果它发生变化,唯一可能改变 lpBuffer 中的值的是 memcpy,这意味着您在调用中覆盖了它(即,它没有执行您想要的操作)认为它正在做...仔细检查你的参数)。

不过,我的猜测是,查看您的代码后,对 memcpy 的调用可能不会发生变化。也就是说,如果在前后检查 lpBuffer 的值显示它没有变化,那么您在调用 memcpy 之前无意中更改了它。您需要跟踪该变化。

Check the value of lpBuffer immediately before you call memcpy and again immediately afterwards. Does it change?

If it changes, the only thing that could have changed the value in lpBuffer is the memcpy, 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 of lpBuffer immediately before and after shows it to be unchanged, you are inadvertantly changing it prior to calling memcpy. You'll need to track that change down.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文