1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-11 01:42:22 +03:00

Repair issues and inefficiencies found during testing.

FossilOrigin-Name: ae973cb1515f9d76409c92a2ca2ffd6b71f32b0b490a4886770e7c1b90f12611
This commit is contained in:
drh
2023-12-04 13:12:45 +00:00
parent 2ff73a5f3e
commit dc138cb186
3 changed files with 39 additions and 96 deletions

View File

@@ -1,5 +1,5 @@
C Better\spre-scan\ssize\sestimations\sfor\sobjects\sin\sthe\sJSON\sparser\sresulting\nin\sfewer\sreallocations\sand\smemmove\soperations. C Repair\sissues\sand\sinefficiencies\sfound\sduring\stesting.
D 2023-12-04T01:14:23.183 D 2023-12-04T13:12:45.155
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -688,7 +688,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51
F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6 F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6
F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71 F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276 F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276
F src/json.c fe5c8d7494ed56126553dbcb5c970dcbbadddfd56871e08b905b8d7fb9903417 F src/json.c fdab711d10347af4e70bbdc4dc3aca516a4ef0e6c4388cc70335ab9201bdf7eb
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36 F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36
F src/main.c 1b89f3de98d1b59fec5bac1d66d6ece21f703821b8eaa0d53d9604c35309f6f9 F src/main.c 1b89f3de98d1b59fec5bac1d66d6ece21f703821b8eaa0d53d9604c35309f6f9
@@ -2145,8 +2145,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 79fb54fbb8b9c30f47cdbd437d24a21542716241e822749e5e28c9fbc449bfa8 P 526b27f90897f5e35dfff7257daf6c4ce4798d649b09b8aecfb02df0449e3c51
R dfc9eac13900dae64608fce7f4f645e4 R a8fc2b32fcf7c8e7f15da5575d63ccf0
U drh U drh
Z 0536405657c5f5cc41f38cd4401ae262 Z 9f46540ea8d4faeba010e6034ffe6d00
# Remove this line to create a well-formed Fossil manifest. # Remove this line to create a well-formed Fossil manifest.

View File

@@ -1 +1 @@
526b27f90897f5e35dfff7257daf6c4ce4798d649b09b8aecfb02df0449e3c51 ae973cb1515f9d76409c92a2ca2ffd6b71f32b0b490a4886770e7c1b90f12611

View File

