Commit 905b1600 authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

1. check allocs

2. don't leave allocated memory behind when returning error
parent 52596c33
Loading
Loading
Loading
Loading
+20 −5
Changes for lib/hash.c: 20 added lines, 5 removed lines.
Original line number Original line Diff line number Diff line
@@ -64,7 +64,8 @@ _hash_element_dtor (void *user, void *element)
  free(e);
  free(e);
}
}


void 
/* return 1 on error, 0 is fine */
int
Curl_hash_init(curl_hash *h, int slots, curl_hash_dtor dtor)
Curl_hash_init(curl_hash *h, int slots, curl_hash_dtor dtor)
{
{
  int i;
  int i;
@@ -74,10 +75,21 @@ Curl_hash_init (curl_hash *h, int slots, curl_hash_dtor dtor)
  h->slots = slots;  
  h->slots = slots;  


  h->table = (curl_llist **) malloc(slots * sizeof(curl_llist *));
  h->table = (curl_llist **) malloc(slots * sizeof(curl_llist *));
  if(h->table) {
    for (i = 0; i < slots; ++i) {
    for (i = 0; i < slots; ++i) {
      h->table[i] = Curl_llist_alloc((curl_llist_dtor) _hash_element_dtor);
      h->table[i] = Curl_llist_alloc((curl_llist_dtor) _hash_element_dtor);
      if(!h->table[i]) {
        while(i--)
          Curl_llist_destroy(h->table[i], NULL);
        free(h->table);
        return 1; /* failure */
      }
      }
    }
    }
    return 0; /* fine */
  }
  else
    return 1; /* failure */
}


curl_hash *
curl_hash *
Curl_hash_alloc(int slots, curl_hash_dtor dtor)
Curl_hash_alloc(int slots, curl_hash_dtor dtor)
@@ -85,10 +97,13 @@ Curl_hash_alloc (int slots, curl_hash_dtor dtor)
  curl_hash *h;
  curl_hash *h;


  h = (curl_hash *) malloc(sizeof(curl_hash));
  h = (curl_hash *) malloc(sizeof(curl_hash));
  if (NULL == h)
  if (h) {
    return NULL;
    if(Curl_hash_init(h, slots, dtor)) {

      /* failure */
  Curl_hash_init(h, slots, dtor);
      free(h);
      h = NULL;
    }
  }


  return h;
  return h;
}
}