Unchecked malloc fixes
[openssl.git] / crypto / threads / th-lock.c
index 1b5765948a95d0cc5dea4f65414d43de103b9c67..7b303b281a4d5180aa5f16c42cb93474930fea05 100644 (file)
@@ -117,6 +117,10 @@ void CRYPTO_thread_setup(void)
     int i;
 
     lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
+    if(!lock_cs) {
+        /* Nothing we can do about this...void function! */
+        return;
+    }
     for (i = 0; i < CRYPTO_num_locks(); i++) {
         lock_cs[i] = CreateMutex(NULL, FALSE, NULL);
     }
@@ -168,6 +172,10 @@ void CRYPTO_thread_setup(void)
 # else
     lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(rwlock_t));
 # endif
+    if(!lock_cs) {
+        /* Nothing we can do about this...void function! */
+        return;
+    }
     lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
     for (i = 0; i < CRYPTO_num_locks(); i++) {
         lock_count[i] = 0;
@@ -200,18 +208,6 @@ void CRYPTO_thread_cleanup(void)
 
 void solaris_locking_callback(int mode, int type, char *file, int line)
 {
-# if 0
-    fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n",
-            CRYPTO_thread_id(),
-            (mode & CRYPTO_LOCK) ? "l" : "u",
-            (type & CRYPTO_READ) ? "r" : "w", file, line);
-# endif
-
-# if 0
-    if (CRYPTO_LOCK_SSL_CERT == type)
-        fprintf(stderr, "(t,m,f,l) %ld %d %s %d\n",
-                CRYPTO_thread_id(), mode, file, line);
-# endif
     if (mode & CRYPTO_LOCK) {
 # ifdef USE_MUTEX
         mutex_lock(&(lock_cs[type]));
@@ -251,6 +247,12 @@ void CRYPTO_thread_setup(void)
     int i;
     char filename[20];
 
+    lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
+    if(!lock_cs) {
+        /* Nothing we can do about this...void function! */
+        return;
+    }
+
     strcpy(filename, "/tmp/mttest.XXXXXX");
     mktemp(filename);
 
@@ -261,7 +263,6 @@ void CRYPTO_thread_setup(void)
     arena = usinit(filename);
     unlink(filename);
 
-    lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *));
     for (i = 0; i < CRYPTO_num_locks(); i++) {
         lock_cs[i] = usnewsema(arena, 1);
     }
@@ -315,6 +316,14 @@ void CRYPTO_thread_setup(void)
 
     lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
     lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
+    if(!lock_cs || !lock_count) {
+        /* Nothing we can do about this...void function! */
+        if(lock_cs)
+            OPENSSL_free(lock_cs);
+        if(lock_count)
+            OPENSSL_free(lock_count);
+        return;
+    }
     for (i = 0; i < CRYPTO_num_locks(); i++) {
         lock_count[i] = 0;
         pthread_mutex_init(&(lock_cs[i]), NULL);
@@ -338,17 +347,6 @@ void thread_cleanup(void)
 
 void pthreads_locking_callback(int mode, int type, char *file, int line)
 {
-# if 0
-    fprintf(stderr, "thread=%4d mode=%s lock=%s %s:%d\n",
-            CRYPTO_thread_id(),
-            (mode & CRYPTO_LOCK) ? "l" : "u",
-            (type & CRYPTO_READ) ? "r" : "w", file, line);
-# endif
-# if 0
-    if (CRYPTO_LOCK_SSL_CERT == type)
-        fprintf(stderr, "(t,m,f,l) %ld %d %s %d\n",
-                CRYPTO_thread_id(), mode, file, line);
-# endif
     if (mode & CRYPTO_LOCK) {
         pthread_mutex_lock(&(lock_cs[type]));
         lock_count[type]++;