Oops. Missing NULL frees.
[openssl.git] / crypto / lhash / lhash.c
index 3e6124894626f6c898f2b520a4f1d24950158978..73d5fdc45ea1bebc612c955d3e22d2bb1932f143 100644 (file)
@@ -1,5 +1,5 @@
 /* crypto/lhash/lhash.c */
-/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * [including the GNU Public Licence.]
  */
 
-char *lh_version="lhash part of SSLeay 0.8.1b 29-Jun-1998";
+char *lh_version="lhash part of OpenSSL 0.9.2 31-Dec-1998";
 
 /* Code for dynamic hash table routines
  * Author - Eric Young v 2.0
  *
+ * 2.2 eay - added #include "crypto.h" so the memory leak checking code is
+ *          present. eay 18-Jun-98
+ *
+ * 2.1 eay - Added an 'error in last operation' flag. eay 6-May-98
+ *
  * 2.0 eay - Fixed a bug that occured when using lh_delete
  *          from inside lh_doall().  As entries were deleted,
  *          the 'table' was 'contract()ed', making some entries
@@ -94,6 +99,7 @@ char *lh_version="lhash part of SSLeay 0.8.1b 29-Jun-1998";
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include "crypto.h"
 #include "lhash.h"
 
 #undef MIN_NODES 
@@ -126,9 +132,9 @@ int (*c)();
        LHASH *ret;
        int i;
 
-       if ((ret=(LHASH *)malloc(sizeof(LHASH))) == NULL)
+       if ((ret=(LHASH *)Malloc(sizeof(LHASH))) == NULL)
                goto err0;
-       if ((ret->b=(LHASH_NODE **)malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)
+       if ((ret->b=(LHASH_NODE **)Malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)
                goto err1;
        for (i=0; i<MIN_NODES; i++)
                ret->b[i]=NULL;
@@ -156,9 +162,10 @@ int (*c)();
        ret->num_retrieve_miss=0;
        ret->num_hash_comps=0;
 
+       ret->error=0;
        return(ret);
 err1:
-       free((char *)ret);
+       Free((char *)ret);
 err0:
        return(NULL);
        }
@@ -169,18 +176,21 @@ LHASH *lh;
        unsigned int i;
        LHASH_NODE *n,*nn;
 
+       if(lh == NULL)
+           return;
+
        for (i=0; i<lh->num_nodes; i++)
                {
                n=lh->b[i];
                while (n != NULL)
                        {
                        nn=n->next;
-                       free(n);
+                       Free(n);
                        n=nn;
                        }
                }
-       free((char *)lh->b);
-       free((char *)lh);
+       Free((char *)lh->b);
+       Free((char *)lh);
        }
 
 char *lh_insert(lh, data)
@@ -191,6 +201,7 @@ char *data;
        LHASH_NODE *nn,**rn;
        char *ret;
 
+       lh->error=0;
        if (lh->up_load <= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))
                expand(lh);
 
@@ -198,8 +209,11 @@ char *data;
 
        if (*rn == NULL)
                {
-               if ((nn=(LHASH_NODE *)malloc(sizeof(LHASH_NODE))) == NULL)
+               if ((nn=(LHASH_NODE *)Malloc(sizeof(LHASH_NODE))) == NULL)
+                       {
+                       lh->error++;
                        return(NULL);
+                       }
                nn->data=data;
                nn->next=NULL;
 #ifndef NO_HASH_COMP
@@ -227,6 +241,7 @@ char *data;
        LHASH_NODE *nn,**rn;
        char *ret;
 
+       lh->error=0;
        rn=getrn(lh,data,&hash);
 
        if (*rn == NULL)
@@ -239,7 +254,7 @@ char *data;
                nn= *rn;
                *rn=nn->next;
                ret=nn->data;
-               free((char *)nn);
+               Free((char *)nn);
                lh->num_delete++;
                }
 
@@ -259,6 +274,7 @@ char *data;
        LHASH_NODE **rn;
        char *ret;
 
+       lh->error=0;
        rn=getrn(lh,data,&hash);
 
        if (*rn == NULL)
@@ -342,11 +358,12 @@ LHASH *lh;
        if ((lh->p) >= lh->pmax)
                {
                j=(int)lh->num_alloc_nodes*2;
-               n=(LHASH_NODE **)realloc((char *)lh->b,
+               n=(LHASH_NODE **)Realloc((char *)lh->b,
                        (unsigned int)sizeof(LHASH_NODE *)*j);
                if (n == NULL)
                        {
 /*                     fputs("realloc error in lhash",stderr); */
+                       lh->error++;
                        lh->p=0;
                        return;
                        }
@@ -370,11 +387,12 @@ LHASH *lh;
        lh->b[lh->p+lh->pmax-1]=NULL; /* 24/07-92 - eay - weird but :-( */
        if (lh->p == 0)
                {
-               n=(LHASH_NODE **)realloc((char *)lh->b,
+               n=(LHASH_NODE **)Realloc((char *)lh->b,
                        (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));
                if (n == NULL)
                        {
 /*                     fputs("realloc error in lhash",stderr); */
+                       lh->error++;
                        return;
                        }
                lh->num_contract_reallocs++;