1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-25 01:02:05 +03:00

Adjust bytea get_bit/set_bit to cope with bytea strings > 256MB.

Since the existing bit number argument can't exceed INT32_MAX, it's
not possible for these functions to manipulate bits beyond the first
256MB of a bytea value.  However, it'd be good if they could do at
least that much, and not fall over entirely for longer bytea values.
Adjust the comparisons to be done in int64 arithmetic so that works.
Also tweak the error reports to show sane values in case of overflow.

Also add some test cases to improve the miserable code coverage
of these functions.

Apply patch to back branches only; HEAD has a better solution
as of commit 26a944cf2.

Extracted from a much larger patch by Movead Li

Discussion: https://postgr.es/m/20200312115135445367128@highgo.ca
This commit is contained in:
Tom Lane
2020-04-07 16:30:55 -04:00
parent 41faafbd75
commit 5d79fc60c5
3 changed files with 105 additions and 4 deletions

View File

@ -3081,11 +3081,12 @@ byteaGetBit(PG_FUNCTION_ARGS)
len = VARSIZE_ANY_EXHDR(v);
if (n < 0 || n >= len * 8)
/* Do comparison arithmetic in int64 in case len exceeds INT_MAX/8 */
if (n < 0 || n >= (int64) len * 8)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("index %d out of valid range, 0..%d",
n, len * 8 - 1)));
n, (int) Min((int64) len * 8 - 1, INT_MAX))));
byteNo = n / 8;
bitNo = n % 8;
@ -3152,11 +3153,12 @@ byteaSetBit(PG_FUNCTION_ARGS)
len = VARSIZE(res) - VARHDRSZ;
if (n < 0 || n >= len * 8)
/* Do comparison arithmetic in int64 in case len exceeds INT_MAX/8 */
if (n < 0 || n >= (int64) len * 8)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("index %d out of valid range, 0..%d",
n, len * 8 - 1)));
n, (int) Min((int64) len * 8 - 1, INT_MAX))));
byteNo = n / 8;
bitNo = n % 8;