x509/by_dir.c: fix run-away pointer (and potential SEGV)
[openssl.git] / crypto / x509 / by_dir.c
index f02168ab92a255ff85645fdb10608bb04b3a0f44..c6602dae4f58b88416a6d2cdbcbb4ab7d0868eaf 100644 (file)
@@ -72,8 +72,6 @@
 #include <openssl/lhash.h>
 #include <openssl/x509.h>
 
-DECLARE_STACK_OF(BY_DIR_HASH)
-DECLARE_STACK_OF(BY_DIR_ENTRY)
 
 typedef struct lookup_dir_hashes_st
        {
@@ -94,6 +92,8 @@ typedef struct lookup_dir_st
        STACK_OF(BY_DIR_ENTRY) *dirs;
        } BY_DIR;
 
+DECLARE_STACK_OF(BY_DIR_HASH)
+DECLARE_STACK_OF(BY_DIR_ENTRY)
 
 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
        char **ret);
@@ -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'))
                        {
@@ -230,7 +230,7 @@ static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
                        for (j=0; j < sk_BY_DIR_ENTRY_num(ctx->dirs); j++)
                                {
                                ent = sk_BY_DIR_ENTRY_value(ctx->dirs, j);
-                               if (strlen(ent->dir) == len &&
+                               if (strlen(ent->dir) == (size_t)len &&
                                    strncmp(ent->dir,ss,(unsigned int)len) == 0)
                                        break;
                                }
@@ -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 */
@@ -477,4 +478,3 @@ finish:
        if (b != NULL) BUF_MEM_free(b);
        return(ok);
        }
-