From 6fea83e52c23c41989c6bdb4da326fe7041a3b8e Mon Sep 17 00:00:00 2001 From: drh Date: Fri, 1 Jul 2011 13:50:44 +0000 Subject: [PATCH 1/2] Ignore the database name on the target table when parsing a CREATE TABLE statement out of the sqlite_master table. This is a fix for ticket [d6ddba6706353] that preserves backwards compatibility. FossilOrigin-Name: 009c96ea7836edf75d65b150055e920079bb45e3 --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/trigger.c | 19 ++++++++++++++++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/manifest b/manifest index fe85fe4d38..7a5f32f68d 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Pass\sthe\sBTREE_UNORDERED\shint\sinto\sboth\ssqlite3BtreeOpen()\sand\sinto\nsqlite3BtreeCreateTable(). -D 2011-06-29T17:11:39.745 +C Ignore\sthe\sdatabase\sname\son\sthe\starget\stable\swhen\sparsing\sa\sCREATE\sTABLE\nstatement\sout\sof\sthe\ssqlite_master\stable.\s\sThis\sis\sa\sfix\sfor\nticket\s[d6ddba6706353]\sthat\spreserves\sbackwards\scompatibility. +D 2011-07-01T13:50:44.953 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in c1d7a7f4fd8da6b1815032efca950e3d5125407e F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -233,7 +233,7 @@ F src/test_vfstrace.c 0b884e06094a746da729119a2cabdc7aa790063d F src/test_wholenumber.c 6129adfbe7c7444f2e60cc785927f3aa74e12290 F src/test_wsd.c 41cadfd9d97fe8e3e4e44f61a4a8ccd6f7ca8fe9 F src/tokenize.c c819d9f72168a035d545a5bdafe9b085b20df705 -F src/trigger.c c836a6caac16ba96611558922106858f6ca3d6bf +F src/trigger.c 1cfb80e2290ef66ea89cb4e821caae65a02c0d56 F src/update.c 80d77311d91ebc06b27149e75701f1b3e9356622 F src/utf.c c53eb7404b3eb5c1cbb5655c6a7a0e0ce6bd50f0 F src/util.c 0f33bbbdfcc4a2d8cf20c3b2a16ffc3b57c58a70 @@ -949,7 +949,7 @@ F tool/symbols.sh bc2a3709940d47c8ac8e0a1fdf17ec801f015a00 F tool/tostr.awk 11760e1b94a5d3dcd42378f3cc18544c06cfa576 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/warnings.sh 2ebae31e1eb352696f3c2f7706a34c084b28c262 -P b9477eb056d120826ed82b0d65e6f27b5d0c087a -R 04bb3d810ba550780b3b6fda2c65d8d3 +P 591de898f41630156cc0fc6ef17dd3ee5e7c479f +R 0219e5330d7b506bd713afee90f76519 U drh -Z 9a15d45fcfccc0dc2281a762dda887ad +Z 5f346c2968ae8ad6ce9a9eb04ceee5d9 diff --git a/manifest.uuid b/manifest.uuid index e1c5a804ad..ae85bf4c8d 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -591de898f41630156cc0fc6ef17dd3ee5e7c479f \ No newline at end of file +009c96ea7836edf75d65b150055e920079bb45e3 \ No newline at end of file diff --git a/src/trigger.c b/src/trigger.c index 6242de5e6c..22c4877b6a 100644 --- a/src/trigger.c +++ b/src/trigger.c @@ -117,15 +117,28 @@ void sqlite3BeginTrigger( goto trigger_cleanup; } } + if( !pTableName || db->mallocFailed ){ + goto trigger_cleanup; + } + + /* A long-standing parser bug is that this syntax was allowed: + ** + ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab .... + ** ^^^^^^^^ + ** + ** To maintain backwards compatibility, ignore the database + ** name on pTableName if we are reparsing our of SQLITE_MASTER. + */ + if( db->init.busy && iDb!=1 ){ + sqlite3DbFree(db, pTableName->a[0].zDatabase); + pTableName->a[0].zDatabase = 0; + } /* If the trigger name was unqualified, and the table is a temp table, ** then set iDb to 1 to create the trigger in the temporary database. ** If sqlite3SrcListLookup() returns 0, indicating the table does not ** exist, the error is caught by the block below. */ - if( !pTableName || db->mallocFailed ){ - goto trigger_cleanup; - } pTab = sqlite3SrcListLookup(pParse, pTableName); if( db->init.busy==0 && pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ From f475e5ae9894f9f406603b2fa831293185fd8fd7 Mon Sep 17 00:00:00 2001 From: drh Date: Fri, 1 Jul 2011 14:22:04 +0000 Subject: [PATCH 2/2] Test case for ticket [d6ddba6706353915ceed] FossilOrigin-Name: 953e169e8a7dac05a0b56b4ef5d500ec8399d37f --- manifest | 12 ++++++------ manifest.uuid | 2 +- test/triggerD.test | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/manifest b/manifest index 7a5f32f68d..52a2aaa76b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Ignore\sthe\sdatabase\sname\son\sthe\starget\stable\swhen\sparsing\sa\sCREATE\sTABLE\nstatement\sout\sof\sthe\ssqlite_master\stable.\s\sThis\sis\sa\sfix\sfor\nticket\s[d6ddba6706353]\sthat\spreserves\sbackwards\scompatibility. -D 2011-07-01T13:50:44.953 +C Test\scase\sfor\sticket\s[d6ddba6706353915ceed] +D 2011-07-01T14:22:04.191 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in c1d7a7f4fd8da6b1815032efca950e3d5125407e F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -840,7 +840,7 @@ F test/trigger9.test 5b0789f1c5c4600961f8e68511b825b87be53e31 F test/triggerA.test eaf11a29db2a11967d2d4b49d37f92bce598194e F test/triggerB.test 56780c031b454abac2340dbb3b71ac5c56c3d7fe F test/triggerC.test 02c690febf608ae20b9af86184a9867f79855b1d -F test/triggerD.test c6add3817351451e419f6ff9e9a259b02b6e2de7 +F test/triggerD.test bfdac1143deee8fb12b6a3640d76e5669a567ff6 F test/tt3_checkpoint.c 415eccce672d681b297485fc20f44cdf0eac93af F test/types.test bf816ce73c7dfcfe26b700c19f97ef4050d194ff F test/types2.test 3555aacf8ed8dc883356e59efc314707e6247a84 @@ -949,7 +949,7 @@ F tool/symbols.sh bc2a3709940d47c8ac8e0a1fdf17ec801f015a00 F tool/tostr.awk 11760e1b94a5d3dcd42378f3cc18544c06cfa576 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/warnings.sh 2ebae31e1eb352696f3c2f7706a34c084b28c262 -P 591de898f41630156cc0fc6ef17dd3ee5e7c479f -R 0219e5330d7b506bd713afee90f76519 +P 009c96ea7836edf75d65b150055e920079bb45e3 +R c79f1fc1420004e852fc4dd4b2df1b0e U drh -Z 5f346c2968ae8ad6ce9a9eb04ceee5d9 +Z fee6ccae1aad0120ae0a15394efb76ef diff --git a/manifest.uuid b/manifest.uuid index ae85bf4c8d..f76287d586 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -009c96ea7836edf75d65b150055e920079bb45e3 \ No newline at end of file +953e169e8a7dac05a0b56b4ef5d500ec8399d37f \ No newline at end of file diff --git a/test/triggerD.test b/test/triggerD.test index 08945081da..0cce4f3cde 100644 --- a/test/triggerD.test +++ b/test/triggerD.test @@ -14,6 +14,12 @@ # the use of these columns in triggers will refer to the column and not # to the actual ROWID. Ticket [34d2ae1c6d08b5271ba5e5592936d4a1d913ffe3] # +# Also, verify that triggers created like this: +# +# CREATE TRIGGER attached.trig AFTER INSERT ON attached.tab ... +# +# can be reparsed as a main database. Ticket [d6ddba6706353915ceedc56b4e3] +# set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -171,4 +177,43 @@ do_test triggerD-3.2 { } {10003 20004} +############################################################################# +# +# Ticket [d6ddba6706353915ceedc56b4e3e72ecb4d77ba4] +# +# The following syntax really should not be allowed: +# +# CREATE TRIGGER xyz.trig BEFORE UPDATE ON xyz.tab BEGIN ... +# +# But a long-standing bug does allow it. And the "xyz.tab" slips into +# the sqlite_master table. We cannot fix the bug simply by disallowing +# "xyz.tab" since that could break legacy applications. We have to +# fix the system so that the "xyz." on "xyz.tab" is ignored. +# Verify that this is the case. +# +do_test triggerD-4.1 { + db close + file delete -force test.db test2.db + sqlite3 db test.db + db eval { + CREATE TABLE t1(x); + ATTACH 'test2.db' AS db2; + CREATE TABLE db2.t2(y); + CREATE TABLE db2.log(z); + CREATE TRIGGER db2.trig AFTER INSERT ON db2.t2 BEGIN + INSERT INTO log(z) VALUES(new.y); + END; + INSERT INTO t2 VALUES(123); + SELECT * FROM log; + } +} {123} +do_test triggerD-4.2 { + sqlite3 db2 test2.db + db2 eval { + INSERT INTO t2 VALUES(234); + SELECT * FROM log; + } +} {123 234} +db2 close + finish_test