1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-08 14:02:16 +03:00

Allow control characters in JSON5 string literals.

[forum:/forumpost/05182119f69c3a92|Forum thread 05182119f69c3a92].

FossilOrigin-Name: 34709c7cc910539e23a830ad8b589a97a88be25e924a59670c1017fb51447dad
This commit is contained in:
drh
2024-01-31 13:46:44 +00:00
parent b202a452ad
commit c24f53635c
4 changed files with 86 additions and 35 deletions

View File

@@ -621,6 +621,40 @@ static void jsonAppendSeparator(JsonString *p){
jsonAppendChar(p, ',');
}
/* c is a control character. Append the canonical JSON representation
** of that control character to p.
**
** This routine assumes that the output buffer has already been enlarged
** sufficiently to hold the worst-case encoding plus a nul terminator.
*/
static void jsonAppendControlChar(JsonString *p, u8 c){
static const char aSpecial[] = {
0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
assert( sizeof(aSpecial)==32 );
assert( aSpecial['\b']=='b' );
assert( aSpecial['\f']=='f' );
assert( aSpecial['\n']=='n' );
assert( aSpecial['\r']=='r' );
assert( aSpecial['\t']=='t' );
assert( c>=0 && c<sizeof(aSpecial) );
assert( p->nUsed+7 <= p->nAlloc );
if( aSpecial[c] ){
p->zBuf[p->nUsed] = '\\';
p->zBuf[p->nUsed+1] = aSpecial[c];
p->nUsed += 2;
}else{
p->zBuf[p->nUsed] = '\\';
p->zBuf[p->nUsed+1] = 'u';
p->zBuf[p->nUsed+2] = '0';
p->zBuf[p->nUsed+3] = '0';
p->zBuf[p->nUsed+4] = "0123456789abcdef"[c>>4];
p->zBuf[p->nUsed+5] = "0123456789abcdef"[c&0xf];
p->nUsed += 6;
}
}
/* Append the N-byte string in zIn to the end of the JsonString string
** under construction. Enclose the string in double-quotes ("...") and
** escape any double-quotes or backslash characters contained within the
@@ -680,35 +714,14 @@ static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
}
c = z[0];
if( c=='"' || c=='\\' ){
json_simple_escape:
if( (p->nUsed+N+3 > p->nAlloc) && jsonStringGrow(p,N+3)!=0 ) return;
p->zBuf[p->nUsed++] = '\\';
p->zBuf[p->nUsed++] = c;
}else if( c=='\'' ){
p->zBuf[p->nUsed++] = c;
}else{
static const char aSpecial[] = {
0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
assert( sizeof(aSpecial)==32 );
assert( aSpecial['\b']=='b' );
assert( aSpecial['\f']=='f' );
assert( aSpecial['\n']=='n' );
assert( aSpecial['\r']=='r' );
assert( aSpecial['\t']=='t' );
assert( c>=0 && c<sizeof(aSpecial) );
if( aSpecial[c] ){
c = aSpecial[c];
goto json_simple_escape;
}
if( (p->nUsed+N+7 > p->nAlloc) && jsonStringGrow(p,N+7)!=0 ) return;
p->zBuf[p->nUsed++] = '\\';
p->zBuf[p->nUsed++] = 'u';
p->zBuf[p->nUsed++] = '0';
p->zBuf[p->nUsed++] = '0';
p->zBuf[p->nUsed++] = "0123456789abcdef"[c>>4];
p->zBuf[p->nUsed++] = "0123456789abcdef"[c&0xf];
jsonAppendControlChar(p, c);
}
z++;
N--;
@@ -1409,6 +1422,9 @@ static u32 jsonbValidityCheck(
if( !jsonIsOk[z[j]] && z[j]!='\'' ){
if( z[j]=='"' ){
if( x==JSONB_TEXTJ ) return j+1;
}else if( z[j]<=0x1f ){
/* Control characters in JSON5 string literals are ok */
if( x==JSONB_TEXTJ ) return j+1;
}else if( z[j]!='\\' || j+1>=k ){
return j+1;
}else if( strchr("\"\\/bfnrt",z[j+1])!=0 ){
@@ -1703,9 +1719,10 @@ json_parse_restart:
return -1;
}
}else if( c<=0x1f ){
/* Control characters are not allowed in strings */
pParse->iErr = j;
return -1;
/* Control characters are not allowed in canonical JSON string
** literals, but are allowed in JSON5 string literals. */
opcode = JSONB_TEXT5;
pParse->hasNonstd = 1;
}else if( c=='"' ){
opcode = JSONB_TEXT5;
}
@@ -2186,7 +2203,7 @@ static u32 jsonTranslateBlobToText(
zIn = (const char*)&pParse->aBlob[i+n];
jsonAppendChar(pOut, '"');
while( sz2>0 ){
for(k=0; k<sz2 && zIn[k]!='\\' && zIn[k]!='"'; k++){}
for(k=0; k<sz2 && (jsonIsOk[(u8)zIn[k]] || zIn[k]=='\''); k++){}
if( k>0 ){
jsonAppendRawNZ(pOut, zIn, k);
if( k>=sz2 ){
@@ -2201,6 +2218,13 @@ static u32 jsonTranslateBlobToText(
sz2--;
continue;
}
if( zIn[0]<=0x1f ){
if( pOut->nUsed+7<pOut->nAlloc && jsonStringGrow(pOut,7) ) break;
jsonAppendControlChar(pOut, zIn[0]);
zIn++;
sz2--;
continue;
}
assert( zIn[0]=='\\' );
assert( sz2>=1 );
if( sz2<2 ){
@@ -4391,7 +4415,7 @@ static void jsonErrorFunc(
/* Convert byte-offset s.iErr into a character offset */
u32 k;
assert( s.zJson!=0 ); /* Because s.oom is false */
for(k=0; k<s.iErr && ALWAYS(s.zJson[k]); k++){
for(k=0; k<s.iErr && s.zJson[k]; k++){
if( (s.zJson[k] & 0xc0)!=0x80 ) iErrPos++;
}
iErrPos++;