Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5b7b382

Browse filesBrowse files
committed
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
1 parent d8a0993 commit 5b7b382
Copy full SHA for 5b7b382

File tree

Expand file treeCollapse file tree

1 file changed

+2
-2
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+2
-2
lines changed

‎src/backend/utils/adt/tsvector.c

Copy file name to clipboardExpand all lines: src/backend/utils/adt/tsvector.c
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ tsvectorrecv(PG_FUNCTION_ARGS)
498498
* But make sure the buffer is large enough first.
499499
*/
500500
while (hdrlen + SHORTALIGN(datalen + lex_len) +
501-
(npos + 1) * sizeof(WordEntryPos) >= len)
501+
sizeof(uint16) + npos * sizeof(WordEntryPos) >= len)
502502
{
503503
len *= 2;
504504
vec = (TSVector) repalloc(vec, len);
@@ -544,7 +544,7 @@ tsvectorrecv(PG_FUNCTION_ARGS)
544544
elog(ERROR, "position information is misordered");
545545
}
546546

547-
datalen += (npos + 1) * sizeof(WordEntry);
547+
datalen += sizeof(uint16) + npos * sizeof(WordEntryPos);
548548
}
549549
}
550550

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.