Skip to content

Commit e4a6ebf

Browse files
committed
Make bit/varbit substring() treat any negative length as meaning "all the rest
of the string". The previous coding treated only -1 that way, and would produce an invalid result value for other negative values. We ought to fix it so that 2-parameter bit substring() is a different C function and the 3-parameter form throws error for negative length, but that takes a pg_proc change which is impractical in the back branches; and in any case somebody might be relying on -1 working this way. So just do this as a back-patchable fix.
1 parent 82170c7 commit e4a6ebf

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/backend/utils/adt/varbit.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/utils/adt/varbit.c,v 1.61 2010/01/07 04:53:34 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/utils/adt/varbit.c,v 1.62 2010/01/07 19:53:11 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -945,13 +945,23 @@ bitsubstr(PG_FUNCTION_ARGS)
945945
*ps;
946946

947947
bitlen = VARBITLEN(arg);
948-
/* If we do not have an upper bound, set bitlen */
949-
if (l == -1)
950-
l = bitlen;
951-
e = s + l;
952948
s1 = Max(s, 1);
953-
e1 = Min(e, bitlen + 1);
954-
if (s1 > bitlen || e1 < 1)
949+
/* If we do not have an upper bound, use end of string */
950+
if (l < 0)
951+
{
952+
e1 = bitlen + 1;
953+
}
954+
else
955+
{
956+
e = s + l;
957+
/* guard against overflow, even though we don't allow L<0 here */
958+
if (e < s)
959+
ereport(ERROR,
960+
(errcode(ERRCODE_SUBSTRING_ERROR),
961+
errmsg("negative substring length not allowed")));
962+
e1 = Min(e, bitlen + 1);
963+
}
964+
if (s1 > bitlen || e1 <= s1)
955965
{
956966
/* Need to return a zero-length bitstring */
957967
len = VARBITTOTALLEN(0);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy