Missing NULL check on OBJ_dup result in x509_name_canon
[openssl.git] / crypto / threads_win.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/crypto.h>
11
12 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
13
14 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
15 {
16     CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION));
17     if (lock == NULL)
18         return NULL;
19
20     /* 0x400 is the spin count value suggested in the documentation */
21     if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) {
22         OPENSSL_free(lock);
23         return NULL;
24     }
25
26     return lock;
27 }
28
29 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
30 {
31     EnterCriticalSection(lock);
32     return 1;
33 }
34
35 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
36 {
37     EnterCriticalSection(lock);
38     return 1;
39 }
40
41 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
42 {
43     LeaveCriticalSection(lock);
44     return 1;
45 }
46
47 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
48 {
49     if (lock == NULL)
50         return;
51
52     DeleteCriticalSection(lock);
53     OPENSSL_free(lock);
54
55     return;
56 }
57
58 #  define ONCE_UNINITED     0
59 #  define ONCE_ININIT       1
60 #  define ONCE_DONE         2
61
62 /*
63  * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
64  * we still have to support.
65  */
66 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
67 {
68     LONG volatile *lock = (LONG *)once;
69     LONG result;
70
71     if (*lock == ONCE_DONE)
72         return 1;
73
74     do {
75         result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
76         if (result == ONCE_UNINITED) {
77             init();
78             *lock = ONCE_DONE;
79             return 1;
80         }
81     } while (result == ONCE_ININIT);
82
83     return (*lock == ONCE_DONE);
84 }
85
86 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
87 {
88     *key = TlsAlloc();
89     if (*key == TLS_OUT_OF_INDEXES)
90         return 0;
91
92     return 1;
93 }
94
95 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
96 {
97     return TlsGetValue(*key);
98 }
99
100 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
101 {
102     if (TlsSetValue(*key, val) == 0)
103         return 0;
104
105     return 1;
106 }
107
108 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
109 {
110     if (TlsFree(*key) == 0)
111         return 0;
112
113     return 1;
114 }
115
116 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
117 {
118     return GetCurrentThreadId();
119 }
120
121 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
122 {
123     return (a == b);
124 }
125
126 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
127 {
128     *ret = InterlockedExchangeAdd(val, amount) + amount;
129     return 1;
130 }
131
132 #endif