- Clean Subject Alternative Name data

diff --git a/library/x509parse.c b/library/x509parse.c
index 2852643..4913bec 100644
--- a/library/x509parse.c
+++ b/library/x509parse.c
@@ -497,6 +497,10 @@
     int ret;
     size_t len;
 
+    if( ( end - *p ) < 1 )
+        return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE +
+                POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
     sig->tag = **p;
 
     if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
@@ -3215,6 +3219,15 @@
             free( seq_prv );
         }
 
+        seq_cur = cert_cur->subject_alt_names.next;
+        while( seq_cur != NULL )
+        {
+            seq_prv = seq_cur;
+            seq_cur = seq_cur->next;
+            memset( seq_prv, 0, sizeof( x509_sequence ) );
+            free( seq_prv );
+        }
+
         if( cert_cur->raw.p != NULL )
         {
             memset( cert_cur->raw.p, 0, cert_cur->raw.len );