From c104beb5db8eff5ec30d2601f4f3ee11d29d64cf Mon Sep 17 00:00:00 2001
From: miod <>
Date: Sun, 18 May 2014 09:39:18 +0000
Subject: No need to check for NULL before invoking free(); use calloc() when
 applicable; further simplify pqueue_find().

From Dimitris Papastamos on tech@
---
 src/lib/libssl/pqueue.c         | 37 +++++++------------------------------
 src/lib/libssl/src/ssl/pqueue.c | 37 +++++++------------------------------
 2 files changed, 14 insertions(+), 60 deletions(-)

(limited to 'src')

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)
 void
 pitem_free(pitem *item)
 {
-	if (item == NULL)
-		return;
-
 	free(item);
 }
 
 pqueue_s *
 pqueue_new(void)
 {
-	pqueue_s *pq = (pqueue_s *)malloc(sizeof(pqueue_s));
-
-	if (pq == NULL)
-		return NULL;
-
-	memset(pq, 0x00, sizeof(pqueue_s));
-	return pq;
+	return (pqueue_s *)calloc(1, sizeof(pqueue_s));
 }
 
 void
 pqueue_free(pqueue_s *pq)
 {
-	if (pq == NULL)
-		return;
-
 	free(pq);
 }
 
@@ -126,9 +114,8 @@ pqueue_insert(pqueue_s *pq, pitem *item)
 		/* we can compare 64-bit value in big-endian encoding
 		 * with memcmp:-) */
 		int cmp = memcmp(next->priority, item->priority,
-				 sizeof(item->priority));
-		if (cmp > 0)		/* next > item */
-		{
+		    sizeof(item->priority));
+		if (cmp > 0) {		/* next > item */
 			item->next = next;
 
 			if (curr == NULL)
@@ -168,23 +155,13 @@ pitem *
 pqueue_find(pqueue_s *pq, unsigned char *prio64be)
 {
 	pitem *next;
-	pitem *found = NULL;
 
-	if (pq->items == NULL)
-		return NULL;
-
-	for (next = pq->items; next != NULL; next = next->next) {
+	for (next = pq->items; next != NULL; next = next->next)
 		if (memcmp(next->priority, prio64be,
-		    sizeof(next->priority)) == 0) {
-			found = next;
-			break;
-		}
-	}
-
-	if (!found)
-		return NULL;
+		    sizeof(next->priority)) == 0)
+			return next;
 
-	return found;
+	return NULL;
 }
 
 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)
 void
 pitem_free(pitem *item)
 {
-	if (item == NULL)
-		return;
-
 	free(item);
 }
 
 pqueue_s *
 pqueue_new(void)
 {
-	pqueue_s *pq = (pqueue_s *)malloc(sizeof(pqueue_s));
-
-	if (pq == NULL)
-		return NULL;
-
-	memset(pq, 0x00, sizeof(pqueue_s));
-	return pq;
+	return (pqueue_s *)calloc(1, sizeof(pqueue_s));
 }
 
 void
 pqueue_free(pqueue_s *pq)
 {
-	if (pq == NULL)
-		return;
-
 	free(pq);
 }
 
@@ -126,9 +114,8 @@ pqueue_insert(pqueue_s *pq, pitem *item)
 		/* we can compare 64-bit value in big-endian encoding
 		 * with memcmp:-) */
 		int cmp = memcmp(next->priority, item->priority,
-				 sizeof(item->priority));
-		if (cmp > 0)		/* next > item */
-		{
+		    sizeof(item->priority));
+		if (cmp > 0) {		/* next > item */
 			item->next = next;
 
 			if (curr == NULL)
@@ -168,23 +155,13 @@ pitem *
 pqueue_find(pqueue_s *pq, unsigned char *prio64be)
 {
 	pitem *next;
-	pitem *found = NULL;
 
-	if (pq->items == NULL)
-		return NULL;
-
-	for (next = pq->items; next != NULL; next = next->next) {
+	for (next = pq->items; next != NULL; next = next->next)
 		if (memcmp(next->priority, prio64be,
-		    sizeof(next->priority)) == 0) {
-			found = next;
-			break;
-		}
-	}
-
-	if (!found)
-		return NULL;
+		    sizeof(next->priority)) == 0)
+			return next;
 
-	return found;
+	return NULL;
 }
 
 pitem *
-- 
cgit v1.2.3-55-g6feb