]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Improvements to error handling.
authordrh <>
Fri, 6 Oct 2023 14:52:49 +0000 (14:52 +0000)
committerdrh <>
Fri, 6 Oct 2023 14:52:49 +0000 (14:52 +0000)
FossilOrigin-Name: b41dd237fb6c0dd7daedeaaf81dbc4fdb31cf511fd32cd3d4ee69db34e389915

manifest
manifest.uuid
src/json.c

index b6d3e12ce0eea703aacb6c7f85215723185cca1d..555f873fb70c6dd831cb26d9b54ef668be038652 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Merge\scompiler\swarning\sfixes\sfrom\strunk\sinto\sthe\sjsonb\sbranch.
-D 2023-10-06T13:05:42.465
+C Improvements\sto\serror\shandling.
+D 2023-10-06T14:52:49.860
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -670,7 +670,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 e89b89a6f36b2281524aea01521690e1d3bc69db2754856e4a03ac271f261346
+F src/json.c fe8cd7b38e947a89fbe8b8099a88ee363d02a6747009ba5246a517de1e15159f
 F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
 F src/loadext.c 98cfba10989b3da6f1807ad42444017742db7f100a54f1032af7a8b1295912c0
 F src/main.c 618aeb399e993cf561864f4b0cf6a331ee4f355cf663635f8d9da3193a46aa40
@@ -2124,8 +2124,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 563cde404cec4c6559ae0c5fc5edbc878fee874b36562ce2fac5049cc8349343 9bf4bfd68080367b58594e0d44b110b3ee9766420f648537fd7bc638dacefb72
-R fa6a8cc346d8604b4e2021455b16ba57
+P 6409d307915f8969f12df2d5ffa961645bd4db7ccfbd6f52237a217b6a867252
+R bb54780cc2adfb5e91d01e5a38070ac9
 U drh
-Z 77ed8049621ac72b8ae81ce5801b8fae
+Z 4eb2d45211dd6d4bee3b0c79a0072e1a
 # Remove this line to create a well-formed Fossil manifest.
index dea74400a22abcfb14d0a072bcd4071fac05d658..950949b6181c15f9ac139ed9db7704d32b92ba97 100644 (file)
@@ -1 +1 @@
-6409d307915f8969f12df2d5ffa961645bd4db7ccfbd6f52237a217b6a867252
\ No newline at end of file
+b41dd237fb6c0dd7daedeaaf81dbc4fdb31cf511fd32cd3d4ee69db34e389915
\ No newline at end of file
index 341957275981f6bf3390db02ac23ad7018d6677b..69c9e67b6b553fb2da52691d013124297f6a03da 100644 (file)
@@ -207,10 +207,15 @@ struct JsonString {
   u64 nAlloc;              /* Bytes of storage available in zBuf[] */
   u64 nUsed;               /* Bytes of zBuf[] currently used */
   u8 bStatic;              /* True if zBuf is static space */
-  u8 bErr;                 /* True if an error has been encountered */
+  u8 eErr;                 /* True if an error has been encountered */
   char zSpace[100];        /* Initial static space */
 };
 
+/* Allowed values for JsonString.eErr */
+#define JSTRING_OOM         0x01   /* Out of memory */
+#define JSTRING_MALFORMED   0x02   /* Malformed JSONB */
+#define JSTRING_ERR         0x04   /* Error already sent to sqlite3_result */
+
 /* A deferred cleanup task.  A list of JsonCleanup objects might be
 ** run when the JsonParse object is destroyed.
 */
