Make sure not to read beyond end of buffer
[oweals/openssl.git] / crypto / lhash / lhash.c
index 60699f45ccfd190a99374868eda6bd1303d968c3..04ea80203cc8ba192b21c186875190179071d70d 100644 (file)
 #include <openssl/crypto.h>
 #include <openssl/lhash.h>
 
-const char *lh_version="lhash" OPENSSL_VERSION_PTEXT;
+const char lh_version[]="lhash" OPENSSL_VERSION_PTEXT;
 
 #undef MIN_NODES 
 #define MIN_NODES      16
@@ -109,7 +109,7 @@ const char *lh_version="lhash" OPENSSL_VERSION_PTEXT;
 
 static void expand(LHASH *lh);
 static void contract(LHASH *lh);
-static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash);
+static LHASH_NODE **getrn(LHASH *lh, const void *data, unsigned long *rhash);
 
 LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)
        {
@@ -197,7 +197,7 @@ void *lh_insert(LHASH *lh, void *data)
                        }
                nn->data=data;
                nn->next=NULL;
-#ifndef NO_HASH_COMP
+#ifndef OPENSSL_NO_HASH_COMP
                nn->hash=hash;
 #endif
                *rn=nn;
@@ -214,7 +214,7 @@ void *lh_insert(LHASH *lh, void *data)
        return(ret);
        }
 
-void *lh_delete(LHASH *lh, void *data)
+void *lh_delete(LHASH *lh, const void *data)
        {
        unsigned long hash;
        LHASH_NODE *nn,**rn;
@@ -245,7 +245,7 @@ void *lh_delete(LHASH *lh, void *data)
        return(ret);
        }
 
-void *lh_retrieve(LHASH *lh, void *data)
+void *lh_retrieve(LHASH *lh, const void *data)
        {
        unsigned long hash;
        LHASH_NODE **rn;
@@ -267,19 +267,8 @@ void *lh_retrieve(LHASH *lh, void *data)
        return(ret);
        }
 
-void lh_doall(LHASH *lh, LHASH_DOALL_FN_TYPE func)
-       {
-       /* Yikes that's bad - we're accepting a function that accepts 2
-        * parameters (albeit we have to waive type-safety here) and then
-        * forcibly calling that callback with *3* parameters leaving the 3rd
-        * NULL. Obviously this "works" otherwise it wouldn't have survived so
-        * long, but is it "good"??
-        * FIXME: Use an internal function from this and the "_arg" version that
-        * doesn't assume the ability to mutate function prototypes so badly. */
-       lh_doall_arg(lh, (LHASH_DOALL_ARG_FN_TYPE)func, NULL);
-       }
-
-void lh_doall_arg(LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)
+static void doall_util_fn(LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
+                         LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
        {
        int i;
        LHASH_NODE *a,*n;
@@ -294,12 +283,25 @@ void lh_doall_arg(LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)
                        /* 28/05/91 - eay - n added so items can be deleted
                         * via lh_doall */
                        n=a->next;
-                       func(a->data,arg);
+                       if(use_arg)
+                               func_arg(a->data,arg);
+                       else
+                               func(a->data);
                        a=n;
                        }
                }
        }
 
+void lh_doall(LHASH *lh, LHASH_DOALL_FN_TYPE func)
+       {
+       doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL);
+       }
+
+void lh_doall_arg(LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)
+       {
+       doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);
+       }
+
 static void expand(LHASH *lh)
        {
        LHASH_NODE **n,**n1,**n2,*np;
@@ -316,7 +318,7 @@ static void expand(LHASH *lh)
        
        for (np= *n1; np != NULL; )
                {
-#ifndef NO_HASH_COMP
+#ifndef OPENSSL_NO_HASH_COMP
                hash=np->hash;
 #else
                hash=lh->hash(np->data);
@@ -337,7 +339,7 @@ static void expand(LHASH *lh)
                {
                j=(int)lh->num_alloc_nodes*2;
                n=(LHASH_NODE **)OPENSSL_realloc(lh->b,
-                       (unsigned int)sizeof(LHASH_NODE *)*j);
+                       (int)(sizeof(LHASH_NODE *)*j));
                if (n == NULL)
                        {
 /*                     fputs("realloc error in lhash",stderr); */
@@ -395,11 +397,11 @@ static void contract(LHASH *lh)
                }
        }
 
-static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash)
+static LHASH_NODE **getrn(LHASH *lh, const void *data, unsigned long *rhash)
        {
        LHASH_NODE **ret,*n1;
        unsigned long hash,nn;
-       int (*cf)();
+       LHASH_COMP_FN_TYPE cf;
 
        hash=(*(lh->hash))(data);
        lh->num_hash_calls++;
@@ -413,7 +415,7 @@ static LHASH_NODE **getrn(LHASH *lh, void *data, unsigned long *rhash)
        ret= &(lh->b[(int)nn]);
        for (n1= *ret; n1 != NULL; n1=n1->next)
                {
-#ifndef NO_HASH_COMP
+#ifndef OPENSSL_NO_HASH_COMP
                lh->num_hash_comps++;
                if (n1->hash != hash)
                        {