Skip to content

Commit d5f139c

Browse files
committed
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
1 parent fe8dd65 commit d5f139c

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

src/backend/tsearch/spell.c

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ lowerstr_ctx(IspellDict *Conf, const char *src)
191191
#define GETWCHAR(W,L,N,T) ( ((const uint8*)(W))[ ((T)==FF_PREFIX) ? (N) : ( (L) - 1 - (N) ) ] )
192192
#define GETCHAR(A,N,T) GETWCHAR( (A)->repl, (A)->replen, N, T )
193193

194-
static char *VoidString = "";
194+
static const char *VoidString = "";
195195

196196
static int
197197
cmpspell(const void *s1, const void *s2)
@@ -346,11 +346,11 @@ cmpaffix(const void *s1, const void *s2)
346346
* sflag: returns an affix flag from sflagset.
347347
*/
348348
static void
349-
getNextFlagFromString(IspellDict *Conf, char **sflagset, char *sflag)
349+
getNextFlagFromString(IspellDict *Conf, const char **sflagset, char *sflag)
350350
{
351351
int32 s;
352-
char *next,
353-
*sbuf = *sflagset;
352+
char *next;
353+
const char *sbuf = *sflagset;
354354
int maxstep;
355355
bool stop = false;
356356
bool met_comma = false;
@@ -453,7 +453,7 @@ getNextFlagFromString(IspellDict *Conf, char **sflagset, char *sflag)
453453
static bool
454454
IsAffixFlagInUse(IspellDict *Conf, int affix, const char *affixflag)
455455
{
456-
char *flagcur;
456+
const char *flagcur;
457457
char flag[BUFSIZ];
458458

459459
if (*affixflag == 0)
@@ -1120,13 +1120,13 @@ addCompoundAffixFlagValue(IspellDict *Conf, char *s, uint32 val)
11201120
* flags s.
11211121
*/
11221122
static int
1123-
getCompoundAffixFlagValue(IspellDict *Conf, char *s)
1123+
getCompoundAffixFlagValue(IspellDict *Conf, const char *s)
11241124
{
11251125
uint32 flag = 0;
11261126
CompoundAffixFlag *found,
11271127
key;
11281128
char sflag[BUFSIZ];
1129-
char *flagcur;
1129+
const char *flagcur;
11301130

11311131
if (Conf->nCompoundAffixFlag == 0)
11321132
return 0;
@@ -1155,7 +1155,7 @@ getCompoundAffixFlagValue(IspellDict *Conf, char *s)
11551155
* Conf->AffixData array and function returns its entry.
11561156
* Else function returns the s parameter.
11571157
*/
1158-
static char *
1158+
static const char *
11591159
getAffixFlagSet(IspellDict *Conf, char *s)
11601160
{
11611161
if (Conf->useFlagAliases && *s != '\0')
@@ -1323,7 +1323,7 @@ NIImportOOAffixes(IspellDict *Conf, const char *filename)
13231323
/* Also reserve place for empty flag set */
13241324
naffix++;
13251325

1326-
Conf->AffixData = (char **) palloc0(naffix * sizeof(char *));
1326+
Conf->AffixData = (const char **) palloc0(naffix * sizeof(char *));
13271327
Conf->lenAffixData = Conf->nAffixData = naffix;
13281328

13291329
/* Add empty flag set into AffixData */
@@ -1571,7 +1571,7 @@ NIImportAffixes(IspellDict *Conf, const char *filename)
15711571
static int
15721572
MergeAffix(IspellDict *Conf, int a1, int a2)
15731573
{
1574-
char **ptr;
1574+
const char **ptr;
15751575

15761576
Assert(a1 < Conf->nAffixData && a2 < Conf->nAffixData);
15771577

@@ -1585,24 +1585,28 @@ MergeAffix(IspellDict *Conf, int a1, int a2)
15851585
if (Conf->nAffixData + 1 >= Conf->lenAffixData)
15861586
{
15871587
Conf->lenAffixData *= 2;
1588-
Conf->AffixData = (char **) repalloc(Conf->AffixData,
1589-
sizeof(char *) * Conf->lenAffixData);
1588+
Conf->AffixData = (const char **) repalloc(Conf->AffixData,
1589+
sizeof(char *) * Conf->lenAffixData);
15901590
}
15911591

15921592
ptr = Conf->AffixData + Conf->nAffixData;
15931593
if (Conf->flagMode == FM_NUM)
15941594
{
1595-
*ptr = cpalloc(strlen(Conf->AffixData[a1]) +
1596-
strlen(Conf->AffixData[a2]) +
1597-
1 /* comma */ + 1 /* \0 */ );
1598-
sprintf(*ptr, "%s,%s", Conf->AffixData[a1], Conf->AffixData[a2]);
1595+
char *p = cpalloc(strlen(Conf->AffixData[a1]) +
1596+
strlen(Conf->AffixData[a2]) +
1597+
1 /* comma */ + 1 /* \0 */ );
1598+
1599+
sprintf(p, "%s,%s", Conf->AffixData[a1], Conf->AffixData[a2]);
1600+
*ptr = p;
15991601
}
16001602
else
16011603
{
1602-
*ptr = cpalloc(strlen(Conf->AffixData[a1]) +
1603-
strlen(Conf->AffixData[a2]) +
1604-
1 /* \0 */ );
1605-
sprintf(*ptr, "%s%s", Conf->AffixData[a1], Conf->AffixData[a2]);
1604+
char *p = cpalloc(strlen(Conf->AffixData[a1]) +
1605+
strlen(Conf->AffixData[a2]) +
1606+
1 /* \0 */ );
1607+
1608+
sprintf(p, "%s%s", Conf->AffixData[a1], Conf->AffixData[a2]);
1609+
*ptr = p;
16061610
}
16071611
ptr++;
16081612
*ptr = NULL;
@@ -1785,7 +1789,7 @@ NISortDictionary(IspellDict *Conf)
17851789
* dictionary. Replace textual flag-field of Conf->Spell entries with
17861790
* indexes into Conf->AffixData array.
17871791
*/
1788-
Conf->AffixData = (char **) palloc0(naffix * sizeof(char *));
1792+
Conf->AffixData = (const char **) palloc0(naffix * sizeof(const char *));
17891793

17901794
curaffix = -1;
17911795
for (i = 0; i < Conf->nspell; i++)
@@ -1954,7 +1958,7 @@ mkVoidAffix(IspellDict *Conf, bool issuffix, int startsuffix)
19541958
* returns false.
19551959
*/
19561960
static bool
1957-
isAffixInUse(IspellDict *Conf, char *affixflag)
1961+
isAffixInUse(IspellDict *Conf, const char *affixflag)
19581962
{
19591963
int i;
19601964

@@ -2169,7 +2173,7 @@ addToResult(char **forms, char **cur, char *word)
21692173
}
21702174

21712175
static char **
2172-
NormalizeSubWord(IspellDict *Conf, char *word, int flag)
2176+
NormalizeSubWord(IspellDict *Conf, const char *word, int flag)
21732177
{
21742178
AffixNodeData *suffix = NULL,
21752179
*prefix = NULL;
@@ -2255,7 +2259,7 @@ NormalizeSubWord(IspellDict *Conf, char *word, int flag)
22552259
if (CheckAffix(newword, swrdlen, prefix->aff[j], flag, pnewword, &baselen))
22562260
{
22572261
/* prefix success */
2258-
char *ff = (prefix->aff[j]->flagflags & suffix->aff[i]->flagflags & FF_CROSSPRODUCT) ?
2262+
const char *ff = (prefix->aff[j]->flagflags & suffix->aff[i]->flagflags & FF_CROSSPRODUCT) ?
22592263
VoidString : prefix->aff[j]->flag;
22602264

22612265
if (FindWord(Conf, pnewword, ff, flag))
@@ -2287,7 +2291,7 @@ typedef struct SplitVar
22872291
} SplitVar;
22882292

22892293
static int
2290-
CheckCompoundAffixes(CMPDAffix **ptr, char *word, int len, bool CheckInPlace)
2294+
CheckCompoundAffixes(CMPDAffix **ptr, const char *word, int len, bool CheckInPlace)
22912295
{
22922296
bool issuffix;
22932297

@@ -2367,7 +2371,7 @@ AddStem(SplitVar *v, char *word)
23672371
}
23682372

23692373
static SplitVar *
2370-
SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, char *word, int wordlen, int startpos, int minpos)
2374+
SplitToVariants(IspellDict *Conf, SPNode *snode, SplitVar *orig, const char *word, int wordlen, int startpos, int minpos)
23712375
{
23722376
SplitVar *var = NULL;
23732377
SPNodeData *StopLow,
@@ -2533,7 +2537,7 @@ addNorm(TSLexeme **lres, TSLexeme **lcur, char *word, int flags, uint16 NVariant
25332537
}
25342538

25352539
TSLexeme *
2536-
NINormalizeWord(IspellDict *Conf, char *word)
2540+
NINormalizeWord(IspellDict *Conf, const char *word)
25372541
{
25382542
char **res;
25392543
TSLexeme *lcur = NULL,

src/include/tsearch/dicts/spell.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ typedef struct spell_struct
6666
* flag is filled in by NIImportDictionary(). After
6767
* NISortDictionary(), d is used instead of flag.
6868
*/
69-
char *flag;
69+
const char *flag;
7070
/* d is used in mkSPNode() */
7171
struct
7272
{
@@ -86,15 +86,15 @@ typedef struct spell_struct
8686
*/
8787
typedef struct aff_struct
8888
{
89-
char *flag;
89+
const char *flag;
9090
/* FF_SUFFIX or FF_PREFIX */
9191
uint32 type:1,
9292
flagflags:7,
9393
issimple:1,
9494
isregis:1,
9595
replen:14;
96-
char *find;
97-
char *repl;
96+
const char *find;
97+
const char *repl;
9898
union
9999
{
100100
/*
@@ -146,7 +146,7 @@ typedef struct AffixNode
146146

147147
typedef struct
148148
{
149-
char *affix;
149+
const char *affix;
150150
int len;
151151
bool issuffix;
152152
} CMPDAffix;
@@ -170,7 +170,7 @@ typedef struct CompoundAffixFlag
170170
union
171171
{
172172
/* Flag name if flagMode is FM_CHAR or FM_LONG */
173-
char *s;
173+
const char *s;
174174
/* Flag name if flagMode is FM_NUM */
175175
uint32 i;
176176
} flag;
@@ -192,7 +192,7 @@ typedef struct
192192

193193
SPNode *Dictionary;
194194
/* Array of sets of affixes */
195-
char **AffixData;
195+
const char **AffixData;
196196
int lenAffixData;
197197
int nAffixData;
198198
bool useFlagAliases;
@@ -229,7 +229,7 @@ typedef struct
229229
size_t avail; /* free space remaining at firstfree */
230230
} IspellDict;
231231

232-
extern TSLexeme *NINormalizeWord(IspellDict *Conf, char *word);
232+
extern TSLexeme *NINormalizeWord(IspellDict *Conf, const char *word);
233233

234234
extern void NIStartBuild(IspellDict *Conf);
235235
extern void NIImportAffixes(IspellDict *Conf, const char *filename);

0 commit comments

Comments
 (0)