]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Simplifications to the json1.c logic.
authordrh <drh@noemail.net>
Mon, 21 Sep 2015 22:53:16 +0000 (22:53 +0000)
committerdrh <drh@noemail.net>
Mon, 21 Sep 2015 22:53:16 +0000 (22:53 +0000)
FossilOrigin-Name: 1646a2bd85f87d03a14fcaec288107f5f6411c6f

ext/misc/json1.c
manifest
manifest.uuid

index e115470f87a3fead6b56890a669bbd32b890a286..4e21b151a71b5404ec7d8995c5441a316f786b51 100644 (file)
@@ -343,7 +343,8 @@ static void jsonRenderNode(
   sqlite3_value **aReplace       /* Replacement values */
 ){
   switch( pNode->eType ){
-    case JSON_NULL: {
+    default: {
+      assert( pNode->eType==JSON_NULL );
       jsonAppendRaw(pOut, "null", 4);
       break;
     }
@@ -441,7 +442,8 @@ static void jsonReturn(
   sqlite3_value **aReplace    /* Array of replacement values */
 ){
   switch( pNode->eType ){
-    case JSON_NULL: {
+    default: {
+      assert( pNode->eType==JSON_NULL );
       sqlite3_result_null(pCtx);
       break;
     }
@@ -468,10 +470,16 @@ static void jsonReturn(
       break;
     }
     case JSON_STRING: {
+#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
+      ** json_insert() and json_replace() and those routines do not
+      ** call jsonReturn() */
       if( pNode->jnFlags & JNODE_RAW ){
         sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
                             SQLITE_TRANSIENT);
-      }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
+      }else 
+#endif
+      assert( (pNode->jnFlags & JNODE_RAW)==0 );
+      if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
         /* JSON formatted without any backslash-escapes */
         sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
                             SQLITE_TRANSIENT);
@@ -604,7 +612,7 @@ static int jsonParseValue(JsonParse *pParse, u32 i){
       while( safe_isspace(pParse->zJson[j]) ){ j++; }
       x = jsonParseValue(pParse, j);
       if( x<0 ){
-        if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
+        if( x==(-2) ) return j+1;
         return -1;
       }
       if( pParse->oom ) return -1;
@@ -634,7 +642,7 @@ static int jsonParseValue(JsonParse *pParse, u32 i){
       while( safe_isspace(pParse->zJson[j]) ){ j++; }
       x = jsonParseValue(pParse, j);
       if( x<0 ){
-        if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
+        if( x==(-3) ) return j+1;
         return -1;
       }
       j = x;
@@ -829,7 +837,12 @@ static JsonNode *jsonLookupStep(
       zKey = zPath + 1;
       for(i=1; zPath[i] && zPath[i]!='"'; i++){}
       nKey = i-1;
-      if( zPath[i] ) i++;
+      if( zPath[i] ){
+        i++;
+      }else{
+        *pzErr = zPath;
+        return 0;
+      }
     }else{
       zKey = zPath;
       for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
@@ -969,6 +982,7 @@ static JsonNode *jsonLookup(
 ){
   const char *zErr = 0;
   JsonNode *pNode = 0;
+  char *zMsg;
 
   if( zPath==0 ) return 0;
   if( zPath[0]!='$' ){
@@ -977,18 +991,17 @@ static JsonNode *jsonLookup(
   }
   zPath++;
   pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
-  return pNode;
+  if( zErr==0 ) return pNode;
 
 lookup_err:
   pParse->nErr++;
-  if( zErr!=0 && pCtx!=0 ){
-    char *z = jsonPathSyntaxError(zErr);
-    if( z ){
-      sqlite3_result_error(pCtx, z, -1);
-      sqlite3_free(z);
-    }else{
-      sqlite3_result_error_nomem(pCtx);
-    }
+  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);
   }
   return 0;
 }
@@ -1111,23 +1124,22 @@ static void jsonArrayLengthFunc(
   JsonParse x;          /* The parse */
   sqlite3_int64 n = 0;
   u32 i;
+  JsonNode *pNode;
 
   if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
-  if( x.nNode ){
-    JsonNode *pNode;
-    if( argc==2 ){
-      const char *zPath = (const char*)sqlite3_value_text(argv[1]);
-      pNode = jsonLookup(&x, zPath, 0, ctx);
-    }else{
-      pNode = x.aNode;
-    }
-    if( pNode==0 ){
-      x.nErr = 1;
-    }else if( pNode->eType==JSON_ARRAY ){
-      assert( (pNode->jnFlags & JNODE_APPEND)==0 );
-      for(i=1; i<=pNode->n; n++){
-        i += jsonNodeSize(&pNode[i]);
-      }
+  assert( x.nNode );
+  if( argc==2 ){
+    const char *zPath = (const char*)sqlite3_value_text(argv[1]);
+    pNode = jsonLookup(&x, zPath, 0, ctx);
+  }else{
+    pNode = x.aNode;
+  }
+  if( pNode==0 ){
+    x.nErr = 1;
+  }else if( pNode->eType==JSON_ARRAY ){
+    assert( (pNode->jnFlags & JNODE_APPEND)==0 );
+    for(i=1; i<=pNode->n; n++){
+      i += jsonNodeSize(&pNode[i]);
     }
   }
   if( x.nErr==0 ) sqlite3_result_int64(ctx, n);
@@ -1240,17 +1252,16 @@ static void jsonRemoveFunc(
 
   if( argc<1 ) return;
   if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
-  if( x.nNode ){
-    for(i=1; i<(u32)argc; i++){
-      zPath = (const char*)sqlite3_value_text(argv[i]);
-      if( zPath==0 ) goto remove_done;
-      pNode = jsonLookup(&x, zPath, 0, ctx);
-      if( x.nErr ) goto remove_done;
-      if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
-    }
-    if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
-      jsonReturnJson(x.aNode, ctx, 0);
-    }
+  assert( x.nNode );
+  for(i=1; i<(u32)argc; i++){
+    zPath = (const char*)sqlite3_value_text(argv[i]);
+    if( zPath==0 ) goto remove_done;
+    pNode = jsonLookup(&x, zPath, 0, ctx);
+    if( x.nErr ) goto remove_done;
+    if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
+  }
+  if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
+    jsonReturnJson(x.aNode, ctx, 0);
   }
 remove_done:
   jsonParseReset(&x);
@@ -1278,22 +1289,21 @@ static void jsonReplaceFunc(
     return;
   }
   if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
-  if( x.nNode ){
-    for(i=1; i<(u32)argc; i+=2){
-      zPath = (const char*)sqlite3_value_text(argv[i]);
-      pNode = jsonLookup(&x, zPath, 0, ctx);
-      if( x.nErr ) goto replace_err;
-      if( pNode ){
-        pNode->jnFlags |= (u8)JNODE_REPLACE;
-        pNode->iVal = (u8)(i+1);
-      }
-    }
-    if( x.aNode[0].jnFlags & JNODE_REPLACE ){
-      sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
-    }else{
-      jsonReturnJson(x.aNode, ctx, argv);
+  assert( x.nNode );
+  for(i=1; i<(u32)argc; i+=2){
+    zPath = (const char*)sqlite3_value_text(argv[i]);
+    pNode = jsonLookup(&x, zPath, 0, ctx);
+    if( x.nErr ) goto replace_err;
+    if( pNode ){
+      pNode->jnFlags |= (u8)JNODE_REPLACE;
+      pNode->iVal = (u8)(i+1);
     }
   }
+  if( x.aNode[0].jnFlags & JNODE_REPLACE ){
+    sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
+  }else{
+    jsonReturnJson(x.aNode, ctx, argv);
+  }
 replace_err:
   jsonParseReset(&x);
 }
@@ -1328,27 +1338,26 @@ static void jsonSetFunc(
     return;
   }
   if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
-  if( x.nNode ){
-    for(i=1; i<(u32)argc; i+=2){
-      zPath = (const char*)sqlite3_value_text(argv[i]);
-      bApnd = 0;
-      pNode = jsonLookup(&x, zPath, &bApnd, ctx);
-      if( x.oom ){
-        sqlite3_result_error_nomem(ctx);
-        goto jsonSetDone;
-      }else if( x.nErr ){
-        goto jsonSetDone;
-      }else if( pNode && (bApnd || bIsSet) ){
-        pNode->jnFlags |= (u8)JNODE_REPLACE;
-        pNode->iVal = (u8)(i+1);
-      }
-    }
-    if( x.aNode[0].jnFlags & JNODE_REPLACE ){
-      sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
-    }else{
-      jsonReturnJson(x.aNode, ctx, argv);
+  assert( x.nNode );
+  for(i=1; i<(u32)argc; i+=2){
+    zPath = (const char*)sqlite3_value_text(argv[i]);
+    bApnd = 0;
+    pNode = jsonLookup(&x, zPath, &bApnd, ctx);
+    if( x.oom ){
+      sqlite3_result_error_nomem(ctx);
+      goto jsonSetDone;
+    }else if( x.nErr ){
+      goto jsonSetDone;
+    }else if( pNode && (bApnd || bIsSet) ){
+      pNode->jnFlags |= (u8)JNODE_REPLACE;
+      pNode->iVal = (u8)(i+1);
     }
   }
+  if( x.aNode[0].jnFlags & JNODE_REPLACE ){
+    sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
+  }else{
+    jsonReturnJson(x.aNode, ctx, argv);
+  }
 jsonSetDone:
   jsonParseReset(&x);
 }
@@ -1367,19 +1376,18 @@ static void jsonTypeFunc(
 ){
   JsonParse x;          /* The parse */
   const char *zPath;
+  JsonNode *pNode;
 
   if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
-  if( x.nNode ){
-    JsonNode *pNode;
-    if( argc==2 ){
-      zPath = (const char*)sqlite3_value_text(argv[1]);
-      pNode = jsonLookup(&x, zPath, 0, ctx);
-    }else{
-      pNode = x.aNode;
-    }
-    if( pNode ){
-      sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
-    }
+  assert( x.nNode );
+  if( argc==2 ){
+    zPath = (const char*)sqlite3_value_text(argv[1]);
+    pNode = jsonLookup(&x, zPath, 0, ctx);
+  }else{
+    pNode = x.aNode;
+  }
+  if( pNode ){
+    sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
   }
   jsonParseReset(&x);
 }
@@ -1399,9 +1407,7 @@ static void jsonValidFunc(
   int rc = 0;
 
   UNUSED_PARAM(argc);
-  if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 
-   && x.nNode>0
-  ){
+  if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 ){
     rc = 1;
   }
   jsonParseReset(&x);
@@ -1754,15 +1760,6 @@ static int jsonEachFilter(
   if( idxNum==0 ) return SQLITE_OK;
   z = (const char*)sqlite3_value_text(argv[0]);
   if( z==0 ) return SQLITE_OK;
-  if( idxNum&2 ){
-    zRoot = (const char*)sqlite3_value_text(argv[1]);
-    if( zRoot==0 ) return SQLITE_OK;
-    if( zRoot[0]!='$' ){
-      sqlite3_free(cur->pVtab->zErrMsg);
-      cur->pVtab->zErrMsg = jsonPathSyntaxError(zRoot);
-      return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
-    }
-  }
   n = sqlite3_value_bytes(argv[0]);
   p->zJson = sqlite3_malloc64( n+1 );
   if( p->zJson==0 ) return SQLITE_NOMEM;
@@ -1783,12 +1780,18 @@ static int jsonEachFilter(
     JsonNode *pNode;
     if( idxNum==3 ){
       const char *zErr = 0;
+      zRoot = (const char*)sqlite3_value_text(argv[1]);
+      if( zRoot==0 ) return SQLITE_OK;
       n = sqlite3_value_bytes(argv[1]);
       p->zRoot = sqlite3_malloc64( n+1 );
       if( p->zRoot==0 ) return SQLITE_NOMEM;
       memcpy(p->zRoot, zRoot, (size_t)n+1);
-      pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
-      if( p->sParse.nErr ){
+      if( zRoot[0]!='$' ){
+        zErr = zRoot;
+      }else{
+        pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
+      }
+      if( zErr ){
         sqlite3_free(cur->pVtab->zErrMsg);
         cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
         jsonEachCursorReset(p);
@@ -1815,7 +1818,7 @@ static int jsonEachFilter(
       p->iEnd = p->i+1;
     }
   }
-  return p->sParse.oom ? SQLITE_NOMEM : SQLITE_OK;
+  return SQLITE_OK;
 }
 
 /* The methods of the json_each virtual table */
index 659721948a52ee8d113cab3ebfd240d6f6bbfa79..202dddea71ad806ee93058c2497898f3da47034e 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\san\soverly-strict\sassert()\sin\sthe\sbtree\slogic.
-D 2015-09-20T22:57:47.045
+C Simplifications\sto\sthe\sjson1.c\slogic.
+D 2015-09-21T22:53:16.694
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 2047811644c5bac91ccdfc2720e49b60965a63a7
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -195,7 +195,7 @@ F ext/misc/eval.c f971962e92ebb8b0a4e6b62949463ee454d88fa2
 F ext/misc/fileio.c d4171c815d6543a9edef8308aab2951413cd8d0f
 F ext/misc/fuzzer.c 4c84635c71c26cfa7c2e5848cf49fe2d2cfcd767
 F ext/misc/ieee754.c b0362167289170627659e84173f5d2e8fee8566e
-F ext/misc/json1.c a1f7dd9b533bcd04160a5eaf2ae93cf7c64f3744
+F ext/misc/json1.c 54f067ea34f6a7426b998ca0fb411d2eefaeb25e
 F ext/misc/nextchar.c 35c8b8baacb96d92abbb34a83a997b797075b342
 F ext/misc/percentile.c bcbee3c061b884eccb80e21651daaae8e1e43c63
 F ext/misc/regexp.c af92cdaa5058fcec1451e49becc7ba44dba023dc
@@ -1387,7 +1387,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P 97cfe346e140e42a604375551f8168276bdbea11
-R e5931d5a57c10bca565f8c3b4aa29af4
+P 825ce3201df21c6b9d5c57dcc6726c306df05220
+R 0407e29107979175c5ef95d6530fb773
 U drh
-Z 432e10bd3b5a58fe33b21908c7a72458
+Z 3c9b1b14e7e53f7b1522a534f080ce63
index 238fad3e650a2cd8fe17da9b818c23ba9fdc2db4..cd9700253a31420c811227ea7d20ffff297439c1 100644 (file)
@@ -1 +1 @@
-825ce3201df21c6b9d5c57dcc6726c306df05220
\ No newline at end of file
+1646a2bd85f87d03a14fcaec288107f5f6411c6f
\ No newline at end of file