]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Repair issues and inefficiencies found during testing.
authordrh <>
Mon, 4 Dec 2023 13:12:45 +0000 (13:12 +0000)
committerdrh <>
Mon, 4 Dec 2023 13:12:45 +0000 (13:12 +0000)
FossilOrigin-Name: ae973cb1515f9d76409c92a2ca2ffd6b71f32b0b490a4886770e7c1b90f12611

manifest
manifest.uuid
src/json.c

index f5a4a19dceb8b7ffa286f8d442454c4f660fe1cd..9ee6f8d2eabdf9dc36d7fe97eb7d182a41aeb9ce 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Better\spre-scan\ssize\sestimations\sfor\sobjects\sin\sthe\sJSON\sparser\sresulting\nin\sfewer\sreallocations\sand\smemmove\soperations.
-D 2023-12-04T01:14:23.183
+C Repair\sissues\sand\sinefficiencies\sfound\sduring\stesting.
+D 2023-12-04T13:12:45.155
 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 fe5c8d7494ed56126553dbcb5c970dcbbadddfd56871e08b905b8d7fb9903417
+F src/json.c fdab711d10347af4e70bbdc4dc3aca516a4ef0e6c4388cc70335ab9201bdf7eb
 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 79fb54fbb8b9c30f47cdbd437d24a21542716241e822749e5e28c9fbc449bfa8
-R dfc9eac13900dae64608fce7f4f645e4
+P 526b27f90897f5e35dfff7257daf6c4ce4798d649b09b8aecfb02df0449e3c51
+R a8fc2b32fcf7c8e7f15da5575d63ccf0
 U drh
-Z 0536405657c5f5cc41f38cd4401ae262
+Z 9f46540ea8d4faeba010e6034ffe6d00
 # Remove this line to create a well-formed Fossil manifest.
index deb93d36ac7afbfbac233e804c006e8435c5f26f..b7d0da89aed43a701eb41e6f5646e52512e2534e 100644 (file)
@@ -1 +1 @@
-526b27f90897f5e35dfff7257daf6c4ce4798d649b09b8aecfb02df0449e3c51
\ No newline at end of file
+ae973cb1515f9d76409c92a2ca2ffd6b71f32b0b490a4886770e7c1b90f12611
\ No newline at end of file
index 598b29051015b270d4d59adcad31d33c2a1c5bc4..6df07b0e66a222c544d28a86ba9f71c4ead35b79 100644 (file)
@@ -570,32 +570,16 @@ static void jsonAppendChar(JsonString *p, char c){
 }
 
 /* Make sure there is a zero terminator on p->zBuf[]
-*/
-static void jsonStringTerminate(JsonString *p){
-  jsonAppendChar(p, 0);
-  p->nUsed--;
-}
-
-/* Try to force the string to be a zero-terminated RCStr string.  In other
-** words, make sure it is not still using the internal zSpace[] static
-** buffer.
 **
 ** Return true on success.  Return false if an OOM prevents this
 ** from happening.
 */
