Skip to contentSkip to conversation

vxWorks/lstLib: Check for nil-pointer

PATCH Applied Niklaus Giger 2 participants 2 messages Oct 23, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vxWorks/lstLib: Check for nil-pointer

Here some trivial fixes with their respective tests to handle the case of nil pointers corrrectly.

---
 include/vxworks/lstLib.h  |    6 +++---
 vxworks/lstLib.c          |    4 +++-
 vxworks/testsuite/lst-1.c |   17 +++++++----------
 3 files changed, 13 insertions(+), 14 deletions(-)
include/vxworks/lstLib.h +3 -3
index b78c2c3..38f0598 100644
@@ -65,7 +65,7 @@ static inline void lstDelete(LIST *l, NODE *n)
 
 static inline NODE *lstFirst(LIST *l)
 {
-	if (list_empty(&l->list))
+	if (l == NULL || list_empty(&l->list))
 		return NULL;
 
 	return list_first_entry(&l->list, struct NODE, link);
@@ -75,7 +75,7 @@ static inline NODE *lstGet(LIST *l)
 {
 	struct NODE *n;
 
-	if (list_empty(&l->list))
+	if (l == NULL || list_empty(&l->list))
 		return NULL;
 
 	n = list_pop_entry(&l->list, struct NODE, link);
@@ -100,7 +100,7 @@ static inline void lstInsert(LIST *l, NODE *nprev, NODE *n)
 
 static inline NODE *lstLast(LIST *l)
 {
-	if (list_empty(&l->list))
+	if (l == NULL || list_empty(&l->list))
 		return NULL;
 
 	return list_last_entry(&l->list, struct NODE, link);
vxworks/lstLib.c +3 -1
index 58358f0..56880b4 100644
@@ -44,7 +44,7 @@ NODE *lstNth(LIST *l, int nodenum)
 	struct holder *holder;
 	int nth;
 
-	if (nodenum <= 0)
+	if (l == 0 || nodenum <= 0)
 		return NULL;
 
 	if (nodenum <= l->count >> 2) { /* nodenum is 1-based. */
@@ -89,6 +89,8 @@ int lstFind(LIST *l, NODE *n)
 {
 	struct holder *holder;
 	int nth = 1;
+	if (l == 0)
+		return ERROR;
 
 	list_for_each(holder, &l->list) {
 		if (holder == &n->link)
vxworks/testsuite/lst-1.c +7 -10
index 8b42692..d95d8b8 100644
@@ -12,16 +12,15 @@ void rootTask(long a0, long a1, long a2, long a3, long a4,
 {
 	NODE first, second, third, fourth;
 	LIST list;
-	int ret;
 
 	traceobj_enter(&trobj);
 
-/*
-  traceobj_assert(&trobj, 0 == lstNth (0, 1));
-  traceobj_assert(&trobj, 0 == lstFirst(0));
-  traceobj_assert(&trobj, 0 == lstLast(0));
-*/
- 	lstInit(&list);
+	traceobj_assert(&trobj, 0 == lstNth (0, 1));
+	traceobj_assert(&trobj, 0 == lstFirst(0));
+	traceobj_assert(&trobj, 0 == lstLast(0));
+	traceobj_assert(&trobj, 0 == lstGet(0));
+
+	lstInit(&list);
 	traceobj_assert(&trobj, 0 == lstCount(&list));
 	traceobj_assert(&trobj, NULL == lstFirst(&list));
 	traceobj_assert(&trobj, 0 == lstNth(&list, 0));
@@ -41,7 +40,7 @@ void rootTask(long a0, long a1, long a2, long a3, long a4,
 	traceobj_assert(&trobj, 2 == lstCount(&list));
 	traceobj_assert(&trobj, &first == lstFirst(&list));
 	traceobj_assert(&trobj, &second == lstLast(&list));
-	traceobj_assert(&trobj, NULL == lstPrevious(&first)); // breaks here under xenomai-solo
+	traceobj_assert(&trobj, NULL == lstPrevious(&first));
 	traceobj_assert(&trobj, 0 == lstNth(&list, 0));
 	traceobj_assert(&trobj, &first  == lstNth(&list, 1));
 	traceobj_assert(&trobj, &second == lstNth(&list, 2));
@@ -127,8 +126,6 @@ void rootTask(long a0, long a1, long a2, long a3, long a4,
 	traceobj_assert(&trobj, 0 == lstNth(&list, 4));
 	traceobj_assert(&trobj, 0 == lstNth(&list, 5));
 
-	traceobj_assert(&trobj, ret == OK);
-
 	traceobj_exit(&trobj);
 }
 

--
1.6.0.2

#1 lore raw
Quoted 2 lines · Niklaus Giger

Here some trivial fixes with their respective tests to handle the case of nil pointers corrrectly.

Merged, thanks.

Quoted 115 lines
---
 include/vxworks/lstLib.h  |    6 +++---
 vxworks/lstLib.c          |    4 +++-
 vxworks/testsuite/lst-1.c |   17 +++++++----------
 3 files changed, 13 insertions(+), 14 deletions(-)
include/vxworks/lstLib.h +3 -3
index b78c2c3..38f0598 100644
@@ -65,7 +65,7 @@ static inline void lstDelete(LIST *l, NODE *n)
 
 static inline NODE *lstFirst(LIST *l)
 {
-	if (list_empty(&l->list))
+	if (l == NULL || list_empty(&l->list))
 		return NULL;
 
 	return list_first_entry(&l->list, struct NODE, link);
@@ -75,7 +75,7 @@ static inline NODE *lstGet(LIST *l)
 {
 	struct NODE *n;
 
-	if (list_empty(&l->list))
+	if (l == NULL || list_empty(&l->list))
 		return NULL;
 
 	n = list_pop_entry(&l->list, struct NODE, link);
@@ -100,7 +100,7 @@ static inline void lstInsert(LIST *l, NODE *nprev, NODE *n)
 
 static inline NODE *lstLast(LIST *l)
 {
-	if (list_empty(&l->list))
+	if (l == NULL || list_empty(&l->list))
 		return NULL;
 
 	return list_last_entry(&l->list, struct NODE, link);
vxworks/lstLib.c +3 -1
index 58358f0..56880b4 100644
@@ -44,7 +44,7 @@ NODE *lstNth(LIST *l, int nodenum)
 	struct holder *holder;
 	int nth;
 
-	if (nodenum <= 0)
+	if (l == 0 || nodenum <= 0)
 		return NULL;
 
 	if (nodenum <= l->count >> 2) { /* nodenum is 1-based. */
@@ -89,6 +89,8 @@ int lstFind(LIST *l, NODE *n)
 {
 	struct holder *holder;
 	int nth = 1;
+	if (l == 0)
+		return ERROR;
 
 	list_for_each(holder, &l->list) {
 		if (holder == &n->link)
vxworks/testsuite/lst-1.c +7 -11
index 8b42692..d95d8b8 100644
@@ -12,16 +12,15 @@ void rootTask(long a0, long a1, long a2, long a3, long a4,
 {
 	NODE first, second, third, fourth;
 	LIST list;
-	int ret;
 
 	traceobj_enter(&trobj);
 
-/*
-  traceobj_assert(&trobj, 0 == lstNth (0, 1));
-  traceobj_assert(&trobj, 0 == lstFirst(0));
-  traceobj_assert(&trobj, 0 == lstLast(0));
-*/
- 	lstInit(&list);
+	traceobj_assert(&trobj, 0 == lstNth (0, 1));
+	traceobj_assert(&trobj, 0 == lstFirst(0));
+	traceobj_assert(&trobj, 0 == lstLast(0));
+	traceobj_assert(&trobj, 0 == lstGet(0));
+
+	lstInit(&list);
 	traceobj_assert(&trobj, 0 == lstCount(&list));
 	traceobj_assert(&trobj, NULL == lstFirst(&list));
 	traceobj_assert(&trobj, 0 == lstNth(&list, 0));
@@ -41,7 +40,7 @@ void rootTask(long a0, long a1, long a2, long a3, long a4,
 	traceobj_assert(&trobj, 2 == lstCount(&list));
 	traceobj_assert(&trobj, &first == lstFirst(&list));
 	traceobj_assert(&trobj, &second == lstLast(&list));
-	traceobj_assert(&trobj, NULL == lstPrevious(&first)); // breaks here under xenomai-solo
+	traceobj_assert(&trobj, NULL == lstPrevious(&first));
 	traceobj_assert(&trobj, 0 == lstNth(&list, 0));
 	traceobj_assert(&trobj, &first  == lstNth(&list, 1));
 	traceobj_assert(&trobj, &second == lstNth(&list, 2));
@@ -127,8 +126,6 @@ void rootTask(long a0, long a1, long a2, long a3, long a4,
 	traceobj_assert(&trobj, 0 == lstNth(&list, 4));
 	traceobj_assert(&trobj, 0 == lstNth(&list, 5));
 
-	traceobj_assert(&trobj, ret == OK);
-
 	traceobj_exit(&trobj);
 }
 
--

1.6.0.2

_______________________________________________
Xenomai-core mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-core

Signature · 1 line
Philippe.