From c5db6fafc018a22bbdc9eea0b057b09d6f92ea3d Mon Sep 17 00:00:00 2001 From: Ewan Young Date: Tue, 25 Aug 2026 01:56:40 +0800 Subject: [PATCH v1 1/2] Don't convert Hunspell compound flags before the flag mode is known The compound flags collected from COMPOUNDFLAG and friends are stored in either the string or the integer member of a union, chosen by the flag mode that the affix file's FLAG line declares. NIImportOOAffixes() converted each flag as soon as it read it, using the mode in effect at that point, and recorded that mode in the entry. Since FLAG may appear anywhere in the file, including after the compound flags, entries written before and after it could disagree about which member of the union holds the flag. cmpcmdflag() takes the mode from its first argument alone and applies it to both, so it can read an integer as a char pointer and pass that to strcmp(). Depending on which way the mismatch goes, the result is a segfault while sorting the array, a segfault in the bsearch() that later looks flags up (the lookup key is built with the final mode, so this happens even when the array itself is consistent), or, when both members happen to be readable, no crash at all and a compound flag that is never found, which silently disables compound word splitting. Fix by keeping the flags as strings while the file is read and converting them once it has been read in full, when the mode is final. This also makes the position of the FLAG line irrelevant, which is how the flags on AF, SFX and PFX lines are already treated: those are parsed in a second pass and so always use the final mode. Note that the old ispell file format reaches addCompoundAffixFlagValue() too, from NIImportAffixes(), and returns without entering NIImportOOAffixes(), so it needs the conversion step as well. --- src/backend/tsearch/spell.c | 99 ++++++++++++++++++++++++++++++------- 1 file changed, 80 insertions(+), 19 deletions(-) diff --git a/src/backend/tsearch/spell.c b/src/backend/tsearch/spell.c index ceea8ead65a..0e2b4f91390 100644 --- a/src/backend/tsearch/spell.c +++ b/src/backend/tsearch/spell.c @@ -1033,31 +1033,41 @@ parse_affentry(const char *str, char *mask, char *find, char *repl) return (*mask && (*find || *repl)); } +/* + * Parse an affix flag written in the "num" flag mode. + */ +static uint32 +parseNumericAffixFlag(const char *s) +{ + char *next; + int i; + + errno = 0; + i = strtol(s, &next, 10); + if (s == next || errno == ERANGE) + ereport(ERROR, + (errcode(ERRCODE_CONFIG_FILE_ERROR), + errmsg("invalid affix flag \"%s\"", s))); + if (i < 0 || i > FLAGNUM_MAXSIZE) + ereport(ERROR, + (errcode(ERRCODE_CONFIG_FILE_ERROR), + errmsg("affix flag \"%s\" is out of range", s))); + + return i; +} + /* * Sets a Hunspell options depending on flag type. + * + * Conf->flagMode must already have its final value, since it decides which + * member of the entry's union is written. See finalizeCompoundAffixFlags(). */ static void setCompoundAffixFlagValue(IspellDict *Conf, CompoundAffixFlag *entry, - char *s, uint32 val) + const char *s, uint32 val) { if (Conf->flagMode == FM_NUM) - { - char *next; - int i; - - errno = 0; - i = strtol(s, &next, 10); - if (s == next || errno == ERANGE) - ereport(ERROR, - (errcode(ERRCODE_CONFIG_FILE_ERROR), - errmsg("invalid affix flag \"%s\"", s))); - if (i < 0 || i > FLAGNUM_MAXSIZE) - ereport(ERROR, - (errcode(ERRCODE_CONFIG_FILE_ERROR), - errmsg("affix flag \"%s\" is out of range", s))); - - entry->flag.i = i; - } + entry->flag.i = parseNumericAffixFlag(s); else entry->flag.s = cpstrdup(Conf, s); @@ -1120,12 +1130,54 @@ addCompoundAffixFlagValue(IspellDict *Conf, const char *s, uint32 val) newValue = Conf->CompoundAffixFlags + Conf->nCompoundAffixFlag; - setCompoundAffixFlagValue(Conf, newValue, sbuf, val); + /* + * Only remember the flag as a string for now. The FLAG option that says + * how flags are spelled may appear anywhere in the affix file, including + * after the compound flags themselves, so the final representation cannot + * be chosen until the whole file has been read. See + * finalizeCompoundAffixFlags(), which fills in flagMode as well. + * + * The interim copy goes in the short-lived build context, since the final + * representation may well not be a string at all. + */ + newValue->flag.s = MemoryContextStrdup(Conf->buildCxt, sbuf); + newValue->value = val; Conf->usecompound = true; Conf->nCompoundAffixFlag++; } +/* + * Convert the compound flags collected by addCompoundAffixFlagValue() to the + * representation implied by the flag mode the affix file ended up declaring. + * + * This must run before the flags are sorted or searched. Doing the conversion + * here rather than while reading the file makes the position of the FLAG line + * irrelevant, which is how the flags on AF, SFX and PFX lines are already + * treated: those are parsed in a second pass over the file, and so always use + * the final flag mode. + */ +static void +finalizeCompoundAffixFlags(IspellDict *Conf) +{ + for (int i = 0; i < Conf->nCompoundAffixFlag; i++) + { + CompoundAffixFlag *entry = Conf->CompoundAffixFlags + i; + + /* + * Replace the interim string with the representation the flag mode + * calls for. In both cases the old value is read before the new one + * is stored, so overwriting the union in place is safe. + */ + if (Conf->flagMode == FM_NUM) + entry->flag.i = parseNumericAffixFlag(entry->flag.s); + else + entry->flag.s = cpstrdup(Conf, entry->flag.s); + + entry->flagMode = Conf->flagMode; + } +} + /* * Returns a set of affix parameters which correspondence to the set of affix * flags s. @@ -1302,6 +1354,9 @@ NIImportOOAffixes(IspellDict *Conf, const char *filename) } tsearch_readline_end(&trst); + /* Conf->flagMode is final now, so the compound flags can be converted */ + finalizeCompoundAffixFlags(Conf); + if (Conf->nCompoundAffixFlag > 1) qsort(Conf->CompoundAffixFlags, Conf->nCompoundAffixFlag, sizeof(CompoundAffixFlag), cmpcmdflag); @@ -1576,6 +1631,12 @@ nextline: pfree(pstr); } tsearch_readline_end(&trst); + + /* + * The old file format has no FLAG command, so the mode is still FM_CHAR + * here, but the flags collected above must be converted all the same. + */ + finalizeCompoundAffixFlags(Conf); return; isnewformat: -- 2.47.3