The jsonb_remove() routine now appears to be working.

FossilOrigin-Name: e76d48137ea823b7810dc8c3b70eb21adabdd6cfbac36050c85d1375e94be1de
diff --git a/manifest b/manifest
index f939886..911d7e5 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Work\stoward\sgetting\sjsonb_remove()\sto\swork\sdirectly\son\sJSONB\sblobs.
-D 2023-11-15T16:10:31.627
+C The\sjsonb_remove()\sroutine\snow\sappears\sto\sbe\sworking.
+D 2023-11-15T18:47:31.537
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -685,7 +685,7 @@
 F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6
 F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
 F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276
-F src/json.c 3ff1ea643cab62e3ddb8be3a64bff2dd94af37ea461ea343054796e7f11638f8
+F src/json.c a647820e1e82fd516df80328ef3372209006bf99b7008d156411c8cd0d4d9fc4
 F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
 F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36
 F src/main.c 1b89f3de98d1b59fec5bac1d66d6ece21f703821b8eaa0d53d9604c35309f6f9
@@ -2141,11 +2141,8 @@
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P ba91408f4c044feda003ef93784ccefb619f99ab64379ced481ee8e9e890fd41
-R 58cf82355c33a53c2234fea2c219b8e4
-T *branch * jsonb-remove
-T *sym-jsonb-remove *
-T -sym-jsonb *
+P a79ff8e58fcaf718a6fb78e145117f1d6d40d133f31e9752bb9c6d484850a27b
+R 6ab8908817380ee469413c76bca0c832
 U drh
-Z 367d226fdddcafa052b6fb6b4d87ed0a
+Z a103fcc805c4c7ff278e886cba60f9d7
 # Remove this line to create a well-formed Fossil manifest.
diff --git a/manifest.uuid b/manifest.uuid
index 7cf8c33..ebac114 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-a79ff8e58fcaf718a6fb78e145117f1d6d40d133f31e9752bb9c6d484850a27b
\ No newline at end of file
+e76d48137ea823b7810dc8c3b70eb21adabdd6cfbac36050c85d1375e94be1de
\ No newline at end of file
diff --git a/src/json.c b/src/json.c
index e66c4b1..fa7913d 100644
--- a/src/json.c
+++ b/src/json.c
@@ -2465,11 +2465,21 @@
 }
 
 /*
-** Return the text of a syntax error message on a JSON path.  Space is
-** obtained from sqlite3_malloc().
+** Compute the text of an error in JSON path syntax.
+**
+** If ctx is not NULL then push the error message into ctx and return NULL.
+*  If ctx is NULL, then return the text of the error message.
 */
