From 78196e366faab94b9cd57320e54c3b2c9bcf157d Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Fri, 14 May 2021 14:45:38 +0100 Subject: [PATCH] Fix search for outdated entries in SSL session cache Signed-off-by: Hanno Becker --- library/ssl_cache.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/library/ssl_cache.c b/library/ssl_cache.c index 367edf51d6..fe4f30cf8d 100644 --- a/library/ssl_cache.c +++ b/library/ssl_cache.c @@ -137,9 +137,6 @@ static int ssl_cache_pick_writing_slot( mbedtls_ssl_cache_context *cache, 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 @@ static int ssl_cache_pick_writing_slot( mbedtls_ssl_cache_context *cache, * 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 @@ static int ssl_cache_pick_writing_slot( mbedtls_ssl_cache_context *cache, { goto found; } - cur = cur->next; + last = cur; } /* Check 2: Is there an outdated entry in the cache? @@ -167,7 +165,7 @@ static int ssl_cache_pick_writing_slot( mbedtls_ssl_cache_context *cache, */ #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 @@ static int ssl_cache_pick_writing_slot( mbedtls_ssl_cache_context *cache, oldest = cur->timestamp; old = cur; } - - last = cur; - cur = cur->next; } #endif /* MBEDTLS_HAVE_TIME */