Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XvBFi-0005wS-2C for pgsql-bugs@arkaria.postgresql.org; Sun, 30 Nov 2014 20:37:54 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XvBFh-0007A5-9l for pgsql-bugs@arkaria.postgresql.org; Sun, 30 Nov 2014 20:37:53 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XvBFf-00079u-OP; Sun, 30 Nov 2014 20:37:51 +0000 Received: from relay6-d.mail.gandi.net ([2001:4b98:c:538::198]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XvBFX-0002v3-TS; Sun, 30 Nov 2014 20:37:49 +0000 Received: from mfilter37-d.gandi.net (mfilter37-d.gandi.net [217.70.178.168]) by relay6-d.mail.gandi.net (Postfix) with ESMTP id 1BC75FB881; Sun, 30 Nov 2014 21:37:40 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mfilter37-d.gandi.net Received: from relay6-d.mail.gandi.net ([217.70.183.198]) by mfilter37-d.gandi.net (mfilter37-d.gandi.net [10.0.15.180]) (amavisd-new, port 10024) with ESMTP id VTAKTeSJPNwl; Sun, 30 Nov 2014 21:37:38 +0100 (CET) X-Originating-IP: 98.27.58.255 Received: from [192.168.10.146] (cpe-098-027-058-255.nc.res.rr.com [98.27.58.255]) (Authenticated sender: adsend@dunslane.net) by relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 4A6B3FB882; Sun, 30 Nov 2014 21:37:36 +0100 (CET) Message-ID: <547B800F.60309@dunslane.net> Date: Sun, 30 Nov 2014 15:37:35 -0500 From: Andrew Dunstan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: Tom Lane CC: bouda@edookit.com, pgsql-bugs@postgresql.org, pgsql-hackers@postgresql.org Subject: Re: [HACKERS] BUG #12070: hstore extension: hstore_to_json_loose produces invalid JSON References: <20141126081059.15510.5034@wrigleys.postgresql.org> <15817.1417018789@sss.pgh.pa.us> <54760440.2080501@dunslane.net> <547B3FC4.8080509@dunslane.net> <9580.1417365900@sss.pgh.pa.us> In-Reply-To: <9580.1417365900@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------020100090300010103050405" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------020100090300010103050405 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit On 11/30/2014 11:45 AM, Tom Lane wrote: > Andrew Dunstan writes: >> what do you want to do about this? In the back branches, exposing a >> function like this would be an API change, wouldn't it? Perhaps there we >> just need to pick up the 100 lines or so involved from json.c and copy >> them into hstore_io.c, suitably modified. In the development branch I >> thing adding the function to the API is the best way. > If we're going to do it by calling some newly-exposed function, I'd be > inclined to fix it the same way in the back branches. Otherwise the > discrepancy between the branches is a big back-patching hazard. > (For instance, if we realize we need to fix a bug in the numeric-parsing > code, what are the odds that we remember to fix hstore's additional copy > in the back branches?) > > The "API break" isn't a big issue imo. The net effect would be that eg > hstore 9.3.6 wouldn't work against a 9.3.5 server. We do that sort of > thing *all the time* --- at least twice in the past year, according to > a quick scan of the commit logs. If you were changing or removing a > function that third-party code might depend on, it'd be problematic, > but an addition has no such risk. > > OK, here's the patch. cheers andrew --------------020100090300010103050405 Content-Type: text/x-diff; name="jsonnumberfix.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="jsonnumberfix.patch" diff --git a/contrib/hstore/hstore_io.c b/contrib/hstore/hstore_io.c index 6ce3047..ee3d696 100644 --- a/contrib/hstore/hstore_io.c +++ b/contrib/hstore/hstore_io.c @@ -1240,7 +1240,6 @@ hstore_to_json_loose(PG_FUNCTION_ARGS) int count = HS_COUNT(in); char *base = STRPTR(in); HEntry *entries = ARRPTR(in); - bool is_number; StringInfoData tmp, dst; @@ -1267,48 +1266,9 @@ hstore_to_json_loose(PG_FUNCTION_ARGS) appendStringInfoString(&dst, "false"); else { - is_number = false; resetStringInfo(&tmp); appendBinaryStringInfo(&tmp, HS_VAL(entries, base, i), HS_VALLEN(entries, i)); - - /* - * don't treat something with a leading zero followed by another - * digit as numeric - could be a zip code or similar - */ - if (tmp.len > 0 && - !(tmp.data[0] == '0' && - isdigit((unsigned char) tmp.data[1])) && - strspn(tmp.data, "+-0123456789Ee.") == tmp.len) - { - /* - * might be a number. See if we can input it as a numeric - * value. Ignore any actual parsed value. - */ - char *endptr = "junk"; - long lval; - - lval = strtol(tmp.data, &endptr, 10); - (void) lval; - if (*endptr == '\0') - { - /* - * strol man page says this means the whole string is - * valid - */ - is_number = true; - } - else - { - /* not an int - try a double */ - double dval; - - dval = strtod(tmp.data, &endptr); - (void) dval; - if (*endptr == '\0') - is_number = true; - } - } - if (is_number) + if (IsValidJsonNumber(tmp.data, tmp.len)) appendBinaryStringInfo(&dst, tmp.data, tmp.len); else escape_json(&dst, tmp.data); diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c index d2bf640..271a2a4 100644 --- a/src/backend/utils/adt/json.c +++ b/src/backend/utils/adt/json.c @@ -173,6 +173,31 @@ lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token) (c) == '_' || \ IS_HIGHBIT_SET(c)) +/* utility function to check if a string is a valid JSON number */ +extern bool +IsValidJsonNumber(char * str, int len) +{ + bool numeric_error; + JsonLexContext dummy_lex; + + + /* json_lex_number expects a leading - to have been eaten already */ + if (*str == '-') + { + dummy_lex.input = str + 1; + dummy_lex.input_length = len - 1; + } + else + { + dummy_lex.input = str; + dummy_lex.input_length = len; + } + + json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error); + + return ! numeric_error; +} + /* * Input. */ @@ -1338,8 +1363,6 @@ datum_to_json(Datum val, bool is_null, StringInfo result, { char *outputstr; text *jsontext; - bool numeric_error; - JsonLexContext dummy_lex; /* callers are expected to ensure that null keys are not passed in */ Assert( ! (key_scalar && is_null)); @@ -1376,25 +1399,14 @@ datum_to_json(Datum val, bool is_null, StringInfo result, break; case JSONTYPE_NUMERIC: outputstr = OidOutputFunctionCall(outfuncoid, val); - if (key_scalar) - { - /* always quote keys */ - escape_json(result, outputstr); - } + /* + * Don't call escape_json for a non-key if it's a valid JSON + * number. + */ + if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr))) + appendStringInfoString(result, outputstr); else - { - /* - * Don't call escape_json for a non-key if it's a valid JSON - * number. - */ - dummy_lex.input = *outputstr == '-' ? outputstr + 1 : outputstr; - dummy_lex.input_length = strlen(dummy_lex.input); - json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error); - if (!numeric_error) - appendStringInfoString(result, outputstr); - else - escape_json(result, outputstr); - } + escape_json(result, outputstr); pfree(outputstr); break; case JSONTYPE_DATE: diff --git a/src/include/utils/jsonapi.h b/src/include/utils/jsonapi.h index df05712..b3a579b 100644 --- a/src/include/utils/jsonapi.h +++ b/src/include/utils/jsonapi.h @@ -117,4 +117,7 @@ extern JsonLexContext *makeJsonLexContextCstringLen(char *json, int len, bool need_escapes); +/* utility function to check if a string is a valid JSON number */ +extern bool IsValidJsonNumber(char * str, int len); + #endif /* JSONAPI_H */ --------------020100090300010103050405 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------020100090300010103050405--