diff options
author | miod <> | 2014-05-18 09:39:18 +0000 |
---|---|---|
committer | miod <> | 2014-05-18 09:39:18 +0000 |
commit | c104beb5db8eff5ec30d2601f4f3ee11d29d64cf (patch) | |
tree | f17e68b85b3db77969d96e539568ba336443ca62 | |
parent | eb5cf6ce393c6622756900826d69f68977774ee7 (diff) | |
download | openbsd-c104beb5db8eff5ec30d2601f4f3ee11d29d64cf.tar.gz openbsd-c104beb5db8eff5ec30d2601f4f3ee11d29d64cf.tar.bz2 openbsd-c104beb5db8eff5ec30d2601f4f3ee11d29d64cf.zip |
No need to check for NULL before invoking free(); use calloc() when
applicable; further simplify pqueue_find().
From Dimitris Papastamos on tech@
-rw-r--r-- | src/lib/libssl/pqueue.c | 37 | ||||
-rw-r--r-- | src/lib/libssl/src/ssl/pqueue.c | 37 |
2 files changed, 14 insertions, 60 deletions
diff --git a/src/lib/libssl/pqueue.c b/src/lib/libssl/pqueue.c index 99c118c3b6..daf5e21b3a 100644 --- a/src/lib/libssl/pqueue.c +++ b/src/lib/libssl/pqueue.c | |||
@@ -84,30 +84,18 @@ pitem_new(unsigned char *prio64be, void *data) | |||
84 | void | 84 | void |
85 | pitem_free(pitem *item) | 85 | pitem_free(pitem *item) |
86 | { | 86 | { |
87 | if (item == NULL) | ||
88 | return; | ||
89 | |||
90 | free(item); | 87 | free(item); |
91 | } | 88 | } |
92 | 89 | ||
93 | pqueue_s * | 90 | pqueue_s * |
94 | pqueue_new(void) | 91 | pqueue_new(void) |
95 | { | 92 | { |
96 | pqueue_s *pq = (pqueue_s *)malloc(sizeof(pqueue_s)); | 93 | return (pqueue_s *)calloc(1, sizeof(pqueue_s)); |
97 | |||
98 | if (pq == NULL) | ||
99 | return NULL; | ||
100 | |||
101 | memset(pq, 0x00, sizeof(pqueue_s)); | ||
102 | return pq; | ||
103 | } | 94 | } |
104 | 95 | ||
105 | void | 96 | void |
106 | pqueue_free(pqueue_s *pq) | 97 | pqueue_free(pqueue_s *pq) |
107 | { | 98 | { |
108 | if (pq == NULL) | ||
109 | return; | ||
110 | |||
111 | free(pq); | 99 | free(pq); |
112 | } | 100 | } |
113 | 101 | ||
@@ -126,9 +114,8 @@ pqueue_insert(pqueue_s *pq, pitem *item) | |||
126 | /* we can compare 64-bit value in big-endian encoding | 114 | /* we can compare 64-bit value in big-endian encoding |
127 | * with memcmp:-) */ | 115 | * with memcmp:-) */ |
128 | int cmp = memcmp(next->priority, item->priority, | 116 | int cmp = memcmp(next->priority, item->priority, |
129 | sizeof(item->priority)); | 117 | sizeof(item->priority)); |
130 | if (cmp > 0) /* next > item */ | 118 | if (cmp > 0) { /* next > item */ |
131 | { | ||
132 | item->next = next; | 119 | item->next = next; |
133 | 120 | ||
134 | if (curr == NULL) | 121 | if (curr == NULL) |
@@ -168,23 +155,13 @@ pitem * | |||
168 | pqueue_find(pqueue_s *pq, unsigned char *prio64be) | 155 | pqueue_find(pqueue_s *pq, unsigned char *prio64be) |
169 | { | 156 | { |
170 | pitem *next; | 157 | pitem *next; |
171 | pitem *found = NULL; | ||
172 | 158 | ||
173 | if (pq->items == NULL) | 159 | for (next = pq->items; next != NULL; next = next->next) |
174 | return NULL; | ||
175 | |||
176 | for (next = pq->items; next != NULL; next = next->next) { | ||
177 | if (memcmp(next->priority, prio64be, | 160 | if (memcmp(next->priority, prio64be, |
178 | sizeof(next->priority)) == 0) { | 161 | sizeof(next->priority)) == 0) |
179 | found = next; | 162 | return next; |
180 | break; | ||
181 | } | ||
182 | } | ||
183 | |||
184 | if (!found) | ||
185 | return NULL; | ||
186 | 163 | ||
187 | return found; | 164 | return NULL; |
188 | } | 165 | } |
189 | 166 | ||
190 | pitem * | 167 | pitem * |
diff --git a/src/lib/libssl/src/ssl/pqueue.c b/src/lib/libssl/src/ssl/pqueue.c index 99c118c3b6..daf5e21b3a 100644 --- a/src/lib/libssl/src/ssl/pqueue.c +++ b/src/lib/libssl/src/ssl/pqueue.c | |||
@@ -84,30 +84,18 @@ pitem_new(unsigned char *prio64be, void *data) | |||
84 | void | 84 | void |
85 | pitem_free(pitem *item) | 85 | pitem_free(pitem *item) |
86 | { | 86 | { |
87 | if (item == NULL) | ||
88 | return; | ||
89 | |||
90 | free(item); | 87 | free(item); |
91 | } | 88 | } |
92 | 89 | ||
93 | pqueue_s * | 90 | pqueue_s * |
94 | pqueue_new(void) | 91 | pqueue_new(void) |
95 | { | 92 | { |
96 | pqueue_s *pq = (pqueue_s *)malloc(sizeof(pqueue_s)); | 93 | return (pqueue_s *)calloc(1, sizeof(pqueue_s)); |
97 | |||
98 | if (pq == NULL) | ||
99 | return NULL; | ||
100 | |||
101 | memset(pq, 0x00, sizeof(pqueue_s)); | ||
102 | return pq; | ||
103 | } | 94 | } |
104 | 95 | ||
105 | void | 96 | void |
106 | pqueue_free(pqueue_s *pq) | 97 | pqueue_free(pqueue_s *pq) |
107 | { | 98 | { |
108 | if (pq == NULL) | ||
109 | return; | ||
110 | |||
111 | free(pq); | 99 | free(pq); |
112 | } | 100 | } |
113 | 101 | ||
@@ -126,9 +114,8 @@ pqueue_insert(pqueue_s *pq, pitem *item) | |||
126 | /* we can compare 64-bit value in big-endian encoding | 114 | /* we can compare 64-bit value in big-endian encoding |
127 | * with memcmp:-) */ | 115 | * with memcmp:-) */ |
128 | int cmp = memcmp(next->priority, item->priority, | 116 | int cmp = memcmp(next->priority, item->priority, |
129 | sizeof(item->priority)); | 117 | sizeof(item->priority)); |
130 | if (cmp > 0) /* next > item */ | 118 | if (cmp > 0) { /* next > item */ |
131 | { | ||
132 | item->next = next; | 119 | item->next = next; |
133 | 120 | ||
134 | if (curr == NULL) | 121 | if (curr == NULL) |
@@ -168,23 +155,13 @@ pitem * | |||
168 | pqueue_find(pqueue_s *pq, unsigned char *prio64be) | 155 | pqueue_find(pqueue_s *pq, unsigned char *prio64be) |
169 | { | 156 | { |
170 | pitem *next; | 157 | pitem *next; |
171 | pitem *found = NULL; | ||
172 | 158 | ||
173 | if (pq->items == NULL) | 159 | for (next = pq->items; next != NULL; next = next->next) |
174 | return NULL; | ||
175 | |||
176 | for (next = pq->items; next != NULL; next = next->next) { | ||
177 | if (memcmp(next->priority, prio64be, | 160 | if (memcmp(next->priority, prio64be, |
178 | sizeof(next->priority)) == 0) { | 161 | sizeof(next->priority)) == 0) |
179 | found = next; | 162 | return next; |
180 | break; | ||
181 | } | ||
182 | } | ||
183 | |||
184 | if (!found) | ||
185 | return NULL; | ||
186 | 163 | ||
187 | return found; | 164 | return NULL; |
188 | } | 165 | } |
189 | 166 | ||
190 | pitem * | 167 | pitem * |