Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is sprintf thread safe?

is sprintf thread safe ?

//Global log buffer 
char logBuffer[20];

logStatus (char * status, int length)
{
  snprintf(logBuffer, 19, status);
  printf ("%s\n", logBuffer);
}

The thread safety of this function totally depends upon the thread safety of snprintf/sprintf .

Updates : thanks for ur answers . i dont mind, if the actual contents gts messed up. but want to confirm that the sprintf would not cause a memory corruption / buffer overflow going beyond 20 bytes in this case, when multiple threads are trying to write to logBuffer ?

like image 665
Jay D Avatar asked Nov 14 '12 19:11

Jay D


People also ask

Is Sprintf secure?

Warning: The sprintf function can be dangerous because it can potentially output more characters than can fit in the allocation size of the string s . Remember that the field width given in a conversion specification is only a minimum value.

Is printf () thread-safe?

the standard C printf() and scanf() functions use stdio so they are thread-safe.

Is Strncpy thread-safe?

strncpy() — Copy Strings Threadsafe: Yes. The strncpy() function copies count characters of string2 to string1 . If count is less than or equal to the length of string2 , a null character (\0) is not appended to the copied string.

Is reading variables thread-safe?

No this operation is not inherently thread safe. Even though the variable is not currently being written to, previous writes to the variable may not yet be visible to all threads. This means two threads can read the same value and get different results creating a race condition.


4 Answers

There is no problem using snprintf() in multiple threads. But here you are writing to a shared string buffer, which I assume is shared across threads.

So your use of this function would not be thread safe.

like image 97
Jonathan Wood Avatar answered Oct 07 '22 06:10

Jonathan Wood


You have several problems with your code.

  1. Your usage of snprintf is very suspicious. Don't use it just to copy a string. Generally don't pass dynamically allocated strings with whatever content as format to any of the printf functions. They interpret the contents and if there is anything in them that resembles a %-format, you are doomed.
  2. Don't use static buffers as you do. This is certainly neither thread safe not re-entrant.
  3. Either use printf with an appropriate format directly, or replace the call by puts.

Then, Linux adheres to the POSIX standard, which requires that the standard IO functions are thread safe.

like image 42
Jens Gustedt Avatar answered Oct 07 '22 06:10

Jens Gustedt


"There is no problem using snprintf() in multiple threads."

Not true.

Not true, at least in case of POSIX functions.

All of the standard vararg functions are not mt-safe - this includes all the printf() family (1), but also every other variadic function as well (2)

  1. sprintf() for example is: "MT-Safe locale|AS-Unsafe heap|AC-Unsafe mem" - what means, that it can fail if locale is set asynchronously or if asynchronous cancellation of threads is used. In other words, special attention must be paid when using such functions in MT environment.

  2. va_arg is not mt-safe: |MT-Safe race:ap|AS-Safe|AC-Unsafe corrupt| - what means, that inter-locking is needed.

Additionally, what should be obvious, even totally mt-safe function can be used in unsafe way - what happens for example if two or more threads are operating the same data/memory areas.

like image 3
vtomazzi Avatar answered Oct 07 '22 08:10

vtomazzi


Your question has an incorrect premise. Even if sprintf itself can be safely called from multiple threads at the same time (as I sure hope it can), your code is not protecting your global variable. The standard library can't possibly help you there.

like image 2
Jamey Sharp Avatar answered Oct 07 '22 08:10

Jamey Sharp