aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlexander Korotkov <akorotkov@postgresql.org>2020-07-11 03:21:00 +0300
committerAlexander Korotkov <akorotkov@postgresql.org>2020-07-11 03:21:57 +0300
commitf4ae676e3178124c2bc2b3a3530efe8f3cdbc442 (patch)
tree1ed49c6a229e21e8535417a8ffdb5af150f03900 /src
parent3ec5f6b53dfb6acfe66a952edef1b6887da91777 (diff)
downloadpostgresql-f4ae676e3178124c2bc2b3a3530efe8f3cdbc442.tar.gz
postgresql-f4ae676e3178124c2bc2b3a3530efe8f3cdbc442.zip
Forbid numeric NaN in jsonpath
SQL standard doesn't define numeric Inf or NaN values. It appears even more ridiculous to support then in jsonpath assuming JSON doesn't support these values as well. This commit forbids returning NaN from .double(), which was previously allowed. NaN can't be result of inner-jsonpath computation over non-NaNs. So, we can not expect NaN in the jsonpath output. Reported-by: Tom Lane Discussion: https://postgr.es/m/203949.1591879542%40sss.pgh.pa.us Author: Alexander Korotkov Reviewed-by: Tom Lane Backpatch-through: 12
Diffstat (limited to 'src')
-rw-r--r--src/backend/utils/adt/jsonb_util.c8
-rw-r--r--src/backend/utils/adt/jsonpath_exec.c15
-rw-r--r--src/test/regress/expected/jsonb_jsonpath.out12
3 files changed, 10 insertions, 25 deletions
diff --git a/src/backend/utils/adt/jsonb_util.c b/src/backend/utils/adt/jsonb_util.c
index 7969f6f5843..53a0bb48b92 100644
--- a/src/backend/utils/adt/jsonb_util.c
+++ b/src/backend/utils/adt/jsonb_util.c
@@ -1728,14 +1728,6 @@ convertJsonbScalar(StringInfo buffer, JEntry *jentry, JsonbValue *scalarVal)
break;
case jbvNumeric:
- /* replace numeric NaN with string "NaN" */
- if (numeric_is_nan(scalarVal->val.numeric))
- {
- appendToBuffer(buffer, "NaN", 3);
- *jentry = 3;
- break;
- }
-
numlen = VARSIZE_ANY(scalarVal->val.numeric);
padlen = padBufferToInt(buffer);
diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c
index d8f3632021e..a557ed1ce1e 100644
--- a/src/backend/utils/adt/jsonpath_exec.c
+++ b/src/backend/utils/adt/jsonpath_exec.c
@@ -975,15 +975,16 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
{
char *tmp = DatumGetCString(DirectFunctionCall1(numeric_out,
NumericGetDatum(jb->val.numeric)));
+ double val;
bool have_error = false;
- (void) float8in_internal_opt_error(tmp,
- NULL,
- "double precision",
- tmp,
- &have_error);
+ val = float8in_internal_opt_error(tmp,
+ NULL,
+ "double precision",
+ tmp,
+ &have_error);
- if (have_error)
+ if (have_error || isinf(val) || isnan(val))
RETURN_ERROR(ereport(ERROR,
(errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
errmsg("numeric argument of jsonpath item method .%s() is out of range for type double precision",
@@ -1004,7 +1005,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
tmp,
&have_error);
- if (have_error || isinf(val))
+ if (have_error || isinf(val) || isnan(val))
RETURN_ERROR(ereport(ERROR,
(errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
errmsg("string argument of jsonpath item method .%s() is not a valid representation of a double precision number",
diff --git a/src/test/regress/expected/jsonb_jsonpath.out b/src/test/regress/expected/jsonb_jsonpath.out
index 0c084a6c6c5..fb0fed11ac7 100644
--- a/src/test/regress/expected/jsonb_jsonpath.out
+++ b/src/test/regress/expected/jsonb_jsonpath.out
@@ -1500,17 +1500,9 @@ ERROR: string argument of jsonpath item method .double() is not a valid represe
select jsonb_path_query('1e1000', '$.double()');
ERROR: numeric argument of jsonpath item method .double() is out of range for type double precision
select jsonb_path_query('"nan"', '$.double()');
- jsonb_path_query
-------------------
- "NaN"
-(1 row)
-
+ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
select jsonb_path_query('"NaN"', '$.double()');
- jsonb_path_query
-------------------
- "NaN"
-(1 row)
-
+ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
select jsonb_path_query('"inf"', '$.double()');
ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
select jsonb_path_query('"-inf"', '$.double()');