Back out the json_check() routine. Instead, throw an error if the input to

a json function (other than json_valid()) is not valid JSON.

FossilOrigin-Name: dc9ce7b18cbe23d065317757234ef9fb8792da7a
This commit is contained in:
drh 2015-08-28 20:07:40 +00:00
parent 2798f0b54b
commit f2df7e71d6
4 changed files with 47 additions and 59 deletions

View File

@ -21,7 +21,9 @@
** This implementation parses JSON text at 250 MB/s, so it is hard to see
** how JSONB might improve on that.)
*/
#if !defined(_SQLITEINT_H_)
#include "sqlite3ext.h"
#endif
SQLITE_EXTENSION_INIT1
#include <assert.h>
#include <string.h>
@ -405,6 +407,20 @@ static void jsonRenderNode(
}
}
/*
** Return a JsonNode and all its descendents as a JSON string.
*/
static void jsonReturnJson(
JsonNode *pNode, /* Node to return */
sqlite3_context *pCtx, /* Return value for this function */
sqlite3_value **aReplace /* Array of replacement values */
){
JsonString s;
jsonInit(&s, pCtx);
jsonRenderNode(pNode, &s, aReplace);
jsonResult(&s);
}
/*
** Make the JsonNode the return value of the function.
*/
@ -509,10 +525,7 @@ static void jsonReturn(
}
case JSON_ARRAY:
case JSON_OBJECT: {
JsonString s;
jsonInit(&s, pCtx);
jsonRenderNode(pNode, &s, aReplace);
jsonResult(&s);
jsonReturnJson(pNode, pCtx, aReplace);
break;
}
}
@ -717,7 +730,13 @@ static int jsonParse(
if( zJson[i] ) i = -1;
}
if( i<0 ){
if( pParse->oom && pCtx!=0 ) sqlite3_result_error_nomem(pCtx);
if( pCtx!=0 ){
if( pParse->oom ){
sqlite3_result_error_nomem(pCtx);
}else{
sqlite3_result_error(pCtx, "malformed JSON", -1);
}
}
jsonParseReset(pParse);
return 1;
}
@ -960,7 +979,7 @@ static void jsonTest1Func(
){
JsonParse x; /* The parse */
if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
jsonReturn(x.aNode, ctx, 0);
jsonReturnJson(x.aNode, ctx, 0);
jsonParseReset(&x);
}
@ -1033,42 +1052,19 @@ static void jsonArrayLengthFunc(
}else{
zPath = 0;
}
if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0]))==0 ){
if( x.nNode ){
JsonNode *pNode = x.aNode;
if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0);
if( pNode->eType==JSON_ARRAY ){
assert( (pNode->jnFlags & JNODE_APPEND)==0 );
for(i=1; i<=pNode->n; n++){
i += jsonNodeSize(&pNode[i]);
}
if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
if( x.nNode ){
JsonNode *pNode = x.aNode;
if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0);
if( pNode->eType==JSON_ARRAY ){
assert( (pNode->jnFlags & JNODE_APPEND)==0 );
for(i=1; i<=pNode->n; n++){
i += jsonNodeSize(&pNode[i]);
}
}
jsonParseReset(&x);
}
if( !x.oom ) sqlite3_result_int64(ctx, n);
}
/*
** json_check(JSON)
**
** Check the JSON argument to verify that it is well-formed. Return a
** compacted version of the argument (with white-space removed) if the
** argument is well-formed. Through an error if the argument is not
** correctly formatted JSON.
*/
static void jsonCheckFunc(
sqlite3_context *ctx,
int argc,
sqlite3_value **argv
){
JsonParse x; /* The parse */
if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ){
sqlite3_result_error(ctx, "malformed JSON", -1);
return;
}
jsonReturn(x.aNode, ctx, argv);
jsonParseReset(&x);
sqlite3_result_int64(ctx, n);
}
/*
@ -1166,7 +1162,7 @@ static void jsonRemoveFunc(
if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
}
if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
jsonReturn(x.aNode, ctx, 0);
jsonReturnJson(x.aNode, ctx, 0);
}
}
jsonParseReset(&x);
@ -1214,7 +1210,7 @@ static void jsonReplaceFunc(
if( x.aNode[0].jnFlags & JNODE_REPLACE ){
sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
}else{
jsonReturn(x.aNode, ctx, argv);
jsonReturnJson(x.aNode, ctx, argv);
}
}
jsonParseReset(&x);
@ -1274,7 +1270,7 @@ static void jsonSetFunc(
if( x.aNode[0].jnFlags & JNODE_REPLACE ){
sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
}else{
jsonReturn(x.aNode, ctx, argv);
jsonReturnJson(x.aNode, ctx, argv);
}
}
jsonSetDone:
@ -1328,7 +1324,7 @@ static void jsonValidFunc(
JsonParse x; /* The parse */
int rc = 0;
if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0]))==0
if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0
&& x.nNode>0
){
rc = 1;
@ -1802,7 +1798,6 @@ int sqlite3_json_init(
{ "json_array", -1, 0, jsonArrayFunc },
{ "json_array_length", 1, 0, jsonArrayLengthFunc },
{ "json_array_length", 2, 0, jsonArrayLengthFunc },
{ "json_check", 1, 0, jsonCheckFunc },
{ "json_extract", 2, 0, jsonExtractFunc },
{ "json_insert", -1, 0, jsonSetFunc },
{ "json_object", -1, 0, jsonObjectFunc },

View File

@ -1,5 +1,5 @@
C Fix\scompiler\swarnings\sin\srbu\scode.
D 2015-08-28T16:41:45.530
C Back\sout\sthe\sjson_check()\sroutine.\s\sInstead,\sthrow\san\serror\sif\sthe\sinput\sto\na\sjson\sfunction\s(other\sthan\sjson_valid())\sis\snot\svalid\sJSON.
D 2015-08-28T20:07:40.320
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in e2218eb228374422969de7b1680eda6864affcef
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -192,7 +192,7 @@ F ext/misc/eval.c f971962e92ebb8b0a4e6b62949463ee454d88fa2
F ext/misc/fileio.c d4171c815d6543a9edef8308aab2951413cd8d0f
F ext/misc/fuzzer.c 4c84635c71c26cfa7c2e5848cf49fe2d2cfcd767
F ext/misc/ieee754.c b0362167289170627659e84173f5d2e8fee8566e
F ext/misc/json1.c 0b2bf3f2c3786fd6873b5f9a89023912c28ac889
F ext/misc/json1.c aa344845c2c211e8a7fd57ccd92901506dacdf5a
F ext/misc/nextchar.c 35c8b8baacb96d92abbb34a83a997b797075b342
F ext/misc/percentile.c bcbee3c061b884eccb80e21651daaae8e1e43c63
F ext/misc/regexp.c af92cdaa5058fcec1451e49becc7ba44dba023dc
@ -810,7 +810,7 @@ F test/journal3.test ff8af941f9e06161d3db1b46bb9f965ff0e7f307
F test/jrnlmode.test 7864d59cf7f6e552b9b99ba0f38acd167edc10fa
F test/jrnlmode2.test 81610545a4e6ed239ea8fa661891893385e23a1d
F test/jrnlmode3.test 556b447a05be0e0963f4311e95ab1632b11c9eaa
F test/json101.test 420fe3917c9a6a0fb7f967f1f641054509acd0e2
F test/json101.test 5dfb181790c123123c8e7981d4d3c941b6cc8af4
F test/keyword1.test 37ef6bba5d2ed5b07ecdd6810571de2956599dff
F test/lastinsert.test 42e948fd6442f07d60acbd15d33fb86473e0ef63
F test/laststmtchanges.test ae613f53819206b3222771828d024154d51db200
@ -1380,7 +1380,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P a84cf4f5d326270a61faf4ff867260f2dd1e68a6
R 25627e3633787096f1c6a6ac573b9337
U dan
Z 8fac2bb28cf0b676d514df6920f608e8
P 0fdc36fe35ae2fc8e9688fe6c53437f4d47502d9
R 72483a04f1004ed82fe324bdb4440aeb
U drh
Z 66ee5efb60acb350500ed2a1605628ef

View File

@ -1 +1 @@
0fdc36fe35ae2fc8e9688fe6c53437f4d47502d9
dc9ce7b18cbe23d065317757234ef9fb8792da7a

View File

@ -63,11 +63,4 @@ do_execsql_test json1-3.4 {
SELECT json_type(json_set('{"a":1,"b":2}','$$.b','{"x":3,"y":4}'),'$.b');
} {object}
do_execsql_test json1-4.1 {
SELECT json_check(' [ 1, 2] ');
} {[1,2]}
do_catchsql_test json1-4.2 {
SELECT json_check(' [ 1, 2 ');
} {1 {malformed JSON}}
finish_test