Hello hackers,

I found an issue about get_bit() and set_bit() function,here it is:
################################
postgres=# select 
get_bit(pg_read_binary_file('/home/movead/temp/file_seek/f_512M'), 0);
2020-03-12 10:05:23.296 CST [10549] ERROR:  index 0 out of valid range, 0..-1
2020-03-12 10:05:23.296 CST [10549] STATEMENT:  select 
get_bit(pg_read_binary_file('/home/movead/temp/file_seek/f_512M'), 0);
ERROR:  index 0 out of valid range, 0..-1
postgres=# select 
set_bit(pg_read_binary_file('/home/movead/temp/file_seek/f_512M'), 0,1);
2020-03-12 10:05:27.959 CST [10549] ERROR:  index 0 out of valid range, 0..-1
2020-03-12 10:05:27.959 CST [10549] STATEMENT:  select 
set_bit(pg_read_binary_file('/home/movead/temp/file_seek/f_512M'), 0,1);
ERROR:  index 0 out of valid range, 0..-1
postgres=#
################################
PostgreSQL can handle bytea size nearby 1G, but now it reports an
error when 512M. And I research it and found it is byteaSetBit() and
byteaGetBit(), it uses an 'int32 len' to hold bit numbers for the long
bytea data, and obvious 512M * 8bit is an overflow for an int32. 
So I fix it and test ok, as below.
################################
postgres=# select 
get_bit(set_bit(pg_read_binary_file('/home/movead/temp/file_seek/f_512M'), 
0,1),0); get_bit --------- 1 (1 row) postgres=# select 
get_bit(set_bit(pg_read_binary_file('/home/movead/temp/file_seek/f_512M'), 
0,0),0); get_bit --------- 0 (1 row) postgres=#
################################


And I do a check about if anything else related bytea has this issue, several 
codes have the same issue:
1. byteaout() When formatting bytea as an escape, the 'len' variable should be 
int64, or
it may use an overflowing number. 2. esc_enc_len() Same as above, the 'len' 
variable should be int64, and the return type
should change as int64. Due to esc_enc_len() has same call struct with 
pg_base64_enc_len() and hex_enc_len(), so I want to change the return value of 
the two function. And the opposite function esc_dec_len() seem nothing wrong. 
3. binary_encode() and binary_decode() Here use an 'int32 resultlen' to accept 
an 'unsigned int' function return, which seem unfortable.
I fix all mentioned above, and patch attachments.
How do you think about that?




Highgo Software (Canada/China/Pakistan) 
URL : www.highgo.ca 
EMAIL: mailto:movead(dot)li(at)highgo(dot)ca

Attachment: long_bytea_string_bug_fix.patch
Description: Binary data

Reply via email to