From 05f45575bb5f3dc814fb1cbd7ca18ab79788dee9 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 19 Apr 2013 14:19:23 +0200 Subject: [PATCH] sodium_init() is not thread-safe. Roll your own locks if you need that. --- README.markdown | 3 +++ src/libsodium/sodium/core.c | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index 8b12369a..f6426303 100644 --- a/README.markdown +++ b/README.markdown @@ -77,6 +77,9 @@ Before doing anything else with the library, call: sodium_init(); +This function is not thread-safe. No other Sodium functions should be +called until it successfully returns. + And if you need to release memory and other resources possibly allocated by the library, call: diff --git a/src/libsodium/sodium/core.c b/src/libsodium/sodium/core.c index f354f1fa..84fad58e 100644 --- a/src/libsodium/sodium/core.c +++ b/src/libsodium/sodium/core.c @@ -2,14 +2,15 @@ #include "core.h" #include "crypto_onetimeauth.h" -static char initialized; +static _Bool initialized; int sodium_init(void) { - if (__sync_lock_test_and_set(&initialized, 1) != 0) { + if (initialized != 0) { return 1; } + initialized = 1; if (crypto_onetimeauth_pick_best_implementation() == NULL) { return -1; }