diff --git a/src/lib-sieve/sieve-script.c b/src/lib-sieve/sieve-script.c
index a9fcbc4687d5723d710e42898296ee7fdbbb9dbf..8b355a9639574eb98e68e098c1c900aa0aa5af09 100644
--- a/src/lib-sieve/sieve-script.c
+++ b/src/lib-sieve/sieve-script.c
@@ -588,9 +588,8 @@ sieve_script_copy_from_default(struct sieve_script *script, const char *newname)
 		struct sieve_script *newscript;
 		enum sieve_error error_code;
 
-		newscript = sieve_storage_open_script(storage->default_for,
-						      newname, &error_code);
-		if (newscript == NULL) {
+		if (sieve_storage_open_script(storage->default_for, newname,
+					      &newscript, &error_code) < 0) {
 			/* Somehow not actually saved */
 			ret = (error_code == SIEVE_ERROR_NOT_FOUND ? 0 : -1);
 		} else if (sieve_script_activate(newscript, (time_t)-1) < 0) {
diff --git a/src/lib-sieve/sieve-storage.c b/src/lib-sieve/sieve-storage.c
index 705195d47ff3068fc2cdaaffd5a1b7818e5d4384..2b243fc579e2d9fd976820eab8adf197114e54e9 100644
--- a/src/lib-sieve/sieve-storage.c
+++ b/src/lib-sieve/sieve-storage.c
@@ -755,17 +755,21 @@ int sieve_storage_get_script(struct sieve_storage *storage, const char *name,
 	return 0;
 }
 
-struct sieve_script *
-sieve_storage_open_script(struct sieve_storage *storage, const char *name,
-			  enum sieve_error *error_code_r)
+int sieve_storage_open_script(struct sieve_storage *storage, const char *name,
+			      struct sieve_script **script_r,
+			      enum sieve_error *error_code_r)
 {
 	struct sieve_instance *svinst = storage->svinst;
 	struct sieve_script *script;
 
+	*script_r = NULL;
+
 	if (sieve_storage_get_script(storage, name, &script, error_code_r) < 0)
-		return NULL;
-	if (sieve_script_open(script, error_code_r) >= 0)
-		return script;
+		return -1;
+	if (sieve_script_open(script, error_code_r) >= 0) {
+		*script_r = script;
+		return 0;
+	}
 
 	/* Error */
 	sieve_script_unref(&script);
@@ -784,13 +788,15 @@ sieve_storage_open_script(struct sieve_storage *storage, const char *name,
 		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;
 }
 
 static int
@@ -824,8 +830,7 @@ int sieve_storage_check_script(struct sieve_storage *storage, const char *name,
 	if (error_code_r == NULL)
 		error_code_r = &error_code;
 
-	script = sieve_storage_open_script(storage, name, error_code_r);
-	if (script == NULL)
+	if (sieve_storage_open_script(storage, name, &script, error_code_r) < 0)
 		return (*error_code_r == SIEVE_ERROR_NOT_FOUND ? 0 : -1);
 
 	sieve_script_unref(&script);
@@ -1268,9 +1273,8 @@ int sieve_storage_save_commit(struct sieve_storage_save_context **_sctx)
 		struct sieve_script *script;
 		enum sieve_error error_code;
 
-		script = sieve_storage_open_script(storage, scriptname,
-						   &error_code);
-		if (script == NULL) {
+		if (sieve_storage_open_script(storage, scriptname,
+					      &script, &error_code) < 0) {
 			/* Somehow not actually saved */
 			ret = (error_code == SIEVE_ERROR_NOT_FOUND ? 0 : -1);
 		} else if (sieve_script_activate(script, (time_t)-1) < 0) {
diff --git a/src/lib-sieve/sieve-storage.h b/src/lib-sieve/sieve-storage.h
index d6ce4e6eafa7442e0aab33e15b5d4241f79ba854..ed28e5cbcaf84488505f97d40d51358e3a81c52d 100644
--- a/src/lib-sieve/sieve-storage.h
+++ b/src/lib-sieve/sieve-storage.h
@@ -45,9 +45,9 @@ void sieve_storage_unref(struct sieve_storage **_storage);
 int sieve_storage_get_script(struct sieve_storage *storage, const char *name,
 			     struct sieve_script **script_r,
 			     enum sieve_error *error_code_r);
-struct sieve_script *
-sieve_storage_open_script(struct sieve_storage *storage, const char *name,
-			  enum sieve_error *error_code_r);
+int sieve_storage_open_script(struct sieve_storage *storage, const char *name,
+			      struct sieve_script **script_r,
+			      enum sieve_error *error_code_r);
 int sieve_storage_check_script(struct sieve_storage *storage, const char *name,
 			       enum sieve_error *error_code_r);
 
diff --git a/src/managesieve/cmd-deletescript.c b/src/managesieve/cmd-deletescript.c
index d7a8ef094545faa3bbcb54d5f13794f91a4bec2f..361c5a6398fdde4be4ce1c6a444daeb242baa4af 100644
--- a/src/managesieve/cmd-deletescript.c
+++ b/src/managesieve/cmd-deletescript.c
@@ -23,8 +23,8 @@ bool cmd_deletescript(struct client_command_context *cmd)
 
 	event_add_str(cmd->event, "script_name", scriptname);
 
-	script = sieve_storage_open_script(storage, scriptname, NULL);
-	if (script == NULL || sieve_script_delete(script, FALSE) < 0) {
+	if (sieve_storage_open_script(storage, scriptname, &script, NULL) < 0 ||
+	    sieve_script_delete(script, FALSE) < 0) {
 		client_command_storage_error(
 			cmd, "Failed to delete script '%s'", scriptname);
 		sieve_script_unref(&script);
diff --git a/src/managesieve/cmd-getscript.c b/src/managesieve/cmd-getscript.c
index 16c690735984b20b6a9d22088ed9e82ced4f97d8..106ce5bfac26ab46a05fe527e1862badd1dfa1fd 100644
--- a/src/managesieve/cmd-getscript.c
+++ b/src/managesieve/cmd-getscript.c
@@ -118,9 +118,8 @@ bool cmd_getscript(struct client_command_context *cmd)
 	ctx->storage = client->storage;
 	ctx->failed = FALSE;
 
-	ctx->script = sieve_storage_open_script(client->storage, scriptname,
-						NULL);
-	if (ctx->script == NULL) {
+	if (sieve_storage_open_script(client->storage, scriptname,
+				      &ctx->script, NULL) < 0) {
 		ctx->failed = TRUE;
 		return cmd_getscript_finish(ctx);
 	}
diff --git a/src/managesieve/cmd-renamescript.c b/src/managesieve/cmd-renamescript.c
index 5018d12a36fb218cd53e8754fe59091673b22309..5eadeeed48be18aafd56b6edccd58e8cc60aa006 100644
--- a/src/managesieve/cmd-renamescript.c
+++ b/src/managesieve/cmd-renamescript.c
@@ -25,8 +25,7 @@ bool cmd_renamescript(struct client_command_context *cmd)
 	event_add_str(cmd->event, "old_script_name", scriptname);
 	event_add_str(cmd->event, "new_script_name", newname);
 
-	script = sieve_storage_open_script(storage, scriptname, NULL);
-	if (script == NULL) {
+	if (sieve_storage_open_script(storage, scriptname, &script, NULL) < 0) {
 		client_command_storage_error(
 			cmd, "Failed to open script '%s' for rename to '%s'",
 			scriptname, newname);
diff --git a/src/managesieve/cmd-setactive.c b/src/managesieve/cmd-setactive.c
index a086977fe43197dcb73ca61ba8f38128b4a14c52..e0da44a4466d0e464f4e68265f2690db8259118e 100644
--- a/src/managesieve/cmd-setactive.c
+++ b/src/managesieve/cmd-setactive.c
@@ -26,8 +26,7 @@ cmd_setactive_activate(struct client_command_context *cmd,
 
 	event_add_str(cmd->event, "script_name", scriptname);
 
-	script = sieve_storage_open_script(storage, scriptname, NULL);
-	if (script == NULL) {
+	if (sieve_storage_open_script(storage, scriptname, &script, NULL) < 0) {
 		client_command_storage_error(
 			cmd, "Failed to open script '%s' for activation",
 			scriptname);
diff --git a/src/plugins/doveadm-sieve/doveadm-sieve-cmd-activate.c b/src/plugins/doveadm-sieve/doveadm-sieve-cmd-activate.c
index 2664bf269a663b7a1bc6156af12ebe8a5573d458..3abe25d7b2d57ee039253723074a79d7783769e1 100644
--- a/src/plugins/doveadm-sieve/doveadm-sieve-cmd-activate.c
+++ b/src/plugins/doveadm-sieve/doveadm-sieve-cmd-activate.c
@@ -27,8 +27,8 @@ static int cmd_sieve_activate_run(struct doveadm_sieve_cmd_context *_ctx)
 	enum sieve_error error_code;
 	int ret = 0;
 
-	script = sieve_storage_open_script(storage, ctx->scriptname, NULL);
-	if (script == NULL) {
+	if (sieve_storage_open_script(storage, ctx->scriptname,
+				      &script, NULL) < 0) {
 		e_error(event, "Failed to activate Sieve script: %s",
 			sieve_storage_get_last_error(storage, &error_code));
 		doveadm_sieve_cmd_failed_error(_ctx, error_code);
diff --git a/src/plugins/doveadm-sieve/doveadm-sieve-cmd-delete.c b/src/plugins/doveadm-sieve/doveadm-sieve-cmd-delete.c
index cde5d5142a16e490fe0153e7c849571ca02e0971..9115746afec8ef6b8eff02b4f9b3b2e10a83e457 100644
--- a/src/plugins/doveadm-sieve/doveadm-sieve-cmd-delete.c
+++ b/src/plugins/doveadm-sieve/doveadm-sieve-cmd-delete.c
@@ -34,10 +34,9 @@ static int cmd_sieve_delete_run(struct doveadm_sieve_cmd_context *_ctx)
 	array_foreach_elem(scriptnames, scriptname) {
 		int sret = 0;
 
-		script = sieve_storage_open_script(storage, scriptname, NULL);
-		if (script == NULL) {
-			sret =  -1;
-		} else {
+		sret = sieve_storage_open_script(storage, scriptname,
+						 &script, NULL);
+		if (sret == 0) {
 			if (sieve_script_delete(script,
 						ctx->ignore_active) < 0) {
 				(void)sieve_storage_get_last_error(
diff --git a/src/plugins/doveadm-sieve/doveadm-sieve-cmd-get.c b/src/plugins/doveadm-sieve/doveadm-sieve-cmd-get.c
index 14220e53a5f8cc4b0380f5c82f78c2981820daae..64ee1444fb90c3e4f554b410bdd29766d8d43dfc 100644
--- a/src/plugins/doveadm-sieve/doveadm-sieve-cmd-get.c
+++ b/src/plugins/doveadm-sieve/doveadm-sieve-cmd-get.c
@@ -27,9 +27,8 @@ static int cmd_sieve_get_run(struct doveadm_sieve_cmd_context *_ctx)
 	enum sieve_error error_code;
 	int ret;
 
-	script = sieve_storage_open_script(_ctx->storage, ctx->scriptname,
-					   &error_code);
-	if (script == NULL ||
+	if (sieve_storage_open_script(_ctx->storage, ctx->scriptname,
+				      &script, &error_code) < 0 ||
 	    sieve_script_get_stream(script, &input, &error_code) < 0) {
 		e_error(event, "Failed to open Sieve script: %s",
 			sieve_storage_get_last_error(_ctx->storage,
diff --git a/src/plugins/doveadm-sieve/doveadm-sieve-cmd-put.c b/src/plugins/doveadm-sieve/doveadm-sieve-cmd-put.c
index 2ce6c193ef9d3ac146c4829f617f98ac127ac7e8..c4529fd4d35efa174db17dc167a57deb356c92df 100644
--- a/src/plugins/doveadm-sieve/doveadm-sieve-cmd-put.c
+++ b/src/plugins/doveadm-sieve/doveadm-sieve-cmd-put.c
@@ -124,10 +124,10 @@ static int cmd_sieve_put_run(struct doveadm_sieve_cmd_context *_ctx)
 		sieve_storage_save_cancel(&save_ctx);
 
 	if (ctx->activate && ret == 0) {
-		struct sieve_script *script =
-			sieve_storage_open_script(storage, ctx->scriptname,
-						  NULL);
-		if (script == NULL ||
+		struct sieve_script *script;
+
+		if (sieve_storage_open_script(storage, ctx->scriptname,
+					      &script, NULL) < 0 ||
 		    sieve_script_activate(script, (time_t)-1) < 0) {
 			e_error(event, "Failed to activate Sieve script: %s",
 				sieve_storage_get_last_error(storage,
diff --git a/src/plugins/doveadm-sieve/doveadm-sieve-cmd-rename.c b/src/plugins/doveadm-sieve/doveadm-sieve-cmd-rename.c
index 5b1b7c76384618cf1fd6121711cc3fdf3dd59e4c..4812c9fc4911a7d91cf6b3900ddeff759db21bcd 100644
--- a/src/plugins/doveadm-sieve/doveadm-sieve-cmd-rename.c
+++ b/src/plugins/doveadm-sieve/doveadm-sieve-cmd-rename.c
@@ -27,8 +27,8 @@ static int cmd_sieve_rename_run(struct doveadm_sieve_cmd_context *_ctx)
 	enum sieve_error error_code;
 	int ret = 0;
 
-	script = sieve_storage_open_script(storage, ctx->oldname, NULL);
-	if (script == NULL) {
+	if (sieve_storage_open_script(storage, ctx->oldname,
+				      &script, NULL) < 0) {
 		e_error(event, "Failed to rename Sieve script: %s",
 			sieve_storage_get_last_error(storage, &error_code));
 		doveadm_sieve_cmd_failed_error(_ctx, error_code);
diff --git a/src/plugins/doveadm-sieve/doveadm-sieve-sync.c b/src/plugins/doveadm-sieve/doveadm-sieve-sync.c
index 786b556ae7d7a193621e4d6bbf847d33f0999cc3..90085f664bd0665e910ca92bb6ac80509463625d 100644
--- a/src/plugins/doveadm-sieve/doveadm-sieve-sync.c
+++ b/src/plugins/doveadm-sieve/doveadm-sieve-sync.c
@@ -127,10 +127,9 @@ static int sieve_attribute_unset_script(struct mail_storage *storage,
 	enum sieve_error error_code;
 	int ret = 0;
 
-	script = sieve_storage_open_script(svstorage, scriptname, NULL);
-	if (script == NULL) {
-		ret = -1;
-	} else {
+	ret = sieve_storage_open_script(svstorage, scriptname,
+					&script, NULL);
+	if (ret == 0) {
 		ret = sieve_script_delete(script, TRUE);
 		sieve_script_unref(&script);
 	}
@@ -186,8 +185,8 @@ sieve_attribute_set_active(struct mail_storage *storage,
 	scriptname++;
 
 	/* Activate specified script */
-	script = sieve_storage_open_script(svstorage, scriptname, NULL);
-	ret = (script == NULL ?
+	ret = (sieve_storage_open_script(svstorage, scriptname,
+					 &script, NULL) < 0 ?
 	       -1 : sieve_script_activate(script, last_change));
 	if (ret < 0) {
 		mail_storage_set_critical(
@@ -562,7 +561,8 @@ sieve_attribute_get_sieve(struct mail_storage *storage, const char *key,
 			"Sieve attributes are available only as streams");
 		return -1;
 	}
-	script = sieve_storage_open_script(svstorage, scriptname, NULL);
+	ret = sieve_storage_open_script(svstorage, scriptname, &script, NULL);
+	i_assert(ret == 0);
 	ret = sieve_attribute_retrieve_script(storage, svstorage, script, FALSE,
 					      value_r, &error);
 	if (ret < 0) {
diff --git a/src/plugins/imap-filter-sieve/imap-filter-sieve.c b/src/plugins/imap-filter-sieve/imap-filter-sieve.c
index 67a2eac6f0d1e0cc89fd4780e2759d0e7d2aa5af..35926a677279bb0c8cb7e1ee9735ed23ab71bbe9 100644
--- a/src/plugins/imap-filter-sieve/imap-filter-sieve.c
+++ b/src/plugins/imap-filter-sieve/imap-filter-sieve.c
@@ -517,11 +517,16 @@ int imap_filter_sieve_open_personal(struct imap_filter_sieve_context *sctx,
 						   error_code_r, error_r) < 0)
 		return -1;
 
-	if (name == NULL)
+	int ret = 0;
+
+	if (name == NULL) {
 		script = sieve_storage_active_script_open(storage, NULL);
-	else
-		script = sieve_storage_open_script(storage, name, NULL);
-	if (script == NULL) {
+		if (script == NULL)
+			ret = -1;
+	} else {
+		ret = sieve_storage_open_script(storage, name, &script, NULL);
+	}
+	if (ret < 0) {
 		*error_r = sieve_storage_get_last_error(storage, &error_code);
 
 		switch (error_code) {
@@ -557,8 +562,7 @@ int imap_filter_sieve_open_global(struct imap_filter_sieve_context *sctx,
 						 error_code_r, error_r) < 0)
 		return -1;
 
-	script = sieve_storage_open_script(storage, name, NULL);
-	if (script == NULL) {
+	if (sieve_storage_open_script(storage, name, &script, NULL) < 0) {
 		*error_r = sieve_storage_get_last_error(storage, &error_code);
 
 		switch (error_code) {
diff --git a/src/plugins/imapsieve/imap-sieve.c b/src/plugins/imapsieve/imap-sieve.c
index 1450e0d8f2e98810fa58adf83dd9bf64e233ca28..9e624c180412322f021e25e84ec9bf147e2eb977 100644
--- a/src/plugins/imapsieve/imap-sieve.c
+++ b/src/plugins/imapsieve/imap-sieve.c
@@ -432,10 +432,9 @@ imap_sieve_run_init_scripts(struct imap_sieve *isieve,
 	user_script = NULL;
 	if (storage != NULL) {
 		i_assert(count < max_len);
-		scripts[count].script =
-			sieve_storage_open_script(storage, script_name,
-						  &error_code);
-		if (scripts[count].script != NULL) {
+		if (sieve_storage_open_script(storage, script_name,
+					      &scripts[count].script,
+					      &error_code) == 0) {
 			user_script = scripts[count].script;
 			count++;
 		} else if (error_code == SIEVE_ERROR_TEMP_FAILURE) {