@@ -570,32 +570,16 @@ static void jsonAppendChar(JsonString *p, char c){
} }
/* Make sure there is a zero terminator on p->zBuf[] /* 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 ** Return true on success. Return false if an OOM prevents this
** from happening. ** from happening.
*/ */
static int jsonForceRCStr(JsonString *p){ static int jsonStringTerminate(JsonString *p){
jsonAppendChar(p, 0); jsonAppendChar(p, 0);
if( p->eErr ) return 0;
p->nUsed--; p->nUsed--;
if( p->bStatic==0 ) return 1; return p->eErr==0;
p->nAlloc = 0;
p->nUsed++;
jsonStringGrow(p, p->nUsed);
p->nUsed--;
return p->bStatic==0;
} }
/* Append a comma separator to the output buffer, if the previous /* Append a comma separator to the output buffer, if the previous
** character is not '[' or '{'. ** character is not '[' or '{'.
*/ */
@@ -751,7 +735,7 @@ static void jsonReturnString(
}else if( p->bStatic ){ }else if( p->bStatic ){
sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
SQLITE_TRANSIENT, SQLITE_UTF8); SQLITE_TRANSIENT, SQLITE_UTF8);
}else if( jsonForceRCStr(p) ){ }else if( jsonStringTerminate(p) ){
if( pParse && pParse->bJsonIsRCStr==0 && pParse->nBlobAlloc>0 ){ if( pParse && pParse->bJsonIsRCStr==0 && pParse->nBlobAlloc>0 ){
int rc; int rc;
pParse->zJson = sqlite3RCStrRef(p->zBuf); pParse->zJson = sqlite3RCStrRef(p->zBuf);
@@ -1071,30 +1055,27 @@ static int jsonBlobMakeEditable(JsonParse *pParse, u32 nExtra){
return 1; return 1;
} }
/* Expand pParse->aBlob and append one bytes.
/* Expand pParse->aBlob and append N bytes.
**
** Return the number of errors.
*/ */
static SQLITE_NOINLINE int jsonBlobExpandAndAppend( static SQLITE_NOINLINE void jsonBlobExpandAndAppendOneByte(
JsonParse *pParse, JsonParse *pParse,
const u8 *aData, u8 c
u32 N
){ ){
if( jsonBlobExpand(pParse, pParse->nBlob+N) ) return 1; jsonBlobExpand(pParse, pParse->nBlob+1);
memcpy(&pParse->aBlob[pParse->nBlob], aData, N); if( pParse->oom==0 ){
pParse->nBlob += N; assert( pParse->nBlob+1<=pParse->nBlobAlloc );
return 0; 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 ){ 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 /* Slow version of jsonBlobAppendNode() that first resizes the
@@ -1937,11 +1918,12 @@ static u32 jsonXlateBlobToText(
sz2--; sz2--;
continue; continue;
} }
if( sz2<2 ){
if( sz2>0 ) pOut->eErr |= JSTRING_MALFORMED;
if( sz2==0 ) break;
}
assert( zIn[0]=='\\' ); assert( zIn[0]=='\\' );
assert( sz2>=1 );
if( sz2<2 ){
pOut->eErr |= JSTRING_MALFORMED;
break;
}
switch( (u8)zIn[1] ){ switch( (u8)zIn[1] ){
case '\'': case '\'':
jsonAppendChar(pOut, '\''); jsonAppendChar(pOut, '\'');
@@ -1950,9 +1932,9 @@ static u32 jsonXlateBlobToText(
jsonAppendRawNZ(pOut, "\\u0009", 6); jsonAppendRawNZ(pOut, "\\u0009", 6);
break; break;
case 'x': case 'x':
if( sz2<2 ){ if( sz2<4 ){
pOut->eErr |= JSTRING_MALFORMED; pOut->eErr |= JSTRING_MALFORMED;
sz2 = 0; sz2 = 2;
break; break;
} }
jsonAppendRawNZ(pOut, "\\u00", 4); jsonAppendRawNZ(pOut, "\\u00", 4);
@@ -1980,7 +1962,7 @@ static u32 jsonXlateBlobToText(
|| (0xa8!=(u8)zIn[3] && 0xa9!=(u8)zIn[3]) || (0xa8!=(u8)zIn[3] && 0xa9!=(u8)zIn[3])
){ ){
pOut->eErr |= JSTRING_MALFORMED; pOut->eErr |= JSTRING_MALFORMED;
k = sz2; sz2 = 2;
break; break;
} }
zIn += 2; zIn += 2;
@@ -1990,11 +1972,7 @@ static u32 jsonXlateBlobToText(
jsonAppendRawNZ(pOut, zIn, 2); jsonAppendRawNZ(pOut, zIn, 2);
break; break;
} }
if( sz2<2 ){ assert( sz2>=2 );
sz2 = 0;
pOut->eErr |= JSTRING_MALFORMED;
break;
}
zIn += 2; zIn += 2;
sz2 -= 2; sz2 -= 2;
} }
@@ -2051,11 +2029,11 @@ static int jsonFuncArgMightBeBinary(sqlite3_value *pJson){
const u8 *aBlob; const u8 *aBlob;
int nBlob; int nBlob;
JsonParse s; JsonParse s;
if( sqlite3_value_type(pJson)!=SQLITE_BLOB ) return 0; assert( sqlite3_value_type(pJson)==SQLITE_BLOB );
aBlob = sqlite3_value_blob(pJson); aBlob = sqlite3_value_blob(pJson);
nBlob = sqlite3_value_bytes(pJson); nBlob = sqlite3_value_bytes(pJson);
if( nBlob<1 ) return 0; 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)); memset(&s, 0, sizeof(s));
s.aBlob = (u8*)aBlob; s.aBlob = (u8*)aBlob;
s.nBlob = nBlob; s.nBlob = nBlob;
@@ -2302,13 +2280,13 @@ static u32 jsonLookupStep(
assert( !pParse->oom ); assert( !pParse->oom );
nIns = ix.nBlob + nKey + v.nBlob; nIns = ix.nBlob + nKey + v.nBlob;
jsonBlobEdit(pParse, j, 0, 0, nIns); jsonBlobEdit(pParse, j, 0, 0, nIns);
if( !pParse->oom ){ if( ALWAYS(!pParse->oom) ){
memcpy(&pParse->aBlob[j], ix.aBlob, ix.nBlob); memcpy(&pParse->aBlob[j], ix.aBlob, ix.nBlob);
k = j + ix.nBlob; k = j + ix.nBlob;
memcpy(&pParse->aBlob[k], zKey, nKey); memcpy(&pParse->aBlob[k], zKey, nKey);
k += nKey; k += nKey;
memcpy(&pParse->aBlob[k], v.aBlob, v.nBlob); memcpy(&pParse->aBlob[k], v.aBlob, v.nBlob);
if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot); if( ALWAYS(pParse->delta) ) jsonAfterEditSizeAdjust(pParse, iRoot);
} }
} }
jsonParseReset(&v); jsonParseReset(&v);
@@ -2394,7 +2372,7 @@ static void jsonReturnTextJsonFromBlob(
JsonParse x; JsonParse x;
JsonString s; JsonString s;
if( aBlob==0 ) return; if( NEVER(aBlob==0) ) return;
memset(&x, 0, sizeof(x)); memset(&x, 0, sizeof(x));
x.aBlob = (u8*)aBlob; x.aBlob = (u8*)aBlob;
x.nBlob = nBlob; x.nBlob = nBlob;
@@ -2423,7 +2401,7 @@ static void jsonReturnFromBlob(
sqlite3 *db = sqlite3_context_db_handle(pCtx); sqlite3 *db = sqlite3_context_db_handle(pCtx);
n = jsonbPayloadSize(pParse, i, &sz); n = jsonbPayloadSize(pParse, i, &sz);
if( n==0 ) return; if( NEVER(n==0) ) return;
switch( pParse->aBlob[i] & 0x0f ){ switch( pParse->aBlob[i] & 0x0f ){
case JSONB_NULL: { case JSONB_NULL: {
sqlite3_result_null(pCtx); sqlite3_result_null(pCtx);
@@ -2605,7 +2583,7 @@ static int jsonFunctionArgToBlob(
static u8 aNull[] = { 0x00 }; static u8 aNull[] = { 0x00 };
memset(pParse, 0, sizeof(pParse[0])); memset(pParse, 0, sizeof(pParse[0]));
switch( eType ){ switch( eType ){
case SQLITE_NULL: { default: {
pParse->aBlob = aNull; pParse->aBlob = aNull;
pParse->nBlob = 1; pParse->nBlob = 1;
return 0; return 0;
@@ -3041,39 +3019,6 @@ static void jsonParseFunc(
jsonShowParse(p); jsonShowParse(p);
jsonParseFree(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 */ #endif /* SQLITE_DEBUG */
/**************************************************************************** /****************************************************************************
@@ -4805,8 +4750,6 @@ void sqlite3RegisterJsonFunctions(void){
JFUNCTION(json_valid, 2,1,0, 0,0,0, jsonValidFunc), JFUNCTION(json_valid, 2,1,0, 0,0,0, jsonValidFunc),
#if SQLITE_DEBUG #if SQLITE_DEBUG
JFUNCTION(json_parse, 1,1,0, 0,0,0, jsonParseFunc), 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 #endif
WAGGREGATE(json_group_array, 1, 0, 0, WAGGREGATE(json_group_array, 1, 0, 0,
jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse, jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,