From 1fb536e718026ca95274fc21bd77077f68500a7f Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan.bosch@open-xchange.com>
Date: Sun, 1 Sep 2024 23:49:57 +0200
Subject: [PATCH] lib-sieve: sieve-storage - Use int for
 sieve_storage_active_script_open() status result

---
 src/lib-sieve/sieve-storage-private.h         |  4 +--
 src/lib-sieve/sieve-storage.c                 | 31 +++++++++++++------
 src/lib-sieve/sieve-storage.h                 |  6 ++--
 .../storage/dict/sieve-dict-storage.c         | 10 +++---
 .../storage/dict/sieve-dict-storage.h         |  2 --
 .../storage/file/sieve-file-storage-active.c  | 21 +++++++------
 .../storage/file/sieve-file-storage.h         |  4 +--
 .../storage/ldap/sieve-ldap-storage.c         | 10 +++---
 .../storage/ldap/sieve-ldap-storage.h         |  2 --
 .../doveadm-sieve/doveadm-sieve-sync.c        |  3 +-
 .../imap-filter-sieve/imap-filter-sieve.c     | 11 +++----
 src/plugins/lda-sieve/lda-sieve-plugin.c      |  7 ++---
 12 files changed, 61 insertions(+), 50 deletions(-)

diff --git a/src/lib-sieve/sieve-storage-private.h b/src/lib-sieve/sieve-storage-private.h
index ebba4a121..589bfd735 100644
--- a/src/lib-sieve/sieve-storage-private.h
+++ b/src/lib-sieve/sieve-storage-private.h
@@ -47,8 +47,8 @@ struct sieve_storage_vfuncs {
 	/* active script */
 	int (*active_script_get_name)(struct sieve_storage *storage,
 				      const char **name_r);
-	struct sieve_script *(*active_script_open)(
-		struct sieve_storage *storage);
+	int (*active_script_open)(struct sieve_storage *storage,
+				  struct sieve_script **script_r);
 	int (*deactivate)(struct sieve_storage *storage);
 	int (*active_script_get_last_change)(struct sieve_storage *storage,
 					     time_t *last_change_r);
diff --git a/src/lib-sieve/sieve-storage.c b/src/lib-sieve/sieve-storage.c
index 559ac6ba2..29e51bc7b 100644
--- a/src/lib-sieve/sieve-storage.c
+++ b/src/lib-sieve/sieve-storage.c
@@ -913,34 +913,47 @@ int sieve_storage_active_script_is_default(struct sieve_storage *storage)
 	return (ret < 0 ? -1 : (is_default ? 1 : 0));
 }
 
