From 9c40f7796c9d33179b602f65f4b587d175fba23c Mon Sep 17 00:00:00 2001
From: Mike Gorse <mgorse@alum.wpi.edu>
Date: Thu, 11 Apr 2019 11:14:58 -0500
Subject: [PATCH 1/2] _asn1_expand_object_id: Limit recursion

Resolves #4
---
 lib/parser_aux.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lib/parser_aux.c b/lib/parser_aux.c
index 786ea64..08c2167 100644
--- a/lib/parser_aux.c
+++ b/lib/parser_aux.c
@@ -675,7 +675,7 @@ _asn1_expand_object_id (asn1_node node)
 {
   asn1_node p, p2, p3, p4, p5;
   char name_root[ASN1_MAX_NAME_SIZE], name2[2 * ASN1_MAX_NAME_SIZE + 1];
-  int move, tlen;
+  int move, tlen, tries;
 
   if (node == NULL)
     return ASN1_ELEMENT_NOT_FOUND;
@@ -684,6 +684,7 @@ _asn1_expand_object_id (asn1_node node)
 
   p = node;
   move = DOWN;
+  tries = 0;
 
   while (!((p == node) && (move == UP)))
     {
@@ -738,7 +739,9 @@ _asn1_expand_object_id (asn1_node node)
 			  p4 = p4->right;
 			}
 		      move = DOWN;
-		      continue;
+		      tries++;
+                      if (tries < 3)
+		        continue;
 		    }
 		}
 	    }
@@ -747,6 +750,7 @@ _asn1_expand_object_id (asn1_node node)
       else
 	move = RIGHT;
 
+      tries = 0;
       if (move == DOWN)
 	{
 	  if (p->down)
-- 
From 57d6bc19d44b0693023031c6dbc0d21a36550f34 Mon Sep 17 00:00:00 2001
From: Mike Gorse <mgorse@alum.wpi.edu>
Date: Thu, 25 Apr 2019 16:40:18 -0500
Subject: [PATCH 2/2] Add reproducer for issues/4 to Test_tree.asn

---
 tests/Test_tree.asn        |  9 +++++++++
 tests/Test_tree_asn1_tab.c | 13 +++++++++++--
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/tests/Test_tree.asn b/tests/Test_tree.asn
index 0ad0dc5..a253bc8 100644
--- a/tests/Test_tree.asn
+++ b/tests/Test_tree.asn
@@ -154,6 +154,15 @@ X520LocalityName ::= CHOICE {
       bmpString           BMPString    }
 
 
+id-xnyTest OBJECT IDENTIFIER  ::=  {id-ix 29 1}
+id-ix OBJECR  ::=  {id-ix 29 2}
+BMPString ::= OCTET S
+}
+UniversalString : BY id O}
+id-ix OBJECT IDENTIFIER  ::=  {2 5}
+id-xnyTest OBJECT IDENTIFIER  ::=  {id-ix 29 1}
+anyTest2  ::= INTEGER
+
 id-Test OBJECT IDENTIFIER  ::=  {1 2 29 2}
 
 END
diff --git a/tests/Test_tree_asn1_tab.c b/tests/Test_tree_asn1_tab.c
index 7869f85..0f211c8 100644
--- a/tests/Test_tree_asn1_tab.c
+++ b/tests/Test_tree_asn1_tab.c
@@ -68,7 +68,7 @@ const asn1_static_node Test_tree_asn1_tab[] = {
   { "set", 1610612751, NULL },
   { NULL, 3, NULL },
   { "oid", 1073741836, NULL },
-  { "time2", 1082130449, NULL },
+  { "time2", 1073741861, NULL },
   { "bol", 1073741828, NULL },
   { "oct", 1073741831, NULL },
   { "bit", 1073758214, NULL },
@@ -81,7 +81,7 @@ const asn1_static_node Test_tree_asn1_tab[] = {
   { "any", 1610637325, NULL },
   { NULL, 4104, "1"},
   { "gen", 1073758235, NULL },
-  { "time1", 16777233, NULL },
+  { "time1", 36, NULL },
   { "SequenceTestTag", 1610612741, NULL },
   { "int1", 1610620931, NULL },
   { NULL, 2056, "2"},
@@ -160,6 +160,15 @@ const asn1_static_node Test_tree_asn1_tab[] = {
   { "universalString", 1073741856, NULL },
   { "utf8String", 1073741858, NULL },
   { "bmpString", 33, NULL },
+  { "id-xnyTest", 1879048204, NULL },
+  { NULL, 1073741825, "id-ix"},
+  { NULL, 1073741825, "29"},
+  { NULL, 1, "1"},
+  { "id-ix", 1880096780, "OBJECR"},
+  { NULL, 1073741825, "id-ix"},
+  { NULL, 1073741825, "29"},
+  { NULL, 1, "2"},
+  { "BMPString", 1073741827, NULL },
   { "id-Test", 805306380, NULL },
   { NULL, 1073741825, "1"},
   { NULL, 1073741825, "2"},
-- 
2.20.1
