Fix search for outdated entries in SSL session cache

Signed-off-by: Hanno Becker <hanno.becker@arm.com>
diff --git a/library/ssl_cache.c b/library/ssl_cache.c
index 367edf5..fe4f30c 100644
--- a/library/ssl_cache.c
+++ b/library/ssl_cache.c
@@ -137,9 +137,6 @@
     int count = 0;
     mbedtls_ssl_cache_entry *cur, *last;
 
-    cur = cache->chain;
-    last = NULL;
-
     /* Check 1: Is there already an entry with the given session ID?
      *
      * If yes, overwrite it.
@@ -148,7 +145,8 @@
      * at the end of this loop, and `last` will point to the last
      * entry, both of which will be used later. */
 
-    while( cur != NULL )
+    last = NULL;
+    for( cur = cache->chain; cur != NULL; cur = cur->next )
     {
         count++;
         if( session_id_len == cur->session_id_len &&
@@ -156,7 +154,7 @@
         {
             goto found;
         }
-        cur = cur->next;
+        last = cur;
     }
 
     /* Check 2: Is there an outdated entry in the cache?
@@ -167,7 +165,7 @@
      */
 
 #if defined(MBEDTLS_HAVE_TIME)
-    while( cur != NULL )
+    for( cur = cache->chain; cur != NULL; cur = cur->next )
     {
         if( cache->timeout != 0 &&
             (int) ( t - cur->timestamp ) > cache->timeout )
@@ -180,9 +178,6 @@
             oldest = cur->timestamp;
             old = cur;
         }
-
-        last = cur;
-        cur = cur->next;
     }
 #endif /* MBEDTLS_HAVE_TIME */