aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2023-10-01 13:16:47 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2023-10-01 13:16:47 -0400
commit5b7b3824648d6324f649bc74713a6b35e53b91ac (patch)
treeecda9a959bbe358ba02a44dacb524a714f99e125 /src
parentd8a09939a3e830c7e2c3a68925d5f4c298dc3336 (diff)
downloadpostgresql-5b7b3824648d6324f649bc74713a6b35e53b91ac.tar.gz
postgresql-5b7b3824648d6324f649bc74713a6b35e53b91ac.zip
Fix datalen calculation in tsvectorrecv().
After receiving position data for a lexeme, tsvectorrecv() advanced its "datalen" value by (npos+1)*sizeof(WordEntry) where the correct calculation is (npos+1)*sizeof(WordEntryPos). This accidentally failed to render the constructed tsvector invalid, but it did result in leaving some wasted space approximately equal to the space consumed by the position data. That could have several bad effects: * Disk space is wasted if the received tsvector is stored into a table as-is. * A legal tsvector could get rejected with "maximum total lexeme length exceeded" if the extra space pushes it over the MAXSTRPOS limit. * In edge cases, the finished tsvector could be assigned a length larger than the allocated size of its palloc chunk, conceivably leading to SIGSEGV when the tsvector gets copied somewhere else. The odds of a field failure of this sort seem low, though valgrind testing could probably have found this. While we're here, let's express the calculation as "sizeof(uint16) + npos * sizeof(WordEntryPos)" to avoid the type pun implicit in the "npos + 1" formulation. It's not wrong given that WordEntryPos had better be 2 bytes to avoid padding problems, but it seems clearer this way. Report and patch by Denis Erokhin. Back-patch to all supported versions. Discussion: https://postgr.es/m/009801d9f2d9$f29730c0$d7c59240$@datagile.ru
Diffstat (limited to 'src')
-rw-r--r--src/backend/utils/adt/tsvector.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index 0e66f362c30..dff0bfe41fc 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -498,7 +498,7 @@ tsvectorrecv(PG_FUNCTION_ARGS)
* But make sure the buffer is large enough first.
*/
while (hdrlen + SHORTALIGN(datalen + lex_len) +
- (npos + 1) * sizeof(WordEntryPos) >= len)
+ sizeof(uint16) + npos * sizeof(WordEntryPos) >= len)
{
len *= 2;
vec = (TSVector) repalloc(vec, len);
@@ -544,7 +544,7 @@ tsvectorrecv(PG_FUNCTION_ARGS)
elog(ERROR, "position information is misordered");
}
- datalen += (npos + 1) * sizeof(WordEntry);
+ datalen += sizeof(uint16) + npos * sizeof(WordEntryPos);
}
}