Skip to content
This repository
Browse code

pkcs15: check pointer before dereferencing

try to resolve USE-AFTER-FREE coverity scan defects 402691-402696

Change-Id: Icf654d439e0022928143f87b6c9dcf702571f9e0
  • Loading branch information...
commit 73730d098be15be42a794f02f35ba216fbec80f2 1 parent f216765
viktorTarasov authored

Showing 1 changed file with 11 additions and 4 deletions. Show diff stats Hide diff stats

  1. 15  src/libopensc/pkcs15.c
15  src/libopensc/pkcs15.c
@@ -1713,6 +1713,8 @@ int sc_pkcs15_add_object(struct sc_pkcs15_card *p15card,
1713 1713
 {
1714 1714
 	struct sc_pkcs15_object *p = p15card->obj_list;
1715 1715
 
  1716
+	if (!obj)
  1717
+		return 0;
1716 1718
 	obj->next = obj->prev = NULL;
1717 1719
 	if (p15card->obj_list == NULL) {
1718 1720
 		p15card->obj_list = obj;
@@ -1731,8 +1733,7 @@ void sc_pkcs15_remove_object(struct sc_pkcs15_card *p15card,
1731 1733
 {
1732 1734
 	if (!obj)
1733 1735
 		return;
1734  
-
1735  
-	if (obj->prev == NULL)
  1736
+	else if (obj->prev == NULL)
1736 1737
 		p15card->obj_list = obj->next;
1737 1738
 	else
1738 1739
 		obj->prev->next = obj->next;
@@ -1742,6 +1743,8 @@ void sc_pkcs15_remove_object(struct sc_pkcs15_card *p15card,
1742 1743
 
1743 1744
 void sc_pkcs15_free_object(struct sc_pkcs15_object *obj)
1744 1745
 {
  1746
+	if (!obj)
  1747
+		return;
1745 1748
 	switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
1746 1749
 	case SC_PKCS15_TYPE_PRKEY:
1747 1750
 		sc_pkcs15_free_prkey_info((sc_pkcs15_prkey_info_t *)obj->data);
@@ -1797,6 +1800,8 @@ int sc_pkcs15_add_df(struct sc_pkcs15_card *p15card, unsigned int type, const sc
1797 1800
 void sc_pkcs15_remove_df(struct sc_pkcs15_card *p15card,
1798 1801
 			 struct sc_pkcs15_df *obj)
1799 1802
 {
  1803
+	if (!obj)
  1804
+		return;
1800 1805
 	if (obj->prev == NULL)
1801 1806
 		p15card->df_list = obj->next;
1802 1807
 	else
@@ -1993,11 +1998,13 @@ int sc_pkcs15_add_unusedspace(struct sc_pkcs15_card *p15card,
1993 1998
 void sc_pkcs15_remove_unusedspace(struct sc_pkcs15_card *p15card,
1994 1999
 			 sc_pkcs15_unusedspace_t *unusedspace)
1995 2000
 {
1996  
-	if (unusedspace->prev == NULL)
  2001
+	if (!unusedspace)
  2002
+		return;
  2003
+	if (!unusedspace->prev)
1997 2004
 		p15card->unusedspace_list = unusedspace->next;
1998 2005
 	else
1999 2006
 		unusedspace->prev->next = unusedspace->next;
2000  
-	if (unusedspace->next != NULL)
  2007
+	if (!unusedspace->next)
2001 2008
 		unusedspace->next->prev = unusedspace->prev;
2002 2009
 	free(unusedspace);
2003 2010
 }

0 notes on commit 73730d0

Please sign in to comment.
Something went wrong with that request. Please try again.