SQLite

Check-in [e2bc51bc61]
Login

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

Overview
Comment:Fix another OOM related problem in fkey.c.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e2bc51bc61d54d103ccffd12106c27a574e4e997
User & Date: dan 2009-09-22 16:55:39.000
Context
2009-09-22
19:53
The NO ACTION action really should be no-action and not RESTRICT. (check-in: 55b263fa2b user: drh tags: trunk)
16:55
Fix another OOM related problem in fkey.c. (check-in: e2bc51bc61 user: dan tags: trunk)
16:08
Fix a problem with ON DELETE SET DEFAULT actions. (check-in: 9406995055 user: dan tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/build.c.
2229
2230
2231
2232
2233
2234
2235
2236



2237
2238
2239
2240
2241
2242
2243
  pFKey->deleteConf = (u8)(flags & 0xff);
  pFKey->updateConf = (u8)((flags >> 8 ) & 0xff);
  pFKey->insertConf = (u8)((flags >> 16 ) & 0xff);

  pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
      pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
  );
  if( pNextTo==pFKey ) goto fk_end;



  if( pNextTo ){
    assert( pNextTo->pPrevTo==0 );
    pFKey->pNextTo = pNextTo;
    pNextTo->pPrevTo = pFKey;
  }

  /* Link the foreign key to the table as the last step.







|
>
>
>







2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
  pFKey->deleteConf = (u8)(flags & 0xff);
  pFKey->updateConf = (u8)((flags >> 8 ) & 0xff);
  pFKey->insertConf = (u8)((flags >> 16 ) & 0xff);

  pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
      pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
  );
  if( pNextTo==pFKey ){
    db->mallocFailed = 1;
    goto fk_end;
  }
  if( pNextTo ){
    assert( pNextTo->pPrevTo==0 );
    pFKey->pNextTo = pNextTo;
    pNextTo->pPrevTo = pFKey;
  }

  /* Link the foreign key to the table as the last step.
Changes to src/fkey.c.
360
361
362
363
364
365
366

367

368
369
370
371
372
373
374
    sqlite3VdbeAddOp1(pParse->pVdbe, OP_DeferredCons, nIncr);
  }else{
    assert( nIncr==1 || nIncr==0 );
    sqlite3HaltConstraint(
      pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
    );
  }

  sqlite3WhereEnd(pWInfo);


  /* Clean up the WHERE clause constructed above. */
  sqlite3ExprDelete(db, pWhere);
}

/*
** This function returns a pointer to the head of a linked list of FK







>
|
>







360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
    sqlite3VdbeAddOp1(pParse->pVdbe, OP_DeferredCons, nIncr);
  }else{
    assert( nIncr==1 || nIncr==0 );
    sqlite3HaltConstraint(
      pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
    );
  }
  if( pWInfo ){
    sqlite3WhereEnd(pWInfo);
  }

  /* Clean up the WHERE clause constructed above. */
  sqlite3ExprDelete(db, pWhere);
}

/*
** This function returns a pointer to the head of a linked list of FK
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555

556
557
558
559
560
561
562
      /* TODO */
    }

    /* Create a SrcList structure containing a single table (the table 
    ** the foreign key that refers to this table is attached to). This
    ** is required for the sqlite3WhereXXX() interface.  */
    pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
    if( !pSrc ) return;
    pSrc->a->pTab = pFKey->pFrom;
    pSrc->a->pTab->nRef++;
    pSrc->a->iCursor = pParse->nTab++;

    /* If this is an UPDATE, and none of the columns associated with this
    ** FK have been modified, do not scan the referencing table. Unlike
    ** the compile-time test implemented above, this is not just an 
    ** optimization. It is required so that immediate foreign keys do not 
    ** throw exceptions when the user executes a statement like:
    **
    **     UPDATE refd_table SET refd_column = refd_column
    */
    if( pChanges ){
      int i;
      int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
      for(i=0; i<pFKey->nCol; i++){
        int iOff = (pIdx ? pIdx->aiColumn[i] : -1) + 1;
        sqlite3VdbeAddOp3(v, OP_Ne, regOld+iOff, iJump, regNew+iOff);
      }
      iGoto = sqlite3VdbeAddOp0(v, OP_Goto);
    }

    if( regNew!=0 && pFKey->isDeferred ){
      fkScanReferences(pParse, pSrc, pIdx, pFKey, aiCol, regNew, -1);
    }
    if( regOld!=0 ){
      /* If there is a RESTRICT action configured for the current operation
      ** on the referenced table of this FK, then throw an exception 
      ** immediately if the FK constraint is violated, even if this is a
      ** deferred trigger. That's what RESTRICT means. To defer checking
      ** the constraint, the FK should specify NO ACTION (represented
      ** using OE_None). NO ACTION is the default.  */
      fkScanReferences(pParse, pSrc, pIdx, pFKey, aiCol, regOld, 
          (pChanges!=0 && pFKey->updateConf!=OE_Restrict)
       || (pChanges==0 && pFKey->deleteConf!=OE_Restrict)
      );
    }

    if( pChanges ){
      sqlite3VdbeJumpHere(v, iGoto);
    }
    sqlite3SrcListDelete(db, pSrc);

    sqlite3DbFree(db, aiCol);
  }
}

