Commit 0b1319ba authored by Andy Polyakov's avatar Andy Polyakov
Browse files

crypto/init.c: improve destructor_key's portability.

It was assumed that CRYPTO_THREAD_LOCAL is universally scalar type,
which doesn't appear to hold true.

Reviewed-by: default avatarKurt Roeckx <kurt@roeckx.be>
(Merged from https://github.com/openssl/openssl/pull/6976)
parent 2d162ea9
Loading
Loading
Loading
Loading
+11 −8
Changes for crypto/init.c: 11 added lines, 8 removed lines.
Original line number Original line Diff line number Diff line
@@ -44,7 +44,10 @@ static int stopped = 0;
 * key value and pull NULL past initialization in the first thread that
 * key value and pull NULL past initialization in the first thread that
 * intends to use libcrypto.
 * intends to use libcrypto.
 */
 */
static CRYPTO_THREAD_LOCAL destructor_key = (CRYPTO_THREAD_LOCAL)-1;
static union {
    long sane;
    CRYPTO_THREAD_LOCAL value;
} destructor_key = { -1 };


static void ossl_init_thread_stop(struct thread_local_inits_st *locals);
static void ossl_init_thread_stop(struct thread_local_inits_st *locals);


@@ -56,17 +59,17 @@ static void ossl_init_thread_destructor(void *local)
static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)
{
{
    struct thread_local_inits_st *local =
    struct thread_local_inits_st *local =
        CRYPTO_THREAD_get_local(&destructor_key);
        CRYPTO_THREAD_get_local(&destructor_key.value);


    if (alloc) {
    if (alloc) {
        if (local == NULL
        if (local == NULL
            && (local = OPENSSL_zalloc(sizeof(*local))) != NULL
            && (local = OPENSSL_zalloc(sizeof(*local))) != NULL
            && !CRYPTO_THREAD_set_local(&destructor_key, local)) {
            && !CRYPTO_THREAD_set_local(&destructor_key.value, local)) {
            OPENSSL_free(local);
            OPENSSL_free(local);
            return NULL;
            return NULL;
        }
        }
    } else {
    } else {
        CRYPTO_THREAD_set_local(&destructor_key, NULL);
        CRYPTO_THREAD_set_local(&destructor_key.value, NULL);
    }
    }


    return local;
    return local;
@@ -103,7 +106,7 @@ DEFINE_RUN_ONCE_STATIC(ossl_init_base)
#endif
#endif
    OPENSSL_cpuid_setup();
    OPENSSL_cpuid_setup();


    destructor_key = key;
    destructor_key.value = key;
    base_inited = 1;
    base_inited = 1;
    return 1;
    return 1;


@@ -409,7 +412,7 @@ static void ossl_init_thread_stop(struct thread_local_inits_st *locals)


void OPENSSL_thread_stop(void)
void OPENSSL_thread_stop(void)
{
{
    if (destructor_key != (CRYPTO_THREAD_LOCAL)-1)
    if (destructor_key.sane != -1)
        ossl_init_thread_stop(ossl_init_get_thread_local(0));
        ossl_init_thread_stop(ossl_init_get_thread_local(0));
}
}


@@ -515,8 +518,8 @@ void OPENSSL_cleanup(void)
        err_free_strings_int();
        err_free_strings_int();
    }
    }


    key = destructor_key;
    key = destructor_key.value;
    destructor_key = (CRYPTO_THREAD_LOCAL)-1;
    destructor_key.sane = -1;
    CRYPTO_THREAD_cleanup_local(&key);
    CRYPTO_THREAD_cleanup_local(&key);


#ifdef OPENSSL_INIT_DEBUG
#ifdef OPENSSL_INIT_DEBUG