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
?
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.
the standard C printf() and scanf() functions use stdio so they are 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.
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.
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.
You have several problems with your code.
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.static
buffers as you do. This is certainly neither
thread safe not re-entrant.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.
"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)
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With