From e348e7ae5727a6da8678036d748e5c5af7deb6c9 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Mon, 30 Apr 2018 12:28:45 -0400 Subject: Prevent infinity and NaN in jsonb/plperl transform MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit jsonb uses numeric internally, and numeric can store NaN, but that is not allowed by jsonb on input, so we shouldn't store it. Also prevent infinity to get a consistent error message. (numeric input would reject infinity anyway.) Reported-by: Dagfinn Ilmari Mannsåker Reviewed-by: Tom Lane --- contrib/jsonb_plperl/jsonb_plperl.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'contrib/jsonb_plperl/jsonb_plperl.c') diff --git a/contrib/jsonb_plperl/jsonb_plperl.c b/contrib/jsonb_plperl/jsonb_plperl.c index cde38b295c5..bde93a71fc0 100644 --- a/contrib/jsonb_plperl/jsonb_plperl.c +++ b/contrib/jsonb_plperl/jsonb_plperl.c @@ -211,10 +211,22 @@ SV_to_JsonbValue(SV *in, JsonbParseState **jsonb_state, bool is_elem) { double nval = SvNV(in); + /* + * jsonb doesn't allow infinity or NaN (per JSON + * specification), but the numeric type that is used for the + * storage accepts NaN, so we have to prevent it here + * explicitly. We don't really have to check for isinf() + * here, as numeric doesn't allow it and it would be caught + * later, but it makes for a nicer error message. + */ if (isinf(nval)) ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - (errmsg("cannot convert infinite value to jsonb")))); + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + (errmsg("cannot convert infinity to jsonb")))); + if (isnan(nval)) + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + (errmsg("cannot convert NaN to jsonb")))); out.type = jbvNumeric; out.val.numeric = -- cgit v1.2.3