From: dan Date: Thu, 5 Feb 2015 17:36:30 +0000 (+0000) Subject: Prevent ota updates from violating NOT NULL constraints. Add a comment to the "limita... X-Git-Tag: version-3.8.11~252^2~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a8ccf3ed740faa517d5dc06e649bafb94677f0a3;p=thirdparty%2Fsqlite.git Prevent ota updates from violating NOT NULL constraints. Add a comment to the "limitations" section of sqlite3ota.h saying that CHECK constraints are not enforced. FossilOrigin-Name: 74e073dd604142212f3d3e1931065d124daabd80 --- diff --git a/ext/ota/ota10.test b/ext/ota/ota10.test index bca34d68cb..02931a3bf5 100644 --- a/ext/ota/ota10.test +++ b/ext/ota/ota10.test @@ -117,6 +117,59 @@ ifcapable fts3 { } {1 {SQLITE_ERROR - SQL logic error or missing database}} } +#-------------------------------------------------------------------- +# Test that it is not possible to violate a NOT NULL constraint by +# applying an OTA update. +# +do_execsql_test 4.1 { + CREATE TABLE t2(a INTEGER NOT NULL, b TEXT NOT NULL, c PRIMARY KEY); + CREATE TABLE t3(a INTEGER NOT NULL, b TEXT NOT NULL, c INTEGER PRIMARY KEY); + CREATE TABLE t4(a, b, PRIMARY KEY(a, b)) WITHOUT ROWID; + + INSERT INTO t2 VALUES(10, 10, 10); + INSERT INTO t3 VALUES(10, 10, 10); + INSERT INTO t4 VALUES(10, 10); + +} + +foreach {tn error ota} { + 2 {SQLITE_CONSTRAINT - NOT NULL constraint failed: t2.a} { + INSERT INTO data_t2 VALUES(NULL, 'abc', 1, 0); + } + 3 {SQLITE_CONSTRAINT - NOT NULL constraint failed: t2.b} { + INSERT INTO data_t2 VALUES(2, NULL, 1, 0); + } + 4 {SQLITE_CONSTRAINT - NOT NULL constraint failed: t2.c} { + INSERT INTO data_t2 VALUES(1, 'abc', NULL, 0); + } + 5 {SQLITE_MISMATCH - datatype mismatch} { + INSERT INTO data_t3 VALUES(1, 'abc', NULL, 0); + } + 6 {SQLITE_CONSTRAINT - NOT NULL constraint failed: t4.b} { + INSERT INTO data_t4 VALUES('a', NULL, 0); + } + 7 {SQLITE_CONSTRAINT - NOT NULL constraint failed: t4.a} { + INSERT INTO data_t4 VALUES(NULL, 'a', 0); + } + 8 {SQLITE_CONSTRAINT - NOT NULL constraint failed: t2.a} { + INSERT INTO data_t2 VALUES(NULL, 0, 10, 'x..'); + } + 9 {SQLITE_CONSTRAINT - NOT NULL constraint failed: t3.b} { + INSERT INTO data_t3 VALUES(10, NULL, 10, '.x.'); + } +} { + set ota " + CREATE TABLE data_t2(a, b, c, ota_control); + CREATE TABLE data_t3(a, b, c, ota_control); + CREATE TABLE data_t4(a, b, ota_control); + $ota + " + do_test 4.$tn { + list [catch { apply_ota $ota } msg] $msg + } [list 1 $error] +} + finish_test + diff --git a/ext/ota/sqlite3ota.c b/ext/ota/sqlite3ota.c index 959622bcf4..dd742c4ab2 100644 --- a/ext/ota/sqlite3ota.c +++ b/ext/ota/sqlite3ota.c @@ -112,6 +112,7 @@ struct OtaObjIter { char **azTblType; /* Array of target column types */ int *aiSrcOrder; /* src table col -> target table col */ unsigned char *abTblPk; /* Array of flags, set on target PK columns */ + unsigned char *abNotNull; /* Array of flags, set on NOT NULL columns */ int eType; /* Table type - an OTA_PK_XXX value */ /* Output variables. zTbl==0 implies EOF. */ @@ -255,6 +256,7 @@ static void otaObjIterFreeCols(OtaObjIter *pIter){ pIter->azTblType = 0; pIter->aiSrcOrder = 0; pIter->abTblPk = 0; + pIter->abNotNull = 0; pIter->nTblCol = 0; sqlite3_free(pIter->zMask); pIter->zMask = 0; @@ -417,7 +419,7 @@ static int otaMPrintfExec(sqlite3ota *p, const char *zFmt, ...){ ** error code in the OTA handle passed as the first argument. */ static void otaAllocateIterArrays(sqlite3ota *p, OtaObjIter *pIter, int nCol){ - int nByte = (sizeof(char*) * 2 + sizeof(int) + sizeof(unsigned char)) * nCol; + int nByte = (2*sizeof(char*) + sizeof(int) + 2*sizeof(unsigned char)) * nCol; char **azNew; assert( p->rc==SQLITE_OK ); @@ -428,6 +430,7 @@ static void otaAllocateIterArrays(sqlite3ota *p, OtaObjIter *pIter, int nCol){ pIter->azTblType = &azNew[nCol]; pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol]; pIter->abTblPk = (unsigned char*)&pIter->aiSrcOrder[nCol]; + pIter->abNotNull = (unsigned char*)&pIter->abTblPk[nCol]; }else{ p->rc = SQLITE_NOMEM; } @@ -655,6 +658,7 @@ static int otaObjIterCacheTableInfo(sqlite3ota *p, OtaObjIter *pIter){ ); }else{ int iPk = sqlite3_column_int(pStmt, 5); + int bNotNull = sqlite3_column_int(pStmt, 3); const char *zType = (const char*)sqlite3_column_text(pStmt, 2); if( i!=iOrder ){ @@ -664,6 +668,7 @@ static int otaObjIterCacheTableInfo(sqlite3ota *p, OtaObjIter *pIter){ pIter->azTblType[iOrder] = otaStrndup(zType, -1, &p->rc); pIter->abTblPk[iOrder] = (iPk!=0); + pIter->abNotNull[iOrder] = (unsigned char)bNotNull || (iPk!=0); iOrder++; } } @@ -1141,8 +1146,9 @@ static void otaCreateImposterTable(sqlite3ota *p, OtaObjIter *pIter){ ** "PRIMARY KEY" to the imposter table column declaration. */ zPk = "PRIMARY KEY "; } - zSql = otaMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s", - zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl + zSql = otaMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s", + zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl, + (pIter->abNotNull[iCol] ? " NOT NULL" : "") ); zComma = ", "; } diff --git a/ext/ota/sqlite3ota.h b/ext/ota/sqlite3ota.h index 44aff2e30d..0749ee9aea 100644 --- a/ext/ota/sqlite3ota.h +++ b/ext/ota/sqlite3ota.h @@ -71,6 +71,8 @@ ** ** * No foreign key violations are detected or reported. ** +** * CHECK constraints are not enforced. +** ** * No constraint handling mode except for "OR ROLLBACK" is supported. ** ** diff --git a/manifest b/manifest index 44587374cc..3f58be9e62 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Figure\sout\sthe\sprimary-key\stype\sof\sa\stable\susing\squeries\sof\ssqlite_master\nand\sthe\stable_info\sand\sindex_list\spragmas,\sobviating\sthe\sneed\sfor\nSQLITE_TESTCTRL_TBLTYPE. -D 2015-02-05T01:49:31.187 +C Prevent\sota\supdates\sfrom\sviolating\sNOT\sNULL\sconstraints.\sAdd\sa\scomment\sto\sthe\s"limitations"\ssection\sof\ssqlite3ota.h\ssaying\sthat\sCHECK\sconstraints\sare\snot\senforced. +D 2015-02-05T17:36:30.528 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 6b9e7677829aa94b9f30949656e27312aefb9a46 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -126,7 +126,7 @@ F ext/misc/wholenumber.c 784b12543d60702ebdd47da936e278aa03076212 F ext/ota/README.txt 78d4a9f78f567d4bf826cf0f02df6254902562ca F ext/ota/ota.c c11a85af71dccc45976622fe7a51169a481caa91 F ext/ota/ota1.test 719854e444dff2ead58ff6b62d8315954bd7762a -F ext/ota/ota10.test ab815dff9cef7248c504f06b888627d236f25e9c +F ext/ota/ota10.test 69bded736f5dd1e07b96e5c51c081e2fd051cb12 F ext/ota/ota2.test 2829bc08ffbb71b605392a68fedfd554763356a7 F ext/ota/ota3.test a77efbce7723332eb688d2b28bf18204fc9614d7 F ext/ota/ota4.test 82434aa39c9acca6cd6317f6b0ab07b0ec6c2e7d @@ -136,8 +136,8 @@ F ext/ota/ota7.test 1fe2c5761705374530e29f70c39693076028221a F ext/ota/ota8.test cd70e63a0c29c45c0906692827deafa34638feda F ext/ota/ota9.test d3eee95dd836824d07a22e5efcdb7bf6e869358b F ext/ota/otafault.test 508ba87c83d632670ac0f94371a465d4bb4d49dd -F ext/ota/sqlite3ota.c 0721c7dae5d16624d708ec13ad372c4738f2a23b -F ext/ota/sqlite3ota.h ce378c0c503f625611713133f9c79704ea4ee7a4 +F ext/ota/sqlite3ota.c 6f4f4e07d48ad51b745a5d30ba601a6895fd15ba +F ext/ota/sqlite3ota.h 22423135fcdf7b242af8039b2ebf8437685e87c9 F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761 F ext/rtree/rtree.c 14e6239434d4e3f65d3e90320713f26aa24e167f F ext/rtree/rtree.h 834dbcb82dc85b2481cde6a07cdadfddc99e9b9e @@ -1255,7 +1255,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 21e95d28a51e56b45a9d4166475972535e53f954 -R 20b83842609fc0bd353b0fba0ba7b9e9 -U drh -Z ce11446a64cadc5d0eb91c8707e9ad41 +P 50ecdfc443b51e3569c6add2fba5132f959c61cb +R 39225d5cd1d4a3167fd9b3981c9a3ddd +U dan +Z 95e5525c749de3fda07c16abd9286707 diff --git a/manifest.uuid b/manifest.uuid index 6416dfb803..189550920b 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -50ecdfc443b51e3569c6add2fba5132f959c61cb \ No newline at end of file +74e073dd604142212f3d3e1931065d124daabd80 \ No newline at end of file