SQLite

Check-in [65572223]
Login

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

Overview
Comment:The -DSQLITE_JSON_BLOB_INPUT_BUG_COMPATIBLE compile-time option causes blob inputs to JSON functions that are not JSONB to be processed as if they where text, immulating historical bugging behavior which some applications have come to rely upon. See forum thread 012136abd5292b8d for discussion.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | blob-as-json
Files: files | file ages | folders
SHA3-256: 65572223583d43e1d61ec029641f9d3ff340e68ecfba8342c8d1b0a91a680f2a
User & Date: drh 2024-01-22 14:16:10
Context
2024-01-23
13:21
If a BLOB looks like JSON when cast to text, then treat it as if it really were JSON. This replicates a long-standing bug in the JSON processing routines, and thereby avoids breaking legacy. (check-in: d79a3769 user: drh tags: blob-as-json)
2024-01-22
15:26
Improved error message when a double-quoted string is used and it seems likely that the user wanted a single-quoted string literal. (check-in: 0a834bd8 user: drh tags: improved-dqs-error-msg, blob-as-json)
14:16
The -DSQLITE_JSON_BLOB_INPUT_BUG_COMPATIBLE compile-time option causes blob inputs to JSON functions that are not JSONB to be processed as if they where text, immulating historical bugging behavior which some applications have come to rely upon. See forum thread 012136abd5292b8d for discussion. (check-in: 65572223 user: drh tags: blob-as-json)
14:01
Fix test script literal.test so that it works with SQLITE_OMIT_ALTER_TABLE builds. (check-in: 4dc00f57 user: dan tags: trunk)
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/json.c.

3212
3213
3214
3215
3216
3217
3218
































3219
3220
3221
3222
3223
3224
3225
  if( rc==JSON_LOOKUP_ERROR ){
    sqlite3_result_error(ctx, "malformed JSON", -1);
  }else{
    jsonBadPathError(ctx, zPath);
  }
  return;
}

































/*
** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob,
** from the SQL function argument pArg.  Return a pointer to the new
** JsonParse object.
**
** Ownership of the new JsonParse object is passed to the caller.  The







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







3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
  if( rc==JSON_LOOKUP_ERROR ){
    sqlite3_result_error(ctx, "malformed JSON", -1);
  }else{
    jsonBadPathError(ctx, zPath);
  }
  return;
}

/*
** If pArg is a blob that seems like a JSONB blob, then initialize
** p to point to that JSONB and return TRUE.  If pArg does not seem like
** a JSONB blob, then return FALSE;
**
** This routine is only called if it is already known that pArg is a
** blob.  The only open question is whether or not the blob appears
** to be a JSONB blob.
*/
static int jsonArgIsJsonb(sqlite3_value *pArg, JsonParse *p){
  u32 n, sz = 0;
  p->aBlob = (u8*)sqlite3_value_blob(pArg);
  p->nBlob = (u32)sqlite3_value_bytes(pArg);
  if( p->nBlob==0 ){
    p->aBlob = 0;
    return 0;
  }
  if( NEVER(p->aBlob==0) ){
    return 0;
  }
  if( (p->aBlob[0] & 0x0f)<=JSONB_OBJECT
   && (n = jsonbPayloadSize(p, 0, &sz))>0
   && sz+n==p->nBlob
   && ((p->aBlob[0] & 0x0f)>JSONB_FALSE || sz==0)
  ){
    return 1;
  }
  p->aBlob = 0;
  p->nBlob = 0;
  return 0;
}

/*
** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob,
** from the SQL function argument pArg.  Return a pointer to the new
** JsonParse object.
**
** Ownership of the new JsonParse object is passed to the caller.  The
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283

3284



3285
3286
3287

3288
3289
3290
3291
3292


3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
    memcpy(p->aBlob, pFromCache->aBlob, nBlob);
    p->nBlobAlloc = p->nBlob = nBlob;
    p->hasNonstd = pFromCache->hasNonstd;
    jsonParseFree(pFromCache);
    return p;
  }
  if( eType==SQLITE_BLOB ){
    u32 n, sz = 0;
    p->aBlob = (u8*)sqlite3_value_blob(pArg);
    p->nBlob = (u32)sqlite3_value_bytes(pArg);
    if( p->nBlob==0 ){
      goto json_pfa_malformed;
    }
    if( NEVER(p->aBlob==0) ){
      goto json_pfa_oom;

    }



    if( (p->aBlob[0] & 0x0f)>JSONB_OBJECT ){
      goto json_pfa_malformed;
    }

    n = jsonbPayloadSize(p, 0, &sz);
    if( n==0 
     || sz+n!=p->nBlob
     || ((p->aBlob[0] & 0x0f)<=JSONB_FALSE && sz>0)
    ){


      goto json_pfa_malformed;
    }
    if( (flgs & JSON_EDITABLE)!=0 && jsonBlobMakeEditable(p, 0)==0 ){
      goto json_pfa_oom;
    }
    return p;
  }
  p->zJson = (char*)sqlite3_value_text(pArg);
  p->nJson = sqlite3_value_bytes(pArg);
  if( p->nJson==0 ) goto json_pfa_malformed;
  if( NEVER(p->zJson==0) ) goto json_pfa_oom;
  if( jsonConvertTextToBlob(p, (flgs & JSON_KEEPERROR) ? 0 : ctx) ){
    if( flgs & JSON_KEEPERROR ){







<
|
<
|
|
|
<
<
>

>
>
>
|
|
<
>
|
|
|
|
<
>
>
|
<
<
<
<
|







3301
3302
3303
3304
3305
3306
3307

3308

3309
3310
3311


3312
3313
3314
3315
3316
3317
3318

3319
3320
3321
3322
3323

3324
3325
3326




3327
3328
3329
3330
3331
3332
3333
3334
    memcpy(p->aBlob, pFromCache->aBlob, nBlob);
    p->nBlobAlloc = p->nBlob = nBlob;
    p->hasNonstd = pFromCache->hasNonstd;
    jsonParseFree(pFromCache);
    return p;
  }
  if( eType==SQLITE_BLOB ){

    if( jsonArgIsJsonb(pArg,p) ){

      if( (flgs & JSON_EDITABLE)!=0 && jsonBlobMakeEditable(p, 0)==0 ){
        goto json_pfa_oom;
      }


      return p;
    }
#if defined(SQLITE_JSON_BLOB_INPUT_BUG_COMPATIBLE)
    /* If the input is a BLOB that is not JSONB, fall through into trying
    ** to process that BLOB as if it where text.  This goes against all
    ** historical documentation about how the SQLite JSON functions are 
    ** suppose to work.  Nevertheless, many SQLite implementations prior to

    ** version 3.45.0 contained a bug such that they did behave this way
    ** and some applications came to depend upon this buggy behavior.  The
    ** SQLITE_JSON_BLOB_INPUT_BUG_COMPATIBLE compile-time option provides
    ** a mechanism for those applications to continue working even after
    ** the bug was fixed.  See

    ** https://sqlite.org/forum/forumpost/012136abd5292b8d */
#else
    goto json_pfa_malformed;




#endif
  }
  p->zJson = (char*)sqlite3_value_text(pArg);
  p->nJson = sqlite3_value_bytes(pArg);
  if( p->nJson==0 ) goto json_pfa_malformed;
  if( NEVER(p->zJson==0) ) goto json_pfa_oom;
  if( jsonConvertTextToBlob(p, (flgs & JSON_KEEPERROR) ? 0 : ctx) ){
    if( flgs & JSON_KEEPERROR ){