Skip to content

Commit 50626ef

Browse files
committed
Fix 3-parameter form of bit substring() to throw error for negative length,
as required by SQL standard.
1 parent e4a6ebf commit 50626ef

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

src/backend/utils/adt/varbit.c

Lines changed: 27 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.62 2010/01/07 19:53:11 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/utils/adt/varbit.c,v 1.63 2010/01/07 20:17:43 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -23,6 +23,9 @@
2323

2424
#define HEXDIG(z) ((z)<10 ? ((z)+'0') : ((z)-10+'A'))
2525

26+
static VarBit *bitsubstring(VarBit *arg, int32 s, int32 l,
27+
bool length_not_specified);
28+
2629

2730
/* common code for bittypmodin and varbittypmodin */
2831
static int32
@@ -927,9 +930,23 @@ bitcat(PG_FUNCTION_ARGS)
927930
Datum
928931
bitsubstr(PG_FUNCTION_ARGS)
929932
{
930-
VarBit *arg = PG_GETARG_VARBIT_P(0);
931-
int32 s = PG_GETARG_INT32(1);
932-
int32 l = PG_GETARG_INT32(2);
933+
PG_RETURN_VARBIT_P(bitsubstring(PG_GETARG_VARBIT_P(0),
934+
PG_GETARG_INT32(1),
935+
PG_GETARG_INT32(2),
936+
false));
937+
}
938+
939+
Datum
940+
bitsubstr_no_len(PG_FUNCTION_ARGS)
941+
{
942+
PG_RETURN_VARBIT_P(bitsubstring(PG_GETARG_VARBIT_P(0),
943+
PG_GETARG_INT32(1),
944+
-1, true));
945+
}
946+
947+
static VarBit *
948+
bitsubstring(VarBit *arg, int32 s, int32 l, bool length_not_specified)
949+
{
933950
VarBit *result;
934951
int bitlen,
935952
rbitlen,
@@ -947,14 +964,17 @@ bitsubstr(PG_FUNCTION_ARGS)
947964
bitlen = VARBITLEN(arg);
948965
s1 = Max(s, 1);
949966
/* If we do not have an upper bound, use end of string */
950-
if (l < 0)
967+
if (length_not_specified)
951968
{
952969
e1 = bitlen + 1;
953970
}
954971
else
955972
{
956973
e = s + l;
957-
/* guard against overflow, even though we don't allow L<0 here */
974+
/*
975+
* A negative value for L is the only way for the end position
976+
* to be before the start. SQL99 says to throw an error.
977+
*/
958978
if (e < s)
959979
ereport(ERROR,
960980
(errcode(ERRCODE_SUBSTRING_ERROR),
@@ -1011,7 +1031,7 @@ bitsubstr(PG_FUNCTION_ARGS)
10111031
}
10121032
}
10131033

1014-
PG_RETURN_VARBIT_P(result);
1034+
return result;
10151035
}
10161036

10171037
/* bitlength, bitoctetlength

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.569 2010/01/06 05:18:18 momjian Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.570 2010/01/07 20:17:43 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201001061
56+
#define CATALOG_VERSION_NO 201001071
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.559 2010/01/05 01:06:56 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.560 2010/01/07 20:17:44 tgl Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.pl reads this file and generates .bki
@@ -2400,7 +2400,7 @@ DESCR("adjust varbit() to typmod length");
24002400

24012401
DATA(insert OID = 1698 ( position PGNSP PGUID 12 1 0 0 f f f t f i 2 0 23 "1560 1560" _null_ _null_ _null_ _null_ bitposition _null_ _null_ _null_ ));
24022402
DESCR("return position of sub-bitstring");
2403-
DATA(insert OID = 1699 ( substring PGNSP PGUID 14 1 0 0 f f f t f i 2 0 1560 "1560 23" _null_ _null_ _null_ _null_ "select pg_catalog.substring($1, $2, -1)" _null_ _null_ _null_ ));
2403+
DATA(insert OID = 1699 ( substring PGNSP PGUID 12 1 0 0 f f f t f i 2 0 1560 "1560 23" _null_ _null_ _null_ _null_ bitsubstr_no_len _null_ _null_ _null_ ));
24042404
DESCR("return portion of bitstring");
24052405

24062406

src/include/utils/varbit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/utils/varbit.h,v 1.29 2010/01/02 16:58:10 momjian Exp $
11+
* $PostgreSQL: pgsql/src/include/utils/varbit.h,v 1.30 2010/01/07 20:17:44 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -88,6 +88,7 @@ extern Datum bitshiftleft(PG_FUNCTION_ARGS);
8888
extern Datum bitshiftright(PG_FUNCTION_ARGS);
8989
extern Datum bitcat(PG_FUNCTION_ARGS);
9090
extern Datum bitsubstr(PG_FUNCTION_ARGS);
91+
extern Datum bitsubstr_no_len(PG_FUNCTION_ARGS);
9192
extern Datum bitlength(PG_FUNCTION_ARGS);
9293
extern Datum bitoctetlength(PG_FUNCTION_ARGS);
9394
extern Datum bitfromint4(PG_FUNCTION_ARGS);

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