Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit5d79fc6

Browse files
committed
Adjust bytea get_bit/set_bit to cope with bytea strings > 256MB.
Since the existing bit number argument can't exceed INT32_MAX, it'snot possible for these functions to manipulate bits beyond the first256MB of a bytea value. However, it'd be good if they could do atleast 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 coverageof these functions.Apply patch to back branches only; HEAD has a better solutionas of commit26a944c.Extracted from a much larger patch by Movead LiDiscussion:https://postgr.es/m/20200312115135445367128@highgo.ca
1 parent41faafb commit5d79fc6

File tree

3 files changed

+105
-4
lines changed

3 files changed

+105
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3081,11 +3081,12 @@ byteaGetBit(PG_FUNCTION_ARGS)
30813081

30823082
len=VARSIZE_ANY_EXHDR(v);
30833083

3084-
if (n<0||n >=len*8)
3084+
/* Do comparison arithmetic in int64 in case len exceeds INT_MAX/8 */
3085+
if (n<0||n >= (int64)len*8)
30853086
ereport(ERROR,
30863087
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
30873088
errmsg("index %d out of valid range, 0..%d",
3088-
n,len*8-1)));
3089+
n,(int)Min((int64)len*8-1,INT_MAX))));
30893090

30903091
byteNo=n /8;
30913092
bitNo=n %8;
@@ -3152,11 +3153,12 @@ byteaSetBit(PG_FUNCTION_ARGS)
31523153

31533154
len=VARSIZE(res)-VARHDRSZ;
31543155

3155-
if (n<0||n >=len*8)
3156+
/* Do comparison arithmetic in int64 in case len exceeds INT_MAX/8 */
3157+
if (n<0||n >= (int64)len*8)
31563158
ereport(ERROR,
31573159
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
31583160
errmsg("index %d out of valid range, 0..%d",
3159-
n,len*8-1)));
3161+
n,(int)Min((int64)len*8-1,INT_MAX))));
31603162

31613163
byteNo=n /8;
31623164
bitNo=n %8;

‎src/test/regress/expected/strings.out

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,82 @@ SELECT sha512('The quick brown fox jumps over the lazy dog.');
15091509
\x91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed
15101510
(1 row)
15111511

1512+
--
1513+
-- encode/decode
1514+
--
1515+
SELECT encode('\x1234567890abcdef00', 'hex');
1516+
encode
1517+
--------------------
1518+
1234567890abcdef00
1519+
(1 row)
1520+
1521+
SELECT decode('1234567890abcdef00', 'hex');
1522+
decode
1523+
----------------------
1524+
\x1234567890abcdef00
1525+
(1 row)
1526+
1527+
SELECT encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea, 'base64');
1528+
encode
1529+
------------------------------------------------------------------------------
1530+
EjRWeJCrze8AARI0VniQq83vAAESNFZ4kKvN7wABEjRWeJCrze8AARI0VniQq83vAAESNFZ4kKvN+
1531+
7wABEjRWeJCrze8AAQ==
1532+
(1 row)
1533+
1534+
SELECT decode(encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea,
1535+
'base64'), 'base64');
1536+
decode
1537+
------------------------------------------------------------------------------------------------------------------------------------------------
1538+
\x1234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef0001
1539+
(1 row)
1540+
1541+
SELECT encode('\x1234567890abcdef00', 'escape');
1542+
encode
1543+
-----------------------------
1544+
\x124Vx\220\253\315\357\000
1545+
(1 row)
1546+
1547+
SELECT decode(encode('\x1234567890abcdef00', 'escape'), 'escape');
1548+
decode
1549+
----------------------
1550+
\x1234567890abcdef00
1551+
(1 row)
1552+
1553+
--
1554+
-- get_bit/set_bit etc
1555+
--
1556+
SELECT get_bit('\x1234567890abcdef00'::bytea, 43);
1557+
get_bit
1558+
---------
1559+
1
1560+
(1 row)
1561+
1562+
SELECT get_bit('\x1234567890abcdef00'::bytea, 99); -- error
1563+
ERROR: index 99 out of valid range, 0..71
1564+
SELECT set_bit('\x1234567890abcdef00'::bytea, 43, 0);
1565+
set_bit
1566+
----------------------
1567+
\x1234567890a3cdef00
1568+
(1 row)
1569+
1570+
SELECT set_bit('\x1234567890abcdef00'::bytea, 99, 0); -- error
1571+
ERROR: index 99 out of valid range, 0..71
1572+
SELECT get_byte('\x1234567890abcdef00'::bytea, 3);
1573+
get_byte
1574+
----------
1575+
120
1576+
(1 row)
1577+
1578+
SELECT get_byte('\x1234567890abcdef00'::bytea, 99); -- error
1579+
ERROR: index 99 out of valid range, 0..8
1580+
SELECT set_byte('\x1234567890abcdef00'::bytea, 7, 11);
1581+
set_byte
1582+
----------------------
1583+
\x1234567890abcd0b00
1584+
(1 row)
1585+
1586+
SELECT set_byte('\x1234567890abcdef00'::bytea, 99, 11); -- error
1587+
ERROR: index 99 out of valid range, 0..8
15121588
--
15131589
-- test behavior of escape_string_warning and standard_conforming_strings options
15141590
--

‎src/test/regress/sql/strings.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,29 @@ SELECT sha384('The quick brown fox jumps over the lazy dog.');
526526
SELECT sha512('');
527527
SELECT sha512('The quick brown fox jumps over the lazy dog.');
528528

529+
--
530+
-- encode/decode
531+
--
532+
SELECT encode('\x1234567890abcdef00','hex');
533+
SELECT decode('1234567890abcdef00','hex');
534+
SELECT encode(('\x'|| repeat('1234567890abcdef0001',7))::bytea,'base64');
535+
SELECT decode(encode(('\x'|| repeat('1234567890abcdef0001',7))::bytea,
536+
'base64'),'base64');
537+
SELECT encode('\x1234567890abcdef00','escape');
538+
SELECT decode(encode('\x1234567890abcdef00','escape'),'escape');
539+
540+
--
541+
-- get_bit/set_bit etc
542+
--
543+
SELECT get_bit('\x1234567890abcdef00'::bytea,43);
544+
SELECT get_bit('\x1234567890abcdef00'::bytea,99);-- error
545+
SELECT set_bit('\x1234567890abcdef00'::bytea,43,0);
546+
SELECT set_bit('\x1234567890abcdef00'::bytea,99,0);-- error
547+
SELECT get_byte('\x1234567890abcdef00'::bytea,3);
548+
SELECT get_byte('\x1234567890abcdef00'::bytea,99);-- error
549+
SELECT set_byte('\x1234567890abcdef00'::bytea,7,11);
550+
SELECT set_byte('\x1234567890abcdef00'::bytea,99,11);-- error
551+
529552
--
530553
-- test behavior of escape_string_warning and standard_conforming_strings options
531554
--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp