summaryrefslogtreecommitdiff
path: root/contrib/jsonb_plpython
diff options
context:
space:
mode:
authorPeter Eisentraut <peter_e@gmx.net>2018-05-02 15:58:34 -0400
committerPeter Eisentraut <peter_e@gmx.net>2018-05-02 16:01:22 -0400
commit40f52b16dd31aa9ddc3bd42daa78459562693567 (patch)
tree5707874a456210194d089c4a59f53eb771d03bdd /contrib/jsonb_plpython
parent0996e4be047fbf476dacea5ffad42cdd3a36b731 (diff)
downloadpostgresql-40f52b16dd31aa9ddc3bd42daa78459562693567.tar.gz
Prevent NaN in jsonb/plpython transform
As in e348e7ae5727a6da8678036d748e5c5af7deb6c9 for jsonb/plperl, prevent putting a NaN into a jsonb numeric field. Tests for this had been removed in 6278a2a262b63faaf47eb2371f6bcb5b6e3ff118, but in case they are ever resurrected: This would change the output of the test1nan() function to an error.
Diffstat (limited to 'contrib/jsonb_plpython')
-rw-r--r--contrib/jsonb_plpython/jsonb_plpython.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/contrib/jsonb_plpython/jsonb_plpython.c b/contrib/jsonb_plpython/jsonb_plpython.c
index 645238f15b..f752d6c3cd 100644
--- a/contrib/jsonb_plpython/jsonb_plpython.c
+++ b/contrib/jsonb_plpython/jsonb_plpython.c
@@ -5,6 +5,7 @@
#include "plpy_typeio.h"
#include "utils/jsonb.h"
#include "utils/fmgrprotos.h"
+#include "utils/numeric.h"
PG_MODULE_MAGIC;
@@ -343,6 +344,16 @@ PLyNumber_ToJsonbValue(PyObject *obj, JsonbValue *jbvNum)
pfree(str);
+ /*
+ * jsonb doesn't allow NaN (per JSON specification), so we have to prevent
+ * it here explicitly. (Infinity is also not allowed in jsonb, but
+ * numeric_in above already catches that.)
+ */
+ if (numeric_is_nan(num))
+ ereport(ERROR,
+ (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+ (errmsg("cannot convert NaN to jsonb"))));
+
jbvNum->type = jbvNumeric;
jbvNum->val.numeric = num;