Skip to content

Commit b5c5f36

Browse files
author
drh
committed
When backing out a character in a constructed string in JSON, first make sure
the string has not been reset by on OOM. FossilOrigin-Name: 950bf9fe7829864e0abe6d71ca0495f346feb5d7943d76c95e55a6b86ea855da
1 parent 0dfdf2d commit b5c5f36

3 files changed

Lines changed: 26 additions & 21 deletions

File tree

manifest

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
C wasm\sbuild:\sreformulate\san\sawk\sinvocation\sto\saccount\sfor\sawks\swhich\sdo\snot\ssupport\sthe\s-e\sflag.\sProblem\sreported\son\sthe\sforum\svia\sa\sdocker-hosted\sbuild.
2-
D 2024-01-16T14:28:59.125
1+
C When\sbacking\sout\sa\scharacter\sin\sa\sconstructed\sstring\sin\sJSON,\sfirst\smake\ssure\nthe\sstring\shas\snot\sbeen\sreset\sby\son\sOOM.
2+
D 2024-01-20T12:19:16.518
33
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
44
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
55
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -697,7 +697,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51
697697
F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6
698698
F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
699699
F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276
700-
F src/json.c 4913fd22c4f0fa30643afb93a4d78d289cd490620e782b31016c3d4b2049b1cc
700+
F src/json.c fdb6b417e997d9b45ffd817c8c9d955dba11b99fa1199f8d03cb8fc5a9ee0941
701701
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
702702
F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36
703703
F src/main.c 438b95162acfa17b7d218f586f5bde11d6ae82bcf030c9611fc537556870ad6b
@@ -2157,14 +2157,9 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
21572157
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
21582158
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
21592159
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
2160-
P 1066602b2b1976fe58b5150777cced894af17c803e068f5918390d6915b46e1d
2161-
Q +5836657929d7d6d3f0d5865c9f6de4e4c00edd53d15a246802b375fa15d0f255
2162-
R db1e652f52a6c391acbbefee0c13a4c1
2163-
T *branch * branch-3.45
2164-
T *sym-branch-3.45 *
2165-
T -sym-release * Cancelled\sby\sbranch.
2166-
T -sym-trunk * Cancelled\sby\sbranch.
2167-
T -sym-version-3.45.0 * Cancelled\sby\sbranch.
2168-
U stephan
2169-
Z 763d16740affbc27389541de43d084d3
2160+
P 90dd51153fd0a6197e2ee49b5492ad120f0bfc324b60651f3d4f47c286887b46
2161+
Q +666690eb433fe38fa527ccbbb8e2c00041a33939da4f6b8bfb737d664f28f0d8
2162+
R b92c7203f755b0af81ba067b0b4d09c9
2163+
U drh
2164+
Z 339bfa943d20e7905e6df807d394df8f
21702165
# Remove this line to create a well-formed Fossil manifest.

manifest.uuid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
90dd51153fd0a6197e2ee49b5492ad120f0bfc324b60651f3d4f47c286887b46
1+
950bf9fe7829864e0abe6d71ca0495f346feb5d7943d76c95e55a6b86ea855da

src/json.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -589,14 +589,24 @@ static void jsonAppendChar(JsonString *p, char c){
589589
}
590590
}
591591

592+
/* Remove a single character from the end of the string
593+
*/
594+
static void jsonStringTrimOneChar(JsonString *p){
595+
if( p->eErr==0 ){
596+
assert( p->nUsed>0 );
597+
p->nUsed--;
598+
}
599+
}
600+
601+
592602
/* Make sure there is a zero terminator on p->zBuf[]
593603
**
594604
** Return true on success. Return false if an OOM prevents this
595605
** from happening.
596606
*/
597607
static int jsonStringTerminate(JsonString *p){
598608
jsonAppendChar(p, 0);
599-
p->nUsed--;
609+
jsonStringTrimOneChar(p);
600610
return p->eErr==0;
601611
}
602612

@@ -2260,7 +2270,7 @@ static u32 jsonTranslateBlobToText(
22602270
j = jsonTranslateBlobToText(pParse, j, pOut);
22612271
jsonAppendChar(pOut, ',');
22622272
}
2263-
if( sz>0 ) pOut->nUsed--;
2273+
if( sz>0 ) jsonStringTrimOneChar(pOut);
22642274
jsonAppendChar(pOut, ']');
22652275
break;
22662276
}
@@ -2274,7 +2284,7 @@ static u32 jsonTranslateBlobToText(
22742284
jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
22752285
}
22762286
if( x & 1 ) pOut->eErr |= JSTRING_MALFORMED;
2277-
if( sz>0 ) pOut->nUsed--;
2287+
if( sz>0 ) jsonStringTrimOneChar(pOut);
22782288
jsonAppendChar(pOut, '}');
22792289
break;
22802290
}
@@ -4402,7 +4412,7 @@ static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
44024412
if( isFinal ){
44034413
if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf);
44044414
}else{
4405-
pStr->nUsed--;
4415+
jsonStringTrimOneChar(pStr);
44064416
}
44074417
return;
44084418
}else if( isFinal ){
@@ -4412,7 +4422,7 @@ static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
44124422
pStr->bStatic = 1;
44134423
}else{
44144424
sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
4415-
pStr->nUsed--;
4425+
jsonStringTrimOneChar(pStr);
44164426
}
44174427
}else{
44184428
sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
@@ -4522,7 +4532,7 @@ static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
45224532
if( isFinal ){
45234533
if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf);
45244534
}else{
4525-
pStr->nUsed--;
4535+
jsonStringTrimOneChar(pStr);
45264536
}
45274537
return;
45284538
}else if( isFinal ){
@@ -4532,7 +4542,7 @@ static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
45324542
pStr->bStatic = 1;
45334543
}else{
45344544
sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
4535-
pStr->nUsed--;
4545+
jsonStringTrimOneChar(pStr);
45364546
}
45374547
}else{
45384548
sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);

0 commit comments

Comments
 (0)