- Fixed logic error on end of 'full' chain

diff --git a/library/x509parse.c b/library/x509parse.c
index 2596559..919be65 100644
--- a/library/x509parse.c
+++ b/library/x509parse.c
@@ -663,13 +663,13 @@
     if( crt == NULL || buf == NULL )
         return( 1 );
 
-    while( crt->version != 0 || crt->next != NULL )
+    while( crt->version != 0 && crt->next != NULL )
         crt = crt->next;
 
     /*
      * Add new certificate on the end of the chain if needed.
      */
-    if ( crt->next == NULL)
+    if ( crt->version != 0 && crt->next == NULL)
     {
         crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );