Skip to content

Commit ba7959d

Browse files
author
drscholl
committed
catch out of memory error conditions in list and hash functions
1 parent a1b9ddf commit ba7959d

File tree

4 files changed

+53
-31
lines changed

4 files changed

+53
-31
lines changed

hash.c

+36-20
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@ HASH *
2222
hash_init (int buckets, hash_destroy f)
2323
{
2424
HASH *h = CALLOC (1, sizeof (HASH));
25+
26+
if (!h)
27+
return 0;
2528
h->numbuckets = buckets;
26-
h->bucket = CALLOC (buckets, sizeof (HASHENT *));
29+
if ((h->bucket = CALLOC (buckets, sizeof (HASHENT *))) == 0)
30+
{
31+
FREE (h);
32+
return 0;
33+
}
2734
h->destroy = f;
2835
return h;
2936
}
@@ -32,7 +39,9 @@ static unsigned int
3239
hash_string (HASH * table, const char *key)
3340
{
3441
unsigned int sum = 0;
35-
for (;*key;key++)
42+
43+
ASSERT (key != 0);
44+
for (; *key; key++)
3645
{
3746
/* shifting by 1 bit prevents abc from having the same hash as acb */
3847
sum<<=1;
@@ -42,18 +51,24 @@ hash_string (HASH * table, const char *key)
4251
return sum;
4352
}
4453

45-
void
54+
int
4655
hash_add (HASH * table, const char *key, void *data)
4756
{
4857
HASHENT *he = CALLOC (1, sizeof (HASHENT));
4958
unsigned int sum;
5059

60+
if (!he)
61+
return -1;
62+
ASSERT (key != 0);
63+
ASSERT (data != 0);
64+
ASSERT (table != 0);
5165
he->key = key;
5266
he->data = data;
5367
sum = hash_string (table, key);
5468
he->next = table->bucket[sum];
5569
table->bucket[sum] = he;
5670
table->dbsize++;
71+
return 0;
5772
}
5873

5974
void *
@@ -62,38 +77,38 @@ hash_lookup (HASH * table, const char *key)
6277
HASHENT *he;
6378
unsigned int sum = hash_string (table, key);
6479
he = table->bucket[sum];
65-
while (he)
80+
81+
for (; he; he = he->next)
6682
{
6783
if (strcasecmp (key, he->key) == 0)
6884
return he->data;
69-
he = he->next;
7085
}
7186
return 0;
7287
}
7388

74-
void
89+
int
7590
hash_remove (HASH * table, const char *key)
7691
{
77-
HASHENT *he, *last = 0;
78-
unsigned int sum = hash_string (table, key);
79-
he = table->bucket[sum];
80-
while (he)
92+
HASHENT **he, *ptr;
93+
unsigned int sum;
94+
95+
ASSERT (table != 0);
96+
ASSERT (key != 0);
97+
sum = hash_string (table, key);
98+
for (he = &table->bucket[sum]; *he; he = &(*he)->next)
8199
{
82-
if (strcasecmp (key, he->key) == 0)
100+
if (!strcasecmp (key, (*he)->key))
83101
{
84-
if (last)
85-
last->next = he->next;
86-
else
87-
table->bucket[sum] = he->next;
102+
ptr = (*he)->next;
88103
if (table->destroy)
89-
table->destroy (he->data);
90-
FREE (he);
104+
table->destroy ((*he)->data);
105+
FREE (*he);
91106
table->dbsize--;
92-
break;
107+
*he = ptr;
108+
return 0;
93109
}
94-
last = he;
95-
he = he->next;
96110
}
111+
return -1;
97112
}
98113

99114
void
@@ -102,6 +117,7 @@ free_hash (HASH * h)
102117
HASHENT *he, *ptr;
103118
int i;
104119

120+
ASSERT (h != 0);
105121
/* destroy remaining entries */
106122
for (i = 0; i < h->numbuckets; i++)
107123
{

hash.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ HASH;
3131
typedef void (*hash_callback_t) (void *, void *);
3232

3333
HASH *hash_init (int, hash_destroy);
34-
void hash_add (HASH *, const char *, void *);
34+
int hash_add (HASH *, const char *, void *);
3535
void *hash_lookup (HASH *, const char *);
36-
void hash_remove (HASH *, const char *);
36+
int hash_remove (HASH *, const char *);
3737
void free_hash (HASH *);
3838
void hash_foreach (HASH *h, hash_callback_t, void *funcdata);
3939

list.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
$Id$ */
66

77
#include <stdlib.h>
8-
#include <stdio.h>
98
#include "list.h"
109
#include "debug.h"
1110

1211
LIST *
1312
list_new (void *data)
1413
{
1514
LIST *ptr = CALLOC (1, sizeof (LIST));
15+
16+
ASSERT (data != 0);
17+
ASSERT (ptr != 0);
1618
if (ptr)
1719
ptr->data = data;
18-
else
19-
fprintf (stderr, "list_new(): OUT OF MEMORY");
2020
return ptr;
2121
}
2222

@@ -38,6 +38,8 @@ list_delete (LIST *list, void *data)
3838
{
3939
LIST **ptr;
4040

41+
ASSERT (list != 0);
42+
ASSERT (data != 0);
4143
for (ptr = &list; *ptr; ptr = &(*ptr)->next)
4244
{
4345
if ((*ptr)->data == data)
@@ -54,20 +56,18 @@ list_append (LIST * l, void *data)
5456
{
5557
LIST *r = l;
5658

59+
ASSERT (data != 0);
5760
if (!l)
5861
{
59-
l = r = list_new (data);
60-
if (!r)
61-
return 0;
62+
r = list_new (data);
63+
ASSERT (r != 0);
6264
}
6365
else
6466
{
6567
while (l->next)
6668
l = l->next;
6769
l->next = list_new (data);
68-
if (!l->next)
69-
return r;
70-
l = l->next;
70+
ASSERT (l->next != 0);
7171
}
7272
return r;
7373
}

opennap.h

+6
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,12 @@ typedef unsigned int socklen_t;
630630
// see snprintf.c
631631
extern int snprintf (char *str,size_t count,const char *fmt,...);
632632
extern int vsnprintf (char *str, size_t count, const char *fmt, va_list args);
633+
extern int _getopt (int, char **, char *);
634+
635+
#define getopt _getopt
636+
637+
extern char *optarg;
638+
extern int optind;
633639

634640
#endif /* !WIN32 */
635641

0 commit comments

Comments
 (0)