From 16f397d55c9f4971a66a7ce9d87d0305ab45eaa7 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 10 Sep 2015 19:17:42 +0200 Subject: [PATCH] prevent segfault in case we hit an empty sequence --- .../der/sequence/der_decode_sequence_flexi.c | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/pk/asn1/der/sequence/der_decode_sequence_flexi.c b/src/pk/asn1/der/sequence/der_decode_sequence_flexi.c index 46458af..caad485 100644 --- a/src/pk/asn1/der/sequence/der_decode_sequence_flexi.c +++ b/src/pk/asn1/der/sequence/der_decode_sequence_flexi.c @@ -361,8 +361,11 @@ int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc /* len update */ totlen += data_offset; - /* link them up y0 */ - l->child->parent = l; + /* the flexi decoder can also do nothing, so make sure a child has been allocated */ + if (l->child) { + /* link them up y0 */ + l->child->parent = l; + } break; @@ -398,12 +401,15 @@ int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc outside: - /* rewind l please */ - while (l->prev != NULL || l->parent != NULL) { - if (l->parent != NULL) { - l = l->parent; - } else { - l = l->prev; + /* in case we processed anything */ + if (totlen) { + /* rewind l please */ + while (l->prev != NULL || l->parent != NULL) { + if (l->parent != NULL) { + l = l->parent; + } else { + l = l->prev; + } } }