Re: BUG #19595: Three memory-safety defects in src/backend/tsearch/spell.c (dictionary loader), PG 18.3
Re: BUG #19595: Three memory-safety defects in src/backend/tsearch/spell.c (dictionary loader), PG 18.3
От:
Alexander Lakhin <exclusion@gmail.com>
Дата:
Hello Tom,
02.08.2026 23:11, Tom Lane wrote:
02.08.2026 23:11, Tom Lane wrote:
Hmph. Not sure I'd call that "memory safety", but yeah, this bit isn't being careful about having a valid intermediate state of the data structure. Thanks for the report!
I discovered one more issue in this area. This OOM condition emulation:
--- a/src/backend/snowball/libstemmer/api.c
+++ b/src/backend/snowball/libstemmer/api.c
@@ -6,3 +6,3 @@ extern struct SN_env * SN_new_env(int alloc_size)
{
- struct SN_env * z = (struct SN_env *) malloc(alloc_size);
+ struct SN_env * z = (rand() % 2 == 0) ? NULL : (struct SN_env *) malloc(alloc_size);
if (z == NULL) return NULL;
leads to `make check` crashes like:
2026-08-30 06:42:24.759 EEST postmaster[1423557] LOG: client backend (PID 1423766) was terminated by signal 11: Segmentation fault
2026-08-30 06:42:24.759 EEST postmaster[1423557] DETAIL: Failed process was running: SELECT ts_delete(to_tsvector('english', 'Rebel spaceships, striking from a hidden base'), 'spaceship')
or
2026-08-30 06:46:50.112 EEST postmaster[1426347] LOG: client backend (PID 1427391) was terminated by signal 11: Segmentation fault
2026-08-30 06:46:50.112 EEST postmaster[1426347] DETAIL: Failed process was running: SELECT ts_lexize('thesaurus', 'one');
Could you please have a look if you're still around?
Best regards,
Alexander
Re: BUG #19595: Three memory-safety defects in src/backend/tsearch/spell.c (dictionary loader), PG 18.3
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Alexander Lakhin writes:
> I discovered one more issue in this area. This OOM condition emulation:
> --- a/src/backend/snowball/libstemmer/api.c
> +++ b/src/backend/snowball/libstemmer/api.c
> @@ -6,3 +6,3 @@ extern struct SN_env * SN_new_env(int alloc_size)
> {
> - struct SN_env * z = (struct SN_env *) malloc(alloc_size);
> + struct SN_env * z = (rand() % 2 == 0) ? NULL : (struct SN_env *) malloc(alloc_size);
> if (z == NULL) return NULL;
> leads to `make check` crashes like:
Hmph. SN_new_env itself is visibly okay with this, so the failure is
in some caller. I'm too tired to dig into it myself, but can you
identify the culprit more precisely?
regards, tom lane
Re: BUG #19595: Three memory-safety defects in src/backend/tsearch/spell.c (dictionary loader), PG 18.3
От:
Alexander Lakhin <exclusion@gmail.com>
Дата:
30.08.2026 07:04, Tom Lane wrote:
Hmph. SN_new_env itself is visibly okay with this, so the failure is in some caller. I'm too tired to dig into it myself, but can you identify the culprit more precisely?
I think it's:
static void
locate_stem_module(DictSnowball *d, const char *lang)
{
...
d->stem = m->stem;
d->z = m->create();
...
(in two places)
then d->z is dereferenced in dsnowball_lexize().
Thank you for your time!
Best regards,
Alexander
Re: BUG #19595: Three memory-safety defects in src/backend/tsearch/spell.c (dictionary loader), PG 18.3
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Andrey Rachitskiy writes:
> In the backend, snowball_runtime.h remaps malloc to palloc
> (src/include/snowball/snowball_runtime.h). api.c includes that
> header via the -I order in the snowball Makefile / meson.build, so
> SN_new_env()'s malloc is palloc. On allocation failure palloc does
> not return NULL. It goes through MemoryContextAllocationFailure().
Ah, right. You can confirm that SN_new_env is really using palloc:
$ nm --ext --undef api.o | grep alloc
U palloc
It's like this to prevent memory leaks while not modifying the
machine-generated Snowball .c files, but I concede it's confusing.
Anyway it looks like we have nothing to do here. The Snowball code
is correct on its own terms to defend against null results, but our
calling code is equally correct to not worry about that.
regards, tom lane