From cf188905eba2defd493f0cf5e51b497c78bd0406 Mon Sep 17 00:00:00 2001 From: Lalit Maganti Date: Mon, 15 Jun 2020 19:47:59 +0100 Subject: sqlite: Upgrade to SQLite 3.32.2 This change is a cherry-pick of aosp/1337179 which is being merged here because of merge conflicts due to changes [1] and [2] which were made in internal master and rvc-dev but not in AOSP. Downloaded from https://www.sqlite.org/2020/sqlite-autoconf-3320200.tar.gz $ sha1sum sqlite-autoconf-3320200.tar.gz 429e3f2d0b16a95ad1025a97b2a328d0b4037575 sqlite-autoconf-3320200.tar.gz dist/orig contains the stock sqlite3 code, as packaged in the tar.gz file above. dist contains a copy of dist/orig, but with the Android.patch file applied. More details about the release are available here: https://www.sqlite.org/releaselog/3_32_2.html [1] ag/0f62c1a0e8b463b5b27035e11478e66d7daec69a [2] ag/88147c430cc041a27d07e593ffea12b7aa586f7a Test: code compiles and device boots with no problems. Test: select sqlite_version() - returns 3.32.2 Test: atest cts/SQLiteDatabaseTest all passed. Test: atest cts/SQLiteSecurityTest Bug: 159105124 Change-Id: I5c5fa2fd90292483bf20d8ae58adad41c48aa4e6 --- README.version | 5 +- dist/Android.patch | 28 +- dist/orig/shell.c | 3189 ++++++- dist/orig/sqlite3.c | 23424 ++++++++++++++++++++++++++++++----------------- dist/orig/sqlite3.h | 862 +- dist/orig/sqlite3ext.h | 29 +- dist/shell.c | 3189 ++++++- dist/sqlite3.c | 23424 ++++++++++++++++++++++++++++++----------------- dist/sqlite3.h | 862 +- dist/sqlite3ext.h | 29 +- 10 files changed, 36812 insertions(+), 18229 deletions(-) diff --git a/README.version b/README.version index e8aeabd..6bfb1b3 100644 --- a/README.version +++ b/README.version @@ -1,4 +1,3 @@ -URL: https://www.sqlite.org/src/tarball/b302b260/SQLite-b302b260.tar.gz -Version: 3.28.0 b2325a6e -This patch fixes app compatibility bug b/147928666 +URL: https://www.sqlite.org/2020/sqlite-autoconf-3320200.tar.gz +Version: 3.32.2 44a58d6c BugComponent: 24950 diff --git a/dist/Android.patch b/dist/Android.patch index cda5b55..99eb4d5 100644 --- a/dist/Android.patch +++ b/dist/Android.patch @@ -1,6 +1,6 @@ ---- orig/shell.c 2020-05-07 10:06:07.883965871 +0100 -+++ shell.c 2020-05-07 10:08:55.333081159 +0100 -@@ -87,6 +87,12 @@ +--- orig/shell.c 2020-06-15 19:44:34.058537056 +0100 ++++ shell.c 2020-06-15 19:44:34.142536089 +0100 +@@ -95,6 +95,12 @@ #endif #include #include @@ -13,7 +13,7 @@ #if !defined(_WIN32) && !defined(WIN32) # include -@@ -11698,6 +11704,23 @@ +@@ -12957,6 +12963,23 @@ sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, editFunc, 0, 0); #endif @@ -37,9 +37,9 @@ if( p->openMode==SHELL_OPEN_ZIPFILE ){ char *zSql = sqlite3_mprintf( "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename); ---- orig/sqlite3.c 2020-05-07 10:06:07.867965765 +0100 -+++ sqlite3.c 2020-05-07 10:08:55.333081159 +0100 -@@ -32446,6 +32446,10 @@ +--- orig/sqlite3.c 2020-06-15 19:44:34.066536964 +0100 ++++ sqlite3.c 2020-06-15 19:44:34.166535813 +0100 +@@ -33395,6 +33395,10 @@ # include #endif @@ -50,7 +50,7 @@ #ifdef HAVE_UTIME # include #endif -@@ -33205,6 +33209,12 @@ +@@ -34155,6 +34159,12 @@ #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); #endif @@ -63,7 +63,7 @@ } return fd; } -@@ -33785,7 +33795,13 @@ +@@ -34735,7 +34745,13 @@ ** and move on. */ static void robust_close(unixFile *pFile, int h, int lineno){ @@ -77,7 +77,7 @@ unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", pFile ? pFile->zPath : 0, lineno); } -@@ -36318,7 +36334,7 @@ +@@ -37269,7 +37285,7 @@ SimulateIOError( rc=1 ); if( rc!=0 ){ storeLastErrno((unixFile*)id, errno); @@ -86,7 +86,7 @@ } *pSize = buf.st_size; -@@ -36354,7 +36370,7 @@ +@@ -37305,7 +37321,7 @@ struct stat buf; /* Used to hold return values of fstat() */ if( osFstat(pFile->h, &buf) ){ @@ -95,7 +95,7 @@ } nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; -@@ -37040,7 +37056,7 @@ +@@ -38000,7 +38016,7 @@ ** with the same permissions. */ if( osFstat(pDbFd->h, &sStat) ){ @@ -104,7 +104,7 @@ goto shm_open_err; } -@@ -124046,7 +124062,7 @@ +@@ -128218,7 +128234,7 @@ } if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){ sqlite3SetString(pzErrMsg, db, "unsupported file format"); @@ -113,7 +113,7 @@ goto initone_error_out; } -@@ -164418,13 +164434,25 @@ +@@ -170128,13 +170144,25 @@ ** module with sqlite. */ if( SQLITE_OK==rc diff --git a/dist/orig/shell.c b/dist/orig/shell.c index 10d8cc1..974f57a 100644 --- a/dist/orig/shell.c +++ b/dist/orig/shell.c @@ -35,6 +35,14 @@ #define _CRT_SECURE_NO_WARNINGS #endif +/* +** Determine if we are dealing with WinRT, which provides only a subset of +** the full Win32 API. +*/ +#if !defined(SQLITE_OS_WINRT) +# define SQLITE_OS_WINRT 0 +#endif + /* ** Warning pragmas copied from msvc.h in the core. */ @@ -147,22 +155,26 @@ typedef unsigned char u8; #if defined(_WIN32) || defined(WIN32) -# include -# include -# define isatty(h) _isatty(h) -# ifndef access -# define access(f,m) _access((f),(m)) -# endif -# ifndef unlink -# define unlink _unlink -# endif -# ifndef strdup -# define strdup _strdup +# if SQLITE_OS_WINRT +# define SQLITE_OMIT_POPEN 1 +# else +# include +# include +# define isatty(h) _isatty(h) +# ifndef access +# define access(f,m) _access((f),(m)) +# endif +# ifndef unlink +# define unlink _unlink +# endif +# ifndef strdup +# define strdup _strdup +# endif +# undef popen +# define popen _popen +# undef pclose +# define pclose _pclose # endif -# undef popen -# define popen _popen -# undef pclose -# define pclose _pclose #else /* Make sure isatty() has a prototype. */ extern int isatty(int); @@ -191,6 +203,9 @@ typedef unsigned char u8; #define ToLower(X) (char)tolower((unsigned char)X) #if defined(_WIN32) || defined(WIN32) +#if SQLITE_OS_WINRT +#include +#endif #include /* string conversion routines only needed on Win32 */ @@ -206,7 +221,7 @@ extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); ** rendering quoted strings that contain \n characters). The following ** routines take care of that. */ -#if defined(_WIN32) || defined(WIN32) +#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT static void setBinaryMode(FILE *file, int isOutput){ if( isOutput ) fflush(file); _setmode(_fileno(file), _O_BINARY); @@ -310,6 +325,7 @@ static int hasTimer(void){ if( getProcessTimesAddr ){ return 1; } else { +#if !SQLITE_OS_WINRT /* GetProcessTimes() isn't supported in WIN95 and some other Windows ** versions. See if the version we are running on has it, and if it ** does, save off a pointer to it and the current process handle. @@ -326,6 +342,7 @@ static int hasTimer(void){ FreeLibrary(hinstLib); } } +#endif } return 0; } @@ -415,6 +432,15 @@ static sqlite3 *globalDb = 0; */ static volatile int seenInterrupt = 0; +#ifdef SQLITE_DEBUG +/* +** Out-of-memory simulator variables +*/ +static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */ +static unsigned int oomRepeat = 0; /* Number of OOMs in a row */ +static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */ +#endif /* SQLITE_DEBUG */ + /* ** This is the name of our program. It is set in main(), used ** in a number of other places, mostly for error messages. @@ -466,6 +492,49 @@ static void shell_out_of_memory(void){ exit(1); } +#ifdef SQLITE_DEBUG +/* This routine is called when a simulated OOM occurs. It is broken +** out as a separate routine to make it easy to set a breakpoint on +** the OOM +*/ +void shellOomFault(void){ + if( oomRepeat>0 ){ + oomRepeat--; + }else{ + oomCounter--; + } +} +#endif /* SQLITE_DEBUG */ + +#ifdef SQLITE_DEBUG +/* This routine is a replacement malloc() that is used to simulate +** Out-Of-Memory (OOM) errors for testing purposes. +*/ +static void *oomMalloc(int nByte){ + if( oomCounter ){ + if( oomCounter==1 ){ + shellOomFault(); + return 0; + }else{ + oomCounter--; + } + } + return defaultMalloc(nByte); +} +#endif /* SQLITE_DEBUG */ + +#ifdef SQLITE_DEBUG +/* Register the OOM simulator. This must occur before any memory +** allocations */ +static void registerOomSimulator(void){ + sqlite3_mem_methods mem; + sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem); + defaultMalloc = mem.xMalloc; + mem.xMalloc = oomMalloc; + sqlite3_config(SQLITE_CONFIG_MALLOC, &mem); +} +#endif + /* ** Write I/O traces to the following stream. */ @@ -983,6 +1052,7 @@ static void shellAddSchemaName( ** We need several support functions from the SQLite core. */ +/* #include "sqlite3.h" */ /* ** We need several things from the ANSI and MSVCRT headers. @@ -1336,6 +1406,7 @@ INT closedir( ** is used. If SIZE is included it must be one of the integers 224, 256, ** 384, or 512, to determine SHA3 hash variant that is computed. */ +/* #include "sqlite3ext.h" */ SQLITE_EXTENSION_INIT1 #include #include @@ -2005,19 +2076,23 @@ int sqlite3_shathree_init( int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0, - sha3Func, 0, 0); + rc = sqlite3_create_function(db, "sha3", 1, + SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, + 0, sha3Func, 0, 0); if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0, - sha3Func, 0, 0); + rc = sqlite3_create_function(db, "sha3", 2, + SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, + 0, sha3Func, 0, 0); } if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0, - sha3QueryFunc, 0, 0); + rc = sqlite3_create_function(db, "sha3_query", 1, + SQLITE_UTF8 | SQLITE_DIRECTONLY, + 0, sha3QueryFunc, 0, 0); } if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0, - sha3QueryFunc, 0, 0); + rc = sqlite3_create_function(db, "sha3_query", 2, + SQLITE_UTF8 | SQLITE_DIRECTONLY, + 0, sha3QueryFunc, 0, 0); } return rc; } @@ -2099,6 +2174,7 @@ int sqlite3_shathree_init( ** And the paths returned in the "name" column of the table are also ** relative to directory $dir. */ +/* #include "sqlite3ext.h" */ SQLITE_EXTENSION_INIT1 #include #include @@ -2419,6 +2495,7 @@ static int writeFile( if( mtime>=0 ){ #if defined(_WIN32) +#if !SQLITE_OS_WINRT /* Windows */ FILETIME lastAccess; FILETIME lastWrite; @@ -2449,6 +2526,7 @@ static int writeFile( }else{ return 1; } +#endif #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */ /* Recent unix */ struct timespec times[2]; @@ -2610,6 +2688,7 @@ static int fsdirConnect( pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) ); if( pNew==0 ) return SQLITE_NOMEM; memset(pNew, 0, sizeof(*pNew)); + sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); } *ppVtab = (sqlite3_vtab*)pNew; return rc; @@ -3003,10 +3082,12 @@ int sqlite3_fileio_init( int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0, + rc = sqlite3_create_function(db, "readfile", 1, + SQLITE_UTF8|SQLITE_DIRECTONLY, 0, readfileFunc, 0, 0); if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0, + rc = sqlite3_create_function(db, "writefile", -1, + SQLITE_UTF8|SQLITE_DIRECTONLY, 0, writefileFunc, 0, 0); } if( rc==SQLITE_OK ){ @@ -3056,6 +3137,7 @@ int sqlite3_fileio_init( ** faster than any human can type. ** */ +/* #include "sqlite3ext.h" */ SQLITE_EXTENSION_INIT1 #include #include @@ -3140,6 +3222,7 @@ static int completionConnect( #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */ #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */ + sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); rc = sqlite3_declare_vtab(db, "CREATE TABLE x(" " candidate TEXT," @@ -3572,6 +3655,7 @@ int sqlite3_completion_init( ** If the file being opened is not an appended database, then this shim is ** a pass-through into the default underlying VFS. **/ +/* #include "sqlite3ext.h" */ SQLITE_EXTENSION_INIT1 #include #include @@ -4200,6 +4284,101 @@ int sqlite3MemTraceDeactivate(void){ } /************************* End ../ext/misc/memtrace.c ********************/ +/************************* Begin ../ext/misc/uint.c ******************/ +/* +** 2020-04-14 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This SQLite extension implements the UINT collating sequence. +** +** UINT works like BINARY for text, except that embedded strings +** of digits compare in numeric order. +** +** * Leading zeros are handled properly, in the sense that +** they do not mess of the maginitude comparison of embedded +** strings of digits. "x00123y" is equal to "x123y". +** +** * Only unsigned integers are recognized. Plus and minus +** signs are ignored. Decimal points and exponential notation +** are ignored. +** +** * Embedded integers can be of arbitrary length. Comparison +** is *not* limited integers that can be expressed as a +** 64-bit machine integer. +*/ +/* #include "sqlite3ext.h" */ +SQLITE_EXTENSION_INIT1 +#include +#include +#include + +/* +** Compare text in lexicographic order, except strings of digits +** compare in numeric order. +*/ +static int uintCollFunc( + void *notUsed, + int nKey1, const void *pKey1, + int nKey2, const void *pKey2 +){ + const unsigned char *zA = (const unsigned char*)pKey1; + const unsigned char *zB = (const unsigned char*)pKey2; + int i=0, j=0, x; + (void)notUsed; + while( i #include @@ -4572,6 +4752,7 @@ static int zipfileConnect( zipfileDequote(pNew->zFile); } } + sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); *ppVtab = (sqlite3_vtab*)pNew; return rc; } @@ -5184,25 +5365,25 @@ static int zipfileDeflate( u8 **ppOut, int *pnOut, /* Output */ char **pzErr /* OUT: Error message */ ){ - sqlite3_int64 nAlloc = compressBound(nIn); - u8 *aOut; int rc = SQLITE_OK; + sqlite3_int64 nAlloc; + z_stream str; + u8 *aOut; + + memset(&str, 0, sizeof(str)); + str.next_in = (Bytef*)aIn; + str.avail_in = nIn; + deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); + nAlloc = deflateBound(&str, nIn); aOut = (u8*)sqlite3_malloc64(nAlloc); if( aOut==0 ){ rc = SQLITE_NOMEM; }else{ int res; - z_stream str; - memset(&str, 0, sizeof(str)); - str.next_in = (Bytef*)aIn; - str.avail_in = nIn; str.next_out = aOut; str.avail_out = nAlloc; - - deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); res = deflate(&str, Z_FINISH); - if( res==Z_STREAM_END ){ *ppOut = aOut; *pnOut = (int)str.total_out; @@ -5511,10 +5692,10 @@ static int zipfileBestIndex( idx = i; } } + pIdxInfo->estimatedCost = 1000.0; if( idx>=0 ){ pIdxInfo->aConstraintUsage[idx].argvIndex = 1; pIdxInfo->aConstraintUsage[idx].omit = 1; - pIdxInfo->estimatedCost = 1000.0; pIdxInfo->idxNum = 1; }else if( unusable ){ return SQLITE_CONSTRAINT; @@ -5636,8 +5817,8 @@ static int zipfileGetMode( ** identical, ignoring any trailing '/' character in either path. */ static int zipfileComparePath(const char *zA, const char *zB, int nB){ int nA = (int)strlen(zA); - if( zA[nA-1]=='/' ) nA--; - if( zB[nB-1]=='/' ) nB--; + if( nA>0 && zA[nA-1]=='/' ) nA--; + if( nB>0 && zB[nB-1]=='/' ) nB--; if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0; return 1; } @@ -5647,6 +5828,10 @@ static int zipfileBegin(sqlite3_vtab *pVtab){ int rc = SQLITE_OK; assert( pTab->pWriteFd==0 ); + if( pTab->zFile==0 || pTab->zFile[0]==0 ){ + pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename"); + return SQLITE_ERROR; + } /* Open a write fd on the file. Also load the entire central directory ** structure into memory. During the transaction any new file data is @@ -5821,6 +6006,7 @@ static int zipfileUpdate( if( rc==SQLITE_OK ){ zPath = (const char*)sqlite3_value_text(apVal[2]); + if( zPath==0 ) zPath = ""; nPath = (int)strlen(zPath); mTime = zipfileGetTime(apVal[4]); } @@ -5830,11 +6016,15 @@ static int zipfileUpdate( ** '/'. This appears to be required for compatibility with info-zip ** (the unzip command on unix). It does not create directories ** otherwise. */ - if( zPath[nPath-1]!='/' ){ + if( nPath<=0 || zPath[nPath-1]!='/' ){ zFree = sqlite3_mprintf("%s/", zPath); - if( zFree==0 ){ rc = SQLITE_NOMEM; } zPath = (const char*)zFree; - nPath++; + if( zFree==0 ){ + rc = SQLITE_NOMEM; + nPath = 0; + }else{ + nPath = (int)strlen(zPath); + } } } @@ -6227,19 +6417,19 @@ void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){ ** at the end of the path. Or, if this is not a directory and the path ** ends in '/' it is an error. */ if( bIsDir==0 ){ - if( zName[nName-1]=='/' ){ + if( nName>0 && zName[nName-1]=='/' ){ zErr = sqlite3_mprintf("non-directory name must not end with /"); rc = SQLITE_ERROR; goto zipfile_step_out; } }else{ - if( zName[nName-1]!='/' ){ + if( nName==0 || zName[nName-1]!='/' ){ zName = zFree = sqlite3_mprintf("%s/", zName); - nName++; if( zName==0 ){ rc = SQLITE_NOMEM; goto zipfile_step_out; } + nName = (int)strlen(zName); }else{ while( nName>1 && zName[nName-2]=='/' ) nName--; } @@ -6398,8 +6588,10 @@ int sqlite3_zipfile_init( ** for working with sqlar archives and used by the shell tool's built-in ** sqlar support. */ +/* #include "sqlite3ext.h" */ SQLITE_EXTENSION_INIT1 #include +#include /* ** Implementation of the "sqlar_compress(X)" SQL function. @@ -6494,10 +6686,12 @@ int sqlite3_sqlar_init( int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "sqlar_compress", 1, SQLITE_UTF8, 0, + rc = sqlite3_create_function(db, "sqlar_compress", 1, + SQLITE_UTF8|SQLITE_INNOCUOUS, 0, sqlarCompressFunc, 0, 0); if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "sqlar_uncompress", 2, SQLITE_UTF8, 0, + rc = sqlite3_create_function(db, "sqlar_uncompress", 2, + SQLITE_UTF8|SQLITE_INNOCUOUS, 0, sqlarUncompressFunc, 0, 0); } return rc; @@ -6518,8 +6712,9 @@ int sqlite3_sqlar_init( ** ************************************************************************* */ - - +#if !defined(SQLITEEXPERT_H) +#define SQLITEEXPERT_H 1 +/* #include "sqlite3.h" */ typedef struct sqlite3expert sqlite3expert; @@ -6672,7 +6867,7 @@ const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport); */ void sqlite3_expert_destroy(sqlite3expert*); - +#endif /* !defined(SQLITEEXPERT_H) */ /************************* End ../ext/expert/sqlite3expert.h ********************/ /************************* Begin ../ext/expert/sqlite3expert.c ******************/ @@ -6688,6 +6883,7 @@ void sqlite3_expert_destroy(sqlite3expert*); ** ************************************************************************* */ +/* #include "sqlite3expert.h" */ #include #include #include @@ -7805,14 +8001,19 @@ int idxFindIndexes( /* int iParent = sqlite3_column_int(pExplain, 1); */ /* int iNotUsed = sqlite3_column_int(pExplain, 2); */ const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3); - int nDetail = STRLEN(zDetail); + int nDetail; int i; + if( !zDetail ) continue; + nDetail = STRLEN(zDetail); + for(i=0; i