]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Continuing simplifications and code cleanup.
authordrh <>
Mon, 4 Dec 2023 17:40:28 +0000 (17:40 +0000)
committerdrh <>
Mon, 4 Dec 2023 17:40:28 +0000 (17:40 +0000)
FossilOrigin-Name: ddf92b5059a9106753fd18b82ba8daa269a62af947561c460790107b83416f0b

manifest
manifest.uuid
src/json.c

index 71f63d320e85c461178317de00a3857a928aa9ab..7f065bdc52414698c379b28899e8326a1b91bd46 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Remove\sreachable\sALWAYS\sand\sNEVER\smacros.
-D 2023-12-04T16:01:39.513
+C Continuing\ssimplifications\sand\scode\scleanup.
+D 2023-12-04T17:40:28.698
 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 486643d34e35a9d33c88ba13810119eb08edf8f7585378379b2880ab2f20884f
+F src/json.c 0e9fc8f2f2fecb1b4e7904ead93aa98313483ca4fea1d564e252e445f717b226
 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 52632c92cb06faf0e804654b3490fd6c199521107bd30c8fcbc3a2a5a488098f
-R e34d44ceb9e1b582fec5d492940f4291
+P f601de3eeabd85993c1f5ee96b62de6fdabbeae2fe8950e00d08feb48d42c498
+R c9ebf0dbf5357376ee89626a26211c09
 U drh
-Z 6c24af6356a01e718b1f63d8aebe720b
+Z 2f59464d9d28542ea091e094068881ef
 # Remove this line to create a well-formed Fossil manifest.
index feb883af6cea8ea5d37036d1a63c5d143fa05464..b3d16e9d7b37e4ef2e39d8d8182d0a35a792fd03 100644 (file)
@@ -1 +1 @@
-f601de3eeabd85993c1f5ee96b62de6fdabbeae2fe8950e00d08feb48d42c498
\ No newline at end of file
+ddf92b5059a9106753fd18b82ba8daa269a62af947561c460790107b83416f0b
\ No newline at end of file
index 10470e62df510c668ace7d2fec4b1163b52fd219..dc884b9fd3b870358f0d621f76511e97a0608f57 100644 (file)
@@ -2029,7 +2029,7 @@ static int jsonFuncArgMightBeBinary(sqlite3_value *pJson){
   const u8 *aBlob;
   int nBlob;
   JsonParse s;
-  assert( sqlite3_value_type(pJson)==SQLITE_BLOB );
+  if( sqlite3_value_type(pJson)!=SQLITE_BLOB ) return 0;
   aBlob = sqlite3_value_blob(pJson);
   nBlob = sqlite3_value_bytes(pJson);
   if( nBlob<1 ) return 0;
@@ -3555,36 +3555,39 @@ static void jsonRemoveFunc(
   p = jsonParseFuncArg(ctx, argv[0], JSON_EDITABLE);
   if( p==0 ) return;
   for(i=1; i<argc; i++){
-    if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
-      goto json_remove_return_null;
-    }
     zPath = (const char*)sqlite3_value_text(argv[i]);
     if( zPath==0 ){
-      goto json_remove_patherror;
+      goto json_remove_done;
     }
     if( zPath[0]!='$' ){
       goto json_remove_patherror;
     }
     if( zPath[1]==0 ){
       /* json_remove(j,'$') returns NULL */
-      goto json_remove_return_null;
+      goto json_remove_done;
     }
     p->eEdit = JEDIT_DEL;
     p->delta = 0;
     rc = jsonLookupStep(p, 0, zPath+1, 0);
-    if( rc==JSON_LOOKUP_NOTFOUND ) continue;
-    if( JSON_LOOKUP_ISERROR(rc) ) goto json_remove_patherror;
+    if( JSON_LOOKUP_ISERROR(rc) ){
+      if( rc==JSON_LOOKUP_NOTFOUND ){
+        continue;  /* No-op */
+      }else if( rc==JSON_LOOKUP_PATHERROR ){
+        jsonBadPathError(ctx, zPath);
+      }else{
+        sqlite3_result_error(ctx, "malformed JSON", -1);
+      }
+      goto json_remove_done;
+    }
   }
   jsonReturnParse(ctx, p);
   jsonParseFree(p);
   return;
 
 json_remove_patherror:
-  jsonParseFree(p);
   jsonBadPathError(ctx, zPath);
-  return;
 
-json_remove_return_null:
+json_remove_done:
   jsonParseFree(p);
   return;
 }
