From b8c28cbb46fbcf7fe46f456b6c375c7535b08c19 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim=20R=C3=BChsen?= <tim.ruehsen@gmx.de>
Date: Tue, 30 Jul 2019 12:07:12 +0200
Subject: [PATCH] Fix memleaks in asn1_array2tree()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Introduce _asn1_delete_structure() that keeps the node list
in sync when deleting a tree structure.

Signed-off-by: Tim Rühsen <tim.ruehsen@gmx.de>
---
 lib/parser_aux.c |  6 +++---
 lib/structure.c  | 25 +++++++++++++++----------
 lib/structure.h  |  5 +++++
 3 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/lib/parser_aux.c b/lib/parser_aux.c
index 37afa5d..0fead92 100644
--- a/lib/parser_aux.c
+++ b/lib/parser_aux.c
@@ -723,14 +723,15 @@ _asn1_expand_object_id (list_type **list, asn1_node node)
 		    {
 		      _asn1_str_cpy (name2, sizeof (name2), name_root);
 		      _asn1_str_cat (name2, sizeof (name2), ".");
-		      _asn1_str_cat (name2, sizeof (name2),
-				     (char *) p2->value);
+		      _asn1_str_cat (name2, sizeof (name2), (char *) p2->value);
 		      p3 = asn1_find_node (node, name2);
 		      if (!p3
 			  || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID)
 			  || !(p3->type & CONST_ASSIGN))
 			return ASN1_ELEMENT_NOT_FOUND;
 		      _asn1_set_down (p, p2->right);
+		      if (p2->down)
+			_asn1_delete_structure (*list, &p2->down, 0);
 		      _asn1_delete_node_from_list(*list, p2);
 		      _asn1_remove_node (p2, 0);
 		      p2 = p;
@@ -805,7 +806,6 @@ _asn1_expand_object_id (list_type **list, asn1_node node)
 	p = _asn1_find_up (p);
     }
 
-
   /*******************************/
   /*       expand DEFAULT        */
   /*******************************/
diff --git a/lib/structure.c b/lib/structure.c
index 8d71970..7f52380 100644
--- a/lib/structure.c
+++ b/lib/structure.c
@@ -206,16 +206,12 @@ asn1_array2tree (const asn1_static_node * array, asn1_node * definitions,
 	*definitions = p;
 
       if (move == DOWN) {
-        if (p_last && p_last->down) {
-	  _asn1_delete_node_from_list (e_list, p_last->down);
-	  _asn1_remove_node (p_last->down, 0);
-	}
+        if (p_last && p_last->down)
+	  _asn1_delete_structure (e_list, &p_last->down, 0); 
 	_asn1_set_down (p_last, p);
       } else if (move == RIGHT) {
-        if (p_last && p_last->right) {
-	  _asn1_delete_node_from_list (e_list, p_last->right);
-	  _asn1_remove_node (p_last->down, 0);
-	}
+        if (p_last && p_last->right)
+	  _asn1_delete_structure (e_list, &p_last->right, 0);
 	_asn1_set_right (p_last, p);
       }
 
@@ -294,7 +290,7 @@ asn1_array2tree (const asn1_static_node * array, asn1_node * definitions,
 int
 asn1_delete_structure (asn1_node * structure)
 {
-  return asn1_delete_structure2(structure, 0);
+  return _asn1_delete_structure (NULL, structure, 0);
 }
 
 /**
@@ -311,6 +307,12 @@ asn1_delete_structure (asn1_node * structure)
 int
 asn1_delete_structure2 (asn1_node * structure, unsigned int flags)
 {
+  return _asn1_delete_structure (NULL, structure, flags);
+}
+
+int
+_asn1_delete_structure (list_type *e_list, asn1_node * structure, unsigned int flags)
+{
   asn1_node p, p2, p3;
 
   if (*structure == NULL)
@@ -330,6 +332,8 @@ asn1_delete_structure2 (asn1_node * structure, unsigned int flags)
 	    {
 	      p3 = _asn1_find_up (p);
 	      _asn1_set_down (p3, p2);
+	      if (e_list)
+		_asn1_delete_node_from_list (e_list, p);
 	      _asn1_remove_node (p, flags);
 	      p = p3;
 	    }
@@ -349,6 +353,8 @@ asn1_delete_structure2 (asn1_node * structure, unsigned int flags)
 		}
 	      else
 		_asn1_set_right (p3, p2);
+	      if (e_list)
+		_asn1_delete_node_from_list (e_list, p);
 	      _asn1_remove_node (p, flags);
 	      p = NULL;
 	    }
@@ -360,7 +366,6 @@ asn1_delete_structure2 (asn1_node * structure, unsigned int flags)
 }
 
 
-
 /**
  * asn1_delete_element:
  * @structure: pointer to the structure that contains the element you
diff --git a/lib/structure.h b/lib/structure.h
index bb6e7a9..075e2f9 100644
--- a/lib/structure.h
+++ b/lib/structure.h
@@ -28,6 +28,8 @@
 #ifndef _STRUCTURE_H
 #define _STRUCTURE_H
 
+#include "parser_aux.h" // list_type
+
 int _asn1_create_static_structure (asn1_node pointer,
 				   char *output_file_name, char *vector_name);
 
@@ -37,4 +39,7 @@ asn1_node _asn1_add_single_node (unsigned int type);
 
 asn1_node _asn1_find_left (asn1_node node);
 
+int
+_asn1_delete_structure (list_type *e_list, asn1_node *structure, unsigned int flags);
+
 #endif
-- 
1.8.3.1