#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))

/*







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>







508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
      /* TODO */
    }

    /* Create a SrcList structure containing a single table (the table 
    ** the foreign key that refers to this table is attached to). This
    ** is required for the sqlite3WhereXXX() interface.  */
    pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
    if( pSrc ){
      pSrc->a->pTab = pFKey->pFrom;
      pSrc->a->pTab->nRef++;
      pSrc->a->iCursor = pParse->nTab++;
  
      /* If this is an UPDATE, and none of the columns associated with this
      ** FK have been modified, do not scan the referencing table. Unlike
      ** the compile-time test implemented above, this is not just an 
      ** optimization. It is required so that immediate foreign keys do not 
      ** throw exceptions when the user executes a statement like:
      **
      **     UPDATE refd_table SET refd_column = refd_column
      */
      if( pChanges ){
        int i;
        int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
        for(i=0; i<pFKey->nCol; i++){
          int iOff = (pIdx ? pIdx->aiColumn[i] : -1) + 1;
          sqlite3VdbeAddOp3(v, OP_Ne, regOld+iOff, iJump, regNew+iOff);
        }
        iGoto = sqlite3VdbeAddOp0(v, OP_Goto);
      }
  
      if( regNew!=0 && pFKey->isDeferred ){
        fkScanReferences(pParse, pSrc, pIdx, pFKey, aiCol, regNew, -1);
      }
      if( regOld!=0 ){
        /* If there is a RESTRICT action configured for the current operation
        ** on the referenced table of this FK, then throw an exception 
        ** immediately if the FK constraint is violated, even if this is a
        ** deferred trigger. That's what RESTRICT means. To defer checking
        ** the constraint, the FK should specify NO ACTION (represented
        ** using OE_None). NO ACTION is the default.  */
        fkScanReferences(pParse, pSrc, pIdx, pFKey, aiCol, regOld, 
            (pChanges!=0 && pFKey->updateConf!=OE_Restrict)
         || (pChanges==0 && pFKey->deleteConf!=OE_Restrict)
        );
      }
  
      if( pChanges ){
        sqlite3VdbeJumpHere(v, iGoto);
      }
      sqlite3SrcListDelete(db, pSrc);
    }
    sqlite3DbFree(db, aiCol);
  }
}

#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))

/*
Changes to test/fkey_malloc.test.
26
27
28
29
30
31
32

















33
34
35
36
  CREATE TABLE t2(x REFERENCES t1 ON UPDATE CASCADE ON DELETE CASCADE);
} -sqlbody {
  INSERT INTO t1 VALUES('aaa', 1);
  INSERT INTO t2 VALUES('aaa');
  UPDATE t1 SET a = 'bbb';
  DELETE FROM t1;
}


















finish_test









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




26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  CREATE TABLE t2(x REFERENCES t1 ON UPDATE CASCADE ON DELETE CASCADE);
} -sqlbody {
  INSERT INTO t1 VALUES('aaa', 1);
  INSERT INTO t2 VALUES('aaa');
  UPDATE t1 SET a = 'bbb';
  DELETE FROM t1;
}

do_malloc_test fkey_malloc-2 -sqlprep {
  PRAGMA foreign_keys = 1;
  CREATE TABLE t1(a, b, UNIQUE(a, b));
} -sqlbody {
  CREATE TABLE t2(x, y, 
    FOREIGN KEY(x, y) REFERENCES t1(a, b) DEFERRABLE INITIALLY DEFERRED
  );
  BEGIN;
    INSERT INTO t2 VALUES('a', 'b');
    INSERT INTO t1 VALUES('a', 'b');
    UPDATE t1 SET a = 'c';
    DELETE FROM t2;
    INSERT INTO t2 VALUES('d', 'b');
    UPDATE t2 SET x = 'c';
  COMMIT;
}

finish_test


Changes to test/quick.test.
54
55
56
57
58
59
60

61
62
63
64
65
66
67
  crash3.test
  crash4.test
  crash5.test
  crash6.test
  crash7.test
  delete3.test
  fts3.test

  fuzz.test
  fuzz3.test
  fuzz_malloc.test
  in2.test
  loadext.test
  memleak.test
  misc7.test







>







54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  crash3.test
  crash4.test
  crash5.test
  crash6.test
  crash7.test
  delete3.test
  fts3.test
  fkey_malloc.test
  fuzz.test
  fuzz3.test
  fuzz_malloc.test
  in2.test
  loadext.test
  memleak.test
  misc7.test