1

I was just writing some debugging in a signal handler, and noticed that from the man7 website, snprintf is not listed as an async safe function. I would think that this would simply modify local variables, so I'm wondering why this is not async safe?

HardcoreHenry
  • 5,909
  • 2
  • 19
  • 44

1 Answers1

1

why this is not async safe?

While snprintf (and sprintf) tend to work just fine in signal handlers, they are not guaranteed to work.

I would think that this would simply modify local variables,

These functions may need to initialize locale machinery, which is definitely not async-signal safe.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • So you're suggesting it may be using a shared global machinery where some variables are per-thread, and thus may be shared with other instances of snprintf. With the word _tend_ are you implying that most systems they may be async safe, or are you suggesting that the race condition is so small, they _tend_ not hit it? – HardcoreHenry Jun 04 '21 at 16:13