Skip to content

Commit 5fb9f96

Browse files
committed
Remove unnecessary type violation in tsvectorrecv().
compareentry() is declared to work on WordEntryIN structs, but tsvectorrecv() is using it in two places to work on WordEntry structs. This is almost okay, since WordEntry is the first field of WordEntryIN. But on machines with 8-byte pointers, WordEntryIN will have a larger alignment spec than WordEntry, and it's at least theoretically possible that the compiler could generate code that depends on the larger alignment. Given the lack of field reports, this may be just a hypothetical bug that upsets nothing except sanitizer tools. Or it may be real on certain hardware but nobody's tried to use tsvectorrecv() on such hardware. In any case we should fix it, and the fix is trivial: just change compareentry() so that it works on WordEntry without any mention of WordEntryIN. We can also get rid of the quite-useless intermediate function WordEntryCMP. Bug: #18875 Reported-by: Alexander Lakhin <exclusion@gmail.com> Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/18875-07a29c49c825a608@postgresql.org Backpatch-through: 13
1 parent 980727b commit 5fb9f96

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/backend/utils/adt/tsvector.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
typedef struct
2626
{
27-
WordEntry entry; /* must be first! */
27+
WordEntry entry; /* must be first, see compareentry */
2828
WordEntryPos *pos;
2929
int poslen; /* number of elements in pos */
3030
} WordEntryIN;
@@ -80,16 +80,19 @@ uniquePos(WordEntryPos *a, int l)
8080
return res + 1 - a;
8181
}
8282

83-
/* Compare two WordEntryIN values for qsort */
83+
/*
84+
* Compare two WordEntry structs for qsort_arg. This can also be used on
85+
* WordEntryIN structs, since those have WordEntry as their first field.
86+
*/
8487
static int
8588
compareentry(const void *va, const void *vb, void *arg)
8689
{
87-
const WordEntryIN *a = (const WordEntryIN *) va;
88-
const WordEntryIN *b = (const WordEntryIN *) vb;
90+
const WordEntry *a = (const WordEntry *) va;
91+
const WordEntry *b = (const WordEntry *) vb;
8992
char *BufferStr = (char *) arg;
9093

91-
return tsCompareString(&BufferStr[a->entry.pos], a->entry.len,
92-
&BufferStr[b->entry.pos], b->entry.len,
94+
return tsCompareString(&BufferStr[a->pos], a->len,
95+
&BufferStr[b->pos], b->len,
9396
false);
9497
}
9598

@@ -168,12 +171,6 @@ uniqueentry(WordEntryIN *a, int l, char *buf, int *outbuflen)
168171
return res + 1 - a;
169172
}
170173

171-
static int
172-
WordEntryCMP(WordEntry *a, WordEntry *b, char *buf)
173-
{
174-
return compareentry(a, b, buf);
175-
}
176-
177174

178175
Datum
179176
tsvectorin(PG_FUNCTION_ARGS)
@@ -512,7 +509,7 @@ tsvectorrecv(PG_FUNCTION_ARGS)
512509

513510
datalen += lex_len;
514511

515-
if (i > 0 && WordEntryCMP(&vec->entries[i],
512+
if (i > 0 && compareentry(&vec->entries[i],
516513
&vec->entries[i - 1],
517514
STRPTR(vec)) <= 0)
518515
needSort = true;

0 commit comments

Comments
 (0)