-static char *jsonPathSyntaxError(const char *zErr){
-  return sqlite3_mprintf("JSON path error near '%q'", zErr);
+static char *jsonPathSyntaxError(const char *zErr, sqlite3_context *ctx){
+  char *zMsg = sqlite3_mprintf("JSON path error near '%q'", zErr);
+  if( ctx==0 ) return zMsg;
+  if( zMsg==0 ){
+    sqlite3_result_error_nomem(ctx);
+  }else{
+    sqlite3_result_error(ctx, zMsg, -1);
+    sqlite3_free(zMsg);
+  }
+  return 0;
 }
 
 /*
@@ -2490,7 +2500,6 @@
 ){
   const char *zErr = 0;
   JsonNode *pNode = 0;
-  char *zMsg;
 
   if( zPath==0 ) return 0;
   if( zPath[0]!='$' ){
@@ -2504,13 +2513,7 @@
 lookup_err:
   pParse->nErr++;
   assert( zErr!=0 && pCtx!=0 );
-  zMsg = jsonPathSyntaxError(zErr);
-  if( zMsg ){
-    sqlite3_result_error(pCtx, zMsg, -1);
-    sqlite3_free(zMsg);
-  }else{
-    sqlite3_result_error_nomem(pCtx);
-  }
+  jsonPathSyntaxError(zErr, pCtx);
   return 0;
 }
 
@@ -3814,11 +3817,15 @@
 */
 static void jsonAfterEditSizeAdjust(JsonParse *pParse, u32 iRoot){
   u32 sz = 0;
+  u32 nBlob;
   assert( pParse->delta!=0 );
+  assert( pParse->nBlobAlloc >= pParse->nBlob );
+  nBlob = pParse->nBlob;
+  pParse->nBlob = pParse->nBlobAlloc;
   (void)jsonbPayloadSize(pParse, iRoot, &sz);
+  pParse->nBlob = nBlob;
   sz += pParse->delta;
   jsonBlobChangePayloadSize(pParse, iRoot, sz);
-  pParse->nBlob += pParse->delta;
 }
 
 /*
@@ -3857,6 +3864,7 @@
         memmove(&pParse->aBlob[iRoot], &pParse->aBlob[iRoot+sz],
                 pParse->nBlob - (iRoot+sz));
         pParse->delta = -(int)sz;
+        pParse->nBlob -= sz;
       }else if( pParse->eEdit==JEDIT_INS ){
         /* Already exists, so json_insert() is a no-op */
       }else{
@@ -3871,6 +3879,7 @@
           memmove(&pParse->aBlob[iRoot+pParse->nIns],
                   &pParse->aBlob[iRoot+sz],
                   pParse->nBlob - iRoot - sz);
+          pParse->nBlob += d;
         }
         memcpy(&pParse->aBlob[iRoot], pParse->aIns, pParse->nIns);
       }
@@ -3981,6 +3990,7 @@
             pParse->nBlob - j);
     memcpy(&pParse->aBlob[j], pParse->aIns, pParse->nIns);
     pParse->delta = pParse->nIns;
+    pParse->nBlob += pParse->nIns;
     jsonAfterEditSizeAdjust(pParse, iRoot);
     return j;
   }
@@ -4254,6 +4264,7 @@
 ){
   int i;
   u32 rc;
+  const char *zPath = 0;
   JsonParse px;
   memset(&px, 0, sizeof(px));
   px.nBlob = sqlite3_value_bytes(argv[0]);
@@ -4261,21 +4272,25 @@
   if( px.aBlob==0 ) return;
   for(i=1; i<argc; i++){
     const char *zPath = (const char*)sqlite3_value_text(argv[i]);
-    if( zPath==0 ) goto jsonRemoveFromBlob_error;
-    if( zPath[0]!='$' ) goto jsonRemoveFromBlob_error;
+    if( zPath==0 ) goto jsonRemoveFromBlob_patherror;
+    if( zPath[0]!='$' ) goto jsonRemoveFromBlob_patherror;
+    if( zPath[1]==0 ){
+      if( px.nBlobAlloc ) sqlite3_free(px.aBlob);
+      return;  /* return NULL if $ is removed */
+    }
     px.eEdit = JEDIT_DEL;
     rc = jsonLookupBlobStep(&px, 0, zPath+1, 0);
     if( rc==JSON_BLOB_NOTFOUND ) continue;
-    if( JSON_BLOB_ISERROR(rc) ) goto jsonRemoveFromBlob_error;
+    if( JSON_BLOB_ISERROR(rc) ) goto jsonRemoveFromBlob_patherror;
   }
   sqlite3_result_blob(ctx, px.aBlob, px.nBlob,
-                      px.nBlobAlloc>0 ? sqlite3_free : SQLITE_DYNAMIC);
+                      px.nBlobAlloc>0 ? SQLITE_DYNAMIC : SQLITE_TRANSIENT);
   return;
 
-jsonRemoveFromBlob_error:
+jsonRemoveFromBlob_patherror:
   if( px.nBlobAlloc ) sqlite3_free(px.aBlob);
+  jsonPathSyntaxError(zPath, ctx);
   return;
-
 }
 
 /****************************************************************************
@@ -5814,7 +5829,7 @@
       }
       if( zErr ){
         sqlite3_free(cur->pVtab->zErrMsg);
-        cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
+        cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr, 0);
         jsonEachCursorReset(p);
         return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
       }else if( pNode==0 ){