@@ -3657,18 +3660,8 @@ static void jsonTypeFunc(
   p = jsonParseFuncArg(ctx, argv[0], 0);
   if( p==0 ) return;
   if( argc==2 ){
-    if( sqlite3_value_type(argv[1])==SQLITE_NULL ){
-      goto json_type_done;
-    }
-    if( sqlite3_value_bytes(argv[1])==0 ){
-      jsonBadPathError(ctx, "");
-      goto json_type_done;
-    }
     zPath = (const char*)sqlite3_value_text(argv[1]);
-    if( zPath==0 ){
-      sqlite3_result_error_nomem(ctx);
-      goto json_type_done;
-    }
+    if( zPath==0 ) goto json_type_done;
     if( zPath[0]!='$' ){
       jsonBadPathError(ctx, zPath);
       goto json_type_done;
@@ -3841,17 +3834,14 @@ static void jsonValidFunc(
 **
 ** If the argument is NULL, return NULL
 **
-** If the argument is TEXT then try to interpret that text as JSON and
+** If the argument is BLOB, do a fast validity check and return non-zero
+** if the check fails.  The returned value does not indicate where in the
+** BLOB the error occurs.
+**
+** Otherwise interpret the argument is TEXT (even if it is numeric) and
 ** return the 1-based character position for where the parser first recognized
 ** that the input was not valid JSON, or return 0 if the input text looks
 ** ok.  JSON-5 extensions are accepted.
-**
-** If the argument is a BLOB then try to interpret the blob as a JSONB
-** and return the 1-based byte offset of the first position that is
-** misformatted.  Return 0 if the input BLOB seems to be well-formed.
-**
-** Numeric inputs are converted into text, which is usually valid
-** JSON-5, so they should return 0.
 */
 static void jsonErrorFunc(
   sqlite3_context *ctx,
@@ -3863,46 +3853,42 @@ static void jsonErrorFunc(
 
   assert( argc==1 );
   UNUSED_PARAMETER(argc);
-  switch( sqlite3_value_type(argv[0]) ){
-    case SQLITE_NULL: {
-      return;
-    }
-    case SQLITE_BLOB: {
-      if( !jsonFuncArgMightBeBinary(argv[0]) ) iErrPos = 1;
-      break;
-    }
-    default: {
-      memset(&s, 0, sizeof(s));
-      s.zJson = (char*)sqlite3_value_text(argv[0]);
-      s.nJson = sqlite3_value_bytes(argv[0]);
-      if( s.nJson==0 ){
-        iErrPos = 1;
-        break;
-      }
-      if( s.zJson==0 ){
-        sqlite3_result_error_nomem(ctx);
-        return;
-      }
-      if( jsonConvertTextToBlob(&s,0) ){
-        u32 k;
-        if( s.oom ){
-          sqlite3_result_error_nomem(ctx);
-          jsonParseReset(&s);
-          return;
-        }
+  memset(&s, 0, sizeof(s));
+  if( jsonFuncArgMightBeBinary(argv[0]) ){
+    JsonString out;
+    jsonStringInit(&out, 0);
+    s.aBlob = (u8*)sqlite3_value_blob(argv[0]);
+    s.nBlob = sqlite3_value_bytes(argv[0]);
+    jsonXlateBlobToText(&s, 0, &out);
+    if( out.eErr ){
+      iErrPos = (out.eErr & JSTRING_MALFORMED)!=0 ? 1 : -1;
+    }
+    jsonStringReset(&out);
+  }else{
+    s.zJson = (char*)sqlite3_value_text(argv[0]);
+    if( s.zJson==0 ) return;  /* NULL input or OOM */
+    s.nJson = sqlite3_value_bytes(argv[0]);
+    if( jsonConvertTextToBlob(&s,0) ){
+      if( s.oom ){
+        iErrPos = -1;
+      }else{
         /* Convert byte-offset s.iErr into a character offset */
-        for(k=0; k<s.iErr && s.zJson[k]; k++){
+        u32 k;
+        for(k=0; k<s.iErr && ALWAYS(s.zJson[k]); k++){
           if( (s.zJson[k] & 0xc0)!=0x80 ) iErrPos++;
         }
         iErrPos++;
       }
-      jsonParseReset(&s);
     }
   }
-  sqlite3_result_int64(ctx, iErrPos);
+  jsonParseReset(&s);
+  if( iErrPos<0 ){
+    sqlite3_result_error_nomem(ctx);
+  }else{
+    sqlite3_result_int64(ctx, iErrPos);
+  }
 }
 
-
 /****************************************************************************
 ** Aggregate SQL function implementations
 ****************************************************************************/
@@ -4562,9 +4548,6 @@ static int jsonEachFilter(
     if( jsonFuncArgMightBeBinary(argv[0]) ){
       p->sParse.nBlob = sqlite3_value_bytes(argv[0]);
       p->sParse.aBlob = (u8*)sqlite3_value_blob(argv[0]);
-      if( p->sParse.aBlob==0 ){
-        return SQLITE_NOMEM;
-      }
     }else{
       goto json_each_malformed_input;
     }