Constify fields and parameters in spell.c

I started by marking VoidString as const, and fixing the fallout by
marking more fields and function arguments as const. It proliferated
quite a lot, but all within spell.c and spell.h.

A more narrow patch to get rid of the static VoidString buffer would
be to replace it with '#define VoidString ""', as C99 allows assigning
"" to a non-const pointer, even though you're not allowed to modify
it. But it seems like good hygiene to mark all these as const. In the
structs, the pointers can point to the constant VoidString, or a
buffer allocated with palloc(), or with compact_palloc(), so you
should not modify them.

Reviewed-by: Andres Freund
Discussion: https://www.postgresql.org/message-id/54c29fb0-edf2-48ea-9814-44e918bbd6e8@iki.fi
This commit is contained in:
Heikki Linnakangas 2024-08-06 23:04:51 +03:00
parent fe8dd65bf2
commit d5f139cb68
2 changed files with 39 additions and 35 deletions

View File

@ -191,7 +191,7 @@ lowerstr_ctx(IspellDict *Conf, const char *src)
#define GETWCHAR(W,L,N,T) ( ((const uint8*)(W))[ ((T)==FF_PREFIX) ? (N) : ( (L) - 1 - (N) ) ] ) #define GETWCHAR(W,L,N,T) ( ((const uint8*)(W))[ ((T)==FF_PREFIX) ? (N) : ( (L) - 1 - (N) ) ] )
#define GETCHAR(A,N,T) GETWCHAR( (A)->repl, (A)->replen, N, T ) #define GETCHAR(A,N,T) GETWCHAR( (A)->repl, (A)->replen, N, T )
static char *VoidString = ""; static const char *VoidString = "";
static int static int
cmpspell(const void *s1, const void *s2) cmpspell(const void *s1, const void *s2)
@ -346,11 +346,11 @@ cmpaffix(const void *s1, const void *s2)
* sflag: returns an affix flag from sflagset. * sflag: returns an affix flag from sflagset.
*/ */
static void static void
getNextFlagFromString(IspellDict *Conf, char **sflagset, char *sflag) getNextFlagFromString(IspellDict *Conf, const char **sflagset, char *sflag)
{ {
int32 s; int32 s;
char *next, char *next;
*sbuf = *sflagset; const char *sbuf = *sflagset;
int maxstep; int maxstep;
bool stop = false; bool stop = false;
bool met_comma = false; bool met_comma = false;
@ -453,7 +453,7 @@ getNextFlagFromString(IspellDict *Conf, char **sflagset, char *sflag)
static bool static bool
IsAffixFlagInUse(IspellDict *Conf, int affix, const char *affixflag) IsAffixFlagInUse(IspellDict *Conf, int affix, const char *affixflag)
{ {
char *flagcur; const char *flagcur;
char flag[BUFSIZ]; char flag[BUFSIZ];
if (*affixflag == 0) if (*affixflag == 0)
@ -1120,13 +1120,13 @@ addCompoundAffixFlagValue(IspellDict *Conf, char *s, uint32 val)
* flags s. * flags s.
*/ */
static int static int
getCompoundAffixFlagValue(IspellDict *Conf, char *s) getCompoundAffixFlagValue(IspellDict *Conf, const char *s)
{ {
uint32 flag = 0; uint32 flag = 0;
CompoundAffixFlag *found, CompoundAffixFlag *found,
key; key;
char sflag[BUFSIZ]; char sflag[BUFSIZ];
char *flagcur; const char *flagcur;
if (Conf->nCompoundAffixFlag == 0) if (Conf->nCompoundAffixFlag == 0)
return 0; return 0;
@ -1155,7 +1155,7 @@ getCompoundAffixFlagValue(IspellDict *Conf, char *s)
* Conf->AffixData array and function returns its entry. * Conf->AffixData array and function returns its entry.
* Else function returns the s parameter. * Else function returns the s parameter.
*/ */
static char * static const char *
getAffixFlagSet(IspellDict *Conf, char *s) getAffixFlagSet(IspellDict *Conf, char *s)
{ {
if (Conf->useFlagAliases && *s != '\0') if (Conf->useFlagAliases && *s != '\0')
@ -1323,7 +1323,7 @@ NIImportOOAffixes(IspellDict *Conf, const char *filename)
/* Also reserve place for empty flag set */ /* Also reserve place for empty flag set */
naffix++; naffix++;
Conf->AffixData = (char **) palloc0(naffix * sizeof(char *)); Conf->AffixData = (const char **) palloc0(naffix * sizeof(char *));
Conf->lenAffixData = Conf->nAffixData = naffix; Conf->lenAffixData = Conf->nAffixData = naffix;
/* Add empty flag set into AffixData */ /* Add empty flag set into AffixData */
@ -1571,7 +1571,7 @@ isnewformat:
static int static int
MergeAffix(IspellDict *Conf, int a1, int a2) MergeAffix(IspellDict *Conf, int a1, int a2)
{ {
char **ptr; const char **ptr;
Assert(a1 < Conf->nAffixData && a2 < Conf->nAffixData); Assert(a1 < Conf->nAffixData && a2 < Conf->nAffixData);
@ -1585,24 +1585,28 @@ MergeAffix(IspellDict *Conf, int a1, int a2)
if (Conf->nAffixData + 1 >= Conf->lenAffixData) if (Conf->nAffixData + 1 >= Conf->lenAffixData)
{ {
Conf->lenAffixData *= 2; Conf->lenAffixData *= 2;
Conf->AffixData = (char **) repalloc(Conf->AffixData, Conf->AffixData = (const char **) repalloc(Conf->AffixData,
sizeof(char *) * Conf->lenAffixData); sizeof(char *) * Conf->lenAffixData);
} }
ptr = Conf->AffixData + Conf->nAffixData; ptr = Conf->AffixData + Conf->nAffixData;
if (Conf->flagMode == FM_NUM) if (Conf->flagMode == FM_NUM)
{ {
*ptr = cpalloc(strlen(Conf->AffixData[a1]) + char *p = cpalloc(strlen(Conf->AffixData[a1]) +
strlen(Conf->AffixData[a2]) + strlen(Conf->AffixData[a2]) +
1 /* comma */ + 1 /* \0 */ ); 1 /* comma */ + 1 /* \0 */ );
sprintf(*ptr, "%s,%s", Conf->AffixData[a1], Conf->AffixData[a2]);
sprintf(p, "%s,%s", Conf->AffixData[a1], Conf->AffixData[a2]);
*ptr = p;
} }
else else
{ {
*ptr = cpalloc(strlen(Conf->AffixData[a1]) + char *p = cpalloc(strlen(Conf->AffixData[a1]) +
strlen(Conf->AffixData[a2]) + strlen(Conf->AffixData[a2]) +
1 /* \0 */ ); 1 /* \0 */ );
sprintf(*ptr, "%s%s", Conf->AffixData[a1], Conf->AffixData[a2]);
sprintf(p, "%s%s", Conf->AffixData[a1], Conf->AffixData[a2]);
*ptr = p;
} }
ptr++; ptr++;
*ptr = NULL; *ptr = NULL;
@ -1785,7 +1789,7 @@ NISortDictionary(IspellDict *Conf)
* dictionary. Replace textual flag-field of Conf->Spell entries with * dictionary. Replace textual flag-field of Conf->Spell entries with
* indexes into Conf->AffixData array. * indexes into Conf->AffixData array.
*/ */
Conf->AffixData = (char **) palloc0(naffix * sizeof(char *)); Conf->AffixData = (const char **) palloc0(naffix * sizeof(const char *));
curaffix = -1; curaffix = -1;
for (i = 0; i < Conf->nspell; i++) for (i = 0; i < Conf->nspell; i++)
@ -1954,7 +1958,7 @@ mkVoidAffix(IspellDict *Conf, bool issuffix, int startsuffix)
* returns false. * returns false.
*/ */
static bool static bool
isAffixInUse(IspellDict *Conf, char *affixflag) isAffixInUse(IspellDict *Conf, const char *affixflag)
{ {
int i; int i;
@ -2169,7 +2173,7 @@ addToResult(char **forms, char **cur, char *word)
} }
static char ** static char **
NormalizeSubWord(IspellDict *Conf, char *word, int flag) NormalizeSubWord(IspellDict *Conf, const char *word, int flag)
{ {
AffixNodeData *suffix = NULL, AffixNodeData *suffix = NULL,
*prefix = NULL; *prefix = NULL;
@ -2255,7 +2259,7 @@ NormalizeSubWord(IspellDict *Conf, char *word, int flag)
if (CheckAffix(newword, swrdlen, prefix->aff[j], flag, pnewword, &baselen)) if (CheckAffix(newword, swrdlen, prefix->aff[j], flag, pnewword, &baselen))
{ {
/* prefix success */ /* prefix success */
char *ff = (prefix->aff[j]->flagflags & suffix->aff[i]->flagflags & FF_CROSSPRODUCT) ? const char *ff = (prefix->aff[j]->flagflags & suffix->aff[i]->flagflags & FF_CROSSPRODUCT) ?
VoidString : prefix->aff[j]->flag; VoidString : prefix->aff[j]->flag;
if (FindWord(Conf, pnewword, ff, flag)) if (FindWord(Conf, pnewword, ff, flag))
@ -2287,7 +2291,7 @@ typedef struct SplitVar
} SplitVar; } SplitVar;
static int static int
CheckCompoundAffixes(CMPDAffix **ptr, char *word, int len, bool CheckInPlace) CheckCompoundAffixes(CMPDAffix **ptr, const char *word, int len, bool CheckInPlace)
{ {
bool issuffix; bool issuffix;
@ -2367,7 +2371,7 @@ AddStem(SplitVar *v, char *word)
} }
static SplitVar * static SplitVar *
SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, char *word, int wordlen, int startpos, int minpos) SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, const char *word, int wordlen, int startpos, int minpos)
{ {
SplitVar *var = NULL; SplitVar *var = NULL;
SPNodeData *StopLow, SPNodeData *StopLow,
@ -2533,7 +2537,7 @@ addNorm(TSLexeme **lres, TSLexeme **lcur, char *word, int flags, uint16 NVariant
} }
TSLexeme * TSLexeme *
NINormalizeWord(IspellDict *Conf, char *word) NINormalizeWord(IspellDict *Conf, const char *word)
{ {
char **res; char **res;
TSLexeme *lcur = NULL, TSLexeme *lcur = NULL,

View File

@ -66,7 +66,7 @@ typedef struct spell_struct
* flag is filled in by NIImportDictionary(). After * flag is filled in by NIImportDictionary(). After
* NISortDictionary(), d is used instead of flag. * NISortDictionary(), d is used instead of flag.
*/ */
char *flag; const char *flag;
/* d is used in mkSPNode() */ /* d is used in mkSPNode() */
struct struct
{ {
@ -86,15 +86,15 @@ typedef struct spell_struct
*/ */
typedef struct aff_struct typedef struct aff_struct
{ {
char *flag; const char *flag;
/* FF_SUFFIX or FF_PREFIX */ /* FF_SUFFIX or FF_PREFIX */
uint32 type:1, uint32 type:1,
flagflags:7, flagflags:7,
issimple:1, issimple:1,
isregis:1, isregis:1,
replen:14; replen:14;
char *find; const char *find;
char *repl; const char *repl;
union union
{ {
/* /*
@ -146,7 +146,7 @@ typedef struct AffixNode
typedef struct typedef struct
{ {
char *affix; const char *affix;
int len; int len;
bool issuffix; bool issuffix;
} CMPDAffix; } CMPDAffix;
@ -170,7 +170,7 @@ typedef struct CompoundAffixFlag
union union
{ {
/* Flag name if flagMode is FM_CHAR or FM_LONG */ /* Flag name if flagMode is FM_CHAR or FM_LONG */
char *s; const char *s;
/* Flag name if flagMode is FM_NUM */ /* Flag name if flagMode is FM_NUM */
uint32 i; uint32 i;
} flag; } flag;
@ -192,7 +192,7 @@ typedef struct
SPNode *Dictionary; SPNode *Dictionary;
/* Array of sets of affixes */ /* Array of sets of affixes */
char **AffixData; const char **AffixData;
int lenAffixData; int lenAffixData;
int nAffixData; int nAffixData;
bool useFlagAliases; bool useFlagAliases;
@ -229,7 +229,7 @@ typedef struct
size_t avail; /* free space remaining at firstfree */ size_t avail; /* free space remaining at firstfree */
} IspellDict; } IspellDict;
extern TSLexeme *NINormalizeWord(IspellDict *Conf, char *word); extern TSLexeme *NINormalizeWord(IspellDict *Conf, const char *word);
extern void NIStartBuild(IspellDict *Conf); extern void NIStartBuild(IspellDict *Conf);
extern void NIImportAffixes(IspellDict *Conf, const char *filename); extern void NIImportAffixes(IspellDict *Conf, const char *filename);