diff --git a/src/lib-sieve/sieve-result.c b/src/lib-sieve/sieve-result.c
index 90a9b75f01220d40fe5752a2b83d19dd82d81dc7..55eb9f54b091dd4861a6a9ef535b3aae5d114058 100644
--- a/src/lib-sieve/sieve-result.c
+++ b/src/lib-sieve/sieve-result.c
@@ -1539,10 +1539,13 @@ sieve_result_implicit_keep_execute(struct sieve_result_execution *rexec,
 
 	/* Scan for deferred keep */
 	aexec = rexec->actions_tail;
-	while (aexec != NULL &&
-	       aexec->state >= SIEVE_ACTION_EXECUTION_STATE_EXECUTED) {
+	while (aexec != NULL) {
 		struct sieve_result_action *rac = aexec->action;
 
+		if (aexec->state < SIEVE_ACTION_EXECUTION_STATE_EXECUTED) {
+			aexec = NULL;
+			break;
+		}
 		if (rac->action.keep && rac->action.def == NULL)
 			break;
 		aexec = aexec->prev;