Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit442e25d

Browse files
committed
Convert enum_in() to report errors softly.
I missed this in my initial survey, probably because I examinedthe contents of pg_type in the postgres database, which lacksany enumerated types.Discussion:https://postgr.es/m/CAAJ_b97KeDWUdpTKGOaFYPv0OicjOu6EW+QYWj-Ywrgj_aEy1g@mail.gmail.com
1 parent361ec43 commit442e25d

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

‎src/backend/utils/adt/enum.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ enum_in(PG_FUNCTION_ARGS)
110110
{
111111
char*name=PG_GETARG_CSTRING(0);
112112
Oidenumtypoid=PG_GETARG_OID(1);
113+
Node*escontext=fcinfo->context;
113114
Oidenumoid;
114115
HeapTupletup;
115116

116117
/* must check length to prevent Assert failure within SearchSysCache */
117118
if (strlen(name) >=NAMEDATALEN)
118-
ereport(ERROR,
119+
ereturn(escontext, (Datum)0,
119120
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
120121
errmsg("invalid input value for enum %s: \"%s\"",
121122
format_type_be(enumtypoid),
@@ -125,13 +126,18 @@ enum_in(PG_FUNCTION_ARGS)
125126
ObjectIdGetDatum(enumtypoid),
126127
CStringGetDatum(name));
127128
if (!HeapTupleIsValid(tup))
128-
ereport(ERROR,
129+
ereturn(escontext, (Datum)0,
129130
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
130131
errmsg("invalid input value for enum %s: \"%s\"",
131132
format_type_be(enumtypoid),
132133
name)));
133134

134-
/* check it's safe to use in SQL */
135+
/*
136+
* Check it's safe to use in SQL. Perhaps we should take the trouble to
137+
* report "unsafe use" softly; but it's unclear that it's worth the
138+
* trouble, or indeed that that is a legitimate bad-input case at all
139+
* rather than an implementation shortcoming.
140+
*/
135141
check_safe_enum_use(tup);
136142

137143
/*

‎src/test/regress/expected/enum.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ SELECT 'mauve'::rainbow;
2424
ERROR: invalid input value for enum rainbow: "mauve"
2525
LINE 1: SELECT 'mauve'::rainbow;
2626
^
27+
-- Also try it with non-error-throwing API
28+
SELECT pg_input_is_valid('red', 'rainbow');
29+
pg_input_is_valid
30+
-------------------
31+
t
32+
(1 row)
33+
34+
SELECT pg_input_is_valid('mauve', 'rainbow');
35+
pg_input_is_valid
36+
-------------------
37+
f
38+
(1 row)
39+
40+
SELECT pg_input_error_message('mauve', 'rainbow');
41+
pg_input_error_message
42+
-----------------------------------------------
43+
invalid input value for enum rainbow: "mauve"
44+
(1 row)
45+
46+
SELECT pg_input_error_message(repeat('too_long', 32), 'rainbow');
47+
pg_input_error_message
48+
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
49+
invalid input value for enum rainbow: "too_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_longtoo_long"
50+
(1 row)
51+
2752
--
2853
-- adding new values
2954
--

‎src/test/regress/sql/enum.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ SELECT COUNT(*) FROM pg_enum WHERE enumtypid = 'rainbow'::regtype;
1515
SELECT'red'::rainbow;
1616
SELECT'mauve'::rainbow;
1717

18+
-- Also try it with non-error-throwing API
19+
SELECT pg_input_is_valid('red','rainbow');
20+
SELECT pg_input_is_valid('mauve','rainbow');
21+
SELECT pg_input_error_message('mauve','rainbow');
22+
SELECT pg_input_error_message(repeat('too_long',32),'rainbow');
23+
1824
--
1925
-- adding new values
2026
--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp