From 83ac9194fb041d80f93a4bf1a852abc9b1471247 Mon Sep 17 00:00:00 2001
From: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Date: Sun, 30 Aug 2026 09:28:11 +0500
Subject: [PATCH] Check for NULL from Snowball create_env in dsnowball_init.

SN_new_env and the per-language create_env wrappers return NULL on
allocation failure, but locate_stem_module stored that NULL and let
dsnowball_init succeed.  Later SN_set_current in dsnowball_lexize then
crashed.  Reject a NULL env after the language module is selected.

Author: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Reported-by: Alexander Lakhin <exclusion@gmail.com>
---
 src/backend/snowball/dict_snowball.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/backend/snowball/dict_snowball.c b/src/backend/snowball/dict_snowball.c
index 182bd156995..1213a59323c 100644
--- a/src/backend/snowball/dict_snowball.c
+++ b/src/backend/snowball/dict_snowball.c
@@ -276,6 +276,14 @@ dsnowball_init(PG_FUNCTION_ARGS)
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 				 errmsg("missing Language parameter")));
 
+	/*
+	 * Snowball create_env returns NULL on allocation failure.  Catch it here
+	 */
+	if (d->z == NULL)
+		ereport(ERROR,
+				(errcode(ERRCODE_OUT_OF_MEMORY),
+				 errmsg("out of memory")));
+
 	d->dictCtx = CurrentMemoryContext;
 
 	PG_RETURN_POINTER(d);
-- 
2.53.0

