memcpy() 函数是可重入的吗?

发布于 2024-10-24 02:38:54 字数 108 浏览 3 评论 0原文

我在信号处理程序中调用一些 C++ 函数,并且我的程序因分段错误而终止。 当我检查 gdb 时,memcpy() 函数是我得到 SIGSEGV 的地方。 我想知道 memcpy() 是否是可重入函数?

I call some C++ functions inside a signal handler and my program is terminated by segmentation fault.
When I check with gdb, memcpy() function is where i get SIGSEGV.
I would like to know if memcpy() is a reentrant function or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

绿光 2024-10-31 02:38:54

在除了高度嵌入式平台之外的所有平台中,它都是可重入的。你提到了 SIGSEGV 所以我认为它不是其中之一。在这种情况下,memcpy() 很可能不是罪魁祸首:这是调用者的错误。如果你要求 memcpy() 复制错误的指针(或错误的长度),那么它就会出错。你可以很容易地做到这一点:

memcpy(NULL, NULL, 123456789);

这将导致 SIGSEGV 并且它会告诉你 memcpy() 引起的。当然,这不是 memcpy 的错——它只是按照你告诉它的去做。您的信号处理程序正在用一些奇怪的东西调用它。对调用者站点的回溯(在 gdb 或您拥有的任何工具中)应该显示您调用它的内容。如果做不到这一点,只需 printf 您传递给 memcpy 的参数即可。

In all but the most highly embedded platforms, it'll be reentrant. You mention SIGSEGV so I assume it's not one of those. In this case it's most likely memcpy() isn't the culprit: it's the caller's fault. If you ask memcpy() to copy bad pointers (or bad length) then it'll be the one which faults. You could easily do this:

memcpy(NULL, NULL, 123456789);

That'll cause a SIGSEGV and it'll tell you memcpy() caused it. Of course, it's not memcpy's fault - it's just doing what you told it. Your signal handler is calling it with something odd. A backtrace (in gdb or whatever tool you have) to the site of the caller should show what you called it with. Failing that, just printf the arguments you're passing to memcpy.

多彩岁月 2024-10-31 02:38:54

有关(非)可重入函数和信号处理程序(就 GNU C 库而言)的一些相关信息可以在 http://www.gnu.org/s/libc/manual/html_node/Nonreentrancy.html#Nonreentrancy

这部分似乎与您的问题特别相关:

  • < p>“仅从内存对象中读取是安全的,只要您可以处理在可以传递信号时可能出现在对象中的任何值。请记住,对某些数据类型的分配需要多个数据类型 程序可以在变量赋值的“中间”运行。”

  • “只要值突然变化,在处理程序可能运行的任何时候都不会干扰任何内容,仅仅写入内存对象是安全的。”

Some relevant information concerning (non)reentrant functions and signal handlers (as far as relevant to the GNU C library) can be found at http://www.gnu.org/s/libc/manual/html_node/Nonreentrancy.html#Nonreentrancy:

This part seems especially relevant to your question:

  • "Merely reading from a memory object is safe provided that you can deal with any of the values that might appear in the object at a time when the signal can be delivered. Keep in mind that assignment to some data types requires more than one instruction, which means that the handler could run “in the middle of” an assignment to the variable if its type is not atomic."

  • "Merely writing into a memory object is safe as long as a sudden change in the value, at any time when the handler might run, will not disturb anything."

要走就滚别墨迹 2024-10-31 02:38:54

我不明白为什么它不能重入。我不确定,但我认为这很大程度上取决于您使用的库。

I don't see why it could not be reentrant. I'm not sure, but i think its a lot based on the libraries you use.

情痴 2024-10-31 02:38:54

除非 memcpy 实现得很糟糕,否则它是可重入的。它仅适用于您提供的内容 - 指针和长度值。所有参数均按值传递,因此一旦激活函数,无论信号和/或其他线程如何,这些值都不会在其堆栈帧上更改。

Unless memcpy is BADLY implemented, it is reentrant. It will only work with what you give it - the pointers and the length value. All parameters are passed by value, so once the function has been activated, these values will not change on its stack frame, regardless of signals and/or other threads.

长安忆 2024-10-31 02:38:54

我认为问题在于您使用无效(或已删除)的指针作为 memcpy 函数的参数,请仔细检查您的代码。

问候。

I think that the problem is that you are using as parameters for the memcpy function a invalid (or deleted) pointer, please check your code carefully.

Regards.

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