Skip to content

Commit 2c6a4f2

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 b9ec812 commit 2c6a4f2

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
@@ -22,7 +22,7 @@
2222

2323
typedef struct
2424
{
25-
WordEntry entry; /* must be first! */
25+
WordEntry entry; /* must be first, see compareentry */
2626
WordEntryPos *pos;
2727
int poslen; /* number of elements in pos */
2828
} WordEntryIN;
@@ -78,16 +78,19 @@ uniquePos(WordEntryPos *a, int l)
7878
return res + 1 - a;
7979
}
8080

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

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

@@ -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)
@@ -505,7 +502,7 @@ tsvectorrecv(PG_FUNCTION_ARGS)
505502

506503
datalen += lex_len;
507504

508-
if (i > 0 && WordEntryCMP(&vec->entries[i],
505+
if (i > 0 && compareentry(&vec->entries[i],
509506
&vec->entries[i - 1],
510507
STRPTR(vec)) <= 0)
511508
needSort = true;

0 commit comments

Comments
 (0)