Skip to content

Commit 90161da

Browse files
committed
Convert a few more datatype input functions to report errors softly.
Convert cash_in and uuid_in to the new style. Amul Sul, minor mods by me Discussion: https://postgr.es/m/CAAJ_b97KeDWUdpTKGOaFYPv0OicjOu6EW+QYWj-Ywrgj_aEy1g@mail.gmail.com
1 parent 47f3f97 commit 90161da

File tree

6 files changed

+58
-9
lines changed

6 files changed

+58
-9
lines changed

src/backend/utils/adt/cash.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Datum
9696
cash_in(PG_FUNCTION_ARGS)
9797
{
9898
char *str = PG_GETARG_CSTRING(0);
99+
Node *escontext = fcinfo->context;
99100
Cash result;
100101
Cash value = 0;
101102
Cash dec = 0;
@@ -209,7 +210,7 @@ cash_in(PG_FUNCTION_ARGS)
209210

210211
if (pg_mul_s64_overflow(value, 10, &value) ||
211212
pg_sub_s64_overflow(value, digit, &value))
212-
ereport(ERROR,
213+
ereturn(escontext, (Datum) 0,
213214
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
214215
errmsg("value \"%s\" is out of range for type %s",
215216
str, "money")));
@@ -234,7 +235,7 @@ cash_in(PG_FUNCTION_ARGS)
234235
{
235236
/* remember we build the value in the negative */
236237
if (pg_sub_s64_overflow(value, 1, &value))
237-
ereport(ERROR,
238+
ereturn(escontext, (Datum) 0,
238239
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
239240
errmsg("value \"%s\" is out of range for type %s",
240241
str, "money")));
@@ -244,7 +245,7 @@ cash_in(PG_FUNCTION_ARGS)
244245
for (; dec < fpoint; dec++)
245246
{
246247
if (pg_mul_s64_overflow(value, 10, &value))
247-
ereport(ERROR,
248+
ereturn(escontext, (Datum) 0,
248249
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
249250
errmsg("value \"%s\" is out of range for type %s",
250251
str, "money")));
@@ -271,7 +272,7 @@ cash_in(PG_FUNCTION_ARGS)
271272
else if (strncmp(s, csymbol, strlen(csymbol)) == 0)
272273
s += strlen(csymbol);
273274
else
274-
ereport(ERROR,
275+
ereturn(escontext, (Datum) 0,
275276
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
276277
errmsg("invalid input syntax for type %s: \"%s\"",
277278
"money", str)));
@@ -284,7 +285,7 @@ cash_in(PG_FUNCTION_ARGS)
284285
if (sgn > 0)
285286
{
286287
if (value == PG_INT64_MIN)
287-
ereport(ERROR,
288+
ereturn(escontext, (Datum) 0,
288289
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
289290
errmsg("value \"%s\" is out of range for type %s",
290291
str, "money")));

src/backend/utils/adt/uuid.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ typedef struct
3131
hyperLogLogState abbr_card; /* cardinality estimator */
3232
} uuid_sortsupport_state;
3333

34-
static void string_to_uuid(const char *source, pg_uuid_t *uuid);
34+
static void string_to_uuid(const char *source, pg_uuid_t *uuid, Node *escontext);
3535
static int uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2);
3636
static int uuid_fast_cmp(Datum x, Datum y, SortSupport ssup);
3737
static bool uuid_abbrev_abort(int memtupcount, SortSupport ssup);
@@ -44,7 +44,7 @@ uuid_in(PG_FUNCTION_ARGS)
4444
pg_uuid_t *uuid;
4545

4646
uuid = (pg_uuid_t *) palloc(sizeof(*uuid));
47-
string_to_uuid(uuid_str, uuid);
47+
string_to_uuid(uuid_str, uuid, fcinfo->context);
4848
PG_RETURN_UUID_P(uuid);
4949
}
5050

@@ -87,7 +87,7 @@ uuid_out(PG_FUNCTION_ARGS)
8787
* digits, is the only one used for output.)
8888
*/
8989
static void
90-
string_to_uuid(const char *source, pg_uuid_t *uuid)
90+
string_to_uuid(const char *source, pg_uuid_t *uuid, Node *escontext)
9191
{
9292
const char *src = source;
9393
bool braces = false;
@@ -130,7 +130,7 @@ string_to_uuid(const char *source, pg_uuid_t *uuid)
130130
return;
131131

132132
syntax_error:
133-
ereport(ERROR,
133+
ereturn(escontext,,
134134
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
135135
errmsg("invalid input syntax for type %s: \"%s\"",
136136
"uuid", source)));

src/test/regress/expected/money.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,31 @@ SELECT '($123,456.78)'::money;
331331
-$123,456.78
332332
(1 row)
333333

334+
-- test non-error-throwing API
335+
SELECT pg_input_is_valid('\x0001', 'money');
336+
pg_input_is_valid
337+
-------------------
338+
f
339+
(1 row)
340+
341+
SELECT pg_input_error_message('\x0001', 'money');
342+
pg_input_error_message
343+
-----------------------------------------------
344+
invalid input syntax for type money: "\x0001"
345+
(1 row)
346+
347+
SELECT pg_input_is_valid('192233720368547758.07', 'money');
348+
pg_input_is_valid
349+
-------------------
350+
f
351+
(1 row)
352+
353+
SELECT pg_input_error_message('192233720368547758.07', 'money');
354+
pg_input_error_message
355+
--------------------------------------------------------------
356+
value "192233720368547758.07" is out of range for type money
357+
(1 row)
358+
334359
-- documented minimums and maximums
335360
SELECT '-92233720368547758.08'::money;
336361
money

src/test/regress/expected/uuid.out

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ INSERT INTO guid1(guid_field) VALUES('11+11111-1111-1111-1111-111111111111');
3939
ERROR: invalid input syntax for type uuid: "11+11111-1111-1111-1111-111111111111"
4040
LINE 1: INSERT INTO guid1(guid_field) VALUES('11+11111-1111-1111-111...
4141
^
42+
-- test non-error-throwing API
43+
SELECT pg_input_is_valid('11', 'uuid');
44+
pg_input_is_valid
45+
-------------------
46+
f
47+
(1 row)
48+
49+
SELECT pg_input_error_message('11', 'uuid');
50+
pg_input_error_message
51+
------------------------------------------
52+
invalid input syntax for type uuid: "11"
53+
(1 row)
54+
4255
--inserting three input formats
4356
INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-1111-111111111111');
4457
INSERT INTO guid1(guid_field) VALUES('{22222222-2222-2222-2222-222222222222}');

src/test/regress/sql/money.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ SELECT '-9223372036854775808'::money;
8888
SELECT '(1)'::money;
8989
SELECT '($123,456.78)'::money;
9090

91+
-- test non-error-throwing API
92+
SELECT pg_input_is_valid('\x0001', 'money');
93+
SELECT pg_input_error_message('\x0001', 'money');
94+
SELECT pg_input_is_valid('192233720368547758.07', 'money');
95+
SELECT pg_input_error_message('192233720368547758.07', 'money');
96+
9197
-- documented minimums and maximums
9298
SELECT '-92233720368547758.08'::money;
9399
SELECT '92233720368547758.07'::money;

src/test/regress/sql/uuid.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ INSERT INTO guid1(guid_field) VALUES('{22222222-2222-2222-2222-222222222222 ');
2323
INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-G111-111111111111');
2424
INSERT INTO guid1(guid_field) VALUES('11+11111-1111-1111-1111-111111111111');
2525

26+
-- test non-error-throwing API
27+
SELECT pg_input_is_valid('11', 'uuid');
28+
SELECT pg_input_error_message('11', 'uuid');
29+
2630
--inserting three input formats
2731
INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-1111-111111111111');
2832
INSERT INTO guid1(guid_field) VALUES('{22222222-2222-2222-2222-222222222222}');

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