-static int jsonForceRCStr(JsonString *p){
+static int jsonStringTerminate(JsonString *p){
   jsonAppendChar(p, 0);
-  if( p->eErr ) return 0;
-  p->nUsed--;
-  if( p->bStatic==0 ) return 1;
-  p->nAlloc = 0;
-  p->nUsed++;
-  jsonStringGrow(p, p->nUsed);
   p->nUsed--;
-  return p->bStatic==0;
+  return p->eErr==0;
 }
 
-
 /* Append a comma separator to the output buffer, if the previous
 ** character is not '[' or '{'.
 */
@@ -751,7 +735,7 @@ static void jsonReturnString(
     }else if( p->bStatic ){
       sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
                             SQLITE_TRANSIENT, SQLITE_UTF8);
-    }else if( jsonForceRCStr(p) ){
+    }else if( jsonStringTerminate(p) ){
       if( pParse && pParse->bJsonIsRCStr==0 && pParse->nBlobAlloc>0 ){
         int rc;
         pParse->zJson = sqlite3RCStrRef(p->zBuf);
@@ -1071,30 +1055,27 @@ static int jsonBlobMakeEditable(JsonParse *pParse, u32 nExtra){
   return 1;
 }
 
-
-/* Expand pParse->aBlob and append N bytes.
-**
-** Return the number of errors.
+/* Expand pParse->aBlob and append one bytes.
 */
-static SQLITE_NOINLINE int jsonBlobExpandAndAppend(
+static SQLITE_NOINLINE void jsonBlobExpandAndAppendOneByte(
   JsonParse *pParse,
-  const u8 *aData,
-  u32 N
+  u8 c
 ){
-  if( jsonBlobExpand(pParse, pParse->nBlob+N) ) return 1;
-  memcpy(&pParse->aBlob[pParse->nBlob], aData, N);
-  pParse->nBlob += N;
-  return 0;
+  jsonBlobExpand(pParse, pParse->nBlob+1);
+  if( pParse->oom==0 ){
+    assert( pParse->nBlob+1<=pParse->nBlobAlloc );
+    pParse->aBlob[pParse->nBlob++] = c;
+  }
 }
 
-/* Append a single character.  Return 1 if an error occurs.
+/* Append a single character.
 */
-static int jsonBlobAppendOneByte(JsonParse *pParse, u8 c){
+static void jsonBlobAppendOneByte(JsonParse *pParse, u8 c){
   if( pParse->nBlob >= pParse->nBlobAlloc ){
-    return jsonBlobExpandAndAppend(pParse, &c, 1);
+    jsonBlobExpandAndAppendOneByte(pParse, c);
+  }else{
+    pParse->aBlob[pParse->nBlob++] = c;
   }
-  pParse->aBlob[pParse->nBlob++] = c;
-  return 0;
 }
 
 /* Slow version of jsonBlobAppendNode() that first resizes the
@@ -1937,11 +1918,12 @@ static u32 jsonXlateBlobToText(
           sz2--;
           continue;
         }
+        assert( zIn[0]=='\\' );
+        assert( sz2>=1 );
         if( sz2<2 ){
-          if( sz2>0 ) pOut->eErr |= JSTRING_MALFORMED;
-          if( sz2==0 ) break;
+          pOut->eErr |= JSTRING_MALFORMED;
+          break;
         }
-        assert( zIn[0]=='\\' );
         switch( (u8)zIn[1] ){
           case '\'':
             jsonAppendChar(pOut, '\'');
@@ -1950,9 +1932,9 @@ static u32 jsonXlateBlobToText(
             jsonAppendRawNZ(pOut, "\\u0009", 6);
             break;
           case 'x':
-            if( sz2<2 ){
+            if( sz2<4 ){
               pOut->eErr |= JSTRING_MALFORMED;
-              sz2 = 0;
+              sz2 = 2;
               break;
             }
             jsonAppendRawNZ(pOut, "\\u00", 4);
@@ -1980,7 +1962,7 @@ static u32 jsonXlateBlobToText(
              || (0xa8!=(u8)zIn[3] && 0xa9!=(u8)zIn[3])
             ){
               pOut->eErr |= JSTRING_MALFORMED;
-              k = sz2;
+              sz2 = 2;
               break;
             }
             zIn += 2;
@@ -1990,11 +1972,7 @@ static u32 jsonXlateBlobToText(
             jsonAppendRawNZ(pOut, zIn, 2);
             break;
         }
-        if( sz2<2 ){
-          sz2 = 0;
-          pOut->eErr |= JSTRING_MALFORMED;
-          break;
-        }
+        assert( sz2>=2 );
         zIn += 2;
         sz2 -= 2;
       }
@@ -2051,11 +2029,11 @@ static int jsonFuncArgMightBeBinary(sqlite3_value *pJson){
   const u8 *aBlob;
   int nBlob;
   JsonParse s;
-  if( sqlite3_value_type(pJson)!=SQLITE_BLOB ) return 0;
+  assert( sqlite3_value_type(pJson)==SQLITE_BLOB );
   aBlob = sqlite3_value_blob(pJson);
   nBlob = sqlite3_value_bytes(pJson);
   if( nBlob<1 ) return 0;
-  if( aBlob==0 || (aBlob[0] & 0x0f)>JSONB_OBJECT ) return 0;
+  if( NEVER(aBlob==0) || (aBlob[0] & 0x0f)>JSONB_OBJECT ) return 0;
   memset(&s, 0, sizeof(s));
   s.aBlob = (u8*)aBlob;
   s.nBlob = nBlob;
@@ -2302,13 +2280,13 @@ static u32 jsonLookupStep(
         assert( !pParse->oom );
         nIns = ix.nBlob + nKey + v.nBlob;
         jsonBlobEdit(pParse, j, 0, 0, nIns);
-        if( !pParse->oom ){
+        if( ALWAYS(!pParse->oom) ){
           memcpy(&pParse->aBlob[j], ix.aBlob, ix.nBlob);
           k = j + ix.nBlob;
           memcpy(&pParse->aBlob[k], zKey, nKey);
           k += nKey;
           memcpy(&pParse->aBlob[k], v.aBlob, v.nBlob);
-          if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
+          if( ALWAYS(pParse->delta) ) jsonAfterEditSizeAdjust(pParse, iRoot);
         }
       }
       jsonParseReset(&v);
@@ -2394,7 +2372,7 @@ static void jsonReturnTextJsonFromBlob(
   JsonParse x;
   JsonString s;
 
-  if( aBlob==0 ) return;
+  if( NEVER(aBlob==0) ) return;
   memset(&x, 0, sizeof(x));
   x.aBlob = (u8*)aBlob;
   x.nBlob = nBlob;
@@ -2423,7 +2401,7 @@ static void jsonReturnFromBlob(
   sqlite3 *db = sqlite3_context_db_handle(pCtx);
 
   n = jsonbPayloadSize(pParse, i, &sz);
-  if( n==0 ) return;
+  if( NEVER(n==0) ) return;
   switch( pParse->aBlob[i] & 0x0f ){
     case JSONB_NULL: {
       sqlite3_result_null(pCtx);
@@ -2605,7 +2583,7 @@ static int jsonFunctionArgToBlob(
   static u8 aNull[] = { 0x00 };
   memset(pParse, 0, sizeof(pParse[0]));
   switch( eType ){
-    case SQLITE_NULL: {
+    default: {
       pParse->aBlob = aNull;
       pParse->nBlob = 1;
       return 0;
@@ -3041,39 +3019,6 @@ static void jsonParseFunc(
   jsonShowParse(p);
   jsonParseFree(p);
 }
-
-/*
-** The json_test1(JSON) function return true (1) if the input is JSON
-** text generated by another json function.  It returns (0) if the input
-** is not known to be JSON.
-*/
-static void jsonTest1Func(
-  sqlite3_context *ctx,
-  int argc,
-  sqlite3_value **argv
-){
-  UNUSED_PARAMETER(argc);
-  sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
-}
-
-/* SQL Function:  jsonb_test2(BLOB_JSON)
-**
-** Render BLOB_JSON back into text.
-** Development testing only.
-*/
-static void jsonbTest2(
-  sqlite3_context *ctx,
-  int argc,
-  sqlite3_value **argv
-){
-  const u8 *aBlob;
-  int nBlob;
-  UNUSED_PARAMETER(argc);
-
-  aBlob = (const u8*)sqlite3_value_blob(argv[0]);
-  nBlob = sqlite3_value_bytes(argv[0]);
-  jsonReturnTextJsonFromBlob(ctx, aBlob, nBlob);
-}
 #endif /* SQLITE_DEBUG */
 
 /****************************************************************************
@@ -4805,8 +4750,6 @@ void sqlite3RegisterJsonFunctions(void){
     JFUNCTION(json_valid,         2,1,0, 0,0,0,          jsonValidFunc),
 #if SQLITE_DEBUG
     JFUNCTION(json_parse,         1,1,0, 0,0,0,          jsonParseFunc),
-    JFUNCTION(json_test1,         1,1,0, 1,0,0,          jsonTest1Func),
-    JFUNCTION(jsonb_test2,        1,1,0, 0,1,0,          jsonbTest2),
 #endif
     WAGGREGATE(json_group_array,  1, 0, 0,
        jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,