-struct sieve_script *
-sieve_storage_active_script_open(struct sieve_storage *storage,
-				 enum sieve_error *error_code_r)
+int sieve_storage_active_script_open(struct sieve_storage *storage,
+				     struct sieve_script **script_r,
+				     enum sieve_error *error_code_r)
 {
 	struct sieve_instance *svinst = storage->svinst;
-	struct sieve_script *script;
+	struct sieve_script *script = NULL;
+	enum sieve_error error_code;
+	int ret;
+
+	*script_r = NULL;
+	if (error_code_r != NULL)
+		*error_code_r = SIEVE_ERROR_NONE;
+	else
+		error_code_r = &error_code;
+	sieve_storage_clear_error(storage);
 
 	i_assert(storage->v.active_script_open != NULL);
-	script = storage->v.active_script_open(storage);
+	ret = storage->v.active_script_open(storage, &script);
+	i_assert(ret <= 0);
 
-	if (script != NULL ||
+	if (ret == 0 ||
 	    (storage->flags & SIEVE_STORAGE_FLAG_SYNCHRONIZING) != 0 ||
 	    storage->default_location == NULL) {
+		i_assert(script != NULL);
 		if (error_code_r != NULL)
 			*error_code_r = storage->error_code;
-		return script;
+		*script_r = script;
+		return ret;
 	}
 
 	/* Try default script location */
 	if (sieve_script_create_open(svinst, storage->default_location, NULL,
 				     &script, error_code_r) < 0)
-		return NULL;
+		return -1;
 
 	script->storage->is_default = TRUE;
 	script->storage->default_for = storage;
 	sieve_storage_ref(storage);
 
-	return script;
+	*script_r = script;
+	return 0;
 }
 
 int sieve_storage_deactivate(struct sieve_storage *storage, time_t mtime)
diff --git a/src/lib-sieve/sieve-storage.h b/src/lib-sieve/sieve-storage.h
index ed28e5cbc..29ab19a1c 100644
--- a/src/lib-sieve/sieve-storage.h
+++ b/src/lib-sieve/sieve-storage.h
@@ -66,9 +66,9 @@ sieve_storage_get_script_sequence(struct sieve_storage *storage,
 int sieve_storage_active_script_get_name(struct sieve_storage *storage,
 					 const char **name_r);
 
-struct sieve_script *
-sieve_storage_active_script_open(struct sieve_storage *storage,
-				 enum sieve_error *error_code_r);
+int sieve_storage_active_script_open(struct sieve_storage *storage,
+				     struct sieve_script **script_r,
+				     enum sieve_error *error_code_r);
 
 int sieve_storage_active_script_get_last_change(struct sieve_storage *storage,
 						time_t *last_change_r);
diff --git a/src/lib-sieve/storage/dict/sieve-dict-storage.c b/src/lib-sieve/storage/dict/sieve-dict-storage.c
index adceb6543..4c7468764 100644
--- a/src/lib-sieve/storage/dict/sieve-dict-storage.c
+++ b/src/lib-sieve/storage/dict/sieve-dict-storage.c
@@ -147,8 +147,9 @@ sieve_dict_storage_get_script(struct sieve_storage *storage, const char *name,
  * Active script
  */
 
-struct sieve_script *
-sieve_dict_storage_active_script_open(struct sieve_storage *storage)
+static int
+sieve_dict_storage_active_script_open(struct sieve_storage *storage,
+				      struct sieve_script **script_r)
 {
 	struct sieve_dict_storage *dstorage =
 		container_of(storage, struct sieve_dict_storage, storage);
@@ -158,10 +159,11 @@ sieve_dict_storage_active_script_open(struct sieve_storage *storage)
 	if (sieve_script_open(&dscript->script, NULL) < 0) {
 		struct sieve_script *script = &dscript->script;
 		sieve_script_unref(&script);
-		return NULL;
+		return -1;
 	}
 
-	return &dscript->script;
+	*script_r = &dscript->script;
+	return 0;
 }
 
 int sieve_dict_storage_active_script_get_name(struct sieve_storage *storage,
diff --git a/src/lib-sieve/storage/dict/sieve-dict-storage.h b/src/lib-sieve/storage/dict/sieve-dict-storage.h
index 9de04aef5..ef7bfd914 100644
--- a/src/lib-sieve/storage/dict/sieve-dict-storage.h
+++ b/src/lib-sieve/storage/dict/sieve-dict-storage.h
@@ -28,8 +28,6 @@ int sieve_dict_storage_get_dict(struct sieve_dict_storage *dstorage,
 				struct dict **dict_r,
 				enum sieve_error *error_code_r);
 
-struct sieve_script *
-sieve_dict_storage_active_script_open(struct sieve_storage *storage);
 int sieve_dict_storage_active_script_get_name(struct sieve_storage *storage,
 					      const char **name_r);
 
diff --git a/src/lib-sieve/storage/file/sieve-file-storage-active.c b/src/lib-sieve/storage/file/sieve-file-storage-active.c
index d7b2e9a66..df2d52761 100644
--- a/src/lib-sieve/storage/file/sieve-file-storage-active.c
+++ b/src/lib-sieve/storage/file/sieve-file-storage-active.c
@@ -233,8 +233,8 @@ int sieve_file_storage_active_script_get_name(struct sieve_storage *storage,
  * Active script
  */
 
-struct sieve_script *
-sieve_file_storage_active_script_open(struct sieve_storage *storage)
+int sieve_file_storage_active_script_open(struct sieve_storage *storage,
+					  struct sieve_script **script_r)
 {
 	struct sieve_file_storage *fstorage =
 		container_of(storage, struct sieve_file_storage, storage);
@@ -242,13 +242,11 @@ sieve_file_storage_active_script_open(struct sieve_storage *storage)
 	const char *scriptfile, *link;
 	int ret;
 
-	sieve_storage_clear_error(storage);
-
 	/* Read the active link */
 	ret = sieve_file_storage_active_read_link(fstorage, &link);
 	if (ret <= 0) {
 		if (ret < 0)
-			return NULL;
+			return -1;
 
 		/* Try to open the active_path as a regular file */
 		if (S_ISDIR(fstorage->st.st_mode)) {
@@ -265,10 +263,11 @@ sieve_file_storage_active_script_open(struct sieve_storage *storage)
 					"Failed to open active path '%s' as regular file: %s",
 					fstorage->active_path, storage->error);
 			}
-			return NULL;
+			return -1;
 		}
 
-		return &fscript->script;
+		*script_r = &fscript->script;
+		return 0;
 	}
 
 	/* Parse the link */
@@ -280,7 +279,7 @@ sieve_file_storage_active_script_open(struct sieve_storage *storage)
 		 */
 		sieve_storage_set_error(storage, SIEVE_ERROR_NOT_FOUND,
 					"Active script is invalid");
-		return NULL;
+		return -1;
 	}
 
 	fscript = sieve_file_script_open_from_path(
@@ -291,7 +290,11 @@ sieve_file_storage_active_script_open(struct sieve_storage *storage)
 			  "Active sieve script symlink %s points to non-existent script "
 			  "(points to %s).", fstorage->active_path, link);
 	}
-	return (fscript != NULL ? &fscript->script : NULL);
+	if (fscript != NULL) {
+		*script_r = &fscript->script;
+		return -1;
+	}
+	return 0;
 }
 
 int sieve_file_storage_active_script_get_last_change(
diff --git a/src/lib-sieve/storage/file/sieve-file-storage.h b/src/lib-sieve/storage/file/sieve-file-storage.h
index 47785daec..4c01dd726 100644
--- a/src/lib-sieve/storage/file/sieve-file-storage.h
+++ b/src/lib-sieve/storage/file/sieve-file-storage.h
@@ -63,8 +63,8 @@ bool sieve_file_storage_active_rescue_regular(
 
 int sieve_file_storage_active_script_get_name(struct sieve_storage *storage,
 					      const char **name_r);
-struct sieve_script *
-sieve_file_storage_active_script_open(struct sieve_storage *storage);
+int sieve_file_storage_active_script_open(struct sieve_storage *storage,
+					  struct sieve_script **script_r);
 
 int sieve_file_storage_active_script_get_file(
 	struct sieve_file_storage *fstorage, const char **file_r);
diff --git a/src/lib-sieve/storage/ldap/sieve-ldap-storage.c b/src/lib-sieve/storage/ldap/sieve-ldap-storage.c
index ea6a75b43..b413c7c7c 100644
--- a/src/lib-sieve/storage/ldap/sieve-ldap-storage.c
+++ b/src/lib-sieve/storage/ldap/sieve-ldap-storage.c
@@ -128,8 +128,9 @@ sieve_ldap_storage_get_script(struct sieve_storage *storage, const char *name,
  * Active script
  */
 
-struct sieve_script *
-sieve_ldap_storage_active_script_open(struct sieve_storage *storage)
+static int
+sieve_ldap_storage_active_script_open(struct sieve_storage *storage,
+				      struct sieve_script **script_r)
 {
 	struct sieve_ldap_storage *lstorage =
 		container_of(storage, struct sieve_ldap_storage, storage);
@@ -139,10 +140,11 @@ sieve_ldap_storage_active_script_open(struct sieve_storage *storage)
 	if (sieve_script_open(&lscript->script, NULL) < 0) {
 		struct sieve_script *script = &lscript->script;
 		sieve_script_unref(&script);
-		return NULL;
+		return -1;
 	}
 
-	return &lscript->script;
+	*script_r = &lscript->script;
+	return 0;
 }
 
 int sieve_ldap_storage_active_script_get_name(struct sieve_storage *storage,
diff --git a/src/lib-sieve/storage/ldap/sieve-ldap-storage.h b/src/lib-sieve/storage/ldap/sieve-ldap-storage.h
index 8a9f27c57..01229be10 100644
--- a/src/lib-sieve/storage/ldap/sieve-ldap-storage.h
+++ b/src/lib-sieve/storage/ldap/sieve-ldap-storage.h
@@ -71,8 +71,6 @@ struct sieve_ldap_storage {
 	struct ldap_connection *conn;
 };
 
-struct sieve_script *
-sieve_ldap_storage_active_script_open(struct sieve_storage *storage);
 int sieve_ldap_storage_active_script_get_name(struct sieve_storage *storage,
 					      const char **name_r);
 
diff --git a/src/plugins/doveadm-sieve/doveadm-sieve-sync.c b/src/plugins/doveadm-sieve/doveadm-sieve-sync.c
index 90085f664..879d6f62c 100644
--- a/src/plugins/doveadm-sieve/doveadm-sieve-sync.c
+++ b/src/plugins/doveadm-sieve/doveadm-sieve-sync.c
@@ -497,8 +497,7 @@ sieve_attribute_get_active_script(struct mail_storage *storage,
 		return ret;
 	}
 
-	script = sieve_storage_active_script_open(svstorage, NULL);
-	if (script == NULL)
+	if (sieve_storage_active_script_open(svstorage, &script, NULL) < 0)
 		return 0;
 
 	ret = sieve_attribute_retrieve_script(storage, svstorage, script, TRUE,
diff --git a/src/plugins/imap-filter-sieve/imap-filter-sieve.c b/src/plugins/imap-filter-sieve/imap-filter-sieve.c
index 35926a677..47ba7d976 100644
--- a/src/plugins/imap-filter-sieve/imap-filter-sieve.c
+++ b/src/plugins/imap-filter-sieve/imap-filter-sieve.c
@@ -517,15 +517,12 @@ int imap_filter_sieve_open_personal(struct imap_filter_sieve_context *sctx,
 						   error_code_r, error_r) < 0)
 		return -1;
 
-	int ret = 0;
+	int ret;
 
-	if (name == NULL) {
-		script = sieve_storage_active_script_open(storage, NULL);
-		if (script == NULL)
-			ret = -1;
-	} else {
+	if (name == NULL)
+		ret = sieve_storage_active_script_open(storage, &script, NULL);
+	else
 		ret = sieve_storage_open_script(storage, name, &script, NULL);
-	}
 	if (ret < 0) {
 		*error_r = sieve_storage_get_last_error(storage, &error_code);
 
diff --git a/src/plugins/lda-sieve/lda-sieve-plugin.c b/src/plugins/lda-sieve/lda-sieve-plugin.c
index e58014c4d..54736a5b8 100644
--- a/src/plugins/lda-sieve/lda-sieve-plugin.c
+++ b/src/plugins/lda-sieve/lda-sieve-plugin.c
@@ -722,10 +722,9 @@ static int lda_sieve_find_scripts(struct lda_sieve_run_context *srctx)
 	if (ret == 0 && error_code == SIEVE_ERROR_NOT_POSSIBLE)
 		return 0;
 	if (ret > 0) {
-		srctx->main_script = sieve_storage_active_script_open(
-			main_storage, &error_code);
-
-		if (srctx->main_script == NULL) {
+		if (sieve_storage_active_script_open(main_storage,
+						     &srctx->main_script,
+						     &error_code) < 0) {
 			switch (error_code) {
 			case SIEVE_ERROR_NOT_FOUND:
 				e_debug(sieve_get_event(svinst),
-- 
GitLab