English 中文(简体)
信号手里的印数如果从四舍五入开始,就会造成断裂。
原标题:snprintf in signal handler creates segmentation fault if started with valgrind

This very simple C program gives me a segmentation fault when running it with valgrind. Its runs fine when started normal. It crashes when you send the USR1 signal to the process.

问题似乎是<代码>snprintf。 处理浮动价值的格式,因为如果你使用舱面(<条码>%s)或载于t(<条码>%d)格式参数,则会进行罚款。

P.S. I know you you shouldn t call any printf family function inside a signal handler, but still why does it only crash with valgrind.

    #include <stdio.h>
    #include <signal.h>

    void sig_usr1(int sig) {
            char buf[128];
            snprintf(buf, sizeof(buf), "%f", 1.0);
    }

    int main(int argc, char **argv) {
            (void) signal(SIGUSR1, sig_usr1);
            while(1);
    }
最佳回答

This is a valgrind bug. It calls your signal handler with a stack that is not 16-byte aligned as required by the ABI. On x86_64, floating point arguments are passed in XMM registers which can only be stored at addresses that are 16-byte aligned. You can work around the problem by compiling for 32-bit (gcc -m32).

问题回答

https://stackoverflow.com/users/714501/cnicutar” 注意,阀门可能会对任何与时间相关的时间产生影响,信号处理器肯定会符合条件。

我不认为<条码>snprintf是安全的,可以在信号手里使用,这样,它就只能因事故而在非瓦尔农案件中工作,然后朝圣,改变时间,并给你带来无伤风险的可怕死亡。

我在此发现了一份信号处理器安全的职能清单(根据PPOSIX.1-2003):

http://linux.die.net/man/2/signal

是的,linux.die.net man pages are a bit out ofdate but the list here (thanks to RedX for findings this one):

页: 1 或者,除了开放BSD之外,它还说:

... asnchronous-safe in OpenBSD but “probable not on other system,” including snprintf (), ...

so the implication is that snprintf is not, in general, safe in a signal handler.

And, thanks to Nemo, we have an authoritative list of functions that are safe for use in signal handlers:

http://pubs.opengroup.org/onlinepubs/9699919799/Functions/V2_chap02.html#tag_15_04_03

先从这一链接开始,然后查询<代码>_Exit ,然后看看名单;然后看snprintf

Also, I remember using write() in a signal handler because fprintf wasn t safe for a signal handler but that was a long time ago.

我没有一份相关标准的副本,因此我可以以任何真正具有权威性的方式支持这一标准,但我认为我已经提到这一点。

http://www.network-theory.co.uk/docs/valgrind/valgrind_27.html

沃尔格里德信号模拟并不像可能那样强大。 提供了基本的PPOSIX符合要求的异构体和单质功能,但可以想象,如果你用信号对事情进行冷静,事情会变得非常令人不安。 工作:无。 不管怎么说,非第9条信号模棱两可的方案必然是不可分的,因此应尽可能避免。

因此,信号手里的print不是PPOSIX的信号过滤器,而阀门则有权打碎你的节目。

为什么print不是信号安全?

《glibc手册》指出:http://www.gnu.org/software/hello/manual/libc/Nonreentrancy.html“rel=“nofollow” http://www.gnu.org/software/hello/manual/libc/Nonreentrancy.html。

如果一项职能使用并修改了你提供的物品,则该功能有可能是不可抗拒的;如果使用同一物体,则有两条电话可以干预。

当你使用溪流时就会出现这种情况。 假设信号手印成纸张。 假设方案是在发出信号时使用同一流的印呼中进行的。 信号手电和节目数据都可能受到腐蚀,因为这两条电话都使用同样的数据结构,即上游本身。

然而,如果你知道,在信号能够到达时,该方案不可能使用手稿的流,那么你是安全的。 如果方案使用其他一些渠道,就没有问题了。

你们可以说,print* 不是在流中,而是在扼杀上。 但是,在内部,斜体的print的确在特殊流上发挥作用:

ftp://sources.redhat.com/pub/glibc/snapshots/glibc-latest.tar.bz2/glibc-20090518/libio/vsnprintf.c

int
_IO_vsnprintf (string, maxlen, format, args)
{
  _IO_strnfile sf; // <<-- FILE*-like descriptor

<代码>%f,glibc的产出代码也在其内部有小型用户:

ftp://sources.redhat.com/pub/glibc/snapshots/glibc-latest.tar.bz2/glibc-20090518/stdio-common/fp.c

/* Allocate buffer for output.  We need two more because while rounding
   it is possible that we need two more characters in front of all the
   other output.  If the amount of memory we have to allocate is too
   large use `malloc  instead of `alloca .  */
size_t wbuffer_to_alloc = (2 + (size_t) chars_needed) * sizeof (wchar_t);
buffer_malloced = ! __libc_use_alloca (chars_needed * 2 * sizeof (wchar_t));
if (__builtin_expect (buffer_malloced, 0))
  {
    wbuffer = (wchar_t *) malloc (wbuffer_to_alloc);
    if (wbuffer == NULL)
      /* Signal an error to the caller.  */
      return -1;
  }
else
  wbuffer = (wchar_t *) alloca (wbuffer_to_alloc);

对您的方案时间安排稍有改动。

可在FAQ

My program crashes normally, but doesn t under Valgrind, or vice versa. What s happening?

When a program runs under Valgrind, its environment is slightly different to when it runs natively. Most of the time this doesn t make any difference, but it can, particularly if your program is buggy.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签