Skip to content

Commit 3b76622

Browse files
committed
Convert contrib/intarray's bqarr_in() to report errors softly
Reviewed by Tom Lane and Amul Sul Discussion: https://postgr.es/m/49e598c2-cfe8-0928-b6fb-d0cc51aab626@dunslane.net
1 parent 24b55cd commit 3b76622

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

contrib/intarray/_int_bool.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef struct
3535
char *buf;
3636
int32 state;
3737
int32 count;
38+
struct Node *escontext;
3839
/* reverse polish notation in list (for temporary usage) */
3940
NODE *str;
4041
/* number in str */
@@ -179,7 +180,7 @@ makepol(WORKSTATE *state)
179180
else
180181
{
181182
if (lenstack == STACKDEPTH)
182-
ereport(ERROR,
183+
ereturn(state->escontext, ERR,
183184
(errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
184185
errmsg("statement too complex")));
185186
stack[lenstack] = val;
@@ -206,10 +207,9 @@ makepol(WORKSTATE *state)
206207
break;
207208
case ERR:
208209
default:
209-
ereport(ERROR,
210+
ereturn(state->escontext, ERR,
210211
(errcode(ERRCODE_SYNTAX_ERROR),
211212
errmsg("syntax error")));
212-
return ERR;
213213
}
214214
}
215215

@@ -483,6 +483,7 @@ bqarr_in(PG_FUNCTION_ARGS)
483483
ITEM *ptr;
484484
NODE *tmp;
485485
int32 pos = 0;
486+
struct Node *escontext = fcinfo->context;
486487

487488
#ifdef BS_DEBUG
488489
StringInfoData pbuf;
@@ -493,16 +494,18 @@ bqarr_in(PG_FUNCTION_ARGS)
493494
state.count = 0;
494495
state.num = 0;
495496
state.str = NULL;
497+
state.escontext = escontext;
496498

497499
/* make polish notation (postfix, but in reverse order) */
498-
makepol(&state);
500+
if (makepol(&state) == ERR)
501+
PG_RETURN_NULL();
499502
if (!state.num)
500-
ereport(ERROR,
503+
ereturn(escontext, (Datum) 0,
501504
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
502505
errmsg("empty query")));
503506

504507
if (state.num > QUERYTYPEMAXITEMS)
505-
ereport(ERROR,
508+
ereturn(escontext, (Datum) 0,
506509
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
507510
errmsg("number of query items (%d) exceeds the maximum allowed (%d)",
508511
state.num, (int) QUERYTYPEMAXITEMS)));

contrib/intarray/expected/_int.out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,21 @@ SELECT '1&(2&(4&(5|!6)))'::query_int;
398398
1 & 2 & 4 & ( 5 | !6 )
399399
(1 row)
400400

401+
-- test non-error-throwing input
402+
SELECT str as "query_int",
403+
pg_input_is_valid(str,'query_int') as ok,
404+
pg_input_error_message(str,'query_int') as errmsg
405+
FROM (VALUES ('1&(2&(4&(5|6)))'),
406+
('1#(2&(4&(5&6)))'),
407+
('foo'))
408+
AS a(str);
409+
query_int | ok | errmsg
410+
-----------------+----+--------------
411+
1&(2&(4&(5|6))) | t |
412+
1#(2&(4&(5&6))) | f | syntax error
413+
foo | f | syntax error
414+
(3 rows)
415+
401416
CREATE TABLE test__int( a int[] );
402417
\copy test__int from 'data/test__int.data'
403418
ANALYZE test__int;

contrib/intarray/sql/_int.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ SELECT '1&2&4&5&6'::query_int;
7575
SELECT '1&(2&(4&(5|6)))'::query_int;
7676
SELECT '1&(2&(4&(5|!6)))'::query_int;
7777

78+
-- test non-error-throwing input
79+
80+
SELECT str as "query_int",
81+
pg_input_is_valid(str,'query_int') as ok,
82+
pg_input_error_message(str,'query_int') as errmsg
83+
FROM (VALUES ('1&(2&(4&(5|6)))'),
84+
('1#(2&(4&(5&6)))'),
85+
('foo'))
86+
AS a(str);
87+
88+
7889

7990
CREATE TABLE test__int( a int[] );
8091
\copy test__int from 'data/test__int.data'

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