From be53500356d2b2006671defd9b59a4fef9846257 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 1 Apr 2011 15:15:58 +0000 Subject: [PATCH] Ensure that it is not possible to add a column to a system table using ALTER TABLE. FossilOrigin-Name: d9707ef8dcd29667b6d366897f6ad02c87aa0041 --- manifest | 16 ++++++++-------- manifest.uuid | 2 +- src/alter.c | 28 ++++++++++++++++++++++------ test/alter.test | 19 +++++++++++++++++++ 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/manifest b/manifest index 54e008c6ee..179b8e01d2 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\sthe\sANALYZE-index\senhancement\sinto\strunk. -D 2011-04-01T14:26:14.943 +C Ensure\sthat\sit\sis\snot\spossible\sto\sadd\sa\scolumn\sto\sa\ssystem\stable\susing\sALTER\sTABLE. +D 2011-04-01T15:15:58.380 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 27701a1653595a1f2187dc61c8117e00a6c1d50f F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -114,7 +114,7 @@ F spec.template 86a4a43b99ebb3e75e6b9a735d5fd293a24e90ca F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b F sqlite3.1 6be1ad09113570e1fc8dcaff84c9b0b337db5ffc F sqlite3.pc.in ae6f59a76e862f5c561eb32a380228a02afc3cad -F src/alter.c 6a0c176e64a34929a4436048066a84ef4f1445b3 +F src/alter.c 280f5c04b11b492703a342222b3de0a999445280 F src/analyze.c d0a673d303f611690fc7a3293aaefed57cccc5c8 F src/attach.c 438ea6f6b5d5961c1f49b737f2ce0f14ce7c6877 F src/auth.c 523da7fb4979469955d822ff9298352d6b31de34 @@ -248,7 +248,7 @@ F src/where.c 176574bfeee13775761ce56416f773b0ec115d3f F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 F test/alias.test 4529fbc152f190268a15f9384a5651bbbabc9d87 F test/all.test 51756962d522e474338e9b2ebb26e7364d4aa125 -F test/alter.test 15f9224868b290d6bf7a63f31437f31aee070636 +F test/alter.test 4e47fb9ea59348b88fce4e8bb49de530128b104c F test/alter2.test 75f731508f1bf27ba09a6075c66cd02216ba464b F test/alter3.test 8677e48d95536f7a6ed86a1a774744dadcc22b07 F test/alter4.test 1e5dd6b951e9f65ca66422edff02e56df82dd403 @@ -921,7 +921,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P 7a6d05dfbc36310683dd51a280e9283cef4f9056 365896cb0868fa476e3b4f5a965a1344a2914cc2 -R 7c9526ecaff8bc8061319f5def8d1613 -U drh -Z 1a31242492e35c93d42d83f1529d907f +P 7e237aea22084416d02b89d5223de4e1ca76882d +R 68179f8f20bd7e83c0daf30e8b4b7cff +U dan +Z 07da6539f8d6e81dde928bab49d2b3fe diff --git a/manifest.uuid b/manifest.uuid index 86580230d4..e4175092fa 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -7e237aea22084416d02b89d5223de4e1ca76882d \ No newline at end of file +d9707ef8dcd29667b6d366897f6ad02c87aa0041 \ No newline at end of file diff --git a/src/alter.c b/src/alter.c index 1534fdf69e..aa3fa929f8 100644 --- a/src/alter.c +++ b/src/alter.c @@ -370,6 +370,22 @@ static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){ #endif } +/* +** Parameter zName is the name of a table that is about to be altered +** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN). +** If the table is a system table, this function leaves an error message +** in pParse->zErr (system tables may not be altered) and returns non-zero. +** +** Or, if zName is not a system table, zero is returned. +*/ +static int isSystemTable(Parse *pParse, const char *zName){ + if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ + sqlite3ErrorMsg(pParse, "table %s may not be altered", zName); + return 1; + } + return 0; +} + /* ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" ** command. @@ -420,14 +436,11 @@ void sqlite3AlterRenameTable( /* Make sure it is not a system table being altered, or a reserved name ** that the table is being renamed to. */ - if( sqlite3Strlen30(pTab->zName)>6 - && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) - ){ - sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName); + if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ goto exit_rename_table; } - if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ - goto exit_rename_table; + if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto + exit_rename_table; } #ifndef SQLITE_OMIT_VIEW @@ -759,6 +772,9 @@ void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ sqlite3ErrorMsg(pParse, "Cannot add a column to a view"); goto exit_begin_add_column; } + if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ + goto exit_begin_add_column; + } assert( pTab->addColOffset>0 ); iDb = sqlite3SchemaToIndex(db, pTab->pSchema); diff --git a/test/alter.test b/test/alter.test index bf7cf00624..d4b72a6ae8 100644 --- a/test/alter.test +++ b/test/alter.test @@ -840,4 +840,23 @@ do_test alter-14.2 { } {1 {Cannot add a PRIMARY KEY column}} +#------------------------------------------------------------------------- +# Test that it is not possible to use ALTER TABLE on any system table. +# +set system_table_list {1 sqlite_master} +catchsql ANALYZE +ifcapable analyze { lappend system_table_list 2 sqlite_stat1 } +ifcapable stat2 { lappend system_table_list 3 sqlite_stat2 } + +foreach {tn tbl} $system_table_list { + do_test alter-15.$tn.1 { + catchsql "ALTER TABLE $tbl RENAME TO xyz" + } [list 1 "table $tbl may not be altered"] + + do_test alter-15.$tn.2 { + catchsql "ALTER TABLE $tbl ADD COLUMN xyz" + } [list 1 "table $tbl may not be altered"] +} + + finish_test