Skip to content

Commit

Permalink
Make crypt and crypt_gensalt use thread-local output buffers.
Browse files Browse the repository at this point in the history
This change makes crypt and crypt_gensalt as thread-safe as
they can be without changing their interfaces.  Solaris
already made this change, and it’s being discussed by glibc
(with suggestion that it should be pushed upstream to the C
and POSIX standards committees):
https://sourceware.org/ml/libc-alpha/2018-10/msg00437.html

Portable programs should still use the r-variants, though,
because this is not a guaranteed feature, it doesn’t make
them not clobber their output buffers on a second call from
within the same thread, and the tradeoff is a some overhead
for initializing a buffer of `sizeof (crypt_data)` and for
copying the computed output to the thread-local output buffer
for each invocation of the crypt function.
  • Loading branch information
besser82 committed Dec 3, 2024
1 parent aa6c75b commit c450c0c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/crypt-static.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@
char *
crypt (const char *key, const char *setting)
{
static TLS_LD struct crypt_data nr_crypt_ctx;
return crypt_r (key, setting, &nr_crypt_ctx);
struct crypt_data nr_crypt_ctx;
static TLS_LD char output[CRYPT_OUTPUT_SIZE];

crypt_r (key, setting, &nr_crypt_ctx);
strcpy_or_abort (output, sizeof (output), nr_crypt_ctx.output);

#if ENABLE_FAILURE_TOKENS
return output;
#else
return output[0] == '*' ? 0 : output;
#endif /* ENABLE_FAILURE_TOKENS */
}
SYMVER_crypt;
#endif
Expand Down

0 comments on commit c450c0c

Please sign in to comment.