SQLite

Check-in [4f1b5229a3]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Escape control characters in JSON. Fix for ticket [ad2559db380abf8].
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4f1b5229a3bbc9d40b7433a5eb3139d59d31dcb1
User & Date: drh 2016-02-04 10:28:57.253
Context
2016-02-04
11:15
Use sqlite3_malloc64() instead of sqlite3_malloc() in the spellfix extension. (check-in: 634d008c34 user: drh tags: trunk)
10:28
Escape control characters in JSON. Fix for ticket [ad2559db380abf8]. (check-in: 4f1b5229a3 user: drh tags: trunk)
09:48
Refinements to synchronous logic: (1) Use PAGER_SYNCHRONOUS_FULL rather than the corresponding magic number. (2) Honor SQLITE_NO_SYNC on xDelete calls with sync (3) Count xDelete syncs during testing (4) Fix #ifs on SQLITE_EXTRA_DURABLE so that directory syncs on journal unlink are off by default. (check-in: e3157cb5ad user: drh tags: trunk)
Changes
Unified Diff Show Whitespace Changes Patch
Changes to ext/misc/json1.c.
272
273
274
275
276
277
278
279
280

281
282






















283
284
285
286
287
288
289
** string.
*/
static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
  u32 i;
  if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
  p->zBuf[p->nUsed++] = '"';
  for(i=0; i<N; i++){
    char c = zIn[i];
    if( c=='"' || c=='\\' ){

      if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
      p->zBuf[p->nUsed++] = '\\';






















    }
    p->zBuf[p->nUsed++] = c;
  }
  p->zBuf[p->nUsed++] = '"';
  assert( p->nUsed<p->nAlloc );
}








|

>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
** string.
*/
static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
  u32 i;
  if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
  p->zBuf[p->nUsed++] = '"';
  for(i=0; i<N; i++){
    unsigned char c = ((unsigned const char*)zIn)[i];
    if( c=='"' || c=='\\' ){
      json_simple_escape:
      if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
      p->zBuf[p->nUsed++] = '\\';
    }else if( c<=0x1f ){
      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' );
      if( aSpecial[c] ){
        c = aSpecial[c];
        goto json_simple_escape;
      }
      if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=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++] = '0' + (c>>4);
      c = "0123456789abcdef"[c&0xf];
    }
    p->zBuf[p->nUsed++] = c;
  }
  p->zBuf[p->nUsed++] = '"';
  assert( p->nUsed<p->nAlloc );
}

Changes to test/json101.test.
337
338
339
340
341
342
343















344
345
  7.7  0  char(0x20,0x09,0x0a,0x0c,0x0d,0x20)
} {
  do_execsql_test json-$tn.1 \
    "SELECT json_valid(printf('%s{%s\"x\"%s:%s9%s}%s',
         $::ws,$::ws,$::ws,$::ws,$::ws,$::ws));" \
  $isvalid
}
















finish_test







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
  7.7  0  char(0x20,0x09,0x0a,0x0c,0x0d,0x20)
} {
  do_execsql_test json-$tn.1 \
    "SELECT json_valid(printf('%s{%s\"x\"%s:%s9%s}%s',
         $::ws,$::ws,$::ws,$::ws,$::ws,$::ws));" \
  $isvalid
}

# Ticket https://www.sqlite.org/src/info/ad2559db380abf8e
# Control characters must be escaped in JSON strings.
#
do_execsql_test json-8.1 {
  DROP TABLE IF EXISTS t8;
  CREATE TABLE t8(a,b);
  INSERT INTO t8(a) VALUES('abc' || char(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35) || 'xyz');
  UPDATE t8 SET b=json_array(a);
  SELECT b FROM t8;
} {{["abc\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#xyz"]}}
do_execsql_test json-8.2 {
  SELECT a=json_extract(b,'$[0]') FROM t8;
} {1}


finish_test