Skip to content

Commit a7187c3

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 <[email protected]> Author: Tom Lane <[email protected]> Discussion: https://postgr.es/m/[email protected] Backpatch-through: 13
1 parent 24da5b2 commit a7187c3

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/backend/utils/adt/tsvector.c

+10-13
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

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

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

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

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

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

177174
Datum
178175
tsvectorin(PG_FUNCTION_ARGS)
@@ -511,7 +508,7 @@ tsvectorrecv(PG_FUNCTION_ARGS)
511508

512509
datalen += lex_len;
513510

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

0 commit comments

Comments
 (0)