From 66795962b59af6deab38b178a8a3b03f223c309a Mon Sep 17 00:00:00 2001 From: drh <> Date: Thu, 30 Nov 2023 19:06:27 +0000 Subject: [PATCH] Enhance json_set() and json_insert() so that they create missing substructure. FossilOrigin-Name: cc7a641ab5ae739d31c24f0ad0caeb15a481a63fa8f13720718ea922c25862ff --- manifest | 12 +++++----- manifest.uuid | 2 +- src/json.c | 63 +++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/manifest b/manifest index 2c2e1cf626..dd2fca3cd7 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Simplification\sof\sthe\snew\sJSON\sinsert/set\stest\scases. -D 2023-11-30T16:17:09.157 +C Enhance\sjson_set()\sand\sjson_insert()\sso\sthat\sthey\screate\smissing\nsubstructure. +D 2023-11-30T19:06:27.003 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -688,7 +688,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51 F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6 F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71 F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276 -F src/json.c 8b509dead923cd44f31e4866d1024f1b602bdf01c9d41a08cc36aa5f1203fdf8 +F src/json.c 92cc05dcf87cdace6fbf2a76e0cb2b030f04ddb1819e024af4ce8885261b4cef F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36 F src/main.c 1b89f3de98d1b59fec5bac1d66d6ece21f703821b8eaa0d53d9604c35309f6f9 @@ -2145,8 +2145,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 146c717c51940b2139befc45ac74e7a1c36ef3c32fd3cfe35b334488eebe6298 -R 8e1036414b3f5ed40f0248ecb25d6019 +P 04c0d5644372446c924a2e31a26edf51ddc563a1990d170b0ed4739e3e8b239b +R 4a2861d7ab7860f80e426ce44e32c50f U drh -Z df0518617355b4fc840dfe4615c492b7 +Z 1fffc27f10fd5ca429d5618946ebb513 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index b89e5d463a..7e844fc2a4 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -04c0d5644372446c924a2e31a26edf51ddc563a1990d170b0ed4739e3e8b239b \ No newline at end of file +cc7a641ab5ae739d31c24f0ad0caeb15a481a63fa8f13720718ea922c25862ff \ No newline at end of file diff --git a/src/json.c b/src/json.c index b3b4f993e6..b8b01bf452 100644 --- a/src/json.c +++ b/src/json.c @@ -3154,6 +3154,7 @@ static u32 jsonLookupBlobStep( u32 i, j, k, nKey, sz, n, iEnd, rc; const char *zKey; u8 x; + static const u8 emptyObject[] = { JSONB_ARRAY, JSONB_OBJECT }; if( zPath[0]==0 ){ if( pParse->eEdit && jsonBlobMakeEditable(pParse, pParse->nIns) ){ @@ -3225,25 +3226,44 @@ static u32 jsonLookupBlobStep( } if( j>iEnd ) return JSON_BLOB_ERROR; if( pParse->eEdit>=JEDIT_INS ){ - u32 nIns; - u8 aLabel[16]; - JsonParse ix; + u32 nIns; /* Total bytes to insert (label+value) */ + JsonParse v; /* BLOB encoding of the value to be inserted */ + JsonParse ix; /* Header of the label to be inserted */ + u8 aLabel[16]; /* Buffer space for ix */ testcase( pParse->eEdit==JEDIT_INS ); testcase( pParse->eEdit==JEDIT_SET ); memset(&ix, 0, sizeof(ix)); ix.aBlob = aLabel; ix.nBlobAlloc = sizeof(aLabel); jsonBlobAppendNodeType(&ix,JSONB_TEXTRAW, nKey); - if( jsonBlobMakeEditable(pParse, ix.nBlob+nKey+pParse->nIns) ){ - nIns = ix.nBlob + nKey + pParse->nIns; + memset(&v, 0, sizeof(v)); + if( zPath[i]==0 ){ + v.nBlob = pParse->nIns; + v.aBlob = pParse->aIns; + }else{ + v.nBlob = 1; + v.aBlob = (u8*)&emptyObject[zPath[i]=='.']; + v.eEdit = pParse->eEdit; + v.nIns = pParse->nIns; + v.aIns = pParse->aIns; + rc = jsonLookupBlobStep(&v, 0, &zPath[i], 0); + if( JSON_BLOB_ISERROR(rc) || v.oom ){ + pParse->oom |= v.oom; + jsonParseReset(&v); + return rc; + } + } + if( jsonBlobMakeEditable(pParse, ix.nBlob+nKey+v.nBlob) ){ + nIns = ix.nBlob + nKey + v.nBlob; jsonBlobEdit(pParse, j, 0, 0, nIns); memcpy(&pParse->aBlob[j], ix.aBlob, ix.nBlob); k = j + ix.nBlob; memcpy(&pParse->aBlob[k], zKey, nKey); k += nKey; - memcpy(&pParse->aBlob[k], pParse->aIns, pParse->nIns); + memcpy(&pParse->aBlob[k], v.aBlob, v.nBlob); if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot); } + jsonParseReset(&v); return j; } }else if( zPath[0]=='[' ){ @@ -3292,12 +3312,31 @@ static u32 jsonLookupBlobStep( } if( j>iEnd ) return JSON_BLOB_ERROR; if( k>0 ) return JSON_BLOB_NOTFOUND; - if( pParse->eEdit>=JEDIT_INS - && jsonBlobMakeEditable(pParse, pParse->nIns) - ){ + if( pParse->eEdit>=JEDIT_INS ){ + JsonParse v; testcase( pParse->eEdit==JEDIT_INS ); testcase( pParse->eEdit==JEDIT_SET ); - jsonBlobEdit(pParse, j, 0, pParse->aIns, pParse->nIns); + memset(&v, 0, sizeof(v)); + if( zPath[i+1]==0 ){ + v.aBlob = pParse->aIns; + v.nBlob = pParse->nIns; + }else{ + v.nBlob = 1; + v.aBlob = (u8*)&emptyObject[zPath[i+1]=='.']; + v.eEdit = pParse->eEdit; + v.nIns = pParse->nIns; + v.aIns = pParse->aIns; + rc = jsonLookupBlobStep(&v, 0, &zPath[i+1], 0); + if( JSON_BLOB_ISERROR(rc) || v.oom ){ + pParse->oom |= v.oom; + jsonParseReset(&v); + return rc; + } + } + if( jsonBlobMakeEditable(pParse, v.nBlob) ){ + jsonBlobEdit(pParse, j, 0, v.aBlob, v.nBlob); + } + jsonParseReset(&v); if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot); return j; } @@ -3754,6 +3793,10 @@ static void jsonReturnParse( JsonParse *p ){ int flgs; + if( p->oom ){ + sqlite3_result_error_nomem(ctx); + return; + } flgs = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx)); if( flgs & JSON_BLOB ){ sqlite3_result_blob(ctx, p->aBlob, p->nBlob, -- 2.39.5