@@ -376,7 +381,7 @@ static void jsonStringZero(JsonString *p){
 */
 static void jsonStringInit(JsonString *p, sqlite3_context *pCtx){
   p->pCtx = pCtx;
-  p->bErr = 0;
+  p->eErr = 0;
   jsonStringZero(p);
 }
 
@@ -391,7 +396,7 @@ static void jsonStringReset(JsonString *p){
 /* Report an out-of-memory (OOM) condition
 */
 static void jsonStringOom(JsonString *p){
-  p->bErr = 1;
+  p->eErr |= JSTRING_OOM;
   sqlite3_result_error_nomem(p->pCtx);
   jsonStringReset(p);
 }
@@ -403,7 +408,7 @@ static int jsonStringGrow(JsonString *p, u32 N){
   u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
   char *zNew;
   if( p->bStatic ){
-    if( p->bErr ) return 1;
+    if( p->eErr ) return 1;
     zNew = sqlite3RCStrNew(nTotal);
     if( zNew==0 ){
       jsonStringOom(p);
@@ -415,7 +420,7 @@ static int jsonStringGrow(JsonString *p, u32 N){
   }else{
     p->zBuf = sqlite3RCStrResize(p->zBuf, nTotal);
     if( p->zBuf==0 ){
-      p->bErr = 1;
+      p->eErr |= JSTRING_OOM;
       jsonStringZero(p);
       return SQLITE_NOMEM;
     }
@@ -488,7 +493,7 @@ static void jsonAppendChar(JsonString *p, char c){
 */
 static int jsonForceRCStr(JsonString *p){
   jsonAppendChar(p, 0);
-  if( p->bErr ) return 0;
+  if( p->eErr ) return 0;
   p->nUsed--;
   if( p->bStatic==0 ) return 1;
   p->nAlloc = 0;
@@ -626,7 +631,7 @@ static void jsonAppendNormalizedString(JsonString *p, const char *zIn, u32 N){
 static void jsonAppendNormalizedInt(JsonString *p, const char *zIn, u32 N){
   char *zBuf = sqlite3_malloc64( N+1 );
   if( zBuf==0 ){
-    p->bErr = 1;
+    p->eErr |= JSTRING_OOM;
     return;
   }
   memcpy(zBuf, zIn, N);
@@ -731,9 +736,9 @@ static void jsonAppendSqlValue(
         px.aBlob = (u8*)sqlite3_value_blob(pValue);
         px.nBlob = sqlite3_value_bytes(pValue);
         jsonRenderBlob(&px, 0, p);
-      }else if( p->bErr==0 ){
+      }else if( p->eErr==0 ){
         sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
-        p->bErr = 2;
+        p->eErr = JSTRING_ERR;
         jsonStringReset(p);
       }
       break;
@@ -748,7 +753,7 @@ static void jsonAppendSqlValue(
 ** The JsonString is reset.
 */
 static void jsonReturnString(JsonString *p){
-  if( p->bErr==0 ){
+  if( p->eErr==0 ){
     int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(p->pCtx));
     if( flags & JSON_BLOB ){
       jsonReturnStringAsBlob(p);
@@ -760,10 +765,13 @@ static void jsonReturnString(JsonString *p){
       sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
                             (void(*)(void*))sqlite3RCStrUnref,
                             SQLITE_UTF8);
+    }else{
+      sqlite3_result_error_nomem(p->pCtx);
     }
-  }
-  if( p->bErr==1 ){
+  }else if( p->eErr & JSTRING_OOM ){
     sqlite3_result_error_nomem(p->pCtx);
+  }else if( p->eErr & JSTRING_MALFORMED ){
+    sqlite3_result_error(p->pCtx, "malformed JSON", -1);
   }
   jsonStringReset(p);
 }
@@ -2974,6 +2982,7 @@ json_parse_restart:
         }
       }
     }
+
   parse_number_2:
     for(j=i+1;; j++){
       c = z[j];
@@ -3236,7 +3245,7 @@ static u32 jsonRenderBlob(
 
   n = jsonbPayloadSize(pParse, i, &sz);
   if( n==0 ){
-    pOut->bErr = 1;
+    pOut->eErr |= JSTRING_MALFORMED;
     return pParse->nBlob+1;
   }
   switch( pParse->aBlob[i] & 0x0f ){
@@ -3258,44 +3267,35 @@ static u32 jsonRenderBlob(
       break;
     }
     case JSONB_INT5: {  /* Integer literal in hexadecimal notation */
-      int k;
+      int k = 2;
       sqlite3_uint64 u = 0;
       const char *zIn = (const char*)&pParse->aBlob[i+n];
-      if( zIn[0]=='-' ){
-        zIn++;
-        sz--;
-        jsonAppendChar(pOut, '-');
+      if( zIn[0]=='+' || zIn[0]=='-' ){
+        if( zIn[0]=='-' ) jsonAppendChar(pOut, '-');
+        k++;
       }
-      for(k=2; k<sz; k++){
+      for(; k<sz; k++){
         u = u*16 + sqlite3HexToInt(zIn[k]);
       }
       jsonPrintf(100,pOut,"%llu",u);
       break;
     }
     case JSONB_FLOAT5: { /* Float literal missing digits beside "." */
-      int k;
+      int k = 0;
       const char *zIn = (const char*)&pParse->aBlob[i+n];
-      if( zIn[0]=='-' ){
-        zIn++;
-        sz--;
-        jsonAppendChar(pOut, '-');
+      if( zIn[0]=='+' || zIn[0]=='-' ){
+        if( zIn[0]=='-' ) jsonAppendChar(pOut, '-');
+        k++;
       }
-      if( zIn[0]=='.' ){
+      if( zIn[k]=='.' ){
         jsonAppendChar(pOut, '0');
       }
-      for(k=0; k<sz; k++){
+      for(; k<sz; k++){
+        jsonAppendChar(pOut, zIn[k]);
         if( zIn[k]=='.' && (k+1==sz || !sqlite3Isdigit(zIn[k+1])) ){
-          k++;
-          jsonAppendRaw(pOut, zIn, k);
-          zIn += k;
-          sz -= k;
           jsonAppendChar(pOut, '0');
-          break;
         }
       }
-      if( sz>0 ){
-        jsonAppendRawNZ(pOut, zIn, sz);
-      }
       break;
     }
     case JSONB_TEXT:
@@ -3808,7 +3808,12 @@ static void jsonReturnTextJsonFromBlob(
   x.nBlob = nBlob;
   jsonStringInit(&s, ctx);
   jsonRenderBlob(&x, 0, &s);
-  jsonReturnString(&s);
+  if( x.nErr ){
+    sqlite3_result_error(ctx, "malformed JSON", -1);
+    jsonStringReset(&s);
+  }else{
+    jsonReturnString(&s);
+  }
 }
 
 
@@ -4371,7 +4376,7 @@ static void jsonExtractFunc(
           jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
           jsonAppendChar(&jx, 0);
         }
-        pNode = jx.bErr ? 0 : jsonLookup(p, jx.zBuf, 0, ctx);
+        pNode = jx.eErr ? 0 : jsonLookup(p, jx.zBuf, 0, ctx);
         jsonStringReset(&jx);
       }else{
         pNode = jsonLookup(p, zPath, 0, ctx);
@@ -4942,9 +4947,9 @@ static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
     pStr->pCtx = ctx;
     jsonAppendChar(pStr, ']');
     flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
-    if( pStr->bErr ){
-      if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
-      assert( pStr->bStatic );
+    if( pStr->eErr ){
+      jsonReturnString(pStr);
+      return;
     }else if( flags & JSON_BLOB ){
       jsonReturnStringAsBlob(pStr);
       if( isFinal ){
@@ -5062,9 +5067,9 @@ static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
     jsonAppendChar(pStr, '}');
     pStr->pCtx = ctx;
     flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
-    if( pStr->bErr ){
-      if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
-      assert( pStr->bStatic );
+    if( pStr->eErr ){
+      jsonReturnString(pStr);
+      return;
     }else if( flags & JSON_BLOB ){
       jsonReturnStringAsBlob(pStr);
       if( isFinal ){