get_cert_by_subject: check for NULL when allocating hent
[openssl.git] / crypto / x509 / by_dir.c
index b355de9b1c27756296735150d98511784da9d840..8f3560a232382e996dedd60709bcc885ca98ccd4 100644 (file)
@@ -218,7 +218,7 @@ static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
 
        s=dir;
        p=s;
-       for (;;p++)
+       do
                {
                if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0'))
                        {
@@ -264,9 +264,7 @@ static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
                                return 0;
                                }
                        }
-               if (*p == '\0')
-                       break;
-               }
+               } while (*p++ != '\0');
        return 1;
        }
 
@@ -392,6 +390,9 @@ static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
                                        postfix,k);
                                }
 #ifndef OPENSSL_NO_POSIX_IO
+#ifdef _WIN32
+#define stat _stat
+#endif
                        {
                        struct stat st;
                        if (stat(b->data,&st) < 0)
@@ -417,11 +418,11 @@ static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
 
                /* we have added it to the cache so now pull
                 * it out again */
-               CRYPTO_r_lock(CRYPTO_LOCK_X509_STORE);
+               CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
                j = sk_X509_OBJECT_find(xl->store_ctx->objs,&stmp);
                if(j != -1) tmp=sk_X509_OBJECT_value(xl->store_ctx->objs,j);
                else tmp = NULL;
-               CRYPTO_r_unlock(CRYPTO_LOCK_X509_STORE);
+               CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
 
 
                /* If a CRL, update the last file suffix added for this */
@@ -443,6 +444,13 @@ static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
                        if (!hent)
                                {
                                hent = OPENSSL_malloc(sizeof(BY_DIR_HASH));
+                               if (hent == NULL)
+                                       {
+                                       CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
+                                       X509err(X509_F_GET_CERT_BY_SUBJECT,ERR_R_MALLOC_FAILURE);
+                                       ok = 0;
+                                       goto finish;
+                                       }
                                hent->hash = h;
                                hent->suffix = k;
                                if (!sk_BY_DIR_HASH_push(ent->hashes, hent))
@@ -477,4 +485,3 @@ finish:
        if (b != NULL) BUF_MEM_free(b);
        return(ok);
        }
-