/ Check-in [3967ebe8]
Login

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

Overview
Comment:Merge in all recent changes from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: 3967ebe83e7cbb1dde26e4c9a6713d5c70fefe46
User & Date: drh 2014-09-21 23:08:54
Context
2014-10-01
01:46
Merge the latest enhancements from trunk. check-in: 2078454a user: drh tags: apple-osx
2014-09-21
23:08
Merge in all recent changes from trunk. check-in: 3967ebe8 user: drh tags: apple-osx
22:31
Correctly handle an ORDER BY clause on an outer query when applying the compound-subquery flattening optimization. Ticket [d11a6e908f]. Also add the SQLITE_ENABLE_SELECTTRACE option for additional debugging and analysis information about select statement processing. check-in: d5880abd user: drh tags: trunk
2014-09-02
15:57
Merge the latest trunk changes into the apple-osx branch. check-in: 696dc935 user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Added ext/misc/showauth.c.

            1  +/*
            2  +** 2014-09-21
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** This SQLite extension adds a debug "authorizer" callback to the database
           14  +** connection.  The callback merely writes the authorization request to
           15  +** standard output and returns SQLITE_OK.
           16  +**
           17  +** This extension can be used (for example) in the command-line shell to
           18  +** trace the operation of the authorizer.
           19  +*/
           20  +#include "sqlite3ext.h"
           21  +SQLITE_EXTENSION_INIT1
           22  +#include <stdio.h>
           23  +
           24  +/*
           25  +** Display the authorization request
           26  +*/
           27  +static int authCallback(
           28  +  void *pClientData,
           29  +  int op,
           30  +  const char *z1,
           31  +  const char *z2,
           32  +  const char *z3,
           33  +  const char *z4
           34  +){
           35  +  const char *zOp;
           36  +  char zOpSpace[50];
           37  +  switch( op ){
           38  +    case SQLITE_CREATE_INDEX:        zOp = "CREATE_INDEX";        break;
           39  +    case SQLITE_CREATE_TABLE:        zOp = "CREATE_TABLE";        break;
           40  +    case SQLITE_CREATE_TEMP_INDEX:   zOp = "CREATE_TEMP_INDEX";   break;
           41  +    case SQLITE_CREATE_TEMP_TABLE:   zOp = "CREATE_TEMP_TABLE";   break;
           42  +    case SQLITE_CREATE_TEMP_TRIGGER: zOp = "CREATE_TEMP_TRIGGER"; break;
           43  +    case SQLITE_CREATE_TEMP_VIEW:    zOp = "CREATE_TEMP_VIEW";    break;
           44  +    case SQLITE_CREATE_TRIGGER:      zOp = "CREATE_TRIGGER";      break;
           45  +    case SQLITE_CREATE_VIEW:         zOp = "CREATE_VIEW";         break;
           46  +    case SQLITE_DELETE:              zOp = "DELETE";              break;
           47  +    case SQLITE_DROP_INDEX:          zOp = "DROP_INDEX";          break;
           48  +    case SQLITE_DROP_TABLE:          zOp = "DROP_TABLE";          break;
           49  +    case SQLITE_DROP_TEMP_INDEX:     zOp = "DROP_TEMP_INDEX";     break;
           50  +    case SQLITE_DROP_TEMP_TABLE:     zOp = "DROP_TEMP_TABLE";     break;
           51  +    case SQLITE_DROP_TEMP_TRIGGER:   zOp = "DROP_TEMP_TRIGGER";   break;
           52  +    case SQLITE_DROP_TEMP_VIEW:      zOp = "DROP_TEMP_VIEW";      break;
           53  +    case SQLITE_DROP_TRIGGER:        zOp = "DROP_TRIGGER";        break;
           54  +    case SQLITE_DROP_VIEW:           zOp = "DROP_VIEW";           break;
           55  +    case SQLITE_INSERT:              zOp = "INSERT";              break;
           56  +    case SQLITE_PRAGMA:              zOp = "PRAGMA";              break;
           57  +    case SQLITE_READ:                zOp = "READ";                break;
           58  +    case SQLITE_SELECT:              zOp = "SELECT";              break;
           59  +    case SQLITE_TRANSACTION:         zOp = "TRANSACTION";         break;
           60  +    case SQLITE_UPDATE:              zOp = "UPDATE";              break;
           61  +    case SQLITE_ATTACH:              zOp = "ATTACH";              break;
           62  +    case SQLITE_DETACH:              zOp = "DETACH";              break;
           63  +    case SQLITE_ALTER_TABLE:         zOp = "ALTER_TABLE";         break;
           64  +    case SQLITE_REINDEX:             zOp = "REINDEX";             break;
           65  +    case SQLITE_ANALYZE:             zOp = "ANALYZE";             break;
           66  +    case SQLITE_CREATE_VTABLE:       zOp = "CREATE_VTABLE";       break;
           67  +    case SQLITE_DROP_VTABLE:         zOp = "DROP_VTABLE";         break;
           68  +    case SQLITE_FUNCTION:            zOp = "FUNCTION";            break;
           69  +    case SQLITE_SAVEPOINT:           zOp = "SAVEPOINT";           break;
           70  +    case SQLITE_COPY:                zOp = "COPY";                break;
           71  +    case SQLITE_RECURSIVE:           zOp = "RECURSIVE";           break;
           72  +
           73  +
           74  +    default: {
           75  +      sqlite3_snprintf(sizeof(zOpSpace), zOpSpace, "%d", op);
           76  +      zOp = zOpSpace;
           77  +      break;
           78  +    }
           79  +  }
           80  +  if( z1==0 ) z1 = "NULL";
           81  +  if( z2==0 ) z2 = "NULL";
           82  +  if( z3==0 ) z3 = "NULL";
           83  +  if( z4==0 ) z4 = "NULL";
           84  +  printf("AUTH: %s,%s,%s,%s,%s\n", zOp, z1, z2, z3, z4);
           85  +  return SQLITE_OK;
           86  +}
           87  +
           88  +
           89  +
           90  +#ifdef _WIN32
           91  +__declspec(dllexport)
           92  +#endif
           93  +int sqlite3_showauth_init(
           94  +  sqlite3 *db, 
           95  +  char **pzErrMsg, 
           96  +  const sqlite3_api_routines *pApi
           97  +){
           98  +  int rc = SQLITE_OK;
           99  +  SQLITE_EXTENSION_INIT2(pApi);
          100  +  (void)pzErrMsg;  /* Unused parameter */
          101  +  rc = sqlite3_set_authorizer(db, authCallback, 0);
          102  +  return rc;
          103  +}

Added ext/userauth/sqlite3userauth.h.

            1  +/*
            2  +** 2014-09-08
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** This file contains the application interface definitions for the
           14  +** user-authentication extension feature.
           15  +**
           16  +** To compile with the user-authentication feature, append this file to
           17  +** end of an SQLite amalgamation header file ("sqlite3.h"), then add
           18  +** the SQLITE_USER_AUTHENTICATION compile-time option.  See the
           19  +** user-auth.txt file in the same source directory as this file for
           20  +** additional information.
           21  +*/
           22  +#ifdef SQLITE_USER_AUTHENTICATION
           23  +
           24  +/*
           25  +** If a database contains the SQLITE_USER table, then the
           26  +** sqlite3_user_authenticate() interface must be invoked with an
           27  +** appropriate username and password prior to enable read and write
           28  +** access to the database.
           29  +**
           30  +** Return SQLITE_OK on success or SQLITE_ERROR if the username/password
           31  +** combination is incorrect or unknown.
           32  +**
           33  +** If the SQLITE_USER table is not present in the database file, then
           34  +** this interface is a harmless no-op returnning SQLITE_OK.
           35  +*/
           36  +int sqlite3_user_authenticate(
           37  +  sqlite3 *db,           /* The database connection */
           38  +  const char *zUsername, /* Username */
           39  +  const char *aPW,       /* Password or credentials */
           40  +  int nPW                /* Number of bytes in aPW[] */
           41  +);
           42  +
           43  +/*
           44  +** The sqlite3_user_add() interface can be used (by an admin user only)
           45  +** to create a new user.  When called on a no-authentication-required
           46  +** database, this routine converts the database into an authentication-
           47  +** required database, automatically makes the added user an
           48  +** administrator, and logs in the current connection as that user.
           49  +** The sqlite3_user_add() interface only works for the "main" database, not
           50  +** for any ATTACH-ed databases.  Any call to sqlite3_user_add() by a
           51  +** non-admin user results in an error.
           52  +*/
           53  +int sqlite3_user_add(
           54  +  sqlite3 *db,           /* Database connection */
           55  +  const char *zUsername, /* Username to be added */
           56  +  const char *aPW,       /* Password or credentials */
           57  +  int nPW,               /* Number of bytes in aPW[] */
           58  +  int isAdmin            /* True to give new user admin privilege */
           59  +);
           60  +
           61  +/*
           62  +** The sqlite3_user_change() interface can be used to change a users
           63  +** login credentials or admin privilege.  Any user can change their own
           64  +** login credentials.  Only an admin user can change another users login
           65  +** credentials or admin privilege setting.  No user may change their own 
           66  +** admin privilege setting.
           67  +*/
           68  +int sqlite3_user_change(
           69  +  sqlite3 *db,           /* Database connection */
           70  +  const char *zUsername, /* Username to change */
           71  +  const char *aPW,       /* New password or credentials */
           72  +  int nPW,               /* Number of bytes in aPW[] */
           73  +  int isAdmin            /* Modified admin privilege for the user */
           74  +);
           75  +
           76  +/*
           77  +** The sqlite3_user_delete() interface can be used (by an admin user only)
           78  +** to delete a user.  The currently logged-in user cannot be deleted,
           79  +** which guarantees that there is always an admin user and hence that
           80  +** the database cannot be converted into a no-authentication-required
           81  +** database.
           82  +*/
           83  +int sqlite3_user_delete(
           84  +  sqlite3 *db,           /* Database connection */
           85  +  const char *zUsername  /* Username to remove */
           86  +);
           87  +
           88  +#endif /* SQLITE_USER_AUTHENTICATION */

Added ext/userauth/user-auth.txt.

            1  +Activate the user authentication logic by including the
            2  +ext/userauth/userauth.c source code file in the build and
            3  +adding the -DSQLITE_USER_AUTHENTICATION compile-time option.
            4  +The ext/userauth/sqlite3userauth.h header file is available to
            5  +applications to define the interface.
            6  +
            7  +When using the SQLite amalgamation, it is sufficient to append
            8  +the ext/userauth/userauth.c source file onto the end of the
            9  +amalgamation.
           10  +
           11  +The following new APIs are available when user authentication is
           12  +activated:
           13  +
           14  +   int sqlite3_user_authenticate(
           15  +     sqlite3 *db,           /* The database connection */
           16  +     const char *zUsername, /* Username */
           17  +     const char *aPW,       /* Password or credentials */
           18  +     int nPW                /* Number of bytes in aPW[] */
           19  +   );
           20  +   
           21  +   int sqlite3_user_add(
           22  +     sqlite3 *db,           /* Database connection */
           23  +     const char *zUsername, /* Username to be added */
           24  +     const char *aPW,       /* Password or credentials */
           25  +     int nPW,               /* Number of bytes in aPW[] */
           26  +     int isAdmin            /* True to give new user admin privilege */
           27  +   );
           28  +   
           29  +   int sqlite3_user_change(
           30  +     sqlite3 *db,           /* Database connection */
           31  +     const char *zUsername, /* Username to change */
           32  +     const void *aPW,       /* Modified password or credentials */
           33  +     int nPW,               /* Number of bytes in aPW[] */
           34  +     int isAdmin            /* Modified admin privilege for the user */
           35  +   );
           36  +   
           37  +   int sqlite3_user_delete(
           38  +     sqlite3 *db,           /* Database connection */
           39  +     const char *zUsername  /* Username to remove */
           40  +   );
           41  +
           42  +With this extension, a database can be marked as requiring authentication.
           43  +By default a database does not require authentication.
           44  +
           45  +The sqlite3_open(), sqlite3_open16(), and sqlite3_open_v2() interfaces
           46  +work as before: they open a new database connection.  However, if the
           47  +database being opened requires authentication, then attempts to read
           48  +or write from the database will fail with an SQLITE_AUTH error until 
           49  +after sqlite3_user_authenticate() has been called successfully.  The 
           50  +sqlite3_user_authenticate() call will return SQLITE_OK if the 
           51  +authentication credentials are accepted and SQLITE_ERROR if not.
           52  +
           53  +Calling sqlite3_user_authenticate() on a no-authentication-required
           54  +database connection is a harmless no-op.  
           55  +
           56  +If the database is encrypted, then sqlite3_key_v2() must be called first,
           57  +with the correct decryption key, prior to invoking sqlite3_user_authenticate().
           58  +
           59  +To recapitulate: When opening an existing unencrypted authentication-
           60  +required database, the call sequence is:
           61  +
           62  +    sqlite3_open_v2()
           63  +    sqlite3_user_authenticate();
           64  +    /* Database is now usable */
           65  +
           66  +To open an existing, encrypted, authentication-required database, the
           67  +call sequence is:
           68  +
           69  +    sqlite3_open_v2();
           70  +    sqlite3_key_v2();
           71  +    sqlite3_user_authenticate();
           72  +    /* Database is now usable */
           73  +
           74  +When opening a no-authentication-required database, the database
           75  +connection is treated as if it was authenticated as an admin user.
           76  +
           77  +When ATTACH-ing new database files to a connection, each newly attached
           78  +database that is an authentication-required database is checked using
           79  +the same username and password as supplied to the main database.  If that
           80  +check fails, then the ATTACH command fails with an SQLITE_AUTH error.
           81  +
           82  +The sqlite3_user_add() interface can be used (by an admin user only)
           83  +to create a new user.  When called on a no-authentication-required
           84  +database and when A is true, the sqlite3_user_add(D,U,P,N,A) routine
           85  +converts the database into an authentication-required database and
           86  +logs in the database connection D as user U with password P,N.
           87  +To convert a no-authentication-required database into an authentication-
           88  +required database, the isAdmin parameter must be true.  If
           89  +sqlite3_user_add(D,U,P,N,A) is called on a no-authentication-required
           90  +database and A is false, then the call fails with an SQLITE_AUTH error.
           91  +
           92  +Any call to sqlite3_user_add() by a non-admin user results in an error.
           93  +
           94  +Hence, to create a new, unencrypted, authentication-required database,
           95  +the call sequence is:
           96  +
           97  +    sqlite3_open_v2();
           98  +    sqlite3_user_add();
           99  +
          100  +And to create a new, encrypted, authentication-required database, the call
          101  +sequence is:
          102  +
          103  +    sqlite3_open_v2();
          104  +    sqlite3_key_v2();
          105  +    sqlite3_user_add();
          106  +
          107  +The sqlite3_user_delete() interface can be used (by an admin user only)
          108  +to delete a user.  The currently logged-in user cannot be deleted,
          109  +which guarantees that there is always an admin user and hence that
          110  +the database cannot be converted into a no-authentication-required
          111  +database.
          112  +
          113  +The sqlite3_user_change() interface can be used to change a users
          114  +login credentials or admin privilege.  Any user can change their own
          115  +password.  Only an admin user can change another users login
          116  +credentials or admin privilege setting.  No user may change their own 
          117  +admin privilege setting.
          118  +
          119  +The sqlite3_set_authorizer() callback is modified to take a 7th parameter
          120  +which is the username of the currently logged in user, or NULL for a
          121  +no-authentication-required database.
          122  +
          123  +-----------------------------------------------------------------------------
          124  +Implementation notes:
          125  +
          126  +An authentication-required database is identified by the presence of a
          127  +new table:
          128  +
          129  +    CREATE TABLE sqlite_user(
          130  +      uname TEXT PRIMARY KEY,
          131  +      isAdmin BOOLEAN,
          132  +      pw BLOB
          133  +    ) WITHOUT ROWID;
          134  +
          135  +The sqlite_user table is inaccessible (unreadable and unwriteable) to
          136  +non-admin users and is read-only for admin users.  However, if the same
          137  +database file is opened by a version of SQLite that omits
          138  +the -DSQLITE_USER_AUTHENTICATION compile-time option, then the sqlite_user
          139  +table will be readable by anybody and writeable by anybody if
          140  +the "PRAGMA writable_schema=ON" statement is run first.
          141  +
          142  +The sqlite_user.pw field is encoded by a built-in SQL function
          143  +"sqlite_crypt(X,Y)".  The two arguments are both BLOBs.  The first argument
          144  +is the plaintext password supplied to the sqlite3_user_authenticate()
          145  +interface.  The second argument is the sqlite_user.pw value and is supplied
          146  +so that the function can extract the "salt" used by the password encoder.
          147  +The result of sqlite_crypt(X,Y) is another blob which is the value that
          148  +ends up being stored in sqlite_user.pw.  To verify credentials X supplied
          149  +by the sqlite3_user_authenticate() routine, SQLite runs:
          150  +
          151  +    sqlite_user.pw == sqlite_crypt(X, sqlite_user.pw)
          152  +
          153  +To compute an appropriate sqlite_user.pw value from a new or modified
          154  +password X, sqlite_crypt(X,NULL) is run.  A new random salt is selected
          155  +when the second argument is NULL.
          156  +
          157  +The built-in version of of sqlite_crypt() uses a simple Ceasar-cypher
          158  +which prevents passwords from being revealed by searching the raw database
          159  +for ASCII text, but is otherwise trivally broken.  For better password
          160  +security, the database should be encrypted using the SQLite Encryption
          161  +Extension or similar technology.  Or, the application can use the
          162  +sqlite3_create_function() interface to provide an alternative
          163  +implementation of sqlite_crypt() that computes a stronger password hash,
          164  +perhaps using a cryptographic hash function like SHA1.

Added ext/userauth/userauth.c.

            1  +/*
            2  +** 2014-09-08
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** This file contains the bulk of the implementation of the
           14  +** user-authentication extension feature.  Some parts of the user-
           15  +** authentication code are contained within the SQLite core (in the
           16  +** src/ subdirectory of the main source code tree) but those parts
           17  +** that could reasonable be separated out are moved into this file.
           18  +**
           19  +** To compile with the user-authentication feature, append this file to
           20  +** end of an SQLite amalgamation, then add the SQLITE_USER_AUTHENTICATION
           21  +** compile-time option.  See the user-auth.txt file in the same source
           22  +** directory as this file for additional information.
           23  +*/
           24  +#ifdef SQLITE_USER_AUTHENTICATION
           25  +#ifndef _SQLITEINT_H_
           26  +# include "sqliteInt.h"
           27  +#endif
           28  +
           29  +/*
           30  +** Prepare an SQL statement for use by the user authentication logic.
           31  +** Return a pointer to the prepared statement on success.  Return a
           32  +** NULL pointer if there is an error of any kind.
           33  +*/
           34  +static sqlite3_stmt *sqlite3UserAuthPrepare(
           35  +  sqlite3 *db,
           36  +  const char *zFormat,
           37  +  ...
           38  +){
           39  +  sqlite3_stmt *pStmt;
           40  +  char *zSql;
           41  +  int rc;
           42  +  va_list ap;
           43  +  int savedFlags = db->flags;
           44  +
           45  +  va_start(ap, zFormat);
           46  +  zSql = sqlite3_vmprintf(zFormat, ap);
           47  +  va_end(ap);
           48  +  if( zSql==0 ) return 0;
           49  +  db->flags |= SQLITE_WriteSchema;
           50  +  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
           51  +  db->flags = savedFlags;
           52  +  sqlite3_free(zSql);
           53  +  if( rc ){
           54  +    sqlite3_finalize(pStmt);
           55  +    pStmt = 0;
           56  +  }
           57  +  return pStmt;
           58  +}
           59  +
           60  +/*
           61  +** Check to see if the sqlite_user table exists in database zDb.
           62  +*/
           63  +static int userTableExists(sqlite3 *db, const char *zDb){
           64  +  int rc;
           65  +  sqlite3_mutex_enter(db->mutex);
           66  +  sqlite3BtreeEnterAll(db);
           67  +  if( db->init.busy==0 ){
           68  +    char *zErr = 0;
           69  +    sqlite3Init(db, &zErr);
           70  +    sqlite3DbFree(db, zErr);
           71  +  }
           72  +  rc = sqlite3FindTable(db, "sqlite_user", zDb)!=0;
           73  +  sqlite3BtreeLeaveAll(db);
           74  +  sqlite3_mutex_leave(db->mutex);
           75  +  return rc;
           76  +}
           77  +
           78  +/*
           79  +** Check to see if database zDb has a "sqlite_user" table and if it does
           80  +** whether that table can authenticate zUser with nPw,zPw.  Write one of
           81  +** the UAUTH_* user authorization level codes into *peAuth and return a
           82  +** result code.
           83  +*/
           84  +static int userAuthCheckLogin(
           85  +  sqlite3 *db,               /* The database connection to check */
           86  +  const char *zDb,           /* Name of specific database to check */
           87  +  u8 *peAuth                 /* OUT: One of UAUTH_* constants */
           88  +){
           89  +  sqlite3_stmt *pStmt;
           90  +  int rc;
           91  +
           92  +  *peAuth = UAUTH_Unknown;
           93  +  if( !userTableExists(db, "main") ){
           94  +    *peAuth = UAUTH_Admin;  /* No sqlite_user table.  Everybody is admin. */
           95  +    return SQLITE_OK;
           96  +  }
           97  +  if( db->auth.zAuthUser==0 ){
           98  +    *peAuth = UAUTH_Fail;
           99  +    return SQLITE_OK;
          100  +  }
          101  +  pStmt = sqlite3UserAuthPrepare(db,
          102  +            "SELECT pw=sqlite_crypt(?1,pw), isAdmin FROM \"%w\".sqlite_user"
          103  +            " WHERE uname=?2", zDb);
          104  +  if( pStmt==0 ) return SQLITE_NOMEM;
          105  +  sqlite3_bind_blob(pStmt, 1, db->auth.zAuthPW, db->auth.nAuthPW,SQLITE_STATIC);
          106  +  sqlite3_bind_text(pStmt, 2, db->auth.zAuthUser, -1, SQLITE_STATIC);
          107  +  rc = sqlite3_step(pStmt);
          108  +  if( rc==SQLITE_ROW && sqlite3_column_int(pStmt,0) ){
          109  +    *peAuth = sqlite3_column_int(pStmt, 1) + UAUTH_User;
          110  +  }else{
          111  +    *peAuth = UAUTH_Fail;
          112  +  }
          113  +  return sqlite3_finalize(pStmt);
          114  +}
          115  +int sqlite3UserAuthCheckLogin(
          116  +  sqlite3 *db,               /* The database connection to check */
          117  +  const char *zDb,           /* Name of specific database to check */
          118  +  u8 *peAuth                 /* OUT: One of UAUTH_* constants */
          119  +){
          120  +  int rc;
          121  +  u8 savedAuthLevel;
          122  +  assert( zDb!=0 );
          123  +  assert( peAuth!=0 );
          124  +  savedAuthLevel = db->auth.authLevel;
          125  +  db->auth.authLevel = UAUTH_Admin;
          126  +  rc = userAuthCheckLogin(db, zDb, peAuth);
          127  +  db->auth.authLevel = savedAuthLevel;
          128  +  return rc;
          129  +}
          130  +
          131  +/*
          132  +** If the current authLevel is UAUTH_Unknown, the take actions to figure
          133  +** out what authLevel should be
          134  +*/
          135  +void sqlite3UserAuthInit(sqlite3 *db){
          136  +  if( db->auth.authLevel==UAUTH_Unknown ){
          137  +    u8 authLevel = UAUTH_Fail;
          138  +    sqlite3UserAuthCheckLogin(db, "main", &authLevel);
          139  +    db->auth.authLevel = authLevel;
          140  +    if( authLevel<UAUTH_Admin ) db->flags &= ~SQLITE_WriteSchema;
          141  +  }
          142  +}
          143  +
          144  +/*
          145  +** Implementation of the sqlite_crypt(X,Y) function.
          146  +**
          147  +** If Y is NULL then generate a new hash for password X and return that
          148  +** hash.  If Y is not null, then generate a hash for password X using the
          149  +** same salt as the previous hash Y and return the new hash.
          150  +*/
          151  +void sqlite3CryptFunc(
          152  +  sqlite3_context *context,
          153  +  int NotUsed,
          154  +  sqlite3_value **argv
          155  +){
          156  +  const char *zIn;
          157  +  int nIn, ii;
          158  +  u8 *zOut;
          159  +  char zSalt[8];
          160  +  zIn = sqlite3_value_blob(argv[0]);
          161  +  nIn = sqlite3_value_bytes(argv[0]);
          162  +  if( sqlite3_value_type(argv[1])==SQLITE_BLOB
          163  +   && sqlite3_value_bytes(argv[1])==nIn+sizeof(zSalt)
          164  +  ){
          165  +    memcpy(zSalt, sqlite3_value_blob(argv[1]), sizeof(zSalt));
          166  +  }else{
          167  +    sqlite3_randomness(sizeof(zSalt), zSalt);
          168  +  }
          169  +  zOut = sqlite3_malloc( nIn+sizeof(zSalt) );
          170  +  if( zOut==0 ){
          171  +    sqlite3_result_error_nomem(context);
          172  +  }else{
          173  +    memcpy(zOut, zSalt, sizeof(zSalt));
          174  +    for(ii=0; ii<nIn; ii++){
          175  +      zOut[ii+sizeof(zSalt)] = zIn[ii]^zSalt[ii&0x7];
          176  +    }
          177  +    sqlite3_result_blob(context, zOut, nIn+sizeof(zSalt), sqlite3_free);
          178  +  }
          179  +}
          180  +
          181  +/*
          182  +** If a database contains the SQLITE_USER table, then the
          183  +** sqlite3_user_authenticate() interface must be invoked with an
          184  +** appropriate username and password prior to enable read and write
          185  +** access to the database.
          186  +**
          187  +** Return SQLITE_OK on success or SQLITE_ERROR if the username/password
          188  +** combination is incorrect or unknown.
          189  +**
          190  +** If the SQLITE_USER table is not present in the database file, then
          191  +** this interface is a harmless no-op returnning SQLITE_OK.
          192  +*/
          193  +int sqlite3_user_authenticate(
          194  +  sqlite3 *db,           /* The database connection */
          195  +  const char *zUsername, /* Username */
          196  +  const char *zPW,       /* Password or credentials */
          197  +  int nPW                /* Number of bytes in aPW[] */
          198  +){
          199  +  int rc;
          200  +  u8 authLevel = UAUTH_Fail;
          201  +  db->auth.authLevel = UAUTH_Unknown;
          202  +  sqlite3_free(db->auth.zAuthUser);
          203  +  sqlite3_free(db->auth.zAuthPW);
          204  +  memset(&db->auth, 0, sizeof(db->auth));
          205  +  db->auth.zAuthUser = sqlite3_mprintf("%s", zUsername);
          206  +  if( db->auth.zAuthUser==0 ) return SQLITE_NOMEM;
          207  +  db->auth.zAuthPW = sqlite3_malloc( nPW+1 );
          208  +  if( db->auth.zAuthPW==0 ) return SQLITE_NOMEM;
          209  +  memcpy(db->auth.zAuthPW,zPW,nPW);
          210  +  db->auth.nAuthPW = nPW;
          211  +  rc = sqlite3UserAuthCheckLogin(db, "main", &authLevel);
          212  +  db->auth.authLevel = authLevel;
          213  +  sqlite3ExpirePreparedStatements(db);
          214  +  if( rc ){
          215  +    return rc;           /* OOM error, I/O error, etc. */
          216  +  }
          217  +  if( authLevel<UAUTH_User ){
          218  +    return SQLITE_AUTH;  /* Incorrect username and/or password */
          219  +  }
          220  +  return SQLITE_OK;      /* Successful login */
          221  +}
          222  +
          223  +/*
          224  +** The sqlite3_user_add() interface can be used (by an admin user only)
          225  +** to create a new user.  When called on a no-authentication-required
          226  +** database, this routine converts the database into an authentication-
          227  +** required database, automatically makes the added user an
          228  +** administrator, and logs in the current connection as that user.
          229  +** The sqlite3_user_add() interface only works for the "main" database, not
          230  +** for any ATTACH-ed databases.  Any call to sqlite3_user_add() by a
          231  +** non-admin user results in an error.
          232  +*/
          233  +int sqlite3_user_add(
          234  +  sqlite3 *db,           /* Database connection */
          235  +  const char *zUsername, /* Username to be added */
          236  +  const char *aPW,       /* Password or credentials */
          237  +  int nPW,               /* Number of bytes in aPW[] */
          238  +  int isAdmin            /* True to give new user admin privilege */
          239  +){
          240  +  sqlite3_stmt *pStmt;
          241  +  int rc;
          242  +  sqlite3UserAuthInit(db);
          243  +  if( db->auth.authLevel<UAUTH_Admin ) return SQLITE_AUTH;
          244  +  if( !userTableExists(db, "main") ){
          245  +    if( !isAdmin ) return SQLITE_AUTH;
          246  +    pStmt = sqlite3UserAuthPrepare(db, 
          247  +              "CREATE TABLE sqlite_user(\n"
          248  +              "  uname TEXT PRIMARY KEY,\n"
          249  +              "  isAdmin BOOLEAN,\n"
          250  +              "  pw BLOB\n"
          251  +              ") WITHOUT ROWID;");
          252  +    if( pStmt==0 ) return SQLITE_NOMEM;
          253  +    sqlite3_step(pStmt);
          254  +    rc = sqlite3_finalize(pStmt);
          255  +    if( rc ) return rc;
          256  +  }
          257  +  pStmt = sqlite3UserAuthPrepare(db, 
          258  +            "INSERT INTO sqlite_user(uname,isAdmin,pw)"
          259  +            " VALUES(%Q,%d,sqlite_crypt(?1,NULL))",
          260  +            zUsername, isAdmin!=0);
          261  +  if( pStmt==0 ) return SQLITE_NOMEM;
          262  +  sqlite3_bind_blob(pStmt, 1, aPW, nPW, SQLITE_STATIC);
          263  +  sqlite3_step(pStmt);
          264  +  rc = sqlite3_finalize(pStmt);
          265  +  if( rc ) return rc;
          266  +  if( db->auth.zAuthUser==0 ){
          267  +    assert( isAdmin!=0 );
          268  +    sqlite3_user_authenticate(db, zUsername, aPW, nPW);
          269  +  }
          270  +  return SQLITE_OK;
          271  +}
          272  +
          273  +/*
          274  +** The sqlite3_user_change() interface can be used to change a users
          275  +** login credentials or admin privilege.  Any user can change their own
          276  +** login credentials.  Only an admin user can change another users login
          277  +** credentials or admin privilege setting.  No user may change their own 
          278  +** admin privilege setting.
          279  +*/
          280  +int sqlite3_user_change(
          281  +  sqlite3 *db,           /* Database connection */
          282  +  const char *zUsername, /* Username to change */
          283  +  const char *aPW,       /* Modified password or credentials */
          284  +  int nPW,               /* Number of bytes in aPW[] */
          285  +  int isAdmin            /* Modified admin privilege for the user */
          286  +){
          287  +  sqlite3_stmt *pStmt;
          288  +  int rc;
          289  +  u8 authLevel;
          290  +
          291  +  authLevel = db->auth.authLevel;
          292  +  if( authLevel<UAUTH_User ){
          293  +    /* Must be logged in to make a change */
          294  +    return SQLITE_AUTH;
          295  +  }
          296  +  if( strcmp(db->auth.zAuthUser, zUsername)!=0 ){
          297  +    if( db->auth.authLevel<UAUTH_Admin ){
          298  +      /* Must be an administrator to change a different user */
          299  +      return SQLITE_AUTH;
          300  +    }
          301  +  }else if( isAdmin!=(authLevel==UAUTH_Admin) ){
          302  +    /* Cannot change the isAdmin setting for self */
          303  +    return SQLITE_AUTH;
          304  +  }
          305  +  db->auth.authLevel = UAUTH_Admin;
          306  +  if( !userTableExists(db, "main") ){
          307  +    /* This routine is a no-op if the user to be modified does not exist */
          308  +  }else{
          309  +    pStmt = sqlite3UserAuthPrepare(db,
          310  +              "UPDATE sqlite_user SET isAdmin=%d, pw=sqlite_crypt(?1,NULL)"
          311  +              " WHERE uname=%Q", isAdmin, zUsername);
          312  +    if( pStmt==0 ){
          313  +      rc = SQLITE_NOMEM;
          314  +    }else{
          315  +      sqlite3_bind_blob(pStmt, 1, aPW, nPW, SQLITE_STATIC);
          316  +      sqlite3_step(pStmt);
          317  +      rc = sqlite3_finalize(pStmt);
          318  +    }
          319  +  }
          320  +  db->auth.authLevel = authLevel;
          321  +  return rc;
          322  +}
          323  +
          324  +/*
          325  +** The sqlite3_user_delete() interface can be used (by an admin user only)
          326  +** to delete a user.  The currently logged-in user cannot be deleted,
          327  +** which guarantees that there is always an admin user and hence that
          328  +** the database cannot be converted into a no-authentication-required
          329  +** database.
          330  +*/
          331  +int sqlite3_user_delete(
          332  +  sqlite3 *db,           /* Database connection */
          333  +  const char *zUsername  /* Username to remove */
          334  +){
          335  +  sqlite3_stmt *pStmt;
          336  +  if( db->auth.authLevel<UAUTH_Admin ){
          337  +    /* Must be an administrator to delete a user */
          338  +    return SQLITE_AUTH;
          339  +  }
          340  +  if( strcmp(db->auth.zAuthUser, zUsername)==0 ){
          341  +    /* Cannot delete self */
          342  +    return SQLITE_AUTH;
          343  +  }
          344  +  if( !userTableExists(db, "main") ){
          345  +    /* This routine is a no-op if the user to be deleted does not exist */
          346  +    return SQLITE_OK;
          347  +  }
          348  +  pStmt = sqlite3UserAuthPrepare(db,
          349  +              "DELETE FROM sqlite_user WHERE uname=%Q", zUsername);
          350  +  if( pStmt==0 ) return SQLITE_NOMEM;
          351  +  sqlite3_step(pStmt);
          352  +  return sqlite3_finalize(pStmt);
          353  +}
          354  +
          355  +#endif /* SQLITE_USER_AUTHENTICATION */

Changes to main.mk.

    42     42   # build the SQLite library and testing tools.
    43     43   ################################################################################
    44     44   
    45     45   # This is how we compile
    46     46   #
    47     47   TCCX =  $(TCC) $(OPTS) -I. -I$(TOP)/src -I$(TOP) 
    48     48   TCCX += -I$(TOP)/ext/rtree -I$(TOP)/ext/icu -I$(TOP)/ext/fts3
    49         -TCCX += -I$(TOP)/ext/async
           49  +TCCX += -I$(TOP)/ext/async -I$(TOP)/ext/userauth
    50     50   
    51     51   # Object files for the SQLite library.
    52     52   #
    53     53   LIBOBJ+= vdbe.o parse.o \
    54     54            alter.o analyze.o attach.o auth.o \
    55     55            backup.o bitvec.o btmutex.o btree.o build.o \
    56     56            callback.o complete.o ctime.o date.o delete.o expr.o fault.o fkey.o \
................................................................................
    63     63            main.o malloc.o mem0.o mem1.o mem2.o mem3.o mem5.o \
    64     64            memjournal.o \
    65     65            mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \
    66     66            notify.o opcodes.o os.o os_unix.o os_win.o \
    67     67            pager.o pcache.o pcache1.o pragma.o prepare.o printf.o \
    68     68            random.o resolve.o rowset.o rtree.o select.o status.o \
    69     69            table.o threads.o tokenize.o trigger.o \
    70         -         update.o util.o vacuum.o \
           70  +         update.o userauth.o util.o vacuum.o \
    71     71            vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o vdbesort.o \
    72     72   	 vdbetrace.o wal.o walker.o where.o utf.o vtab.o
    73     73   
    74     74   
    75     75   
    76     76   # All of the source code files.
    77     77   #
................................................................................
   212    212     $(TOP)/ext/icu/icu.c
   213    213   SRC += \
   214    214     $(TOP)/ext/rtree/sqlite3rtree.h \
   215    215     $(TOP)/ext/rtree/rtree.h \
   216    216     $(TOP)/ext/rtree/rtree.c
   217    217   SRC += \
   218    218     $(TOP)/ext/sqlrr/sqlrr.c
   219         -
          219  +SRC += \
          220  +  $(TOP)/ext/userauth/userauth.c \
          221  +  $(TOP)/ext/userauth/sqlite3userauth.h
   220    222   
   221    223   # Generated source code files
   222    224   #
   223    225   SRC += \
   224    226     keywordhash.h \
   225    227     opcodes.c \
   226    228     opcodes.h \
................................................................................
   360    362      $(TOP)/src/vdbeInt.h \
   361    363      $(TOP)/src/whereInt.h
   362    364   
   363    365   # Header files used by extensions
   364    366   #
   365    367   EXTHDR += \
   366    368     $(TOP)/ext/sqlrr/sqlrr.h
          369  +EXTHDR += \
          370  +  $(TOP)/ext/userauth/sqlite3userauth.h
   367    371   
   368    372   # This is the default Makefile target.  The objects listed here
   369    373   # are what get build when you type just "make" with no arguments.
   370    374   #
   371    375   all:	sqlite3.h libsqlite3.a sqlite3$(EXE)
   372    376   
   373    377   libsqlite3.a:	$(LIBOBJ)
................................................................................
   540    544   	$(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_unicode2.c
   541    545   
   542    546   fts3_write.o:	$(TOP)/ext/fts3/fts3_write.c $(HDR) $(EXTHDR)
   543    547   	$(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_write.c
   544    548   
   545    549   rtree.o:	$(TOP)/ext/rtree/rtree.c $(HDR) $(EXTHDR)
   546    550   	$(TCCX) -DSQLITE_CORE -c $(TOP)/ext/rtree/rtree.c
          551  +
          552  +userauth.o:	$(TOP)/ext/userauth/userauth.c $(HDR) $(EXTHDR)
          553  +	$(TCCX) -DSQLITE_CORE -c $(TOP)/ext/userauth/userauth.c
   547    554   
   548    555   
   549    556   # Rules for building test programs and for running tests
   550    557   #
   551    558   tclsqlite3:	$(TOP)/src/tclsqlite.c libsqlite3.a
   552    559   	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 -o tclsqlite3 \
   553    560   		$(TOP)/src/tclsqlite.c libsqlite3.a $(LIBTCL) $(THREADLIB)

Changes to src/alter.c.

   170    170     int len = 0;
   171    171     char *zRet;
   172    172     sqlite3 *db = sqlite3_context_db_handle(context);
   173    173   
   174    174     UNUSED_PARAMETER(NotUsed);
   175    175   
   176    176     /* The principle used to locate the table name in the CREATE TRIGGER 
   177         -  ** statement is that the table name is the first token that is immediatedly
   178         -  ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
          177  +  ** statement is that the table name is the first token that is immediately
          178  +  ** preceded by either TK_ON or TK_DOT and immediately followed by one
   179    179     ** of TK_WHEN, TK_BEGIN or TK_FOR.
   180    180     */
   181    181     if( zSql ){
   182    182       do {
   183    183   
   184    184         if( !*zCsr ){
   185    185           /* Ran out of input before finding the table name. Return NULL. */

Changes to src/analyze.c.

    31     31   ** SQLITE_ENABLE_STAT3 defined.  The functionality of sqlite_stat3
    32     32   ** is a superset of sqlite_stat2.  The sqlite_stat4 is an enhanced
    33     33   ** version of sqlite_stat3 and is only available when compiled with
    34     34   ** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later.  It is
    35     35   ** not possible to enable both STAT3 and STAT4 at the same time.  If they
    36     36   ** are both enabled, then STAT4 takes precedence.
    37     37   **
    38         -** For most applications, sqlite_stat1 provides all the statisics required
           38  +** For most applications, sqlite_stat1 provides all the statistics required
    39     39   ** for the query planner to make good choices.
    40     40   **
    41     41   ** Format of sqlite_stat1:
    42     42   **
    43     43   ** There is normally one row per index, with the index identified by the
    44     44   ** name in the idx column.  The tbl column is the name of the table to
    45     45   ** which the index belongs.  In each such row, the stat column will be
................................................................................
   383    383   **
   384    384   ** For indexes on ordinary rowid tables, N==K+1.  But for indexes on
   385    385   ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
   386    386   ** PRIMARY KEY of the table.  The covering index that implements the
   387    387   ** original WITHOUT ROWID table as N==K as a special case.
   388    388   **
   389    389   ** This routine allocates the Stat4Accum object in heap memory. The return 
   390         -** value is a pointer to the the Stat4Accum object.  The datatype of the
          390  +** value is a pointer to the Stat4Accum object.  The datatype of the
   391    391   ** return value is BLOB, but it is really just a pointer to the Stat4Accum
   392    392   ** object.
   393    393   */
   394    394   static void statInit(
   395    395     sqlite3_context *context,
   396    396     int argc,
   397    397     sqlite3_value **argv
................................................................................
  1197   1197       sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
  1198   1198       sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
  1199   1199       sqlite3VdbeChangeP5(v, 2+IsStat34);
  1200   1200       sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
  1201   1201   
  1202   1202       /* Add the entry to the stat1 table. */
  1203   1203       callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
  1204         -    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
         1204  +    assert( "BBB"[0]==SQLITE_AFF_TEXT );
         1205  +    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
  1205   1206       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
  1206   1207       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
  1207   1208       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  1208   1209   
  1209   1210       /* Add the entries to the stat3 or stat4 table. */
  1210   1211   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1211   1212       {
................................................................................
  1260   1261     ** name and the row count as the content.
  1261   1262     */
  1262   1263     if( pOnlyIdx==0 && needTableCnt ){
  1263   1264       VdbeComment((v, "%s", pTab->zName));
  1264   1265       sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
  1265   1266       jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
  1266   1267       sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
  1267         -    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
         1268  +    assert( "BBB"[0]==SQLITE_AFF_TEXT );
         1269  +    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
  1268   1270       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
  1269   1271       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
  1270   1272       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  1271   1273       sqlite3VdbeJumpHere(v, jZeroRows);
  1272   1274     }
  1273   1275   }
  1274   1276   
................................................................................
  1579   1581         tRowcnt nSum = 0;         /* Number of terms contributing to sumEq */
  1580   1582         tRowcnt avgEq = 0;
  1581   1583         tRowcnt nDLt = pFinal->anDLt[iCol];
  1582   1584   
  1583   1585         /* Set nSum to the number of distinct (iCol+1) field prefixes that
  1584   1586         ** occur in the stat4 table for this index before pFinal. Set
  1585   1587         ** sumEq to the sum of the nEq values for column iCol for the same
  1586         -      ** set (adding the value only once where there exist dupicate 
         1588  +      ** set (adding the value only once where there exist duplicate 
  1587   1589         ** prefixes).  */
  1588   1590         for(i=0; i<(pIdx->nSample-1); i++){
  1589   1591           if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){
  1590   1592             sumEq += aSample[i].anEq[iCol];
  1591   1593             nSum++;
  1592   1594           }
  1593   1595         }

Changes to src/attach.c.

   203    203     ** we found it.
   204    204     */
   205    205     if( rc==SQLITE_OK ){
   206    206       sqlite3BtreeEnterAll(db);
   207    207       rc = sqlite3Init(db, &zErrDyn);
   208    208       sqlite3BtreeLeaveAll(db);
   209    209     }
          210  +#ifdef SQLITE_USER_AUTHENTICATION
          211  +  if( rc==SQLITE_OK ){
          212  +    u8 newAuth = 0;
          213  +    rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
          214  +    if( newAuth<db->auth.authLevel ){
          215  +      rc = SQLITE_AUTH_USER;
          216  +    }
          217  +  }
          218  +#endif
   210    219     if( rc ){
   211    220       int iDb = db->nDb - 1;
   212    221       assert( iDb>=2 );
   213    222       if( db->aDb[iDb].pBt ){
   214    223         sqlite3BtreeClose(db->aDb[iDb].pBt);
   215    224         db->aDb[iDb].pBt = 0;
   216    225         db->aDb[iDb].pSchema = 0;

Changes to src/auth.c.

    69     69   */
    70     70   int sqlite3_set_authorizer(
    71     71     sqlite3 *db,
    72     72     int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
    73     73     void *pArg
    74     74   ){
    75     75     sqlite3_mutex_enter(db->mutex);
    76         -  db->xAuth = xAuth;
           76  +  db->xAuth = (sqlite3_xauth)xAuth;
    77     77     db->pAuthArg = pArg;
    78     78     sqlite3ExpirePreparedStatements(db);
    79     79     sqlite3_mutex_leave(db->mutex);
    80     80     return SQLITE_OK;
    81     81   }
    82     82   
    83     83   /*
................................................................................
   104    104     const char *zCol,               /* Column name */
   105    105     int iDb                         /* Index of containing database. */
   106    106   ){
   107    107     sqlite3 *db = pParse->db;       /* Database handle */
   108    108     char *zDb = db->aDb[iDb].zName; /* Name of attached database */
   109    109     int rc;                         /* Auth callback return code */
   110    110   
   111         -  rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
          111  +  rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
          112  +#ifdef SQLITE_USER_AUTHENTICATION
          113  +                 ,db->auth.zAuthUser
          114  +#endif
          115  +                );
   112    116     if( rc==SQLITE_DENY ){
   113    117       if( db->nDb>2 || iDb!=0 ){
   114    118         sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
   115    119       }else{
   116    120         sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
   117    121       }
   118    122       pParse->rc = SQLITE_AUTH;
................................................................................
   204    208     if( db->init.busy || IN_DECLARE_VTAB ){
   205    209       return SQLITE_OK;
   206    210     }
   207    211   
   208    212     if( db->xAuth==0 ){
   209    213       return SQLITE_OK;
   210    214     }
   211         -  rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
          215  +  rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
          216  +#ifdef SQLITE_USER_AUTHENTICATION
          217  +                 ,db->auth.zAuthUser
          218  +#endif
          219  +                );
   212    220     if( rc==SQLITE_DENY ){
   213    221       sqlite3ErrorMsg(pParse, "not authorized");
   214    222       pParse->rc = SQLITE_AUTH;
   215    223     }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
   216    224       rc = SQLITE_DENY;
   217    225       sqliteAuthBadReturnCode(pParse);
   218    226     }

Changes to src/btmutex.c.

   102    102   ** and thus help the sqlite3BtreeLock() routine to run much faster
   103    103   ** in the common case.
   104    104   */
   105    105   static void SQLITE_NOINLINE btreeLockCarefully(Btree *p){
   106    106     Btree *pLater;
   107    107   
   108    108     /* In most cases, we should be able to acquire the lock we
   109         -  ** want without having to go throught the ascending lock
          109  +  ** want without having to go through the ascending lock
   110    110     ** procedure that follows.  Just be sure not to block.
   111    111     */
   112    112     if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
   113    113       p->pBt->db = p->db;
   114    114       p->locked = 1;
   115    115       return;
   116    116     }

Changes to src/btree.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** This file implements a external (disk-based) database using BTrees.
           12  +** This file implements an external (disk-based) database using BTrees.
    13     13   ** See the header comment on "btreeInt.h" for additional information.
    14     14   ** Including a description of file format and an overview of operation.
    15     15   */
    16     16   #include "btreeInt.h"
    17     17   
    18     18   /*
    19     19   ** The header string that appears at the beginning of every
................................................................................
   608    608     /* If this is an intKey table, then the above call to BtreeKeySize()
   609    609     ** stores the integer key in pCur->nKey. In this case this value is
   610    610     ** all that is required. Otherwise, if pCur is not open on an intKey
   611    611     ** table, then malloc space for and store the pCur->nKey bytes of key 
   612    612     ** data.
   613    613     */
   614    614     if( 0==pCur->apPage[0]->intKey ){
   615         -    void *pKey = sqlite3Malloc( (int)pCur->nKey );
          615  +    void *pKey = sqlite3Malloc( pCur->nKey );
   616    616       if( pKey ){
   617    617         rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
   618    618         if( rc==SQLITE_OK ){
   619    619           pCur->pKey = pKey;
   620    620         }else{
   621    621           sqlite3_free(pKey);
   622    622         }
................................................................................
  1146   1146   ** Defragment the page given.  All Cells are moved to the
  1147   1147   ** end of the page and all free space is collected into one
  1148   1148   ** big FreeBlk that occurs in between the header and cell
  1149   1149   ** pointer array and the cell content area.
  1150   1150   */
  1151   1151   static int defragmentPage(MemPage *pPage){
  1152   1152     int i;                     /* Loop counter */
  1153         -  int pc;                    /* Address of a i-th cell */
         1153  +  int pc;                    /* Address of the i-th cell */
  1154   1154     int hdr;                   /* Offset to the page header */
  1155   1155     int size;                  /* Size of a cell */
  1156   1156     int usableSize;            /* Number of usable bytes on a page */
  1157   1157     int cellOffset;            /* Offset to the cell pointer array */
  1158   1158     int cbrk;                  /* Offset to the cell content area */
  1159   1159     int nCell;                 /* Number of cells on the page */
  1160   1160     unsigned char *data;       /* The page data */
................................................................................
  2624   2624   ** in assert() expressions, so it is only compiled if NDEBUG is not
  2625   2625   ** defined.
  2626   2626   **
  2627   2627   ** Only write cursors are counted if wrOnly is true.  If wrOnly is
  2628   2628   ** false then all cursors are counted.
  2629   2629   **
  2630   2630   ** For the purposes of this routine, a cursor is any cursor that
  2631         -** is capable of reading or writing to the databse.  Cursors that
         2631  +** is capable of reading or writing to the database.  Cursors that
  2632   2632   ** have been tripped into the CURSOR_FAULT state are not counted.
  2633   2633   */
  2634   2634   static int countValidCursors(BtShared *pBt, int wrOnly){
  2635   2635     BtCursor *pCur;
  2636   2636     int r = 0;
  2637   2637     for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
  2638   2638       if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
................................................................................
  3088   3088   
  3089   3089   /*
  3090   3090   ** Perform a single step of an incremental-vacuum. If successful, return
  3091   3091   ** SQLITE_OK. If there is no work to do (and therefore no point in 
  3092   3092   ** calling this function again), return SQLITE_DONE. Or, if an error 
  3093   3093   ** occurs, return some other error code.
  3094   3094   **
  3095         -** More specificly, this function attempts to re-organize the database so 
         3095  +** More specifically, this function attempts to re-organize the database so 
  3096   3096   ** that the last page of the file currently in use is no longer in use.
  3097   3097   **
  3098   3098   ** Parameter nFin is the number of pages that this database would contain
  3099   3099   ** were this function called until it returns SQLITE_DONE.
  3100   3100   **
  3101   3101   ** If the bCommit parameter is non-zero, this function assumes that the 
  3102   3102   ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE 
  3103         -** or an error. bCommit is passed true for an auto-vacuum-on-commmit 
         3103  +** or an error. bCommit is passed true for an auto-vacuum-on-commit 
  3104   3104   ** operation, or false for an incremental vacuum.
  3105   3105   */
  3106   3106   static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
  3107   3107     Pgno nFreeList;           /* Number of pages still on the free-list */
  3108   3108     int rc;
  3109   3109   
  3110   3110     assert( sqlite3_mutex_held(pBt->mutex) );
................................................................................
  3563   3563   
  3564   3564     btreeEndTransaction(p);
  3565   3565     sqlite3BtreeLeave(p);
  3566   3566     return rc;
  3567   3567   }
  3568   3568   
  3569   3569   /*
  3570         -** Start a statement subtransaction. The subtransaction can can be rolled
         3570  +** Start a statement subtransaction. The subtransaction can be rolled
  3571   3571   ** back independently of the main transaction. You must start a transaction 
  3572   3572   ** before starting a subtransaction. The subtransaction is ended automatically 
  3573   3573   ** if the main transaction commits or rolls back.
  3574   3574   **
  3575   3575   ** Statement subtransactions are used around individual SQL statements
  3576   3576   ** that are contained within a BEGIN...COMMIT block.  If a constraint
  3577   3577   ** error occurs within the statement, the effect of that one statement
................................................................................
  3797   3797   ** BtCursor.info is a cache of the information in the current cell.
  3798   3798   ** Using this cache reduces the number of calls to btreeParseCell().
  3799   3799   **
  3800   3800   ** 2007-06-25:  There is a bug in some versions of MSVC that cause the
  3801   3801   ** compiler to crash when getCellInfo() is implemented as a macro.
  3802   3802   ** But there is a measureable speed advantage to using the macro on gcc
  3803   3803   ** (when less compiler optimizations like -Os or -O0 are used and the
  3804         -** compiler is not doing agressive inlining.)  So we use a real function
         3804  +** compiler is not doing aggressive inlining.)  So we use a real function
  3805   3805   ** for MSVC and a macro for everything else.  Ticket #2457.
  3806   3806   */
  3807   3807   #ifndef NDEBUG
  3808   3808     static void assertCellInfo(BtCursor *pCur){
  3809   3809       CellInfo info;
  3810   3810       int iPage = pCur->iPage;
  3811   3811       memset(&info, 0, sizeof(info));
................................................................................
  4014   4014   ** Data is read to or from the buffer pBuf.
  4015   4015   **
  4016   4016   ** The content being read or written might appear on the main page
  4017   4017   ** or be scattered out on multiple overflow pages.
  4018   4018   **
  4019   4019   ** If the current cursor entry uses one or more overflow pages and the
  4020   4020   ** eOp argument is not 2, this function may allocate space for and lazily 
  4021         -** popluates the overflow page-list cache array (BtCursor.aOverflow). 
         4021  +** populates the overflow page-list cache array (BtCursor.aOverflow). 
  4022   4022   ** Subsequent calls use this cache to make seeking to the supplied offset 
  4023   4023   ** more efficient.
  4024   4024   **
  4025   4025   ** Once an overflow page-list cache has been allocated, it may be
  4026   4026   ** invalidated if some other cursor writes to the same table, or if
  4027   4027   ** the cursor is moved to a different row. Additionally, in auto-vacuum
  4028   4028   ** mode, the following events may invalidate an overflow page-list cache.
................................................................................
  4216   4216       return SQLITE_CORRUPT_BKPT;
  4217   4217     }
  4218   4218     return rc;
  4219   4219   }
  4220   4220   
  4221   4221   /*
  4222   4222   ** Read part of the key associated with cursor pCur.  Exactly
  4223         -** "amt" bytes will be transfered into pBuf[].  The transfer
         4223  +** "amt" bytes will be transferred into pBuf[].  The transfer
  4224   4224   ** begins at "offset".
  4225   4225   **
  4226   4226   ** The caller must ensure that pCur is pointing to a valid row
  4227   4227   ** in the table.
  4228   4228   **
  4229   4229   ** Return SQLITE_OK on success or an error code if anything goes
  4230   4230   ** wrong.  An error is returned if "offset+amt" is larger than
................................................................................
  4769   4769           */
  4770   4770           nCell = pCell[0];
  4771   4771           if( nCell<=pPage->max1bytePayload ){
  4772   4772             /* This branch runs if the record-size field of the cell is a
  4773   4773             ** single byte varint and the record fits entirely on the main
  4774   4774             ** b-tree page.  */
  4775   4775             testcase( pCell+nCell+1==pPage->aDataEnd );
  4776         -          c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0);
         4776  +          c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
  4777   4777           }else if( !(pCell[1] & 0x80) 
  4778   4778             && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
  4779   4779           ){
  4780   4780             /* The record-size field is a 2 byte varint and the record 
  4781   4781             ** fits entirely on the main b-tree page.  */
  4782   4782             testcase( pCell+nCell+2==pPage->aDataEnd );
  4783         -          c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0);
         4783  +          c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
  4784   4784           }else{
  4785   4785             /* The record flows over onto one or more overflow pages. In
  4786   4786             ** this case the whole cell needs to be parsed, a buffer allocated
  4787   4787             ** and accessPayload() used to retrieve the record into the
  4788   4788             ** buffer before VdbeRecordCompare() can be called. */
  4789   4789             void *pCellKey;
  4790   4790             u8 * const pCellBody = pCell - pPage->childPtrSize;
................................................................................
  4797   4797             }
  4798   4798             pCur->aiIdx[pCur->iPage] = (u16)idx;
  4799   4799             rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
  4800   4800             if( rc ){
  4801   4801               sqlite3_free(pCellKey);
  4802   4802               goto moveto_finish;
  4803   4803             }
  4804         -          c = xRecordCompare(nCell, pCellKey, pIdxKey, 0);
         4804  +          c = xRecordCompare(nCell, pCellKey, pIdxKey);
  4805   4805             sqlite3_free(pCellKey);
  4806   4806           }
  4807   4807           assert( 
  4808   4808               (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
  4809   4809            && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
  4810   4810           );
  4811   4811           if( c<0 ){
................................................................................
  5912   5912   }
  5913   5913   
  5914   5914   /*
  5915   5915   ** Add a list of cells to a page.  The page should be initially empty.
  5916   5916   ** The cells are guaranteed to fit on the page.
  5917   5917   */
  5918   5918   static void assemblePage(
  5919         -  MemPage *pPage,   /* The page to be assemblied */
         5919  +  MemPage *pPage,   /* The page to be assembled */
  5920   5920     int nCell,        /* The number of cells to add to this page */
  5921   5921     u8 **apCell,      /* Pointers to cell bodies */
  5922   5922     u16 *aSize        /* Sizes of the cells */
  5923   5923   ){
  5924   5924     int i;            /* Loop counter */
  5925   5925     u8 *pCellptr;     /* Address of next cell pointer */
  5926   5926     int cellbody;     /* Address of next cell body */
................................................................................
  6578   6578       if( rc ) goto balance_cleanup;
  6579   6579       releasePage(apOld[i]);
  6580   6580       apOld[i] = 0;
  6581   6581       i++;
  6582   6582     }
  6583   6583   
  6584   6584     /*
  6585         -  ** Put the new pages in accending order.  This helps to
         6585  +  ** Put the new pages in ascending order.  This helps to
  6586   6586     ** keep entries in the disk file in order so that a scan
  6587   6587     ** of the table is a linear scan through the file.  That
  6588   6588     ** in turn helps the operating system to deliver pages
  6589   6589     ** from the disk more rapidly.
  6590   6590     **
  6591   6591     ** An O(n^2) insertion sort algorithm is used, but since
  6592   6592     ** n is never more than NB (a small constant), that should
................................................................................
  6973   6973            && pPage->aiOvfl[0]==pPage->nCell
  6974   6974            && pParent->pgno!=1
  6975   6975            && pParent->nCell==iIdx
  6976   6976           ){
  6977   6977             /* Call balance_quick() to create a new sibling of pPage on which
  6978   6978             ** to store the overflow cell. balance_quick() inserts a new cell
  6979   6979             ** into pParent, which may cause pParent overflow. If this
  6980         -          ** happens, the next interation of the do-loop will balance pParent 
         6980  +          ** happens, the next iteration of the do-loop will balance pParent 
  6981   6981             ** use either balance_nonroot() or balance_deeper(). Until this
  6982   6982             ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
  6983   6983             ** buffer. 
  6984   6984             **
  6985   6985             ** The purpose of the following assert() is to check that only a
  6986   6986             ** single call to balance_quick() is made for each call to this
  6987   6987             ** function. If this were not verified, a subtle bug involving reuse
................................................................................
  7050   7050   ** For an INTKEY table, only the nKey value of the key is used.  pKey is
  7051   7051   ** ignored.  For a ZERODATA table, the pData and nData are both ignored.
  7052   7052   **
  7053   7053   ** If the seekResult parameter is non-zero, then a successful call to
  7054   7054   ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
  7055   7055   ** been performed. seekResult is the search result returned (a negative
  7056   7056   ** number if pCur points at an entry that is smaller than (pKey, nKey), or
  7057         -** a positive value if pCur points at an etry that is larger than 
         7057  +** a positive value if pCur points at an entry that is larger than 
  7058   7058   ** (pKey, nKey)). 
  7059   7059   **
  7060   7060   ** If the seekResult parameter is non-zero, then the caller guarantees that
  7061   7061   ** cursor pCur is pointing at the existing copy of a row that is to be
  7062   7062   ** overwritten.  If the seekResult parameter is 0, then cursor pCur may
  7063   7063   ** point to any entry or to no entry at all and so this function has to seek
  7064   7064   ** the cursor before the new key can be inserted.
................................................................................
  7207   7207   
  7208   7208   end_insert:
  7209   7209     return rc;
  7210   7210   }
  7211   7211   
  7212   7212   /*
  7213   7213   ** Delete the entry that the cursor is pointing to.  The cursor
  7214         -** is left pointing at a arbitrary location.
         7214  +** is left pointing at an arbitrary location.
  7215   7215   */
  7216   7216   int sqlite3BtreeDelete(BtCursor *pCur){
  7217   7217     Btree *p = pCur->pBtree;
  7218   7218     BtShared *pBt = p->pBt;              
  7219   7219     int rc;                              /* Return code */
  7220   7220     MemPage *pPage;                      /* Page to delete cell from */
  7221   7221     unsigned char *pCell;                /* Pointer to cell to delete */
................................................................................
  7905   7905     pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
  7906   7906   }
  7907   7907   
  7908   7908   
  7909   7909   /*
  7910   7910   ** Add 1 to the reference count for page iPage.  If this is the second
  7911   7911   ** reference to the page, add an error message to pCheck->zErrMsg.
  7912         -** Return 1 if there are 2 ore more references to the page and 0 if
         7912  +** Return 1 if there are 2 or more references to the page and 0 if
  7913   7913   ** if this is the first reference to the page.
  7914   7914   **
  7915   7915   ** Also check that the page number is in bounds.
  7916   7916   */
  7917   7917   static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
  7918   7918     if( iPage==0 ) return 1;
  7919   7919     if( iPage>pCheck->nPage ){

Changes to src/btreeInt.h.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** This file implements a external (disk-based) database using BTrees.
           12  +** This file implements an external (disk-based) database using BTrees.
    13     13   ** For a detailed discussion of BTrees, refer to
    14     14   **
    15     15   **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
    16     16   **     "Sorting And Searching", pages 473-480. Addison-Wesley
    17     17   **     Publishing Company, Reading, Massachusetts.
    18     18   **
    19     19   ** The basic idea is that each page of the file contains N database
................................................................................
   131    131   **      3       2      number of cells on this page
   132    132   **      5       2      first byte of the cell content area
   133    133   **      7       1      number of fragmented free bytes
   134    134   **      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
   135    135   **
   136    136   ** The flags define the format of this btree page.  The leaf flag means that
   137    137   ** this page has no children.  The zerodata flag means that this page carries
   138         -** only keys and no data.  The intkey flag means that the key is a integer
          138  +** only keys and no data.  The intkey flag means that the key is an integer
   139    139   ** which is stored in the key size entry of the cell header rather than in
   140    140   ** the payload area.
   141    141   **
   142    142   ** The cell pointer array begins on the first byte after the page header.
   143    143   ** The cell pointer array contains zero or more 2-byte numbers which are
   144    144   ** offsets from the beginning of the page to the cell content in the cell
   145    145   ** content area.  The cell pointers occur in sorted order.  The system strives
................................................................................
   540    540   **   The table that this cursor was opened on still exists, but has been 
   541    541   **   modified since the cursor was last used. The cursor position is saved
   542    542   **   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 
   543    543   **   this state, restoreCursorPosition() can be called to attempt to
   544    544   **   seek the cursor to the saved position.
   545    545   **
   546    546   ** CURSOR_FAULT:
   547         -**   A unrecoverable error (an I/O error or a malloc failure) has occurred
          547  +**   An unrecoverable error (an I/O error or a malloc failure) has occurred
   548    548   **   on a different connection that shares the BtShared cache with this
   549    549   **   cursor.  The error has left the cache in an inconsistent state.
   550    550   **   Do nothing else with this cursor.  Any attempt to use the cursor
   551    551   **   should return the error code stored in BtCursor.skip
   552    552   */
   553    553   #define CURSOR_INVALID           0
   554    554   #define CURSOR_VALID             1

Changes to src/build.c.

   151    151     */
   152    152     v = sqlite3GetVdbe(pParse);
   153    153     assert( !pParse->isMultiWrite 
   154    154          || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
   155    155     if( v ){
   156    156       while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){}
   157    157       sqlite3VdbeAddOp0(v, OP_Halt);
          158  +
          159  +#if SQLITE_USER_AUTHENTICATION
          160  +    if( pParse->nTableLock>0 && db->init.busy==0 ){
          161  +      sqlite3UserAuthInit(db);
          162  +      if( db->auth.authLevel<UAUTH_User ){
          163  +        pParse->rc = SQLITE_AUTH_USER;
          164  +        sqlite3ErrorMsg(pParse, "user not authenticated");
          165  +        return;
          166  +      }
          167  +    }
          168  +#endif
   158    169   
   159    170       /* The cookie mask contains one bit for each database file open.
   160    171       ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
   161    172       ** set for each database that is used.  Generate code to start a
   162    173       ** transaction on each used database and to verify the schema cookie
   163    174       ** on each used database.
   164    175       */
................................................................................
   267    278     sqlite3RunParser(pParse, zSql, &zErrMsg);
   268    279     sqlite3DbFree(db, zErrMsg);
   269    280     sqlite3DbFree(db, zSql);
   270    281     memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
   271    282     pParse->nested--;
   272    283   }
   273    284   
          285  +#if SQLITE_USER_AUTHENTICATION
          286  +/*
          287  +** Return TRUE if zTable is the name of the system table that stores the
          288  +** list of users and their access credentials.
          289  +*/
          290  +int sqlite3UserAuthTable(const char *zTable){
          291  +  return sqlite3_stricmp(zTable, "sqlite_user")==0;
          292  +}
          293  +#endif
          294  +
   274    295   /*
   275    296   ** Locate the in-memory structure that describes a particular database
   276    297   ** table given the name of that table and (optionally) the name of the
   277    298   ** database containing the table.  Return NULL if not found.
   278    299   **
   279    300   ** If zDatabase is 0, all databases are searched for the table and the
   280    301   ** first matching table is returned.  (No checking for duplicate table
................................................................................
   285    306   */
   286    307   Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
   287    308     Table *p = 0;
   288    309     int i;
   289    310     assert( zName!=0 );
   290    311     /* All mutexes are required for schema access.  Make sure we hold them. */
   291    312     assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
          313  +#if SQLITE_USER_AUTHENTICATION
          314  +  /* Only the admin user is allowed to know that the sqlite_user table
          315  +  ** exists */
          316  +  if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
          317  +    return 0;
          318  +  }
          319  +#endif
   292    320     for(i=OMIT_TEMPDB; i<db->nDb; i++){
   293    321       int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
   294    322       if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
   295    323       assert( sqlite3SchemaMutexHeld(db, j, 0) );
   296    324       p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
   297    325       if( p ) break;
   298    326     }
................................................................................
   329    357       if( zDbase ){
   330    358         sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
   331    359       }else{
   332    360         sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
   333    361       }
   334    362       pParse->checkSchema = 1;
   335    363     }
          364  +#if SQLITE_USER_AUTHENICATION
          365  +  else if( pParse->db->auth.authLevel<UAUTH_User ){
          366  +    sqlite3ErrorMsg(pParse, "user not authenticated");
          367  +    p = 0;
          368  +  }
          369  +#endif
   336    370     return p;
   337    371   }
   338    372   
   339    373   /*
   340    374   ** Locate the table identified by *p.
   341    375   **
   342    376   ** This is a wrapper around sqlite3LocateTable(). The difference between
................................................................................
  1139   1173       }
  1140   1174     }
  1141   1175   
  1142   1176     /* If pszEst is not NULL, store an estimate of the field size.  The
  1143   1177     ** estimate is scaled so that the size of an integer is 1.  */
  1144   1178     if( pszEst ){
  1145   1179       *pszEst = 1;   /* default size is approx 4 bytes */
  1146         -    if( aff<=SQLITE_AFF_NONE ){
         1180  +    if( aff<SQLITE_AFF_NUMERIC ){
  1147   1181         if( zChar ){
  1148   1182           while( zChar[0] ){
  1149   1183             if( sqlite3Isdigit(zChar[0]) ){
  1150   1184               int v = 0;
  1151   1185               sqlite3GetInt32(zChar, &v);
  1152   1186               v = v/4 + 1;
  1153   1187               if( v>255 ) v = 255;
................................................................................
  1510   1544     }
  1511   1545     sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
  1512   1546     k = sqlite3Strlen30(zStmt);
  1513   1547     identPut(zStmt, &k, p->zName);
  1514   1548     zStmt[k++] = '(';
  1515   1549     for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
  1516   1550       static const char * const azType[] = {
  1517         -        /* SQLITE_AFF_TEXT    */ " TEXT",
  1518   1551           /* SQLITE_AFF_NONE    */ "",
         1552  +        /* SQLITE_AFF_TEXT    */ " TEXT",
  1519   1553           /* SQLITE_AFF_NUMERIC */ " NUM",
  1520   1554           /* SQLITE_AFF_INTEGER */ " INT",
  1521   1555           /* SQLITE_AFF_REAL    */ " REAL"
  1522   1556       };
  1523   1557       int len;
  1524   1558       const char *zType;
  1525   1559   
  1526   1560       sqlite3_snprintf(n-k, &zStmt[k], zSep);
  1527   1561       k += sqlite3Strlen30(&zStmt[k]);
  1528   1562       zSep = zSep2;
  1529   1563       identPut(zStmt, &k, pCol->zName);
  1530         -    assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
  1531         -    assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
  1532         -    testcase( pCol->affinity==SQLITE_AFF_TEXT );
         1564  +    assert( pCol->affinity-SQLITE_AFF_NONE >= 0 );
         1565  +    assert( pCol->affinity-SQLITE_AFF_NONE < ArraySize(azType) );
  1533   1566       testcase( pCol->affinity==SQLITE_AFF_NONE );
         1567  +    testcase( pCol->affinity==SQLITE_AFF_TEXT );
  1534   1568       testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
  1535   1569       testcase( pCol->affinity==SQLITE_AFF_INTEGER );
  1536   1570       testcase( pCol->affinity==SQLITE_AFF_REAL );
  1537   1571       
  1538         -    zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
         1572  +    zType = azType[pCol->affinity - SQLITE_AFF_NONE];
  1539   1573       len = sqlite3Strlen30(zType);
  1540   1574       assert( pCol->affinity==SQLITE_AFF_NONE 
  1541   1575               || pCol->affinity==sqlite3AffinityType(zType, 0) );
  1542   1576       memcpy(&zStmt[k], zType, len);
  1543   1577       k += len;
  1544   1578       assert( k<=n );
  1545   1579     }
................................................................................
  1615   1649   ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
  1616   1650   ** Changes include:
  1617   1651   **
  1618   1652   **     (1)  Convert the OP_CreateTable into an OP_CreateIndex.  There is
  1619   1653   **          no rowid btree for a WITHOUT ROWID.  Instead, the canonical
  1620   1654   **          data storage is a covering index btree.
  1621   1655   **     (2)  Bypass the creation of the sqlite_master table entry
  1622         -**          for the PRIMARY KEY as the the primary key index is now
         1656  +**          for the PRIMARY KEY as the primary key index is now
  1623   1657   **          identified by the sqlite_master table entry of the table itself.
  1624   1658   **     (3)  Set the Index.tnum of the PRIMARY KEY Index object in the
  1625   1659   **          schema to the rootpage from the main table.
  1626   1660   **     (4)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
  1627   1661   **     (5)  Add all table columns to the PRIMARY KEY Index object
  1628   1662   **          so that the PRIMARY KEY is a covering index.  The surplus
  1629   1663   **          columns are part of KeyInfo.nXField and are not used for
................................................................................
  1636   1670     Index *pPk;
  1637   1671     int nPk;
  1638   1672     int i, j;
  1639   1673     sqlite3 *db = pParse->db;
  1640   1674     Vdbe *v = pParse->pVdbe;
  1641   1675   
  1642   1676     /* Convert the OP_CreateTable opcode that would normally create the
  1643         -  ** root-page for the table into a OP_CreateIndex opcode.  The index
         1677  +  ** root-page for the table into an OP_CreateIndex opcode.  The index
  1644   1678     ** created will become the PRIMARY KEY index.
  1645   1679     */
  1646   1680     if( pParse->addrCrTab ){
  1647   1681       assert( v );
  1648   1682       sqlite3VdbeGetOp(v, pParse->addrCrTab)->opcode = OP_CreateIndex;
  1649   1683     }
  1650   1684   
................................................................................
  2048   2082   */
  2049   2083   int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
  2050   2084     Table *pSelTab;   /* A fake table from which we get the result set */
  2051   2085     Select *pSel;     /* Copy of the SELECT that implements the view */
  2052   2086     int nErr = 0;     /* Number of errors encountered */
  2053   2087     int n;            /* Temporarily holds the number of cursors assigned */
  2054   2088     sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
  2055         -  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
         2089  +  sqlite3_xauth xAuth;       /* Saved xAuth pointer */
  2056   2090   
  2057   2091     assert( pTable );
  2058   2092   
  2059   2093   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2060   2094     if( sqlite3VtabCallConnect(pParse, pTable) ){
  2061   2095       return SQLITE_ERROR;
  2062   2096     }
................................................................................
  2650   2684     int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
  2651   2685     int addr1;                     /* Address of top of loop */
  2652   2686     int addr2;                     /* Address to jump to for next iteration */
  2653   2687     int tnum;                      /* Root page of index */
  2654   2688     int iPartIdxLabel;             /* Jump to this label to skip a row */
  2655   2689     Vdbe *v;                       /* Generate code into this virtual machine */
  2656   2690     KeyInfo *pKey;                 /* KeyInfo for index */
  2657         -  int regRecord;                 /* Register holding assemblied index record */
         2691  +  int regRecord;                 /* Register holding assembled index record */
  2658   2692     sqlite3 *db = pParse->db;      /* The database connection */
  2659   2693     int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
  2660   2694   
  2661   2695   #ifndef SQLITE_OMIT_AUTHORIZATION
  2662   2696     if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
  2663   2697         db->aDb[iDb].zName ) ){
  2664   2698       return;
................................................................................
  2863   2897       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  2864   2898     }
  2865   2899     pDb = &db->aDb[iDb];
  2866   2900   
  2867   2901     assert( pTab!=0 );
  2868   2902     assert( pParse->nErr==0 );
  2869   2903     if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
         2904  +       && db->init.busy==0
         2905  +#if SQLITE_USER_AUTHENTICATION
         2906  +       && sqlite3UserAuthTable(pTab->zName)==0
         2907  +#endif
  2870   2908          && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
  2871   2909       sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
  2872   2910       goto exit_create_index;
  2873   2911     }
  2874   2912   #ifndef SQLITE_OMIT_VIEW
  2875   2913     if( pTab->pSelect ){
  2876   2914       sqlite3ErrorMsg(pParse, "views may not be indexed");
................................................................................
  3250   3288     return pRet;
  3251   3289   }
  3252   3290   
  3253   3291   /*
  3254   3292   ** Fill the Index.aiRowEst[] array with default information - information
  3255   3293   ** to be used when we have not run the ANALYZE command.
  3256   3294   **
  3257         -** aiRowEst[0] is suppose to contain the number of elements in the index.
         3295  +** aiRowEst[0] is supposed to contain the number of elements in the index.
  3258   3296   ** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
  3259   3297   ** number of rows in the table that match any particular value of the
  3260   3298   ** first column of the index.  aiRowEst[2] is an estimate of the number
  3261   3299   ** of rows that match any particular combination of the first 2 columns
  3262   3300   ** of the index.  And so forth.  It must always be the case that
  3263   3301   *
  3264   3302   **           aiRowEst[N]<=aiRowEst[N-1]
................................................................................
  3629   3667   /*
  3630   3668   ** This routine is called by the parser to add a new term to the
  3631   3669   ** end of a growing FROM clause.  The "p" parameter is the part of
  3632   3670   ** the FROM clause that has already been constructed.  "p" is NULL
  3633   3671   ** if this is the first term of the FROM clause.  pTable and pDatabase
  3634   3672   ** are the name of the table and database named in the FROM clause term.
  3635   3673   ** pDatabase is NULL if the database name qualifier is missing - the
  3636         -** usual case.  If the term has a alias, then pAlias points to the
         3674  +** usual case.  If the term has an alias, then pAlias points to the
  3637   3675   ** alias token.  If the term is a subquery, then pSubquery is the
  3638   3676   ** SELECT statement that the subquery encodes.  The pTable and
  3639   3677   ** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
  3640   3678   ** parameters are the content of the ON and USING clauses.
  3641   3679   **
  3642   3680   ** Return a new SrcList which encodes is the FROM with the new
  3643   3681   ** term added.

Changes to src/callback.c.

   138    138   /*
   139    139   ** Locate and return an entry from the db.aCollSeq hash table. If the entry
   140    140   ** specified by zName and nName is not found and parameter 'create' is
   141    141   ** true, then create a new entry. Otherwise return NULL.
   142    142   **
   143    143   ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
   144    144   ** array of three CollSeq structures. The first is the collation sequence
   145         -** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
          145  +** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
   146    146   **
   147    147   ** Stored immediately after the three collation sequences is a copy of
   148    148   ** the collation sequence name. A pointer to this string is stored in
   149    149   ** each collation sequence structure.
   150    150   */
   151    151   static CollSeq *findCollSeqEntry(
   152    152     sqlite3 *db,          /* Database connection */

Changes to src/complete.c.

    66     66   **   (2) NORMAL    We are in the middle of statement which ends with a single
    67     67   **                 semicolon.
    68     68   **
    69     69   **   (3) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of 
    70     70   **                 a statement.
    71     71   **
    72     72   **   (4) CREATE    The keyword CREATE has been seen at the beginning of a
    73         -**                 statement, possibly preceeded by EXPLAIN and/or followed by
           73  +**                 statement, possibly preceded by EXPLAIN and/or followed by
    74     74   **                 TEMP or TEMPORARY
    75     75   **
    76     76   **   (5) TRIGGER   We are in the middle of a trigger definition that must be
    77     77   **                 ended by a semicolon, the keyword END, and another semicolon.
    78     78   **
    79     79   **   (6) SEMI      We've seen the first semicolon in the ";END;" that occurs at
    80     80   **                 the end of a trigger definition.
    81     81   **
    82     82   **   (7) END       We've seen the ";END" of the ";END;" that occurs at the end
    83         -**                 of a trigger difinition.
           83  +**                 of a trigger definition.
    84     84   **
    85     85   ** Transitions between states above are determined by tokens extracted
    86     86   ** from the input.  The following tokens are significant:
    87     87   **
    88     88   **   (0) tkSEMI      A semicolon.
    89     89   **   (1) tkWS        Whitespace.
    90     90   **   (2) tkOTHER     Any other SQL token.
................................................................................
   119    119        /* 4  CREATE: */ {    1,  4,     2,       2,      2,    4,       5,   2, },
   120    120        /* 5 TRIGGER: */ {    6,  5,     5,       5,      5,    5,       5,   5, },
   121    121        /* 6    SEMI: */ {    6,  6,     5,       5,      5,    5,       5,   7, },
   122    122        /* 7     END: */ {    1,  7,     5,       5,      5,    5,       5,   5, },
   123    123     };
   124    124   #else
   125    125     /* If triggers are not supported by this compile then the statement machine
   126         -  ** used to detect the end of a statement is much simplier
          126  +  ** used to detect the end of a statement is much simpler
   127    127     */
   128    128     static const u8 trans[3][3] = {
   129    129                        /* Token:           */
   130    130        /* State:       **  SEMI  WS  OTHER */
   131    131        /* 0 INVALID: */ {    1,  0,     2, },
   132    132        /* 1   START: */ {    1,  1,     2, },
   133    133        /* 2  NORMAL: */ {    1,  2,     2, },

Changes to src/ctime.c.

   363    363     "TEST",
   364    364   #endif
   365    365   #if defined(SQLITE_THREADSAFE)
   366    366     "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
   367    367   #endif
   368    368   #ifdef SQLITE_USE_ALLOCA
   369    369     "USE_ALLOCA",
          370  +#endif
          371  +#ifdef SQLITE_USER_AUTHENTICATION
          372  +  "USER_AUTHENTICATION",
   370    373   #endif
   371    374   #ifdef SQLITE_WIN32_MALLOC
   372    375     "WIN32_MALLOC",
   373    376   #endif
   374    377   #ifdef SQLITE_ZERO_MALLOC
   375    378     "ZERO_MALLOC"
   376    379   #endif

Changes to src/date.c.

    20     20   ** dates and times are stored as the number of days since noon
    21     21   ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
    22     22   ** calendar system. 
    23     23   **
    24     24   ** 1970-01-01 00:00:00 is JD 2440587.5
    25     25   ** 2000-01-01 00:00:00 is JD 2451544.5
    26     26   **
    27         -** This implemention requires years to be expressed as a 4-digit number
           27  +** This implementation requires years to be expressed as a 4-digit number
    28     28   ** which means that only dates between 0000-01-01 and 9999-12-31 can
    29     29   ** be represented, even though julian day numbers allow a much wider
    30     30   ** range of dates.
    31     31   **
    32     32   ** The Gregorian calendar system is used for all dates and times,
    33     33   ** even those that predate the Gregorian calendar.  Historians usually
    34     34   ** use the Julian calendar for dates prior to 1582-10-15 and for some

Changes to src/delete.c.

    86     86   ** pWhere argument is an optional WHERE clause that restricts the
    87     87   ** set of rows in the view that are to be added to the ephemeral table.
    88     88   */
    89     89   void sqlite3MaterializeView(
    90     90     Parse *pParse,       /* Parsing context */
    91     91     Table *pView,        /* View definition */
    92     92     Expr *pWhere,        /* Optional WHERE clause to be added */
    93         -  int iCur             /* Cursor number for ephemerial table */
           93  +  int iCur             /* Cursor number for ephemeral table */
    94     94   ){
    95     95     SelectDest dest;
    96     96     Select *pSel;
    97     97     SrcList *pFrom;
    98     98     sqlite3 *db = pParse->db;
    99     99     int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
   100    100     pWhere = sqlite3ExprDup(db, pWhere, 0);
................................................................................
   244    244     int iKey;              /* Memory cell holding key of row to be deleted */
   245    245     i16 nKey;              /* Number of memory cells in the row key */
   246    246     int iEphCur = 0;       /* Ephemeral table holding all primary key values */
   247    247     int iRowSet = 0;       /* Register for rowset of rows to delete */
   248    248     int addrBypass = 0;    /* Address of jump over the delete logic */
   249    249     int addrLoop = 0;      /* Top of the delete loop */
   250    250     int addrDelete = 0;    /* Jump directly to the delete logic */
   251         -  int addrEphOpen = 0;   /* Instruction to open the Ephermeral table */
          251  +  int addrEphOpen = 0;   /* Instruction to open the Ephemeral table */
   252    252    
   253    253   #ifndef SQLITE_OMIT_TRIGGER
   254    254     int isView;                  /* True if attempting to delete from a view */
   255    255     Trigger *pTrigger;           /* List of table triggers, if required */
   256    256   #endif
   257    257   
   258    258     memset(&sContext, 0, sizeof(sContext));
................................................................................
   324    324     if( v==0 ){
   325    325       goto delete_from_cleanup;
   326    326     }
   327    327     if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
   328    328     sqlite3BeginWriteOperation(pParse, 1, iDb);
   329    329   
   330    330     /* If we are trying to delete from a view, realize that view into
   331         -  ** a ephemeral table.
          331  +  ** an ephemeral table.
   332    332     */
   333    333   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
   334    334     if( isView ){
   335    335       sqlite3MaterializeView(pParse, pTab, pWhere, iTabCur);
   336    336       iDataCur = iIdxCur = iTabCur;
   337    337     }
   338    338   #endif
................................................................................
   378    378       if( HasRowid(pTab) ){
   379    379         /* For a rowid table, initialize the RowSet to an empty set */
   380    380         pPk = 0;
   381    381         nPk = 1;
   382    382         iRowSet = ++pParse->nMem;
   383    383         sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
   384    384       }else{
   385         -      /* For a WITHOUT ROWID table, create an ephermeral table used to
          385  +      /* For a WITHOUT ROWID table, create an ephemeral table used to
   386    386         ** hold all primary keys for rows to be deleted. */
   387    387         pPk = sqlite3PrimaryKeyIndex(pTab);
   388    388         assert( pPk!=0 );
   389    389         nPk = pPk->nKeyCol;
   390    390         iPk = pParse->nMem+1;
   391    391         pParse->nMem += nPk;
   392    392         iEphCur = pParse->nTab++;
................................................................................
   553    553     sqlite3AuthContextPop(&sContext);
   554    554     sqlite3SrcListDelete(db, pTabList);
   555    555     sqlite3ExprDelete(db, pWhere);
   556    556     sqlite3DbFree(db, aToOpen);
   557    557     return;
   558    558   }
   559    559   /* Make sure "isView" and other macros defined above are undefined. Otherwise
   560         -** thely may interfere with compilation of other functions in this file
          560  +** they may interfere with compilation of other functions in this file
   561    561   ** (or in another file, if this file becomes part of the amalgamation).  */
   562    562   #ifdef isView
   563    563    #undef isView
   564    564   #endif
   565    565   #ifdef pTrigger
   566    566    #undef pTrigger
   567    567   #endif

Changes to src/expr.c.

    18     18   ** Return the 'affinity' of the expression pExpr if any.
    19     19   **
    20     20   ** If pExpr is a column, a reference to a column via an 'AS' alias,
    21     21   ** or a sub-select with a column as the return value, then the 
    22     22   ** affinity of that column is returned. Otherwise, 0x00 is returned,
    23     23   ** indicating no affinity for the expression.
    24     24   **
    25         -** i.e. the WHERE clause expresssions in the following statements all
           25  +** i.e. the WHERE clause expressions in the following statements all
    26     26   ** have an affinity:
    27     27   **
    28     28   ** CREATE TABLE t1(a);
    29     29   ** SELECT * FROM t1 WHERE a;
    30     30   ** SELECT a AS b FROM t1 WHERE b;
    31     31   ** SELECT * FROM t1 WHERE (select a from t1);
    32     32   */
................................................................................
   497    497         pRoot->flags |= EP_Collate & pLeft->flags;
   498    498       }
   499    499       exprSetHeight(pRoot);
   500    500     }
   501    501   }
   502    502   
   503    503   /*
   504         -** Allocate a Expr node which joins as many as two subtrees.
          504  +** Allocate an Expr node which joins as many as two subtrees.
   505    505   **
   506    506   ** One or both of the subtrees can be NULL.  Return a pointer to the new
   507    507   ** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
   508    508   ** free the subtrees and return NULL.
   509    509   */
   510    510   Expr *sqlite3PExpr(
   511    511     Parse *pParse,          /* Parsing context */
................................................................................
   607    607   **
   608    608   ** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
   609    609   ** sure "nnn" is not too be to avoid a denial of service attack when
   610    610   ** the SQL statement comes from an external source.
   611    611   **
   612    612   ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
   613    613   ** as the previous instance of the same wildcard.  Or if this is the first
   614         -** instance of the wildcard, the next sequenial variable number is
          614  +** instance of the wildcard, the next sequential variable number is
   615    615   ** assigned.
   616    616   */
   617    617   void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
   618    618     sqlite3 *db = pParse->db;
   619    619     const char *z;
   620    620   
   621    621     if( pExpr==0 ) return;
................................................................................
   742    742   ** return value with EP_Reduced|EP_TokenOnly.
   743    743   **
   744    744   ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
   745    745   ** (unreduced) Expr objects as they or originally constructed by the parser.
   746    746   ** During expression analysis, extra information is computed and moved into
   747    747   ** later parts of teh Expr object and that extra information might get chopped
   748    748   ** off if the expression is reduced.  Note also that it does not work to
   749         -** make a EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
          749  +** make an EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
   750    750   ** to reduce a pristine expression tree from the parser.  The implementation
   751    751   ** of dupedExprStructSize() contain multiple assert() statements that attempt
   752    752   ** to enforce this constraint.
   753    753   */
   754    754   static int dupedExprStructSize(Expr *p, int flags){
   755    755     int nSize;
   756    756     assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
................................................................................
   811    811   }
   812    812   
   813    813   /*
   814    814   ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 
   815    815   ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 
   816    816   ** to store the copy of expression p, the copies of p->u.zToken
   817    817   ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
   818         -** if any. Before returning, *pzBuffer is set to the first byte passed the
          818  +** if any. Before returning, *pzBuffer is set to the first byte past the
   819    819   ** portion of the buffer copied into by this function.
   820    820   */
   821    821   static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
   822    822     Expr *pNew = 0;                      /* Value to return */
   823    823     if( p ){
   824    824       const int isReduced = (flags&EXPRDUP_REDUCE);
   825    825       u8 *zAlloc;
................................................................................
  1065   1065     pNew->iLimit = 0;
  1066   1066     pNew->iOffset = 0;
  1067   1067     pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
  1068   1068     pNew->addrOpenEphm[0] = -1;
  1069   1069     pNew->addrOpenEphm[1] = -1;
  1070   1070     pNew->nSelectRow = p->nSelectRow;
  1071   1071     pNew->pWith = withDup(db, p->pWith);
         1072  +  sqlite3SelectSetName(pNew, p->zSelName);
  1072   1073     return pNew;
  1073   1074   }
  1074   1075   #else
  1075   1076   Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
  1076   1077     assert( p==0 );
  1077   1078     return 0;
  1078   1079   }
................................................................................
  1537   1538   ** An existing b-tree might be used if the RHS expression pX is a simple
  1538   1539   ** subquery such as:
  1539   1540   **
  1540   1541   **     SELECT <column> FROM <table>
  1541   1542   **
  1542   1543   ** If the RHS of the IN operator is a list or a more complex subquery, then
  1543   1544   ** an ephemeral table might need to be generated from the RHS and then
  1544         -** pX->iTable made to point to the ephermeral table instead of an
         1545  +** pX->iTable made to point to the ephemeral table instead of an
  1545   1546   ** existing table.
  1546   1547   **
  1547   1548   ** The inFlags parameter must contain exactly one of the bits
  1548   1549   ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP.  If inFlags contains
  1549   1550   ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
  1550   1551   ** fast membership test.  When the IN_INDEX_LOOP bit is set, the
  1551   1552   ** IN index will be used to loop over all values of the RHS of the
................................................................................
  1667   1668       }
  1668   1669     }
  1669   1670   
  1670   1671     /* If no preexisting index is available for the IN clause
  1671   1672     ** and IN_INDEX_NOOP is an allowed reply
  1672   1673     ** and the RHS of the IN operator is a list, not a subquery
  1673   1674     ** and the RHS is not contant or has two or fewer terms,
  1674         -  ** then it is not worth creating an ephermeral table to evaluate
         1675  +  ** then it is not worth creating an ephemeral table to evaluate
  1675   1676     ** the IN operator so return IN_INDEX_NOOP.
  1676   1677     */
  1677   1678     if( eType==0
  1678   1679      && (inFlags & IN_INDEX_NOOP_OK)
  1679   1680      && !ExprHasProperty(pX, EP_xIsSelect)
  1680   1681      && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
  1681   1682     ){
................................................................................
  2428   2429   }
  2429   2430   
  2430   2431   /*
  2431   2432   ** Generate code to move content from registers iFrom...iFrom+nReg-1
  2432   2433   ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
  2433   2434   */
  2434   2435   void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
  2435         -  int i;
  2436         -  struct yColCache *p;
  2437   2436     assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
  2438   2437     sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
  2439         -  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
  2440         -    int x = p->iReg;
  2441         -    if( x>=iFrom && x<iFrom+nReg ){
  2442         -      p->iReg += iTo-iFrom;
  2443         -    }
  2444         -  }
         2438  +  sqlite3ExprCacheRemove(pParse, iFrom, nReg);
  2445   2439   }
  2446   2440   
  2447   2441   #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
  2448   2442   /*
  2449   2443   ** Return true if any register in the range iFrom..iTo (inclusive)
  2450   2444   ** is used as part of the column cache.
  2451   2445   **
................................................................................
  2754   2748         pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
  2755   2749         if( pDef==0 || pDef->xFunc==0 ){
  2756   2750           sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
  2757   2751           break;
  2758   2752         }
  2759   2753   
  2760   2754         /* Attempt a direct implementation of the built-in COALESCE() and
  2761         -      ** IFNULL() functions.  This avoids unnecessary evalation of
         2755  +      ** IFNULL() functions.  This avoids unnecessary evaluation of
  2762   2756         ** arguments past the first non-NULL argument.
  2763   2757         */
  2764   2758         if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
  2765   2759           int endCoalesce = sqlite3VdbeMakeLabel(v);
  2766   2760           assert( nFarg>=2 );
  2767   2761           sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
  2768   2762           for(i=1; i<nFarg; i++){
................................................................................
  3193   3187       sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
  3194   3188     }else{
  3195   3189       sqlite3ExprCode(pParse, pExpr, target);
  3196   3190     }
  3197   3191   }
  3198   3192   
  3199   3193   /*
  3200         -** Generate code that evalutes the given expression and puts the result
         3194  +** Generate code that evaluates the given expression and puts the result
  3201   3195   ** in register target.
  3202   3196   **
  3203   3197   ** Also make a copy of the expression results into another "cache" register
  3204   3198   ** and modify the expression so that the next time it is evaluated,
  3205   3199   ** the result is a copy of the cache register.
  3206   3200   **
  3207   3201   ** This routine is used for expressions that are used multiple 
................................................................................
  3548   3542   **    x BETWEEN y AND z
  3549   3543   **
  3550   3544   ** The above is equivalent to 
  3551   3545   **
  3552   3546   **    x>=y AND x<=z
  3553   3547   **
  3554   3548   ** Code it as such, taking care to do the common subexpression
  3555         -** elementation of x.
         3549  +** elimination of x.
  3556   3550   */
  3557   3551   static void exprCodeBetween(
  3558   3552     Parse *pParse,    /* Parsing and code generating context */
  3559   3553     Expr *pExpr,      /* The BETWEEN expression */
  3560   3554     int dest,         /* Jump here if the jump is taken */
  3561   3555     int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
  3562   3556     int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
................................................................................
  4285   4279   }
  4286   4280   
  4287   4281   /*
  4288   4282   ** Deallocate a register, making available for reuse for some other
  4289   4283   ** purpose.
  4290   4284   **
  4291   4285   ** If a register is currently being used by the column cache, then
  4292         -** the dallocation is deferred until the column cache line that uses
         4286  +** the deallocation is deferred until the column cache line that uses
  4293   4287   ** the register becomes stale.
  4294   4288   */
  4295   4289   void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
  4296   4290     if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
  4297   4291       int i;
  4298   4292       struct yColCache *p;
  4299   4293       for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){

Changes to src/fkey.c.

   169    169   **
   170    170   **   3) No parent key columns were provided explicitly as part of the
   171    171   **      foreign key definition, and the parent table does not have a
   172    172   **      PRIMARY KEY, or
   173    173   **
   174    174   **   4) No parent key columns were provided explicitly as part of the
   175    175   **      foreign key definition, and the PRIMARY KEY of the parent table 
   176         -**      consists of a a different number of columns to the child key in 
          176  +**      consists of a different number of columns to the child key in 
   177    177   **      the child table.
   178    178   **
   179    179   ** then non-zero is returned, and a "foreign key mismatch" error loaded
   180    180   ** into pParse. If an OOM error occurs, non-zero is returned and the
   181    181   ** pParse->db->mallocFailed flag is set.
   182    182   */
   183    183   int sqlite3FkLocateIndex(

Changes to src/func.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** This file contains the C-language implementions for many of the SQL
           12  +** This file contains the C-language implementations for many of the SQL
    13     13   ** functions of SQLite.  (Some function, and in particular the date and
    14     14   ** time functions, are implemented separately.)
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   #include <stdlib.h>
    18     18   #include <assert.h>
    19     19   #include "vdbeInt.h"
................................................................................
   324    324       while( *z && p1 ){
   325    325         SQLITE_SKIP_UTF8(z);
   326    326         p1--;
   327    327       }
   328    328       for(z2=z; *z2 && p2; p2--){
   329    329         SQLITE_SKIP_UTF8(z2);
   330    330       }
   331         -    sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
          331  +    sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
          332  +                          SQLITE_UTF8);
   332    333     }else{
   333    334       if( p1+p2>len ){
   334    335         p2 = len-p1;
   335    336         if( p2<0 ) p2 = 0;
   336    337       }
   337         -    sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
          338  +    sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
   338    339     }
   339    340   }
   340    341   
   341    342   /*
   342    343   ** Implementation of the round() function
   343    344   */
   344    345   #ifndef SQLITE_OMIT_FLOATING_POINT
................................................................................
   389    390     assert( nByte>0 );
   390    391     testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
   391    392     testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
   392    393     if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
   393    394       sqlite3_result_error_toobig(context);
   394    395       z = 0;
   395    396     }else{
   396         -    z = sqlite3Malloc((int)nByte);
          397  +    z = sqlite3Malloc(nByte);
   397    398       if( !z ){
   398    399         sqlite3_result_error_nomem(context);
   399    400       }
   400    401     }
   401    402     return z;
   402    403   }
   403    404   
................................................................................
  1040   1041       }else{
  1041   1042         *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
  1042   1043         *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
  1043   1044         *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
  1044   1045         *zOut++ = 0x80 + (u8)(c & 0x3F);
  1045   1046       }                                                    \
  1046   1047     }
  1047         -  sqlite3_result_text(context, (char*)z, (int)(zOut-z), sqlite3_free);
         1048  +  sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
  1048   1049   }
  1049   1050   
  1050   1051   /*
  1051   1052   ** The hex() function.  Interpret the argument as a blob.  Return
  1052   1053   ** a hexadecimal rendering as text.
  1053   1054   */
  1054   1055   static void hexFunc(
................................................................................
  1490   1491       cmp = sqlite3MemCompare(pBest, pArg, pColl);
  1491   1492       if( (max && cmp<0) || (!max && cmp>0) ){
  1492   1493         sqlite3VdbeMemCopy(pBest, pArg);
  1493   1494       }else{
  1494   1495         sqlite3SkipAccumulatorLoad(context);
  1495   1496       }
  1496   1497     }else{
         1498  +    pBest->db = sqlite3_context_db_handle(context);
  1497   1499       sqlite3VdbeMemCopy(pBest, pArg);
  1498   1500     }
  1499   1501   }
  1500   1502   static void minMaxFinalize(sqlite3_context *context){
  1501   1503     sqlite3_value *pRes;
  1502   1504     pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
  1503   1505     if( pRes ){
................................................................................
  1637   1639     assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
  1638   1640     assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
  1639   1641     *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
  1640   1642     return 1;
  1641   1643   }
  1642   1644   
  1643   1645   /*
  1644         -** All all of the FuncDef structures in the aBuiltinFunc[] array above
         1646  +** All of the FuncDef structures in the aBuiltinFunc[] array above
  1645   1647   ** to the global function hash table.  This occurs at start-time (as
  1646   1648   ** a consequence of calling sqlite3_initialize()).
  1647   1649   **
  1648   1650   ** After this routine runs
  1649   1651   */
  1650   1652   void sqlite3RegisterGlobalFunctions(void){
  1651   1653     /*
................................................................................
  1661   1663       FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
  1662   1664       FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
  1663   1665       FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
  1664   1666       FUNCTION(trim,               1, 3, 0, trimFunc         ),
  1665   1667       FUNCTION(trim,               2, 3, 0, trimFunc         ),
  1666   1668       FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
  1667   1669       FUNCTION(min,                0, 0, 1, 0                ),
  1668         -    AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
         1670  +    AGGREGATE2(min,              1, 0, 1, minmaxStep,      minMaxFinalize,
         1671  +                                          SQLITE_FUNC_MINMAX ),
  1669   1672       FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
  1670   1673       FUNCTION(max,                0, 1, 1, 0                ),
  1671         -    AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
         1674  +    AGGREGATE2(max,              1, 1, 1, minmaxStep,      minMaxFinalize,
         1675  +                                          SQLITE_FUNC_MINMAX ),
  1672   1676       FUNCTION2(typeof,            1, 0, 0, typeofFunc,  SQLITE_FUNC_TYPEOF),
  1673   1677       FUNCTION2(length,            1, 0, 0, lengthFunc,  SQLITE_FUNC_LENGTH),
  1674   1678       FUNCTION(instr,              2, 0, 0, instrFunc        ),
  1675   1679       FUNCTION(substr,             2, 0, 0, substrFunc       ),
  1676   1680       FUNCTION(substr,             3, 0, 0, substrFunc       ),
  1677   1681       FUNCTION(printf,            -1, 0, 0, printfFunc       ),
  1678   1682       FUNCTION(unicode,            1, 0, 0, unicodeFunc      ),
................................................................................
  1694   1698       FUNCTION2(likely,            1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
  1695   1699       VFUNCTION(random,            0, 0, 0, randomFunc       ),
  1696   1700       VFUNCTION(randomblob,        1, 0, 0, randomBlob       ),
  1697   1701       FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
  1698   1702       FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
  1699   1703       FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
  1700   1704       FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
         1705  +#if SQLITE_USER_AUTHENTICATION
         1706  +    FUNCTION(sqlite_crypt,       2, 0, 0, sqlite3CryptFunc ),
         1707  +#endif
  1701   1708   #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  1702   1709       FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
  1703   1710       FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
  1704   1711   #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
  1705   1712       FUNCTION(quote,              1, 0, 0, quoteFunc        ),
  1706   1713       VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
  1707   1714       VFUNCTION(changes,           0, 0, 0, changes          ),
................................................................................
  1714   1721     #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1715   1722       FUNCTION(load_extension,     1, 0, 0, loadExt          ),
  1716   1723       FUNCTION(load_extension,     2, 0, 0, loadExt          ),
  1717   1724     #endif
  1718   1725       AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
  1719   1726       AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
  1720   1727       AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
  1721         - /* AGGREGATE(count,             0, 0, 0, countStep,       countFinalize  ), */
  1722         -    {0,SQLITE_UTF8|SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0},
         1728  +    AGGREGATE2(count,            0, 0, 0, countStep,       countFinalize,
         1729  +               SQLITE_FUNC_COUNT  ),
  1723   1730       AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
  1724   1731       AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
  1725   1732       AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
  1726   1733     
  1727   1734       LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
  1728   1735     #ifdef SQLITE_CASE_SENSITIVE_LIKE
  1729   1736       LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),

Changes to src/global.c.

     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13         -** This file contains definitions of global variables and contants.
           13  +** This file contains definitions of global variables and constants.
    14     14   */
    15     15   #include "sqliteInt.h"
    16     16   
    17     17   /* An array to map all upper-case characters into their corresponding
    18     18   ** lower-case character. 
    19     19   **
    20     20   ** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not

Changes to src/insert.c.

    52     52   /*
    53     53   ** Return a pointer to the column affinity string associated with index
    54     54   ** pIdx. A column affinity string has one character for each column in 
    55     55   ** the table, according to the affinity of the column:
    56     56   **
    57     57   **  Character      Column affinity
    58     58   **  ------------------------------
    59         -**  'a'            TEXT
    60         -**  'b'            NONE
    61         -**  'c'            NUMERIC
    62         -**  'd'            INTEGER
    63         -**  'e'            REAL
           59  +**  'A'            NONE
           60  +**  'B'            TEXT
           61  +**  'C'            NUMERIC
           62  +**  'D'            INTEGER
           63  +**  'F'            REAL
    64     64   **
    65         -** An extra 'd' is appended to the end of the string to cover the
           65  +** An extra 'D' is appended to the end of the string to cover the
    66     66   ** rowid that appears as the last column in every index.
    67     67   **
    68     68   ** Memory for the buffer containing the column index affinity string
    69     69   ** is managed along with the rest of the Index structure. It will be
    70     70   ** released when sqlite3DeleteIndex() is called.
    71     71   */
    72     72   const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
................................................................................
   107    107   ** then just set the P4 operand of the previous opcode (which should  be
   108    108   ** an OP_MakeRecord) to the affinity string.
   109    109   **
   110    110   ** A column affinity string has one character per column:
   111    111   **
   112    112   **  Character      Column affinity
   113    113   **  ------------------------------
   114         -**  'a'            TEXT
   115         -**  'b'            NONE
   116         -**  'c'            NUMERIC
   117         -**  'd'            INTEGER
   118         -**  'e'            REAL
          114  +**  'A'            NONE
          115  +**  'B'            TEXT
          116  +**  'C'            NUMERIC
          117  +**  'D'            INTEGER
          118  +**  'E'            REAL
   119    119   */
   120    120   void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
   121    121     int i;
   122    122     char *zColAff = pTab->zColAff;
   123    123     if( zColAff==0 ){
   124    124       sqlite3 *db = sqlite3VdbeDb(v);
   125    125       zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
................................................................................
   406    406   **         insert the select result into <table> from R..R+n
   407    407   **         goto C
   408    408   **      D: cleanup
   409    409   **
   410    410   ** The 4th template is used if the insert statement takes its
   411    411   ** values from a SELECT but the data is being inserted into a table
   412    412   ** that is also read as part of the SELECT.  In the third form,
   413         -** we have to use a intermediate table to store the results of
          413  +** we have to use an intermediate table to store the results of
   414    414   ** the select.  The template is like this:
   415    415   **
   416    416   **         X <- A
   417    417   **         goto B
   418    418   **      A: setup for the SELECT
   419    419   **         loop over the tables in the SELECT
   420    420   **           load value into register R..R+n
................................................................................
   571    571   
   572    572     /* If this is an AUTOINCREMENT table, look up the sequence number in the
   573    573     ** sqlite_sequence table and store it in memory cell regAutoinc.
   574    574     */
   575    575     regAutoinc = autoIncBegin(pParse, iDb, pTab);
   576    576   
   577    577     /* Allocate registers for holding the rowid of the new row,
   578         -  ** the content of the new row, and the assemblied row record.
          578  +  ** the content of the new row, and the assembled row record.
   579    579     */
   580    580     regRowid = regIns = pParse->nMem+1;
   581    581     pParse->nMem += pTab->nCol + 1;
   582    582     if( IsVirtual(pTab) ){
   583    583       regRowid++;
   584    584       pParse->nMem++;
   585    585     }
................................................................................
  1023   1023     sqlite3ExprListDelete(db, pList);
  1024   1024     sqlite3SelectDelete(db, pSelect);
  1025   1025     sqlite3IdListDelete(db, pColumn);
  1026   1026     sqlite3DbFree(db, aRegIdx);
  1027   1027   }
  1028   1028   
  1029   1029   /* Make sure "isView" and other macros defined above are undefined. Otherwise
  1030         -** thely may interfere with compilation of other functions in this file
         1030  +** they may interfere with compilation of other functions in this file
  1031   1031   ** (or in another file, if this file becomes part of the amalgamation).  */
  1032   1032   #ifdef isView
  1033   1033    #undef isView
  1034   1034   #endif
  1035   1035   #ifdef pTrigger
  1036   1036    #undef pTrigger
  1037   1037   #endif
................................................................................
  1139   1139     Index *pIdx;         /* Pointer to one of the indices */
  1140   1140     Index *pPk = 0;      /* The PRIMARY KEY index */
  1141   1141     sqlite3 *db;         /* Database connection */
  1142   1142     int i;               /* loop counter */
  1143   1143     int ix;              /* Index loop counter */
  1144   1144     int nCol;            /* Number of columns */
  1145   1145     int onError;         /* Conflict resolution strategy */
  1146         -  int j1;              /* Addresss of jump instruction */
         1146  +  int j1;              /* Address of jump instruction */
  1147   1147     int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
  1148   1148     int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
  1149   1149     int ipkTop = 0;      /* Top of the rowid change constraint check */
  1150   1150     int ipkBottom = 0;   /* Bottom of the rowid change constraint check */
  1151   1151     u8 isUpdate;         /* True if this is an UPDATE operation */
  1152   1152     u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
  1153   1153     int regRowid = -1;   /* Register holding ROWID value */
................................................................................
  1543   1543     int appendBias,     /* True if this is likely to be an append */
  1544   1544     int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
  1545   1545   ){
  1546   1546     Vdbe *v;            /* Prepared statements under construction */
  1547   1547     Index *pIdx;        /* An index being inserted or updated */
  1548   1548     u8 pik_flags;       /* flag values passed to the btree insert */
  1549   1549     int regData;        /* Content registers (after the rowid) */
  1550         -  int regRec;         /* Register holding assemblied record for the table */
         1550  +  int regRec;         /* Register holding assembled record for the table */
  1551   1551     int i;              /* Loop counter */
  1552   1552     u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
  1553   1553   
  1554   1554     v = sqlite3GetVdbe(pParse);
  1555   1555     assert( v!=0 );
  1556   1556     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
  1557   1557     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
................................................................................
  1668   1668   
  1669   1669   
  1670   1670   #ifdef SQLITE_TEST
  1671   1671   /*
  1672   1672   ** The following global variable is incremented whenever the
  1673   1673   ** transfer optimization is used.  This is used for testing
  1674   1674   ** purposes only - to make sure the transfer optimization really
  1675         -** is happening when it is suppose to.
         1675  +** is happening when it is supposed to.
  1676   1676   */
  1677   1677   int sqlite3_xferopt_count;
  1678   1678   #endif /* SQLITE_TEST */
  1679   1679   
  1680   1680   
  1681   1681   #ifndef SQLITE_OMIT_XFER_OPT
  1682   1682   /*
................................................................................
  1735   1735   
  1736   1736   /*
  1737   1737   ** Attempt the transfer optimization on INSERTs of the form
  1738   1738   **
  1739   1739   **     INSERT INTO tab1 SELECT * FROM tab2;
  1740   1740   **
  1741   1741   ** The xfer optimization transfers raw records from tab2 over to tab1.  
  1742         -** Columns are not decoded and reassemblied, which greatly improves
         1742  +** Columns are not decoded and reassembled, which greatly improves
  1743   1743   ** performance.  Raw index records are transferred in the same way.
  1744   1744   **
  1745   1745   ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
  1746   1746   ** There are lots of rules for determining compatibility - see comments
  1747   1747   ** embedded in the code for details.
  1748   1748   **
  1749   1749   ** This routine returns TRUE if the optimization is guaranteed to be used.

Changes to src/legacy.c.

   129    129     if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
   130    130     sqlite3DbFree(db, azCols);
   131    131   #ifdef SQLITE_ENABLE_SQLRR
   132    132     SRRecExecEnd(db);
   133    133   #endif
   134    134   
   135    135     rc = sqlite3ApiExit(db, rc);
   136         -  if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
          136  +  if( rc!=SQLITE_OK && pzErrMsg ){
   137    137       int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
   138    138       *pzErrMsg = sqlite3Malloc(nErrMsg);
   139    139       if( *pzErrMsg ){
   140    140         memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
   141    141       }else{
   142    142         rc = SQLITE_NOMEM;
   143    143         sqlite3Error(db, SQLITE_NOMEM);

Changes to src/lempar.c.

   267    267   ** Inputs:
   268    268   ** A pointer to the function used to allocate memory.
   269    269   **
   270    270   ** Outputs:
   271    271   ** A pointer to a parser.  This pointer is used in subsequent calls
   272    272   ** to Parse and ParseFree.
   273    273   */
   274         -void *ParseAlloc(void *(*mallocProc)(size_t)){
          274  +void *ParseAlloc(void *(*mallocProc)(u64)){
   275    275     yyParser *pParser;
   276         -  pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
          276  +  pParser = (yyParser*)(*mallocProc)( (u64)sizeof(yyParser) );
   277    277     if( pParser ){
   278    278       pParser->yyidx = -1;
   279    279   #ifdef YYTRACKMAXSTACKDEPTH
   280    280       pParser->yyidxMax = 0;
   281    281   #endif
   282    282   #if YYSTACKDEPTH<=0
   283    283       pParser->yystack = NULL;

Changes to src/loadext.c.

   386    386     sqlite3_stmt_busy,
   387    387     sqlite3_stmt_readonly,
   388    388     sqlite3_stricmp,
   389    389     sqlite3_uri_boolean,
   390    390     sqlite3_uri_int64,
   391    391     sqlite3_uri_parameter,
   392    392     sqlite3_vsnprintf,
   393         -  sqlite3_wal_checkpoint_v2
          393  +  sqlite3_wal_checkpoint_v2,
          394  +  /* Version 3.8.7 and later */
          395  +  sqlite3_auto_extension,
          396  +  sqlite3_bind_blob64,
          397  +  sqlite3_bind_text64,
          398  +  sqlite3_cancel_auto_extension,
          399  +  sqlite3_load_extension,
          400  +  sqlite3_malloc64,
          401  +  sqlite3_msize,
          402  +  sqlite3_realloc64,
          403  +  sqlite3_reset_auto_extension,
          404  +  sqlite3_result_blob64,
          405  +  sqlite3_result_text64,
          406  +  sqlite3_strglob
   394    407   };
   395    408   
   396    409   /*
   397    410   ** Attempt to load an SQLite extension library contained in the file
   398    411   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
   399    412   ** default entry point name (sqlite3_extension_init) is used.  Use
   400    413   ** of the default name is recommended.

Changes to src/main.c.

   984    984     }
   985    985     sqlite3HashClear(&db->aModule);
   986    986   #endif
   987    987   
   988    988     sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
   989    989     sqlite3ValueFree(db->pErr);
   990    990     sqlite3CloseExtensions(db);
          991  +#if SQLITE_USER_AUTHENTICATION
          992  +  sqlite3_free(db->auth.zAuthUser);
          993  +  sqlite3_free(db->auth.zAuthPW);
          994  +#endif
   991    995   
   992    996     db->magic = SQLITE_MAGIC_ERROR;
   993    997   
   994    998     /* The temp-database schema is allocated differently from the other schema
   995    999     ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
   996   1000     ** So it needs to be freed here. Todo: Why not roll the temp schema into
   997   1001     ** the same sqliteMalloc() as the one that allocates the database 
................................................................................
  2661   2665       }
  2662   2666       sqlite3Error(db, rc);
  2663   2667       goto opendb_out;
  2664   2668     }
  2665   2669     db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
  2666   2670     db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
  2667   2671   
  2668         -
  2669   2672     /* The default safety_level for the main database is 'full'; for the temp
  2670   2673     ** database it is 'NONE'. This matches the pager layer defaults.  
  2671   2674     */
  2672   2675     db->aDb[0].zName = "main";
  2673   2676     db->aDb[0].safety_level = 3;
  2674   2677     db->aDb[1].zName = "temp";
  2675   2678     db->aDb[1].safety_level = 1;
................................................................................
  2971   2974   ** by the next COMMIT or ROLLBACK.
  2972   2975   */
  2973   2976   int sqlite3_get_autocommit(sqlite3 *db){
  2974   2977     return db->autoCommit;
  2975   2978   }
  2976   2979   
  2977   2980   /*
  2978         -** The following routines are subtitutes for constants SQLITE_CORRUPT,
         2981  +** The following routines are substitutes for constants SQLITE_CORRUPT,
  2979   2982   ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
  2980         -** constants.  They server two purposes:
         2983  +** constants.  They serve two purposes:
  2981   2984   **
  2982   2985   **   1.  Serve as a convenient place to set a breakpoint in a debugger
  2983   2986   **       to detect when version error conditions occurs.
  2984   2987   **
  2985   2988   **   2.  Invoke sqlite3_log() to provide the source code location where
  2986   2989   **       a low-level error is first detected.
  2987   2990   */
................................................................................
  3296   3299       ** Set the PENDING byte to the value in the argument, if X>0.
  3297   3300       ** Make no changes if X==0.  Return the value of the pending byte
  3298   3301       ** as it existing before this routine was called.
  3299   3302       **
  3300   3303       ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
  3301   3304       ** an incompatible database file format.  Changing the PENDING byte
  3302   3305       ** while any database connection is open results in undefined and
  3303         -    ** dileterious behavior.
         3306  +    ** deleterious behavior.
  3304   3307       */
  3305   3308       case SQLITE_TESTCTRL_PENDING_BYTE: {
  3306   3309         rc = PENDING_BYTE;
  3307   3310   #ifndef SQLITE_OMIT_WSD
  3308   3311         {
  3309   3312           unsigned int newVal = va_arg(ap, unsigned int);
  3310   3313           if( newVal ) sqlite3PendingByte = newVal;

Changes to src/malloc.c.

   290    290     return nFull;
   291    291   }
   292    292   
   293    293   /*
   294    294   ** Allocate memory.  This routine is like sqlite3_malloc() except that it
   295    295   ** assumes the memory subsystem has already been initialized.
   296    296   */
   297         -void *sqlite3Malloc(int n){
          297  +void *sqlite3Malloc(u64 n){
   298    298     void *p;
   299         -  if( n<=0               /* IMP: R-65312-04917 */ 
   300         -   || n>=0x7fffff00
   301         -  ){
          299  +  if( n==0 || n>=0x7fffff00 ){
   302    300       /* A memory allocation of a number of bytes which is near the maximum
   303    301       ** signed integer value might cause an integer overflow inside of the
   304    302       ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
   305    303       ** 255 bytes of overhead.  SQLite itself will never use anything near
   306    304       ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
   307    305       p = 0;
   308    306     }else if( sqlite3GlobalConfig.bMemstat ){
   309    307       sqlite3_mutex_enter(mem0.mutex);
   310         -    mallocWithAlarm(n, &p);
          308  +    mallocWithAlarm((int)n, &p);
   311    309       sqlite3_mutex_leave(mem0.mutex);
   312    310     }else{
   313         -    p = sqlite3GlobalConfig.m.xMalloc(n);
          311  +    p = sqlite3GlobalConfig.m.xMalloc((int)n);
   314    312     }
   315    313     assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
   316    314     return p;
   317    315   }
   318    316   
   319    317   /*
   320    318   ** This version of the memory allocation is for use by the application.
................................................................................
   321    319   ** First make sure the memory subsystem is initialized, then do the
   322    320   ** allocation.
   323    321   */
   324    322   void *sqlite3_malloc(int n){
   325    323   #ifndef SQLITE_OMIT_AUTOINIT
   326    324     if( sqlite3_initialize() ) return 0;
   327    325   #endif
          326  +  return n<=0 ? 0 : sqlite3Malloc(n);
          327  +}
          328  +void *sqlite3_malloc64(sqlite3_uint64 n){
          329  +#ifndef SQLITE_OMIT_AUTOINIT
          330  +  if( sqlite3_initialize() ) return 0;
          331  +#endif
   328    332     return sqlite3Malloc(n);
   329    333   }
   330    334   
   331    335   /*
   332    336   ** Each thread may only have a single outstanding allocation from
   333    337   ** xScratchMalloc().  We verify this constraint in the single-threaded
   334    338   ** case by setting scratchAllocOut to 1 when an allocation
................................................................................
   443    447   */
   444    448   int sqlite3MallocSize(void *p){
   445    449     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   446    450     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   447    451     return sqlite3GlobalConfig.m.xSize(p);
   448    452   }
   449    453   int sqlite3DbMallocSize(sqlite3 *db, void *p){
   450         -  assert( db!=0 );
   451         -  assert( sqlite3_mutex_held(db->mutex) );
   452         -  if( isLookaside(db, p) ){
   453         -    return db->lookaside.sz;
          454  +  if( db==0 ){
          455  +    return sqlite3MallocSize(p);
   454    456     }else{
   455         -    assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   456         -    assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   457         -    assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   458         -    return sqlite3GlobalConfig.m.xSize(p);
          457  +    assert( sqlite3_mutex_held(db->mutex) );
          458  +    if( isLookaside(db, p) ){
          459  +      return db->lookaside.sz;
          460  +    }else{
          461  +      assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
          462  +      assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
          463  +      assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
          464  +      return sqlite3GlobalConfig.m.xSize(p);
          465  +    }
   459    466     }
   460    467   }
          468  +sqlite3_uint64 sqlite3_msize(void *p){
          469  +  return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
          470  +}
   461    471   
   462    472   /*
   463    473   ** Free memory previously obtained from sqlite3Malloc().
   464    474   */
   465    475   void sqlite3_free(void *p){
   466    476     if( p==0 ) return;  /* IMP: R-49053-54554 */
   467    477     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
................................................................................
   515    525     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   516    526     sqlite3_free(p);
   517    527   }
   518    528   
   519    529   /*
   520    530   ** Change the size of an existing memory allocation
   521    531   */
   522         -void *sqlite3Realloc(void *pOld, int nBytes){
          532  +void *sqlite3Realloc(void *pOld, u64 nBytes){
   523    533     int nOld, nNew, nDiff;
   524    534     void *pNew;
   525    535     if( pOld==0 ){
   526    536       return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
   527    537     }
   528         -  if( nBytes<=0 ){
          538  +  if( nBytes==0 ){
   529    539       sqlite3_free(pOld); /* IMP: R-31593-10574 */
   530    540       return 0;
   531    541     }
   532    542     if( nBytes>=0x7fffff00 ){
   533    543       /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
   534    544       return 0;
   535    545     }
   536    546     nOld = sqlite3MallocSize(pOld);
   537    547     /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
   538    548     ** argument to xRealloc is always a value returned by a prior call to
   539    549     ** xRoundup. */
   540         -  nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
          550  +  nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
   541    551     if( nOld==nNew ){
   542    552       pNew = pOld;
   543    553     }else if( sqlite3GlobalConfig.bMemstat ){
   544    554       sqlite3_mutex_enter(mem0.mutex);
   545         -    sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
          555  +    sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
   546    556       nDiff = nNew - nOld;
   547    557       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 
   548    558             mem0.alarmThreshold-nDiff ){
   549    559         sqlite3MallocAlarm(nDiff);
   550    560       }
   551    561       assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
   552    562       assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
   553    563       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   554    564       if( pNew==0 && mem0.alarmCallback ){
   555         -      sqlite3MallocAlarm(nBytes);
          565  +      sqlite3MallocAlarm((int)nBytes);
   556    566         pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   557    567       }
   558    568       if( pNew ){
   559    569         nNew = sqlite3MallocSize(pNew);
   560    570         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
   561    571       }
   562    572       sqlite3_mutex_leave(mem0.mutex);
................................................................................
   571    581   ** The public interface to sqlite3Realloc.  Make sure that the memory
   572    582   ** subsystem is initialized prior to invoking sqliteRealloc.
   573    583   */
   574    584   void *sqlite3_realloc(void *pOld, int n){
   575    585   #ifndef SQLITE_OMIT_AUTOINIT
   576    586     if( sqlite3_initialize() ) return 0;
   577    587   #endif
          588  +  if( n<0 ) n = 0;
          589  +  return sqlite3Realloc(pOld, n);
          590  +}
          591  +void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
          592  +#ifndef SQLITE_OMIT_AUTOINIT
          593  +  if( sqlite3_initialize() ) return 0;
          594  +#endif
   578    595     return sqlite3Realloc(pOld, n);
   579    596   }
   580    597   
   581    598   
   582    599   /*
   583    600   ** Allocate and zero memory.
   584    601   */ 
   585         -void *sqlite3MallocZero(int n){
          602  +void *sqlite3MallocZero(u64 n){
   586    603     void *p = sqlite3Malloc(n);
   587    604     if( p ){
   588         -    memset(p, 0, n);
          605  +    memset(p, 0, (size_t)n);
   589    606     }
   590    607     return p;
   591    608   }
   592    609   
   593    610   /*
   594    611   ** Allocate and zero memory.  If the allocation fails, make
   595    612   ** the mallocFailed flag in the connection pointer.
   596    613   */
   597         -void *sqlite3DbMallocZero(sqlite3 *db, int n){
          614  +void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
   598    615     void *p = sqlite3DbMallocRaw(db, n);
   599    616     if( p ){
   600         -    memset(p, 0, n);
          617  +    memset(p, 0, (size_t)n);
   601    618     }
   602    619     return p;
   603    620   }
   604    621   
   605    622   /*
   606    623   ** Allocate and zero memory.  If the allocation fails, make
   607    624   ** the mallocFailed flag in the connection pointer.
................................................................................
   616    633   **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
   617    634   **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
   618    635   **         if( b ) a[10] = 9;
   619    636   **
   620    637   ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
   621    638   ** that all prior mallocs (ex: "a") worked too.
   622    639   */
   623         -void *sqlite3DbMallocRaw(sqlite3 *db, int n){
          640  +void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
   624    641     void *p;
   625    642     assert( db==0 || sqlite3_mutex_held(db->mutex) );
   626    643     assert( db==0 || db->pnBytesFreed==0 );
   627    644   #ifndef SQLITE_OMIT_LOOKASIDE
   628    645     if( db ){
   629    646       LookasideSlot *pBuf;
   630    647       if( db->mallocFailed ){
................................................................................
   660    677     return p;
   661    678   }
   662    679   
   663    680   /*
   664    681   ** Resize the block of memory pointed to by p to n bytes. If the
   665    682   ** resize fails, set the mallocFailed flag in the connection object.
   666    683   */
   667         -void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
          684  +void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
   668    685     void *pNew = 0;
   669    686     assert( db!=0 );
   670    687     assert( sqlite3_mutex_held(db->mutex) );
   671    688     if( db->mallocFailed==0 ){
   672    689       if( p==0 ){
   673    690         return sqlite3DbMallocRaw(db, n);
   674    691       }
................................................................................
   681    698           memcpy(pNew, p, db->lookaside.sz);
   682    699           sqlite3DbFree(db, p);
   683    700         }
   684    701       }else{
   685    702         assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   686    703         assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   687    704         sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   688         -      pNew = sqlite3_realloc(p, n);
          705  +      pNew = sqlite3_realloc64(p, n);
   689    706         if( !pNew ){
   690    707           sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
   691    708           db->mallocFailed = 1;
   692    709         }
   693    710         sqlite3MemdebugSetType(pNew, MEMTYPE_DB | 
   694    711               (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
   695    712       }
................................................................................
   697    714     return pNew;
   698    715   }
   699    716   
   700    717   /*
   701    718   ** Attempt to reallocate p.  If the reallocation fails, then free p
   702    719   ** and set the mallocFailed flag in the database connection.
   703    720   */
   704         -void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
          721  +void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
   705    722     void *pNew;
   706    723     pNew = sqlite3DbRealloc(db, p, n);
   707    724     if( !pNew ){
   708    725       sqlite3DbFree(db, p);
   709    726     }
   710    727     return pNew;
   711    728   }
................................................................................
   727    744     assert( (n&0x7fffffff)==n );
   728    745     zNew = sqlite3DbMallocRaw(db, (int)n);
   729    746     if( zNew ){
   730    747       memcpy(zNew, z, n);
   731    748     }
   732    749     return zNew;
   733    750   }
   734         -char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
          751  +char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
   735    752     char *zNew;
   736    753     if( z==0 ){
   737    754       return 0;
   738    755     }
   739    756     assert( (n&0x7fffffff)==n );
   740    757     zNew = sqlite3DbMallocRaw(db, n+1);
   741    758     if( zNew ){
   742         -    memcpy(zNew, z, n);
          759  +    memcpy(zNew, z, (size_t)n);
   743    760       zNew[n] = 0;
   744    761     }
   745    762     return zNew;
   746    763   }
   747    764   
   748    765   /*
   749    766   ** Create a string from the zFromat argument and the va_list that follows.

Changes to src/mem1.c.

   184    184   
   185    185   /*
   186    186   ** Like realloc().  Resize an allocation previously obtained from
   187    187   ** sqlite3MemMalloc().
   188    188   **
   189    189   ** For this low-level interface, we know that pPrior!=0.  Cases where
   190    190   ** pPrior==0 while have been intercepted by higher-level routine and
   191         -** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
          191  +** redirected to xMalloc.  Similarly, we know that nByte>0 because
   192    192   ** cases where nByte<=0 will have been intercepted by higher-level
   193    193   ** routines and redirected to xFree.
   194    194   */
   195    195   static void *sqlite3MemRealloc(void *pPrior, int nByte){
   196    196   #ifdef SQLITE_MALLOCSIZE
   197    197     void *p = SQLITE_REALLOC(pPrior, nByte);
   198    198     if( p==0 ){

Changes to src/mem5.c.

    24     24   ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
    25     25   **
    26     26   ** This memory allocator uses the following algorithm:
    27     27   **
    28     28   **   1.  All memory allocations sizes are rounded up to a power of 2.
    29     29   **
    30     30   **   2.  If two adjacent free blocks are the halves of a larger block,
    31         -**       then the two blocks are coalesed into the single larger block.
           31  +**       then the two blocks are coalesced into the single larger block.
    32     32   **
    33     33   **   3.  New memory is allocated from the first available free block.
    34     34   **
    35     35   ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
    36     36   ** Concerning Dynamic Storage Allocation". Journal of the Association for
    37     37   ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
    38     38   ** 

Changes to src/memjournal.c.

    22     22   typedef struct FileChunk FileChunk;
    23     23   
    24     24   /* Space to hold the rollback journal is allocated in increments of
    25     25   ** this many bytes.
    26     26   **
    27     27   ** The size chosen is a little less than a power of two.  That way,
    28     28   ** the FileChunk object will have a size that almost exactly fills
    29         -** a power-of-two allocation.  This mimimizes wasted space in power-of-two
           29  +** a power-of-two allocation.  This minimizes wasted space in power-of-two
    30     30   ** memory allocators.
    31     31   */
    32     32   #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
    33     33   
    34     34   /*
    35     35   ** The rollback journal is composed of a linked list of these structures.
    36     36   */

Changes to src/mutex.h.

    21     21   */
    22     22   
    23     23   
    24     24   /*
    25     25   ** Figure out what version of the code to use.  The choices are
    26     26   **
    27     27   **   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
    28         -**                             mutexes implemention cannot be overridden
           28  +**                             mutexes implementation cannot be overridden
    29     29   **                             at start-time.
    30     30   **
    31     31   **   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
    32     32   **                             mutual exclusion is provided.  But this
    33     33   **                             implementation can be overridden at
    34     34   **                             start-time.
    35     35   **

Changes to src/os.h.

   116    116   **
   117    117   ** The following #defines specify the range of bytes used for locking.
   118    118   ** SHARED_SIZE is the number of bytes available in the pool from which
   119    119   ** a random byte is selected for a shared lock.  The pool of bytes for
   120    120   ** shared locks begins at SHARED_FIRST. 
   121    121   **
   122    122   ** The same locking strategy and
   123         -** byte ranges are used for Unix.  This leaves open the possiblity of having
          123  +** byte ranges are used for Unix.  This leaves open the possibility of having
   124    124   ** clients on win95, winNT, and unix all talking to the same shared file
   125    125   ** and all locking correctly.  To do so would require that samba (or whatever
   126    126   ** tool is being used for file sharing) implements locks correctly between
   127    127   ** windows and unix.  I'm guessing that isn't likely to happen, but by
   128    128   ** using the same locking range we are at least open to the possibility.
   129    129   **
   130    130   ** Locking in windows is manditory.  For this reason, we cannot store

Changes to src/os_unix.c.

   590    590   # if defined(__linux__) && defined(_GNU_SOURCE)
   591    591   #  define HAVE_MREMAP 1
   592    592   # else
   593    593   #  define HAVE_MREMAP 0
   594    594   # endif
   595    595   #endif
   596    596   
          597  +/*
          598  +** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
          599  +** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
          600  +*/
          601  +#ifdef __ANDROID__
          602  +# define lseek lseek64
          603  +#endif
          604  +
   597    605   /*
   598    606   ** Different Unix systems declare open() in different ways.  Same use
   599    607   ** open(const char*,int,mode_t).  Others use open(const char*,int,...).
   600    608   ** The difference is important when using a pointer to the function.
   601    609   **
   602    610   ** The safest way to deal with the problem is to always use this wrapper
   603    611   ** which always has the same well-defined interface.
................................................................................
   922    930   }
   923    931   #endif
   924    932   
   925    933   
   926    934   #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
   927    935   /*
   928    936   ** Helper function for printing out trace information from debugging
   929         -** binaries. This returns the string represetation of the supplied
          937  +** binaries. This returns the string representation of the supplied
   930    938   ** integer lock-type.
   931    939   */
   932    940   static const char *azFileLock(int eFileLock){
   933    941     switch( eFileLock ){
   934    942       case NO_LOCK: return "NONE";
   935    943       case SHARED_LOCK: return "SHARED";
   936    944       case RESERVED_LOCK: return "RESERVED";
................................................................................
   999   1007   }
  1000   1008   #undef osFcntl
  1001   1009   #define osFcntl lockTrace
  1002   1010   #endif /* SQLITE_LOCK_TRACE */
  1003   1011   
  1004   1012   /*
  1005   1013   ** Retry ftruncate() calls that fail due to EINTR
         1014  +**
         1015  +** All calls to ftruncate() within this file should be made through this wrapper.
         1016  +** On the Android platform, bypassing the logic below could lead to a corrupt
         1017  +** database.
  1006   1018   */
  1007   1019   static int robust_ftruncate(int h, sqlite3_int64 sz){
  1008   1020     int rc;
         1021  +#ifdef __ANDROID__
         1022  +  /* On Android, ftruncate() always uses 32-bit offsets, even if 
         1023  +  ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
         1024  +  ** truncate a file to any size larger than 2GiB. Silently ignore any
         1025  +  ** such attempts.  */
         1026  +  if( sz>(sqlite3_int64)0x7FFFFFFF ){
         1027  +    rc = SQLITE_OK;
         1028  +  }else
         1029  +#endif
  1009   1030     do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
  1010   1031     return rc;
  1011   1032   }
  1012   1033   
  1013   1034   /*
  1014   1035   ** This routine translates a standard POSIX errno code into something
  1015   1036   ** useful to the clients of the sqlite3 functions.  Specifically, it is
................................................................................
  3483   3504   /*
  3484   3505   ** Seek to the offset passed as the second argument, then read cnt 
  3485   3506   ** bytes into pBuf. Return the number of bytes actually read.
  3486   3507   **
  3487   3508   ** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
  3488   3509   ** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
  3489   3510   ** one system to another.  Since SQLite does not define USE_PREAD
  3490         -** any any form by default, we will not attempt to define _XOPEN_SOURCE.
         3511  +** in any form by default, we will not attempt to define _XOPEN_SOURCE.
  3491   3512   ** See tickets #2741 and #2681.
  3492   3513   **
  3493   3514   ** To avoid stomping the errno value on a failed read the lastErrno value
  3494   3515   ** is set before returning.
  3495   3516   */
  3496   3517   static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
  3497   3518     int got;
................................................................................
  4001   4022     ** actual file size after the operation may be larger than the requested
  4002   4023     ** size).
  4003   4024     */
  4004   4025     if( pFile->szChunk>0 ){
  4005   4026       nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
  4006   4027     }
  4007   4028   
  4008         -  rc = robust_ftruncate(pFile->h, (off_t)nByte);
         4029  +  rc = robust_ftruncate(pFile->h, nByte);
  4009   4030     if( rc ){
  4010   4031       storeLastErrno(pFile, errno);
  4011   4032       return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
  4012   4033     }else{
  4013   4034   #ifdef SQLITE_DEBUG
  4014   4035       /* If we are doing a normal write to a database file (as opposed to
  4015   4036       ** doing a hot-journal rollback or a write to some file other than a
................................................................................
  4438   4459   
  4439   4460   static int unixLockstatePid(unixFile *, pid_t, int *);
  4440   4461   
  4441   4462   #endif /* (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__) */
  4442   4463   
  4443   4464   
  4444   4465   /*
  4445         -** If *pArg is inititially negative then this is a query.  Set *pArg to
         4466  +** If *pArg is initially negative then this is a query.  Set *pArg to
  4446   4467   ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
  4447   4468   **
  4448   4469   ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
  4449   4470   */
  4450   4471   static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
  4451   4472     if( *pArg<0 ){
  4452   4473       *pArg = (pFile->ctrlFlags & mask)!=0;
................................................................................
  4665   4686   }
  4666   4687   #endif /* __QNXNTO__ */
  4667   4688   
  4668   4689   /*
  4669   4690   ** Return the device characteristics for the file.
  4670   4691   **
  4671   4692   ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
  4672         -** However, that choice is contraversial since technically the underlying
         4693  +** However, that choice is controversial since technically the underlying
  4673   4694   ** file system does not always provide powersafe overwrites.  (In other
  4674   4695   ** words, after a power-loss event, parts of the file that were never
  4675   4696   ** written might end up being altered.)  However, non-PSOW behavior is very,
  4676   4697   ** very rare.  And asserting PSOW makes a large reduction in the amount
  4677   4698   ** of required I/O for journaling, since a lot of padding is eliminated.
  4678   4699   **  Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
  4679   4700   ** available to turn it off and URI query parameter available to turn it off.
................................................................................
  6078   6099   ** the correct finder-function for that VFS.
  6079   6100   **
  6080   6101   ** Most finder functions return a pointer to a fixed sqlite3_io_methods
  6081   6102   ** object.  The only interesting finder-function is autolockIoFinder, which
  6082   6103   ** looks at the filesystem type and tries to guess the best locking
  6083   6104   ** strategy from that.
  6084   6105   **
  6085         -** For finder-funtion F, two objects are created:
         6106  +** For finder-function F, two objects are created:
  6086   6107   **
  6087   6108   **    (1) The real finder-function named "FImpt()".
  6088   6109   **
  6089   6110   **    (2) A constant pointer to this function named just "F".
  6090   6111   **
  6091   6112   **
  6092   6113   ** A pointer to the F pointer is used as the pAppData value for VFS
................................................................................
  6340   6361   }
  6341   6362   static const sqlite3_io_methods 
  6342   6363     *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
  6343   6364   
  6344   6365   #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
  6345   6366   
  6346   6367   /*
  6347         -** An abstract type for a pointer to a IO method finder function:
         6368  +** An abstract type for a pointer to an IO method finder function:
  6348   6369   */
  6349   6370   typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
  6350   6371   
  6351   6372   
  6352   6373   /****************************************************************************
  6353   6374   **************************** sqlite3_vfs methods ****************************
  6354   6375   **
................................................................................
  6656   6677     /* A stat() call may fail for various reasons. If this happens, it is
  6657   6678     ** almost certain that an open() call on the same path will also fail.
  6658   6679     ** For this reason, if an error occurs in the stat() call here, it is
  6659   6680     ** ignored and -1 is returned. The caller will try to open a new file
  6660   6681     ** descriptor on the same path, fail, and return an error to SQLite.
  6661   6682     **
  6662   6683     ** Even if a subsequent open() call does succeed, the consequences of
  6663         -  ** not searching for a resusable file descriptor are not dire.  */
         6684  +  ** not searching for a reusable file descriptor are not dire.  */
  6664   6685     if( 0==osStat(zPath, &sStat) ){
  6665   6686       unixInodeInfo *pInode;
  6666   6687   
  6667   6688       unixEnterMutex();
  6668   6689       pInode = inodeList;
  6669   6690       while( pInode && (pInode->fileId.dev!=sStat.st_dev
  6670   6691                        || pInode->fileId.ino!=sStat.st_ino) ){
................................................................................
  6687   6708   /*
  6688   6709   ** This function is called by unixOpen() to determine the unix permissions
  6689   6710   ** to create new files with. If no error occurs, then SQLITE_OK is returned
  6690   6711   ** and a value suitable for passing as the third argument to open(2) is
  6691   6712   ** written to *pMode. If an IO error occurs, an SQLite error code is 
  6692   6713   ** returned and the value of *pMode is not modified.
  6693   6714   **
  6694         -** In most cases cases, this routine sets *pMode to 0, which will become
         6715  +** In most cases, this routine sets *pMode to 0, which will become
  6695   6716   ** an indication to robust_open() to create the file using
  6696   6717   ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
  6697   6718   ** But if the file being opened is a WAL or regular journal file, then 
  6698   6719   ** this function queries the file-system for the permissions on the 
  6699   6720   ** corresponding database file and sets *pMode to this value. Whenever 
  6700   6721   ** possible, WAL and journal files are created using the same permissions 
  6701   6722   ** as the associated database file.
................................................................................
  7498   7519   **
  7499   7520   ** The conch file - to use a proxy file, sqlite must first "hold the conch"
  7500   7521   ** by taking an sqlite-style shared lock on the conch file, reading the
  7501   7522   ** contents and comparing the host's unique host ID (see below) and lock
  7502   7523   ** proxy path against the values stored in the conch.  The conch file is
  7503   7524   ** stored in the same directory as the database file and the file name
  7504   7525   ** is patterned after the database file name as ".<databasename>-conch".
  7505         -** If the conch file does not exist, or it's contents do not match the
         7526  +** If the conch file does not exist, or its contents do not match the
  7506   7527   ** host ID and/or proxy path, then the lock is escalated to an exclusive
  7507   7528   ** lock and the conch file contents is updated with the host ID and proxy
  7508   7529   ** path and the lock is downgraded to a shared lock again.  If the conch
  7509   7530   ** is held by another process (with a shared lock), the exclusive lock
  7510   7531   ** will fail and SQLITE_BUSY is returned.
  7511   7532   **
  7512   7533   ** The proxy file - a single-byte file used for all advisory file locks
................................................................................
  7550   7571   **       lock proxy files, only used when LOCKPROXYDIR is not set.
  7551   7572   **    
  7552   7573   **    
  7553   7574   ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
  7554   7575   ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
  7555   7576   ** force proxy locking to be used for every database file opened, and 0
  7556   7577   ** will force automatic proxy locking to be disabled for all database
  7557         -** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
         7578  +** files (explicitly calling the SQLITE_SET_LOCKPROXYFILE pragma or
  7558   7579   ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
  7559   7580   */
  7560   7581   
  7561   7582   /*
  7562   7583   ** Proxy locking is only available on MacOSX 
  7563   7584   */
  7564   7585   #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE

Changes to src/os_win.c.

  1282   1282     assert( sleepObj!=NULL );
  1283   1283     osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
  1284   1284   #else
  1285   1285     osSleep(milliseconds);
  1286   1286   #endif
  1287   1287   }
  1288   1288   
         1289  +#if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINRT && SQLITE_THREADSAFE>0
  1289   1290   DWORD sqlite3Win32Wait(HANDLE hObject){
  1290   1291     DWORD rc;
  1291   1292     while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
  1292   1293                                          TRUE))==WAIT_IO_COMPLETION ){}
  1293   1294     return rc;
  1294   1295   }
         1296  +#endif
  1295   1297   
  1296   1298   /*
  1297   1299   ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
  1298   1300   ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
  1299   1301   **
  1300   1302   ** Here is an interesting observation:  Win95, Win98, and WinME lack
  1301   1303   ** the LockFileEx() API.  But we can still statically link against that
................................................................................
  3122   3124     pFile->locktype = (u8)locktype;
  3123   3125     OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
  3124   3126              pFile->h, pFile->locktype, sqlite3ErrName(rc)));
  3125   3127     return rc;
  3126   3128   }
  3127   3129   
  3128   3130   /*
  3129         -** If *pArg is inititially negative then this is a query.  Set *pArg to
         3131  +** If *pArg is initially negative then this is a query.  Set *pArg to
  3130   3132   ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
  3131   3133   **
  3132   3134   ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
  3133   3135   */
  3134   3136   static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
  3135   3137     if( *pArg<0 ){
  3136   3138       *pArg = (pFile->ctrlFlags & mask)!=0;
................................................................................
  4145   4147              osGetCurrentProcessId(), pFd, iOff, p));
  4146   4148   
  4147   4149     if( p ){
  4148   4150       pFd->nFetchOut--;
  4149   4151     }else{
  4150   4152       /* FIXME:  If Windows truly always prevents truncating or deleting a
  4151   4153       ** file while a mapping is held, then the following winUnmapfile() call
  4152         -    ** is unnecessary can can be omitted - potentially improving
         4154  +    ** is unnecessary can be omitted - potentially improving
  4153   4155       ** performance.  */
  4154   4156       winUnmapfile(pFd);
  4155   4157     }
  4156   4158   
  4157   4159     assert( pFd->nFetchOut>=0 );
  4158   4160   #endif
  4159   4161   

Changes to src/pager.c.

    72     72   ** 
    73     73   ** (6) If a master journal file is used, then all writes to the database file
    74     74   **     are synced prior to the master journal being deleted.
    75     75   ** 
    76     76   ** Definition: Two databases (or the same database at two points it time)
    77     77   ** are said to be "logically equivalent" if they give the same answer to
    78     78   ** all queries.  Note in particular the content of freelist leaf
    79         -** pages can be changed arbitarily without effecting the logical equivalence
           79  +** pages can be changed arbitrarily without affecting the logical equivalence
    80     80   ** of the database.
    81     81   ** 
    82     82   ** (7) At any time, if any subset, including the empty set and the total set,
    83     83   **     of the unsynced changes to a rollback journal are removed and the 
    84         -**     journal is rolled back, the resulting database file will be logical
           84  +**     journal is rolled back, the resulting database file will be logically
    85     85   **     equivalent to the database file at the beginning of the transaction.
    86     86   ** 
    87     87   ** (8) When a transaction is rolled back, the xTruncate method of the VFS
    88     88   **     is called to restore the database file to the same size it was at
    89     89   **     the beginning of the transaction.  (In some VFSes, the xTruncate
    90     90   **     method is a no-op, but that does not change the fact the SQLite will
    91     91   **     invoke it.)
................................................................................
   374    374   **
   375    375   ** This is usually safe. If an xUnlock fails or appears to fail, there may 
   376    376   ** be a few redundant xLock() calls or a lock may be held for longer than
   377    377   ** required, but nothing really goes wrong.
   378    378   **
   379    379   ** The exception is when the database file is unlocked as the pager moves
   380    380   ** from ERROR to OPEN state. At this point there may be a hot-journal file 
   381         -** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
          381  +** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
   382    382   ** transition, by the same pager or any other). If the call to xUnlock()
   383    383   ** fails at this point and the pager is left holding an EXCLUSIVE lock, this
   384    384   ** can confuse the call to xCheckReservedLock() call made later as part
   385    385   ** of hot-journal detection.
   386    386   **
   387    387   ** xCheckReservedLock() is defined as returning true "if there is a RESERVED 
   388    388   ** lock held by this process or any others". So xCheckReservedLock may 
................................................................................
   457    457   ** Bits of the Pager.doNotSpill flag.  See further description below.
   458    458   */
   459    459   #define SPILLFLAG_OFF         0x01      /* Never spill cache.  Set via pragma */
   460    460   #define SPILLFLAG_ROLLBACK    0x02      /* Current rolling back, so do not spill */
   461    461   #define SPILLFLAG_NOSYNC      0x04      /* Spill is ok, but do not sync */
   462    462   
   463    463   /*
   464         -** A open page cache is an instance of struct Pager. A description of
          464  +** An open page cache is an instance of struct Pager. A description of
   465    465   ** some of the more important member variables follows:
   466    466   **
   467    467   ** eState
   468    468   **
   469    469   **   The current 'state' of the pager object. See the comment and state
   470    470   **   diagram above for a description of the pager state.
   471    471   **
................................................................................
   629    629     u8 tempFile;                /* zFilename is a temporary or immutable file */
   630    630     u8 noLock;                  /* Do not lock (except in WAL mode) */
   631    631     u8 readOnly;                /* True for a read-only database */
   632    632     u8 memDb;                   /* True to inhibit all file I/O */
   633    633   
   634    634     /**************************************************************************
   635    635     ** The following block contains those class members that change during
   636         -  ** routine opertion.  Class members not in this block are either fixed
          636  +  ** routine operation.  Class members not in this block are either fixed
   637    637     ** when the pager is first created or else only change when there is a
   638    638     ** significant mode change (such as changing the page_size, locking_mode,
   639    639     ** or the journal_mode).  From another view, these class members describe
   640    640     ** the "state" of the pager, while other class members describe the
   641    641     ** "configuration" of the pager.
   642    642     */
   643    643     u8 eState;                  /* Pager state (OPEN, READER, WRITER_LOCKED..) */
................................................................................
  2428   2428     ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
  2429   2429     ** sufficient space (in zMasterPtr) to hold the names of master
  2430   2430     ** journal files extracted from regular rollback-journals.
  2431   2431     */
  2432   2432     rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
  2433   2433     if( rc!=SQLITE_OK ) goto delmaster_out;
  2434   2434     nMasterPtr = pVfs->mxPathname+1;
  2435         -  zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
         2435  +  zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1);
  2436   2436     if( !zMasterJournal ){
  2437   2437       rc = SQLITE_NOMEM;
  2438   2438       goto delmaster_out;
  2439   2439     }
  2440   2440     zMasterPtr = &zMasterJournal[nMasterJournal+1];
  2441   2441     rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
  2442   2442     if( rc!=SQLITE_OK ) goto delmaster_out;
................................................................................
  2501   2501   **
  2502   2502   ** If the main database file is not open, or the pager is not in either
  2503   2503   ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size 
  2504   2504   ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes). 
  2505   2505   ** If the file on disk is currently larger than nPage pages, then use the VFS
  2506   2506   ** xTruncate() method to truncate it.
  2507   2507   **
  2508         -** Or, it might might be the case that the file on disk is smaller than 
         2508  +** Or, it might be the case that the file on disk is smaller than 
  2509   2509   ** nPage pages. Some operating system implementations can get confused if 
  2510   2510   ** you try to truncate a file to some size that is larger than it 
  2511   2511   ** currently is, so detect this case and write a single zero byte to 
  2512   2512   ** the end of the new file instead.
  2513   2513   **
  2514   2514   ** If successful, return SQLITE_OK. If an IO error occurs while modifying
  2515   2515   ** the database file, return the error code to the caller.
................................................................................
  2560   2560     }
  2561   2561     return iRet;
  2562   2562   }
  2563   2563   
  2564   2564   /*
  2565   2565   ** Set the value of the Pager.sectorSize variable for the given
  2566   2566   ** pager based on the value returned by the xSectorSize method
  2567         -** of the open database file. The sector size will be used used 
         2567  +** of the open database file. The sector size will be used 
  2568   2568   ** to determine the size and alignment of journal header and 
  2569   2569   ** master journal pointers within created journal files.
  2570   2570   **
  2571   2571   ** For temporary files the effective sector size is always 512 bytes.
  2572   2572   **
  2573   2573   ** Otherwise, for non-temporary files, the effective sector size is
  2574   2574   ** the value returned by the xSectorSize() method rounded up to 32 if
................................................................................
  3626   3626       if( rc==SQLITE_OK ){
  3627   3627         pNew = (char *)sqlite3PageMalloc(pageSize);
  3628   3628         if( !pNew ) rc = SQLITE_NOMEM;
  3629   3629       }
  3630   3630   
  3631   3631       if( rc==SQLITE_OK ){
  3632   3632         pager_reset(pPager);
  3633         -      pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
  3634         -      pPager->pageSize = pageSize;
  3635   3633         sqlite3PageFree(pPager->pTmpSpace);
  3636   3634         pPager->pTmpSpace = pNew;
  3637   3635         rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
  3638   3636       }
         3637  +    if( rc==SQLITE_OK ){
         3638  +      pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
         3639  +      pPager->pageSize = pageSize;
         3640  +    }
  3639   3641     }
  3640   3642   
  3641   3643     *pPageSize = pPager->pageSize;
  3642   3644     if( rc==SQLITE_OK ){
  3643   3645       if( nReserve<0 ) nReserve = pPager->nReserve;
  3644   3646       assert( nReserve>=0 && nReserve<1000 );
  3645   3647       pPager->nReserve = (i16)nReserve;
................................................................................
  3764   3766   ** the lock. If the lock is obtained successfully, set the Pager.state 
  3765   3767   ** variable to locktype before returning.
  3766   3768   */
  3767   3769   static int pager_wait_on_lock(Pager *pPager, int locktype){
  3768   3770     int rc;                              /* Return code */
  3769   3771   
  3770   3772     /* Check that this is either a no-op (because the requested lock is 
  3771         -  ** already held, or one of the transistions that the busy-handler
         3773  +  ** already held), or one of the transitions that the busy-handler
  3772   3774     ** may be invoked during, according to the comment above
  3773   3775     ** sqlite3PagerSetBusyhandler().
  3774   3776     */
  3775   3777     assert( (pPager->eLock>=locktype)
  3776   3778          || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
  3777   3779          || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
  3778   3780     );
................................................................................
  4392   4394     ** pages belonging to the same sector.
  4393   4395     **
  4394   4396     ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
  4395   4397     ** regardless of whether or not a sync is required.  This is set during
  4396   4398     ** a rollback or by user request, respectively.
  4397   4399     **
  4398   4400     ** Spilling is also prohibited when in an error state since that could
  4399         -  ** lead to database corruption.   In the current implementaton it 
         4401  +  ** lead to database corruption.   In the current implementation it 
  4400   4402     ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
  4401   4403     ** while in the error state, hence it is impossible for this routine to
  4402   4404     ** be called in the error state.  Nevertheless, we include a NEVER()
  4403   4405     ** test for the error state as a safeguard against future changes.
  4404   4406     */
  4405   4407     if( NEVER(pPager->errCode) ) return SQLITE_OK;
  4406   4408     testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
................................................................................
  4939   4941               }
  4940   4942               if( !jrnlOpen ){
  4941   4943                 sqlite3OsClose(pPager->jfd);
  4942   4944               }
  4943   4945               *pExists = (first!=0);
  4944   4946             }else if( rc==SQLITE_CANTOPEN ){
  4945   4947               /* If we cannot open the rollback journal file in order to see if
  4946         -            ** its has a zero header, that might be due to an I/O error, or
         4948  +            ** it has a zero header, that might be due to an I/O error, or
  4947   4949               ** it might be due to the race condition described above and in
  4948   4950               ** ticket #3883.  Either way, assume that the journal is hot.
  4949   4951               ** This might be a false positive.  But if it is, then the
  4950   4952               ** automatic journal playback and recovery mechanism will deal
  4951   4953               ** with it under an EXCLUSIVE lock where we do not need to
  4952   4954               ** worry so much with race conditions.
  4953   4955               */
................................................................................
  7270   7272   ** A read-lock must be held on the pager when this function is called. If
  7271   7273   ** the pager is in WAL mode and the WAL file currently contains one or more
  7272   7274   ** frames, return the size in bytes of the page images stored within the
  7273   7275   ** WAL frames. Otherwise, if this is not a WAL database or the WAL file
  7274   7276   ** is empty, return 0.
  7275   7277   */
  7276   7278   int sqlite3PagerWalFramesize(Pager *pPager){
  7277         -  assert( pPager->eState==PAGER_READER );
         7279  +  assert( pPager->eState>=PAGER_READER );
  7278   7280     return sqlite3WalFramesize(pPager->pWal);
  7279   7281   }
  7280   7282   #endif
  7281   7283   
  7282   7284   #endif /* SQLITE_OMIT_DISKIO */

Changes to src/parse.y.

   455    455     A = pRhs;
   456    456   }
   457    457   %type multiselect_op {int}
   458    458   multiselect_op(A) ::= UNION(OP).             {A = @OP;}
   459    459   multiselect_op(A) ::= UNION ALL.             {A = TK_ALL;}
   460    460   multiselect_op(A) ::= EXCEPT|INTERSECT(OP).  {A = @OP;}
   461    461   %endif SQLITE_OMIT_COMPOUND_SELECT
   462         -oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y)
          462  +oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y)
   463    463                    groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). {
   464    464     A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset);
          465  +#if SELECTTRACE_ENABLED
          466  +  /* Populate the Select.zSelName[] string that is used to help with
          467  +  ** query planner debugging, to differentiate between multiple Select
          468  +  ** objects in a complex query.
          469  +  **
          470  +  ** If the SELECT keyword is immediately followed by a C-style comment
          471  +  ** then extract the first few alphanumeric characters from within that
          472  +  ** comment to be the zSelName value.  Otherwise, the label is #N where
          473  +  ** is an integer that is incremented with each SELECT statement seen.
          474  +  */
          475  +  if( A!=0 ){
          476  +    const char *z = S.z+6;
          477  +    int i;
          478  +    sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "#%d",
          479  +                     ++pParse->nSelect);
          480  +    while( z[0]==' ' ) z++;
          481  +    if( z[0]=='/' && z[1]=='*' ){
          482  +      z += 2;
          483  +      while( z[0]==' ' ) z++;
          484  +      for(i=0; sqlite3Isalnum(z[i]); i++){}
          485  +      sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z);
          486  +    }
          487  +  }
          488  +#endif /* SELECTRACE_ENABLED */
   465    489   }
   466    490   oneselect(A) ::= values(X).    {A = X;}
   467    491   
   468    492   %type values {Select*}
   469    493   %destructor values {sqlite3SelectDelete(pParse->db, $$);}
   470    494   values(A) ::= VALUES LP nexprlist(X) RP. {
   471    495     A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0,0);

Changes to src/pcache.c.

    41     41   # define expensive_assert(X)  assert(X)
    42     42   #else
    43     43   # define expensive_assert(X)
    44     44   #endif
    45     45   
    46     46   /********************************** Linked List Management ********************/
    47     47   
    48         -#if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
    49         -/*
    50         -** Check that the pCache->pSynced variable is set correctly. If it
    51         -** is not, either fail an assert or return zero. Otherwise, return
    52         -** non-zero. This is only used in debugging builds, as follows:
    53         -**
    54         -**   expensive_assert( pcacheCheckSynced(pCache) );
    55         -*/
    56         -static int pcacheCheckSynced(PCache *pCache){
    57         -  PgHdr *p;
    58         -  for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
    59         -    assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
    60         -  }
    61         -  return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
    62         -}
    63         -#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
    64         -
    65     48   /* Allowed values for second argument to pcacheManageDirtyList() */
    66     49   #define PCACHE_DIRTYLIST_REMOVE   1    /* Remove pPage from dirty list */
    67     50   #define PCACHE_DIRTYLIST_ADD      2    /* Add pPage to the dirty list */
    68     51   #define PCACHE_DIRTYLIST_FRONT    3    /* Move pPage to the front of the list */
    69     52   
    70     53   /*
    71     54   ** Manage pPage's participation on the dirty list.  Bits of the addRemove
................................................................................
   103     86         if( p->pDirty==0 && p->bPurgeable ){
   104     87           assert( p->eCreate==1 );
   105     88           p->eCreate = 2;
   106     89         }
   107     90       }
   108     91       pPage->pDirtyNext = 0;
   109     92       pPage->pDirtyPrev = 0;
   110         -    expensive_assert( pcacheCheckSynced(p) );
   111     93     }
   112     94     if( addRemove & PCACHE_DIRTYLIST_ADD ){
   113     95       assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
   114     96     
   115     97       pPage->pDirtyNext = p->pDirty;
   116     98       if( pPage->pDirtyNext ){
   117     99         assert( pPage->pDirtyNext->pDirtyPrev==0 );
   118    100         pPage->pDirtyNext->pDirtyPrev = pPage;
   119         -    }else if( p->bPurgeable ){
   120         -      assert( p->eCreate==2 );
   121         -      p->eCreate = 1;
          101  +    }else{
          102  +      p->pDirtyTail = pPage;
          103  +      if( p->bPurgeable ){
          104  +        assert( p->eCreate==2 );
          105  +        p->eCreate = 1;
          106  +      }
   122    107       }
   123    108       p->pDirty = pPage;
   124         -    if( !p->pDirtyTail ){
   125         -      p->pDirtyTail = pPage;
   126         -    }
   127    109       if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
   128    110         p->pSynced = pPage;
   129    111       }
   130         -    expensive_assert( pcacheCheckSynced(p) );
   131    112     }
   132    113   }
   133    114   
   134    115   /*
   135    116   ** Wrapper around the pluggable caches xUnpin method. If the cache is
   136    117   ** being used for an in-memory database, this function is a no-op.
   137    118   */
................................................................................
   300    281   
   301    282   
   302    283     /* Find a dirty page to write-out and recycle. First try to find a 
   303    284     ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
   304    285     ** cleared), but if that is not possible settle for any other 
   305    286     ** unreferenced dirty page.
   306    287     */
   307         -  expensive_assert( pcacheCheckSynced(pCache) );
   308    288     for(pPg=pCache->pSynced; 
   309    289         pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
   310    290         pPg=pPg->pDirtyPrev
   311    291     );
   312    292     pCache->pSynced = pPg;
   313    293     if( !pPg ){
   314    294       for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
................................................................................
   386    366       pCache->pPage1 = pPgHdr;
   387    367     }
   388    368     return pPgHdr;
   389    369   }
   390    370   
   391    371   /*
   392    372   ** Decrement the reference count on a page. If the page is clean and the
   393         -** reference count drops to 0, then it is made elible for recycling.
          373  +** reference count drops to 0, then it is made eligible for recycling.
   394    374   */
   395    375   void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
   396    376     assert( p->nRef>0 );
   397    377     p->nRef--;
   398    378     if( p->nRef==0 ){
   399    379       p->pCache->nRef--;
   400    380       if( (p->flags&PGHDR_DIRTY)==0 ){
   401    381         pcacheUnpin(p);
   402         -    }else{
          382  +    }else if( p->pDirtyPrev!=0 ){
   403    383         /* Move the page to the head of the dirty list. */
   404    384         pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
   405    385       }
   406    386     }
   407    387   }
   408    388   
   409    389   /*

Changes to src/pcache1.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file implements the default page cache implementation (the
    14     14   ** sqlite3_pcache interface). It also contains part of the implementation
    15     15   ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
    16         -** If the default page cache implementation is overriden, then neither of
           16  +** If the default page cache implementation is overridden, then neither of
    17     17   ** these two features are available.
    18     18   */
    19     19   
    20     20   #include "sqliteInt.h"
    21     21   
    22     22   typedef struct PCache1 PCache1;
    23     23   typedef struct PgHdr1 PgHdr1;
    24     24   typedef struct PgFreeslot PgFreeslot;
    25     25   typedef struct PGroup PGroup;
    26     26   
    27     27   /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
    28         -** of one or more PCaches that are able to recycle each others unpinned
           28  +** of one or more PCaches that are able to recycle each other's unpinned
    29     29   ** pages when they are under memory pressure.  A PGroup is an instance of
    30     30   ** the following object.
    31     31   **
    32     32   ** This page cache implementation works in one of two modes:
    33     33   **
    34     34   **   (1)  Every PCache is the sole member of its own PGroup.  There is
    35     35   **        one PGroup per PCache.

Changes to src/pragma.c.

  1409   1409       }else{
  1410   1410         int mask = aPragmaNames[mid].iArg;    /* Mask of bits to set or clear. */
  1411   1411         if( db->autoCommit==0 ){
  1412   1412           /* Foreign key support may not be enabled or disabled while not
  1413   1413           ** in auto-commit mode.  */
  1414   1414           mask &= ~(SQLITE_ForeignKeys);
  1415   1415         }
         1416  +#if SQLITE_USER_AUTHENTICATION
         1417  +      if( db->auth.authLevel==UAUTH_User ){
         1418  +        /* Do not allow non-admin users to modify the schema arbitrarily */
         1419  +        mask &= ~(SQLITE_WriteSchema);
         1420  +      }
         1421  +#endif
  1416   1422   
  1417   1423         if( sqlite3GetBoolean(zRight, 0) ){
  1418   1424           db->flags |= mask;
  1419   1425         }else{
  1420   1426           db->flags &= ~mask;
  1421   1427           if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
  1422   1428         }

Changes to src/prepare.c.

   327    327     {
   328    328       char *zSql;
   329    329       zSql = sqlite3MPrintf(db, 
   330    330           "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
   331    331           db->aDb[iDb].zName, zMasterName);
   332    332   #ifndef SQLITE_OMIT_AUTHORIZATION
   333    333       {
   334         -      int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
          334  +      sqlite3_xauth xAuth;
   335    335         xAuth = db->xAuth;
   336    336         db->xAuth = 0;
   337    337   #endif
   338    338         rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
   339    339   #ifndef SQLITE_OMIT_AUTHORIZATION
   340    340         db->xAuth = xAuth;
   341    341       }
................................................................................
   393    393   ** file was of zero-length, then the DB_Empty flag is also set.
   394    394   */
   395    395   int sqlite3Init(sqlite3 *db, char **pzErrMsg){
   396    396     int i, rc;
   397    397     int commit_internal = !(db->flags&SQLITE_InternChanges);
   398    398     
   399    399     assert( sqlite3_mutex_held(db->mutex) );
          400  +  assert( db->init.busy==0 );
   400    401     rc = SQLITE_OK;
   401    402     db->init.busy = 1;
   402    403     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
   403    404       if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
   404    405       rc = sqlite3InitOne(db, i, pzErrMsg);
   405    406       if( rc ){
   406    407         sqlite3ResetOneSchema(db, i);
................................................................................
   408    409     }
   409    410   
   410    411     /* Once all the other databases have been initialized, load the schema
   411    412     ** for the TEMP database. This is loaded last, as the TEMP database
   412    413     ** schema may contain references to objects in other databases.
   413    414     */
   414    415   #ifndef SQLITE_OMIT_TEMPDB
   415         -  if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
   416         -                    && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
          416  +  assert( db->nDb>1 );
          417  +  if( rc==SQLITE_OK && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
   417    418       rc = sqlite3InitOne(db, 1, pzErrMsg);
   418    419       if( rc ){
   419    420         sqlite3ResetOneSchema(db, 1);
   420    421       }
   421    422     }
   422    423   #endif
   423    424   

Changes to src/printf.c.

     9      9   **
    10     10   ** This file contains code for a set of "printf"-like routines.  These
    11     11   ** routines format strings much like the printf() from the standard C
    12     12   ** library, though the implementation here has enhancements to support
    13     13   ** SQLlite.
    14     14   */
    15     15   #include "sqliteInt.h"
           16  +
           17  +/*
           18  +** If the strchrnul() library function is available, then set
           19  +** HAVE_STRCHRNUL.  If that routine is not available, this module
           20  +** will supply its own.  The built-in version is slower than
           21  +** the glibc version so the glibc version is definitely preferred.
           22  +*/
           23  +#if !defined(HAVE_STRCHRNUL)
           24  +# if defined(linux)
           25  +#  define HAVE_STRCHRNUL 1
           26  +# else
           27  +#  define HAVE_STRCHRNUL 0
           28  +# endif
           29  +#endif
           30  +
    16     31   
    17     32   /*
    18     33   ** Conversion types fall into various categories as defined by the
    19     34   ** following enumeration.
    20     35   */
    21     36   #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
    22     37   #define etFLOAT       2 /* Floating point.  %f */
................................................................................
   220    235       useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
   221    236     }else{
   222    237       bArgList = useIntern = 0;
   223    238     }
   224    239     for(; (c=(*fmt))!=0; ++fmt){
   225    240       if( c!='%' ){
   226    241         bufpt = (char *)fmt;
   227         -      while( (c=(*++fmt))!='%' && c!=0 ){};
          242  +#if HAVE_STRCHRNUL
          243  +      fmt = strchrnul(fmt, '%');
          244  +#else
          245  +      do{ fmt++; }while( *fmt && *fmt != '%' );
          246  +#endif
   228    247         sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
   229         -      if( c==0 ) break;
          248  +      if( *fmt==0 ) break;
   230    249       }
   231    250       if( (c=(*++fmt))==0 ){
   232    251         sqlite3StrAccumAppend(pAccum, "%", 1);
   233    252         break;
   234    253       }
   235    254       /* Find out what flags are present */
   236    255       flag_leftjustify = flag_plussign = flag_blanksign = 
................................................................................
   900    919     z = sqlite3VMPrintf(db, zFormat, ap);
   901    920     va_end(ap);
   902    921     return z;
   903    922   }
   904    923   
   905    924   /*
   906    925   ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
   907         -** the string and before returnning.  This routine is intended to be used
          926  +** the string and before returning.  This routine is intended to be used
   908    927   ** to modify an existing string.  For example:
   909    928   **
   910    929   **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
   911    930   **
   912    931   */
   913    932   char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
   914    933     va_list ap;

Changes to src/resolve.c.

   715    715               ** likelihood(X,0.9375).
   716    716               ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to
   717    717               ** likelihood(X,0.9375). */
   718    718               /* TUNING: unlikely() probability is 0.0625.  likely() is 0.9375 */
   719    719               pExpr->iTable = pDef->zName[0]=='u' ? 62 : 938;
   720    720             }             
   721    721           }
   722         -      }
   723    722   #ifndef SQLITE_OMIT_AUTHORIZATION
   724         -      if( pDef ){
   725    723           auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
   726    724           if( auth!=SQLITE_OK ){
   727    725             if( auth==SQLITE_DENY ){
   728    726               sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
   729    727                                       pDef->zName);
   730    728               pNC->nErr++;
   731    729             }
   732    730             pExpr->op = TK_NULL;
   733    731             return WRC_Prune;
   734    732           }
          733  +#endif
   735    734           if( pDef->funcFlags & SQLITE_FUNC_CONSTANT ) ExprSetProperty(pExpr,EP_Constant);
   736    735         }
   737         -#endif
   738    736         if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
   739    737           sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
   740    738           pNC->nErr++;
   741    739           is_agg = 0;
   742    740         }else if( no_such_func && pParse->db->init.busy==0 ){
   743    741           sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
   744    742           pNC->nErr++;
................................................................................
   753    751           NameContext *pNC2 = pNC;
   754    752           pExpr->op = TK_AGG_FUNCTION;
   755    753           pExpr->op2 = 0;
   756    754           while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
   757    755             pExpr->op2++;
   758    756             pNC2 = pNC2->pNext;
   759    757           }
   760         -        if( pNC2 ) pNC2->ncFlags |= NC_HasAgg;
          758  +        assert( pDef!=0 );
          759  +        if( pNC2 ){
          760  +          assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
          761  +          testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
          762  +          pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
          763  +
          764  +        }
   761    765           pNC->ncFlags |= NC_AllowAgg;
   762    766         }
   763    767         /* FIX ME:  Compute pExpr->affinity based on the expected return
   764    768         ** type of the function 
   765    769         */
   766    770         return WRC_Prune;
   767    771       }
................................................................................
  1114   1118         }
  1115   1119       }
  1116   1120     }
  1117   1121     return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
  1118   1122   }
  1119   1123   
  1120   1124   /*
  1121         -** Resolve names in the SELECT statement p and all of its descendents.
         1125  +** Resolve names in the SELECT statement p and all of its descendants.
  1122   1126   */
  1123   1127   static int resolveSelectStep(Walker *pWalker, Select *p){
  1124   1128     NameContext *pOuterNC;  /* Context that contains this SELECT */
  1125   1129     NameContext sNC;        /* Name context of this SELECT */
  1126   1130     int isCompound;         /* True if p is a compound select */
  1127   1131     int nCompound;          /* Number of compound terms processed so far */
  1128   1132     Parse *pParse;          /* Parsing context */
................................................................................
  1218   1222     
  1219   1223       /* If there are no aggregate functions in the result-set, and no GROUP BY 
  1220   1224       ** expression, do not allow aggregates in any of the other expressions.
  1221   1225       */
  1222   1226       assert( (p->selFlags & SF_Aggregate)==0 );
  1223   1227       pGroupBy = p->pGroupBy;
  1224   1228       if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
  1225         -      p->selFlags |= SF_Aggregate;
         1229  +      assert( NC_MinMaxAgg==SF_MinMaxAgg );
         1230  +      p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
  1226   1231       }else{
  1227   1232         sNC.ncFlags &= ~NC_AllowAgg;
  1228   1233       }
  1229   1234     
  1230   1235       /* If a HAVING clause is present, then there must be a GROUP BY clause.
  1231   1236       */
  1232   1237       if( p->pHaving && !pGroupBy ){
................................................................................
  1346   1351   ** An error message is left in pParse if anything is amiss.  The number
  1347   1352   ** if errors is returned.
  1348   1353   */
  1349   1354   int sqlite3ResolveExprNames( 
  1350   1355     NameContext *pNC,       /* Namespace to resolve expressions in. */
  1351   1356     Expr *pExpr             /* The expression to be analyzed. */
  1352   1357   ){
  1353         -  u8 savedHasAgg;
         1358  +  u16 savedHasAgg;
  1354   1359     Walker w;
  1355   1360   
  1356   1361     if( pExpr==0 ) return 0;
  1357   1362   #if SQLITE_MAX_EXPR_DEPTH>0
  1358   1363     {
  1359   1364       Parse *pParse = pNC->pParse;
  1360   1365       if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
  1361   1366         return 1;
  1362   1367       }
  1363   1368       pParse->nHeight += pExpr->nHeight;
  1364   1369     }
  1365   1370   #endif
  1366         -  savedHasAgg = pNC->ncFlags & NC_HasAgg;
  1367         -  pNC->ncFlags &= ~NC_HasAgg;
         1371  +  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
         1372  +  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
  1368   1373     memset(&w, 0, sizeof(w));
  1369   1374     w.xExprCallback = resolveExprStep;
  1370   1375     w.xSelectCallback = resolveSelectStep;
  1371   1376     w.pParse = pNC->pParse;
  1372   1377     w.u.pNC = pNC;
  1373   1378     sqlite3WalkExpr(&w, pExpr);
  1374   1379   #if SQLITE_MAX_EXPR_DEPTH>0
................................................................................
  1375   1380     pNC->pParse->nHeight -= pExpr->nHeight;
  1376   1381   #endif
  1377   1382     if( pNC->nErr>0 || w.pParse->nErr>0 ){
  1378   1383       ExprSetProperty(pExpr, EP_Error);
  1379   1384     }
  1380   1385     if( pNC->ncFlags & NC_HasAgg ){
  1381   1386       ExprSetProperty(pExpr, EP_Agg);
  1382         -  }else if( savedHasAgg ){
  1383         -    pNC->ncFlags |= NC_HasAgg;
  1384   1387     }
         1388  +  pNC->ncFlags |= savedHasAgg;
  1385   1389     return ExprHasProperty(pExpr, EP_Error);
  1386   1390   }
  1387   1391   
  1388   1392   
  1389   1393   /*
  1390   1394   ** Resolve all names in all expressions of a SELECT and in all
  1391   1395   ** decendents of the SELECT, including compounds off of p->pPrior,

Changes to src/rowset.c.

    46     46   ** value added by the INSERT will not be visible to the second TEST.
    47     47   ** The initial batch number is zero, so if the very first TEST contains
    48     48   ** a non-zero batch number, it will see all prior INSERTs.
    49     49   **
    50     50   ** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
    51     51   ** that is attempted.
    52     52   **
    53         -** The cost of an INSERT is roughly constant.  (Sometime new memory
           53  +** The cost of an INSERT is roughly constant.  (Sometimes new memory
    54     54   ** has to be allocated on an INSERT.)  The cost of a TEST with a new
    55     55   ** batch number is O(NlogN) where N is the number of elements in the RowSet.
    56     56   ** The cost of a TEST using the same batch number is O(logN).  The cost
    57     57   ** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
    58     58   ** primitives are constant time.  The cost of DESTROY is O(N).
    59     59   **
    60     60   ** There is an added cost of O(N) when switching between TEST and
................................................................................
   439    439     }
   440    440   }
   441    441   
   442    442   /*
   443    443   ** Check to see if element iRowid was inserted into the rowset as
   444    444   ** part of any insert batch prior to iBatch.  Return 1 or 0.
   445    445   **
   446         -** If this is the first test of a new batch and if there exist entires
   447         -** on pRowSet->pEntry, then sort those entires into the forest at
          446  +** If this is the first test of a new batch and if there exist entries
          447  +** on pRowSet->pEntry, then sort those entries into the forest at
   448    448   ** pRowSet->pForest so that they can be tested.
   449    449   */
   450    450   int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
   451    451     struct RowSetEntry *p, *pTree;
   452    452   
   453    453     /* This routine is never called after sqlite3RowSetNext() */
   454    454     assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );

Changes to src/select.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle SELECT statements in SQLite.
    14     14   */
    15     15   #include "sqliteInt.h"
           16  +
           17  +/*
           18  +** Trace output macros
           19  +*/
           20  +#if SELECTTRACE_ENABLED
           21  +/***/ int sqlite3SelectTrace = 0;
           22  +# define SELECTTRACE(K,P,S,X)  \
           23  +  if(sqlite3SelectTrace&(K))   \
           24  +    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",(S)->zSelName,(S)),\
           25  +    sqlite3DebugPrintf X
           26  +#else
           27  +# define SELECTTRACE(K,P,S,X)
           28  +#endif
           29  +
    16     30   
    17     31   /*
    18     32   ** An instance of the following object is used to record information about
    19     33   ** how to process the DISTINCT keyword, to simplify passing that information
    20     34   ** into the selectInnerLoop() routine.
    21     35   */
    22     36   typedef struct DistinctCtx DistinctCtx;
................................................................................
   121    135       pNew = 0;
   122    136     }else{
   123    137       assert( pNew->pSrc!=0 || pParse->nErr>0 );
   124    138     }
   125    139     assert( pNew!=&standin );
   126    140     return pNew;
   127    141   }
          142  +
          143  +#if SELECTTRACE_ENABLED
          144  +/*
          145  +** Set the name of a Select object
          146  +*/
          147  +void sqlite3SelectSetName(Select *p, const char *zName){
          148  +  if( p && zName ){
          149  +    sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName);
          150  +  }
          151  +}
          152  +#endif
          153  +
   128    154   
   129    155   /*
   130    156   ** Delete the given Select structure and all of its substructures.
   131    157   */
   132    158   void sqlite3SelectDelete(sqlite3 *db, Select *p){
   133    159     if( p ){
   134    160       clearSelect(db, p);
................................................................................
   484    510       pParse->nMem += nBase;
   485    511     }
   486    512     sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, SQLITE_ECEL_DUP);
   487    513     if( bSeq ){
   488    514       sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
   489    515     }
   490    516     if( nPrefixReg==0 ){
   491         -    sqlite3VdbeAddOp3(v, OP_Move, regData, regBase+nExpr+bSeq, nData);
          517  +    sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
   492    518     }
   493    519   
   494    520     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord);
   495    521     if( nOBSat>0 ){
   496    522       int regPrevKey;   /* The first nOBSat columns of the previous row */
   497    523       int addrFirst;    /* Address of the OP_IfNot opcode */
   498    524       int addrJmp;      /* Address of the OP_Jump opcode */
................................................................................
   520    546       addrJmp = sqlite3VdbeCurrentAddr(v);
   521    547       sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
   522    548       pSort->labelBkOut = sqlite3VdbeMakeLabel(v);
   523    549       pSort->regReturn = ++pParse->nMem;
   524    550       sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
   525    551       sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
   526    552       sqlite3VdbeJumpHere(v, addrFirst);
   527         -    sqlite3VdbeAddOp3(v, OP_Move, regBase, regPrevKey, pSort->nOBSat);
          553  +    sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
   528    554       sqlite3VdbeJumpHere(v, addrJmp);
   529    555     }
   530    556     if( pSort->sortFlags & SORTFLAG_UseSorter ){
   531    557       op = OP_SorterInsert;
   532    558     }else{
   533    559       op = OP_IdxInsert;
   534    560     }
................................................................................
  1006   1032   **
  1007   1033   ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
  1008   1034   ** KeyInfo structure is appropriate for initializing a virtual index to
  1009   1035   ** implement that clause.  If the ExprList is the result set of a SELECT
  1010   1036   ** then the KeyInfo structure is appropriate for initializing a virtual
  1011   1037   ** index to implement a DISTINCT test.
  1012   1038   **
  1013         -** Space to hold the KeyInfo structure is obtain from malloc.  The calling
         1039  +** Space to hold the KeyInfo structure is obtained from malloc.  The calling
  1014   1040   ** function is responsible for seeing that this structure is eventually
  1015   1041   ** freed.
  1016   1042   */
  1017   1043   static KeyInfo *keyInfoFromExprList(
  1018   1044     Parse *pParse,       /* Parsing context */
  1019   1045     ExprList *pList,     /* Form the KeyInfo object from this ExprList */
  1020   1046     int iStart,          /* Begin with this column of pList */
................................................................................
  1537   1563         sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
  1538   1564       }
  1539   1565     }
  1540   1566     generateColumnTypes(pParse, pTabList, pEList);
  1541   1567   }
  1542   1568   
  1543   1569   /*
  1544         -** Given a an expression list (which is really the list of expressions
         1570  +** Given an expression list (which is really the list of expressions
  1545   1571   ** that form the result set of a SELECT statement) compute appropriate
  1546   1572   ** column names for a table that would hold the expression list.
  1547   1573   **
  1548   1574   ** All column names will be unique.
  1549   1575   **
  1550   1576   ** Only the column names are computed.  Column.zType, Column.zColl,
  1551   1577   ** and other fields of Column are zeroed.
................................................................................
  1610   1636       }
  1611   1637       if( db->mallocFailed ){
  1612   1638         sqlite3DbFree(db, zName);
  1613   1639         break;
  1614   1640       }
  1615   1641   
  1616   1642       /* Make sure the column name is unique.  If the name is not unique,
  1617         -    ** append a integer to the name so that it becomes unique.
         1643  +    ** append an integer to the name so that it becomes unique.
  1618   1644       */
  1619   1645       nName = sqlite3Strlen30(zName);
  1620   1646       for(j=cnt=0; j<i; j++){
  1621   1647         if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
  1622   1648           char *zNewName;
  1623   1649           int k;
  1624   1650           for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){}
................................................................................
  3094   3120   ** optimized.
  3095   3121   **
  3096   3122   ** This routine attempts to rewrite queries such as the above into
  3097   3123   ** a single flat select, like this:
  3098   3124   **
  3099   3125   **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
  3100   3126   **
  3101         -** The code generated for this simpification gives the same result
         3127  +** The code generated for this simplification gives the same result
  3102   3128   ** but only has to scan the data once.  And because indices might 
  3103   3129   ** exist on the table t1, a complete scan of the data might be
  3104   3130   ** avoided.
  3105   3131   **
  3106   3132   ** Flattening is only attempted if all of the following are true:
  3107   3133   **
  3108   3134   **   (1)  The subquery and the outer query do not both use aggregates.
................................................................................
  3127   3153   **        single NULL.
  3128   3154   **
  3129   3155   **   (8)  The subquery does not use LIMIT or the outer query is not a join.
  3130   3156   **
  3131   3157   **   (9)  The subquery does not use LIMIT or the outer query does not use
  3132   3158   **        aggregates.
  3133   3159   **
  3134         -**  (10)  The subquery does not use aggregates or the outer query does not
  3135         -**        use LIMIT.
         3160  +**  (**)  Restriction (10) was removed from the code on 2005-02-05 but we
         3161  +**        accidently carried the comment forward until 2014-09-15.  Original
         3162  +**        text: "The subquery does not use aggregates or the outer query does not
         3163  +**        use LIMIT."
  3136   3164   **
  3137   3165   **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
  3138   3166   **
  3139   3167   **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
  3140   3168   **        a separate restriction deriving from ticket #350.
  3141   3169   **
  3142   3170   **  (13)  The subquery and outer query do not both use LIMIT.
................................................................................
  3191   3219   **  (22)  The subquery is not a recursive CTE.
  3192   3220   **
  3193   3221   **  (23)  The parent is not a recursive CTE, or the sub-query is not a
  3194   3222   **        compound query. This restriction is because transforming the
  3195   3223   **        parent to a compound query confuses the code that handles
  3196   3224   **        recursive queries in multiSelect().
  3197   3225   **
         3226  +**  (24)  The subquery is not an aggregate that uses the built-in min() or 
         3227  +**        or max() functions.  (Without this restriction, a query like:
         3228  +**        "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
         3229  +**        return the value X for which Y was maximal.)
         3230  +**
  3198   3231   **
  3199   3232   ** In this routine, the "p" parameter is a pointer to the outer query.
  3200   3233   ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
  3201   3234   ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
  3202   3235   **
  3203   3236   ** If flattening is not attempted, this routine is a no-op and returns 0.
  3204   3237   ** If flattening is attempted this routine returns 1.
................................................................................
  3238   3271     pSub = pSubitem->pSelect;
  3239   3272     assert( pSub!=0 );
  3240   3273     if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
  3241   3274     if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
  3242   3275     pSubSrc = pSub->pSrc;
  3243   3276     assert( pSubSrc );
  3244   3277     /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
  3245         -  ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
         3278  +  ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
  3246   3279     ** because they could be computed at compile-time.  But when LIMIT and OFFSET
  3247   3280     ** became arbitrary expressions, we were forced to add restrictions (13)
  3248   3281     ** and (14). */
  3249   3282     if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
  3250   3283     if( pSub->pOffset ) return 0;                          /* Restriction (14) */
  3251   3284     if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
  3252   3285       return 0;                                            /* Restriction (15) */
................................................................................
  3263   3296        return 0;                                           /* Restriction (11) */
  3264   3297     }
  3265   3298     if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
  3266   3299     if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
  3267   3300     if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
  3268   3301        return 0;         /* Restriction (21) */
  3269   3302     }
  3270         -  if( pSub->selFlags & SF_Recursive ) return 0;          /* Restriction (22)  */
  3271         -  if( (p->selFlags & SF_Recursive) && pSub->pPrior ) return 0;       /* (23)  */
         3303  +  testcase( pSub->selFlags & SF_Recursive );
         3304  +  testcase( pSub->selFlags & SF_MinMaxAgg );
         3305  +  if( pSub->selFlags & (SF_Recursive|SF_MinMaxAgg) ){
         3306  +    return 0; /* Restrictions (22) and (24) */
         3307  +  }
         3308  +  if( (p->selFlags & SF_Recursive) && pSub->pPrior ){
         3309  +    return 0; /* Restriction (23) */
         3310  +  }
  3272   3311   
  3273   3312     /* OBSOLETE COMMENT 1:
  3274   3313     ** Restriction 3:  If the subquery is a join, make sure the subquery is 
  3275   3314     ** not used as the right operand of an outer join.  Examples of why this
  3276   3315     ** is not allowed:
  3277   3316     **
  3278   3317     **         t1 LEFT OUTER JOIN (t2 JOIN t3)
................................................................................
  3338   3377         for(ii=0; ii<p->pOrderBy->nExpr; ii++){
  3339   3378           if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
  3340   3379         }
  3341   3380       }
  3342   3381     }
  3343   3382   
  3344   3383     /***** If we reach this point, flattening is permitted. *****/
         3384  +  SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n",
         3385  +                   pSub->zSelName, pSub, iFrom));
  3345   3386   
  3346   3387     /* Authorize the subquery */
  3347   3388     pParse->zAuthContext = pSubitem->zName;
  3348   3389     TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
  3349   3390     testcase( i==SQLITE_DENY );
  3350   3391     pParse->zAuthContext = zSavedAuthContext;
  3351   3392   
................................................................................
  3390   3431       Select *pPrior = p->pPrior;
  3391   3432       p->pOrderBy = 0;
  3392   3433       p->pSrc = 0;
  3393   3434       p->pPrior = 0;
  3394   3435       p->pLimit = 0;
  3395   3436       p->pOffset = 0;
  3396   3437       pNew = sqlite3SelectDup(db, p, 0);
         3438  +    sqlite3SelectSetName(pNew, pSub->zSelName);
  3397   3439       p->pOffset = pOffset;
  3398   3440       p->pLimit = pLimit;
  3399   3441       p->pOrderBy = pOrderBy;
  3400   3442       p->pSrc = pSrc;
  3401   3443       p->op = TK_ALL;
  3402   3444       if( pNew==0 ){
  3403   3445         p->pPrior = pPrior;
  3404   3446       }else{
  3405   3447         pNew->pPrior = pPrior;
  3406   3448         if( pPrior ) pPrior->pNext = pNew;
  3407   3449         pNew->pNext = p;
  3408   3450         p->pPrior = pNew;
         3451  +      SELECTTRACE(2,pParse,p,
         3452  +         ("compound-subquery flattener creates %s.%p as peer\n",
         3453  +         pNew->zSelName, pNew));
  3409   3454       }
  3410   3455       if( db->mallocFailed ) return 1;
  3411   3456     }
  3412   3457   
  3413   3458     /* Begin flattening the iFrom-th entry of the FROM clause 
  3414   3459     ** in the outer query.
  3415   3460     */
................................................................................
  3531   3576       }
  3532   3577       substExprList(db, pParent->pEList, iParent, pSub->pEList);
  3533   3578       if( isAgg ){
  3534   3579         substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
  3535   3580         pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
  3536   3581       }
  3537   3582       if( pSub->pOrderBy ){
         3583  +      /* At this point, any non-zero iOrderByCol values indicate that the
         3584  +      ** ORDER BY column expression is identical to the iOrderByCol'th
         3585  +      ** expression returned by SELECT statement pSub. Since these values
         3586  +      ** do not necessarily correspond to columns in SELECT statement pParent,
         3587  +      ** zero them before transfering the ORDER BY clause.
         3588  +      **
         3589  +      ** Not doing this may cause an error if a subsequent call to this
         3590  +      ** function attempts to flatten a compound sub-query into pParent
         3591  +      ** (the only way this can happen is if the compound sub-query is
         3592  +      ** currently part of pSub->pSrc). See ticket [d11a6e908f].  */
         3593  +      ExprList *pOrderBy = pSub->pOrderBy;
         3594  +      for(i=0; i<pOrderBy->nExpr; i++){
         3595  +        pOrderBy->a[i].u.x.iOrderByCol = 0;
         3596  +      }
  3538   3597         assert( pParent->pOrderBy==0 );
  3539         -      pParent->pOrderBy = pSub->pOrderBy;
         3598  +      assert( pSub->pPrior==0 );
         3599  +      pParent->pOrderBy = pOrderBy;
  3540   3600         pSub->pOrderBy = 0;
  3541   3601       }else if( pParent->pOrderBy ){
  3542   3602         substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
  3543   3603       }
  3544   3604       if( pSub->pWhere ){
  3545   3605         pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
  3546   3606       }else{
................................................................................
  3624   3684   
  3625   3685     assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
  3626   3686     return eRet;
  3627   3687   }
  3628   3688   
  3629   3689   /*
  3630   3690   ** The select statement passed as the first argument is an aggregate query.
  3631         -** The second argment is the associated aggregate-info object. This 
         3691  +** The second argument is the associated aggregate-info object. This 
  3632   3692   ** function tests if the SELECT is of the form:
  3633   3693   **
  3634   3694   **   SELECT count(*) FROM <tbl>
  3635   3695   **
  3636   3696   ** where table is a database table, not a sub-select or view. If the query
  3637   3697   ** does match this pattern, then a pointer to the Table object representing
  3638   3698   ** <tbl> is returned. Otherwise, 0 is returned.
................................................................................
  3954   4014   **         element of the FROM clause.
  3955   4015   **
  3956   4016   **    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that 
  3957   4017   **         defines FROM clause.  When views appear in the FROM clause,
  3958   4018   **         fill pTabList->a[].pSelect with a copy of the SELECT statement
  3959   4019   **         that implements the view.  A copy is made of the view's SELECT
  3960   4020   **         statement so that we can freely modify or delete that statement
  3961         -**         without worrying about messing up the presistent representation
         4021  +**         without worrying about messing up the persistent representation
  3962   4022   **         of the view.
  3963   4023   **
  3964         -**    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
         4024  +**    (3)  Add terms to the WHERE clause to accommodate the NATURAL keyword
  3965   4025   **         on joins and the ON and USING clause of joins.
  3966   4026   **
  3967   4027   **    (4)  Scan the list of columns in the result set (pEList) looking
  3968   4028   **         for instances of the "*" operator or the TABLE.* operator.
  3969   4029   **         If found, expand each "*" to be every column in every table
  3970   4030   **         and TABLE.* to be every column in TABLE.
  3971   4031   **
................................................................................
  4048   4108         pTab->nRef++;
  4049   4109   #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
  4050   4110         if( pTab->pSelect || IsVirtual(pTab) ){
  4051   4111           /* We reach here if the named table is a really a view */
  4052   4112           if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
  4053   4113           assert( pFrom->pSelect==0 );
  4054   4114           pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
         4115  +        sqlite3SelectSetName(pFrom->pSelect, pTab->zName);
  4055   4116           sqlite3WalkSelect(pWalker, pFrom->pSelect);
  4056   4117         }
  4057   4118   #endif
  4058   4119       }
  4059   4120   
  4060   4121       /* Locate the index named by the INDEXED BY clause, if any. */
  4061   4122       if( sqlite3IndexedByLookup(pParse, pFrom) ){
................................................................................
  4582   4643   
  4583   4644     db = pParse->db;
  4584   4645     if( p==0 || db->mallocFailed || pParse->nErr ){
  4585   4646       return 1;
  4586   4647     }
  4587   4648     if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
  4588   4649     memset(&sAggInfo, 0, sizeof(sAggInfo));
         4650  +#if SELECTTRACE_ENABLED
         4651  +  pParse->nSelectIndent++;
         4652  +  SELECTTRACE(1,pParse,p, ("begin processing\n"));
         4653  +#endif
  4589   4654   
  4590   4655     assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
  4591   4656     assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
  4592   4657     assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
  4593   4658     assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
  4594   4659     if( IgnorableOrderby(pDest) ){
  4595   4660       assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || 
................................................................................
  4738   4803   
  4739   4804   #ifndef SQLITE_OMIT_COMPOUND_SELECT
  4740   4805     /* If there is are a sequence of queries, do the earlier ones first.
  4741   4806     */
  4742   4807     if( p->pPrior ){
  4743   4808       rc = multiSelect(pParse, p, pDest);
  4744   4809       explainSetInteger(pParse->iSelectId, iRestoreSelectId);
         4810  +#if SELECTTRACE_ENABLED
         4811  +    SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
         4812  +    pParse->nSelectIndent--;
         4813  +#endif
  4745   4814       return rc;
  4746   4815     }
  4747   4816   #endif
  4748   4817   
  4749   4818     /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and 
  4750   4819     ** if the select-list is the same as the ORDER BY list, then this query
  4751   4820     ** can be rewritten as a GROUP BY. In other words, this:
................................................................................
  5337   5406     */
  5338   5407     if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
  5339   5408       generateColumnNames(pParse, pTabList, pEList);
  5340   5409     }
  5341   5410   
  5342   5411     sqlite3DbFree(db, sAggInfo.aCol);
  5343   5412     sqlite3DbFree(db, sAggInfo.aFunc);
         5413  +#if SELECTTRACE_ENABLED
         5414  +  SELECTTRACE(1,pParse,p,("end processing\n"));
         5415  +  pParse->nSelectIndent--;
         5416  +#endif
  5344   5417     return rc;
  5345   5418   }
  5346   5419   
  5347   5420   #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
  5348   5421   void sqlite3PrintExpr(Expr *p);
  5349   5422   void sqlite3PrintExprList(ExprList *pList);
  5350   5423   void sqlite3PrintSelect(Select *p, int indent);

Changes to src/shell.c.

    29     29   #endif
    30     30   
    31     31   #include <stdlib.h>
    32     32   #include <string.h>
    33     33   #include <stdio.h>
    34     34   #include <assert.h>
    35     35   #include "sqlite3.h"
           36  +#if SQLITE_USER_AUTHENTICATION
           37  +# include "sqlite3userauth.h"
           38  +#endif
    36     39   #include <ctype.h>
    37     40   #include <stdarg.h>
    38     41   
    39     42   #if !defined(_WIN32) && !defined(WIN32)
    40     43   # include <signal.h>
    41     44   # if !defined(__RTP__) && !defined(_WRS_KERNEL)
    42     45   #  include <pwd.h>
................................................................................
  3093   3096       }else if( rc != SQLITE_OK ){
  3094   3097         fprintf(stderr,"Error: querying schema information\n");
  3095   3098         rc = 1;
  3096   3099       }else{
  3097   3100         rc = 0;
  3098   3101       }
  3099   3102     }else
         3103  +
         3104  +
         3105  +#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
         3106  +  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
         3107  +    extern int sqlite3SelectTrace;
         3108  +    sqlite3SelectTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
         3109  +  }else
         3110  +#endif
         3111  +
  3100   3112   
  3101   3113   #ifdef SQLITE_DEBUG
  3102   3114     /* Undocumented commands for internal testing.  Subject to change
  3103   3115     ** without notice. */
  3104   3116     if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
  3105   3117       if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
  3106   3118         int i, v;
................................................................................
  3431   3443         sqlite3_trace(p->db, 0, 0);
  3432   3444       }else{
  3433   3445         sqlite3_trace(p->db, sql_trace_callback, p->traceOut);
  3434   3446       }
  3435   3447   #endif
  3436   3448     }else
  3437   3449   
         3450  +#if SQLITE_USER_AUTHENTICATION
         3451  +  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
         3452  +    if( nArg<2 ){
         3453  +      fprintf(stderr, "Usage: .user SUBCOMMAND ...\n");
         3454  +      rc = 1;
         3455  +      goto meta_command_exit;
         3456  +    }
         3457  +    open_db(p, 0);
         3458  +    if( strcmp(azArg[1],"login")==0 ){
         3459  +      if( nArg!=4 ){
         3460  +        fprintf(stderr, "Usage: .user login USER PASSWORD\n");
         3461  +        rc = 1;
         3462  +        goto meta_command_exit;
         3463  +      }
         3464  +      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
         3465  +                                    (int)strlen(azArg[3]));
         3466  +      if( rc ){
         3467  +        fprintf(stderr, "Authentication failed for user %s\n", azArg[2]);
         3468  +        rc = 1;
         3469  +      }
         3470  +    }else if( strcmp(azArg[1],"add")==0 ){
         3471  +      if( nArg!=5 ){
         3472  +        fprintf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
         3473  +        rc = 1;
         3474  +        goto meta_command_exit;
         3475  +      }
         3476  +      rc = sqlite3_user_add(p->db, azArg[2],
         3477  +                            azArg[3], (int)strlen(azArg[3]),
         3478  +                            booleanValue(azArg[4]));
         3479  +      if( rc ){
         3480  +        fprintf(stderr, "User-Add failed: %d\n", rc);
         3481  +        rc = 1;
         3482  +      }
         3483  +    }else if( strcmp(azArg[1],"edit")==0 ){
         3484  +      if( nArg!=5 ){
         3485  +        fprintf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
         3486  +        rc = 1;
         3487  +        goto meta_command_exit;
         3488  +      }
         3489  +      rc = sqlite3_user_change(p->db, azArg[2],
         3490  +                              azArg[3], (int)strlen(azArg[3]),
         3491  +                              booleanValue(azArg[4]));
         3492  +      if( rc ){
         3493  +        fprintf(stderr, "User-Edit failed: %d\n", rc);
         3494  +        rc = 1;
         3495  +      }
         3496  +    }else if( strcmp(azArg[1],"delete")==0 ){
         3497  +      if( nArg!=3 ){
         3498  +        fprintf(stderr, "Usage: .user delete USER\n");
         3499  +        rc = 1;
         3500  +        goto meta_command_exit;
         3501  +      }
         3502  +      rc = sqlite3_user_delete(p->db, azArg[2]);
         3503  +      if( rc ){
         3504  +        fprintf(stderr, "User-Delete failed: %d\n", rc);
         3505  +        rc = 1;
         3506  +      }
         3507  +    }else{
         3508  +      fprintf(stderr, "Usage: .user login|add|edit|delete ...\n");
         3509  +      rc = 1;
         3510  +      goto meta_command_exit;
         3511  +    }    
         3512  +  }else
         3513  +#endif /* SQLITE_USER_AUTHENTICATION */
         3514  +
  3438   3515     if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
  3439   3516       fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
  3440   3517           sqlite3_libversion(), sqlite3_sourceid());
  3441   3518     }else
  3442   3519   
  3443   3520     if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
  3444   3521       const char *zDbName = nArg==2 ? azArg[1] : "main";

Changes to src/sqlite.h.in.

   488    488   #define SQLITE_CONSTRAINT_TRIGGER      (SQLITE_CONSTRAINT | (7<<8))
   489    489   #define SQLITE_CONSTRAINT_UNIQUE       (SQLITE_CONSTRAINT | (8<<8))
   490    490   #define SQLITE_CONSTRAINT_VTAB         (SQLITE_CONSTRAINT | (9<<8))
   491    491   #define SQLITE_CONSTRAINT_ROWID        (SQLITE_CONSTRAINT |(10<<8))
   492    492   #define SQLITE_NOTICE_RECOVER_WAL      (SQLITE_NOTICE | (1<<8))
   493    493   #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
   494    494   #define SQLITE_WARNING_AUTOINDEX       (SQLITE_WARNING | (1<<8))
          495  +#define SQLITE_AUTH_USER               (SQLITE_AUTH | (1<<8))
   495    496   
   496    497   /*
   497    498   ** CAPI3REF: Flags For File Open Operations
   498    499   **
   499    500   ** These bit values are intended for use in the
   500    501   ** 3rd parameter to the [sqlite3_open_v2()] interface and
   501    502   ** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
................................................................................
  2095   2096   ** the handler returns 0 which causes [sqlite3_step()] to return
  2096   2097   ** [SQLITE_BUSY].
  2097   2098   **
  2098   2099   ** ^Calling this routine with an argument less than or equal to zero
  2099   2100   ** turns off all busy handlers.
  2100   2101   **
  2101   2102   ** ^(There can only be a single busy handler for a particular
  2102         -** [database connection] any any given moment.  If another busy handler
         2103  +** [database connection] at any given moment.  If another busy handler
  2103   2104   ** was defined  (using [sqlite3_busy_handler()]) prior to calling
  2104   2105   ** this routine, that other busy handler is cleared.)^
  2105   2106   **
  2106   2107   ** See also:  [PRAGMA busy_timeout]
  2107   2108   */
  2108   2109   int sqlite3_busy_timeout(sqlite3*, int ms);
  2109   2110   
................................................................................
  2298   2299   **
  2299   2300   ** ^The sqlite3_malloc() routine returns a pointer to a block
  2300   2301   ** of memory at least N bytes in length, where N is the parameter.
  2301   2302   ** ^If sqlite3_malloc() is unable to obtain sufficient free
  2302   2303   ** memory, it returns a NULL pointer.  ^If the parameter N to
  2303   2304   ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
  2304   2305   ** a NULL pointer.
         2306  +**
         2307  +** ^The sqlite3_malloc64(N) routine works just like
         2308  +** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
         2309  +** of a signed 32-bit integer.
  2305   2310   **
  2306   2311   ** ^Calling sqlite3_free() with a pointer previously returned
  2307   2312   ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
  2308   2313   ** that it might be reused.  ^The sqlite3_free() routine is
  2309   2314   ** a no-op if is called with a NULL pointer.  Passing a NULL pointer
  2310   2315   ** to sqlite3_free() is harmless.  After being freed, memory
  2311   2316   ** should neither be read nor written.  Even reading previously freed
  2312   2317   ** memory might result in a segmentation fault or other severe error.
  2313   2318   ** Memory corruption, a segmentation fault, or other severe error
  2314   2319   ** might result if sqlite3_free() is called with a non-NULL pointer that
  2315   2320   ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
  2316   2321   **
  2317         -** ^(The sqlite3_realloc() interface attempts to resize a
  2318         -** prior memory allocation to be at least N bytes, where N is the
  2319         -** second parameter.  The memory allocation to be resized is the first
  2320         -** parameter.)^ ^ If the first parameter to sqlite3_realloc()
         2322  +** ^The sqlite3_realloc(X,N) interface attempts to resize a
         2323  +** prior memory allocation X to be at least N bytes.
         2324  +** ^If the X parameter to sqlite3_realloc(X,N)
  2321   2325   ** is a NULL pointer then its behavior is identical to calling
  2322         -** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
  2323         -** ^If the second parameter to sqlite3_realloc() is zero or
         2326  +** sqlite3_malloc(N).
         2327  +** ^If the N parameter to sqlite3_realloc(X,N) is zero or
  2324   2328   ** negative then the behavior is exactly the same as calling
  2325         -** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
  2326         -** ^sqlite3_realloc() returns a pointer to a memory allocation
  2327         -** of at least N bytes in size or NULL if sufficient memory is unavailable.
         2329  +** sqlite3_free(X).
         2330  +** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
         2331  +** of at least N bytes in size or NULL if insufficient memory is available.
  2328   2332   ** ^If M is the size of the prior allocation, then min(N,M) bytes
  2329   2333   ** of the prior allocation are copied into the beginning of buffer returned
  2330         -** by sqlite3_realloc() and the prior allocation is freed.
  2331         -** ^If sqlite3_realloc() returns NULL, then the prior allocation
  2332         -** is not freed.
         2334  +** by sqlite3_realloc(X,N) and the prior allocation is freed.
         2335  +** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
         2336  +** prior allocation is not freed.
  2333   2337   **
  2334         -** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
         2338  +** ^The sqlite3_realloc64(X,N) interfaces works the same as
         2339  +** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
         2340  +** of a 32-bit signed integer.
         2341  +**
         2342  +** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
         2343  +** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
         2344  +** sqlite3_msize(X) returns the size of that memory allocation in bytes.
         2345  +** ^The value returned by sqlite3_msize(X) might be larger than the number
         2346  +** of bytes requested when X was allocated.  ^If X is a NULL pointer then
         2347  +** sqlite3_msize(X) returns zero.  If X points to something that is not
         2348  +** the beginning of memory allocation, or if it points to a formerly
         2349  +** valid memory allocation that has now been freed, then the behavior
         2350  +** of sqlite3_msize(X) is undefined and possibly harmful.
         2351  +**
         2352  +** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
         2353  +** sqlite3_malloc64(), and sqlite3_realloc64()
  2335   2354   ** is always aligned to at least an 8 byte boundary, or to a
  2336   2355   ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
  2337   2356   ** option is used.
  2338   2357   **
  2339   2358   ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
  2340   2359   ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
  2341   2360   ** implementation of these routines to be omitted.  That capability
................................................................................
  2355   2374   ** not yet been released.
  2356   2375   **
  2357   2376   ** The application must not read or write any part of
  2358   2377   ** a block of memory after it has been released using
  2359   2378   ** [sqlite3_free()] or [sqlite3_realloc()].
  2360   2379   */
  2361   2380   void *sqlite3_malloc(int);
         2381  +void *sqlite3_malloc64(sqlite3_uint64);
  2362   2382   void *sqlite3_realloc(void*, int);
         2383  +void *sqlite3_realloc64(void*, sqlite3_uint64);
  2363   2384   void sqlite3_free(void*);
         2385  +sqlite3_uint64 sqlite3_msize(void*);
  2364   2386   
  2365   2387   /*
  2366   2388   ** CAPI3REF: Memory Allocator Statistics
  2367   2389   **
  2368   2390   ** SQLite provides these two interfaces for reporting on the status
  2369   2391   ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
  2370   2392   ** routines, which form the built-in memory allocation subsystem.
................................................................................
  3365   3387   ** number of <u>bytes</u> in the value, not the number of characters.)^
  3366   3388   ** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
  3367   3389   ** is negative, then the length of the string is
  3368   3390   ** the number of bytes up to the first zero terminator.
  3369   3391   ** If the fourth parameter to sqlite3_bind_blob() is negative, then
  3370   3392   ** the behavior is undefined.
  3371   3393   ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
  3372         -** or sqlite3_bind_text16() then that parameter must be the byte offset
         3394  +** or sqlite3_bind_text16() or sqlite3_bind_text64() then
         3395  +** that parameter must be the byte offset
  3373   3396   ** where the NUL terminator would occur assuming the string were NUL
  3374   3397   ** terminated.  If any NUL characters occur at byte offsets less than 
  3375   3398   ** the value of the fourth parameter then the resulting string value will
  3376   3399   ** contain embedded NULs.  The result of expressions involving strings
  3377   3400   ** with embedded NULs is undefined.
  3378   3401   **
  3379   3402   ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
................................................................................
  3383   3406   ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.  
  3384   3407   ** ^If the fifth argument is
  3385   3408   ** the special value [SQLITE_STATIC], then SQLite assumes that the
  3386   3409   ** information is in static, unmanaged space and does not need to be freed.
  3387   3410   ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
  3388   3411   ** SQLite makes its own private copy of the data immediately, before
  3389   3412   ** the sqlite3_bind_*() routine returns.
         3413  +**
         3414  +** ^The sixth argument to sqlite3_bind_text64() must be one of
         3415  +** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
         3416  +** to specify the encoding of the text in the third parameter.  If
         3417  +** the sixth argument to sqlite3_bind_text64() is not how of the
         3418  +** allowed values shown above, or if the text encoding is different
         3419  +** from the encoding specified by the sixth parameter, then the behavior
         3420  +** is undefined.
  3390   3421   **
  3391   3422   ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
  3392   3423   ** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
  3393   3424   ** (just an integer to hold its size) while it is being processed.
  3394   3425   ** Zeroblobs are intended to serve as placeholders for BLOBs whose
  3395   3426   ** content is later written using
  3396   3427   ** [sqlite3_blob_open | incremental BLOB I/O] routines.
................................................................................
  3404   3435   ** result is undefined and probably harmful.
  3405   3436   **
  3406   3437   ** ^Bindings are not cleared by the [sqlite3_reset()] routine.
  3407   3438   ** ^Unbound parameters are interpreted as NULL.
  3408   3439   **
  3409   3440   ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
  3410   3441   ** [error code] if anything goes wrong.
         3442  +** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
         3443  +** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
         3444  +** [SQLITE_MAX_LENGTH].
  3411   3445   ** ^[SQLITE_RANGE] is returned if the parameter
  3412   3446   ** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
  3413   3447   **
  3414   3448   ** See also: [sqlite3_bind_parameter_count()],
  3415   3449   ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
  3416   3450   */
  3417   3451   int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
         3452  +int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
         3453  +                        void(*)(void*));
  3418   3454   int sqlite3_bind_double(sqlite3_stmt*, int, double);
  3419   3455   int sqlite3_bind_int(sqlite3_stmt*, int, int);
  3420   3456   int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
  3421   3457   int sqlite3_bind_null(sqlite3_stmt*, int);
  3422         -int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
         3458  +int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
  3423   3459   int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
         3460  +int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
         3461  +                         void(*)(void*), unsigned char encoding);
  3424   3462   int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
  3425   3463   int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
  3426   3464   
  3427   3465   /*
  3428   3466   ** CAPI3REF: Number Of SQL Parameters
  3429   3467   **
  3430   3468   ** ^This routine can be used to find the number of [SQL parameters]
................................................................................
  4165   4203   ** extract values from the [sqlite3_value] objects.
  4166   4204   **
  4167   4205   ** These routines work only with [protected sqlite3_value] objects.
  4168   4206   ** Any attempt to use these routines on an [unprotected sqlite3_value]
  4169   4207   ** object results in undefined behavior.
  4170   4208   **
  4171   4209   ** ^These routines work just like the corresponding [column access functions]
  4172         -** except that  these routines take a single [protected sqlite3_value] object
         4210  +** except that these routines take a single [protected sqlite3_value] object
  4173   4211   ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
  4174   4212   **
  4175   4213   ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
  4176   4214   ** in the native byte-order of the host machine.  ^The
  4177   4215   ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
  4178   4216   ** extract UTF-16 strings as big-endian and little-endian respectively.
  4179   4217   **
................................................................................
  4408   4446   ** of the application-defined function to be the 64-bit signed integer
  4409   4447   ** value given in the 2nd argument.
  4410   4448   **
  4411   4449   ** ^The sqlite3_result_null() interface sets the return value
  4412   4450   ** of the application-defined function to be NULL.
  4413   4451   **
  4414   4452   ** ^The sqlite3_result_text(), sqlite3_result_text16(),
  4415         -** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
         4453  +** sqlite3_result_text16le(), and sqlite3_result_text16be()
  4416   4454   ** set the return value of the application-defined function to be
  4417   4455   ** a text string which is represented as UTF-8, UTF-16 native byte order,
  4418   4456   ** UTF-16 little endian, or UTF-16 big endian, respectively.
         4457  +** ^The sqlite3_result_text64() interface sets the return value of an
         4458  +** application-defined function to be a text string in an encoding
         4459  +** specified by the fifth (and last) parameter, which must be one
         4460  +** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
  4419   4461   ** ^SQLite takes the text result from the application from
  4420   4462   ** the 2nd parameter of the sqlite3_result_text* interfaces.
  4421   4463   ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4422   4464   ** is negative, then SQLite takes result text from the 2nd parameter
  4423   4465   ** through the first zero character.
  4424   4466   ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4425   4467   ** is non-negative, then as many bytes (not characters) of the text
................................................................................
  4455   4497   ** kind of [sqlite3_value] object can be used with this interface.
  4456   4498   **
  4457   4499   ** If these routines are called from within the different thread
  4458   4500   ** than the one containing the application-defined function that received
  4459   4501   ** the [sqlite3_context] pointer, the results are undefined.
  4460   4502   */
  4461   4503   void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
         4504  +void sqlite3_result_blob64(sqlite3_context*,const void*,sqlite3_uint64,void(*)(void*));
  4462   4505   void sqlite3_result_double(sqlite3_context*, double);
  4463   4506   void sqlite3_result_error(sqlite3_context*, const char*, int);
  4464   4507   void sqlite3_result_error16(sqlite3_context*, const void*, int);
  4465   4508   void sqlite3_result_error_toobig(sqlite3_context*);
  4466   4509   void sqlite3_result_error_nomem(sqlite3_context*);
  4467   4510   void sqlite3_result_error_code(sqlite3_context*, int);
  4468   4511   void sqlite3_result_int(sqlite3_context*, int);
  4469   4512   void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
  4470   4513   void sqlite3_result_null(sqlite3_context*);
  4471   4514   void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
         4515  +void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
         4516  +                           void(*)(void*), unsigned char encoding);
  4472   4517   void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
  4473   4518   void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
  4474   4519   void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
  4475   4520   void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
  4476   4521   void sqlite3_result_zeroblob(sqlite3_context*, int n);
  4477   4522   
  4478   4523   /*
................................................................................
  6363   6408   ** <dd>This parameter returns the number malloc attempts that might have
  6364   6409   ** been satisfied using lookaside memory but failed due to all lookaside
  6365   6410   ** memory already being in use.
  6366   6411   ** Only the high-water value is meaningful;
  6367   6412   ** the current value is always zero.)^
  6368   6413   **
  6369   6414   ** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
  6370         -** <dd>This parameter returns the approximate number of of bytes of heap
         6415  +** <dd>This parameter returns the approximate number of bytes of heap
  6371   6416   ** memory used by all pager caches associated with the database connection.)^
  6372   6417   ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
  6373   6418   **
  6374   6419   ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
  6375         -** <dd>This parameter returns the approximate number of of bytes of heap
         6420  +** <dd>This parameter returns the approximate number of bytes of heap
  6376   6421   ** memory used to store the schema for all databases associated
  6377   6422   ** with the connection - main, temp, and any [ATTACH]-ed databases.)^ 
  6378   6423   ** ^The full amount of memory used by the schemas is reported, even if the
  6379   6424   ** schema memory is shared with other database connections due to
  6380   6425   ** [shared cache mode] being enabled.
  6381   6426   ** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
  6382   6427   **
  6383   6428   ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
  6384         -** <dd>This parameter returns the approximate number of of bytes of heap
         6429  +** <dd>This parameter returns the approximate number of bytes of heap
  6385   6430   ** and lookaside memory used by all prepared statements associated with
  6386   6431   ** the database connection.)^
  6387   6432   ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
  6388   6433   ** </dd>
  6389   6434   **
  6390   6435   ** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
  6391   6436   ** <dd>This parameter returns the number of pager cache hits that have

Changes to src/sqlite3ext.h.

    24     24   /*
    25     25   ** The following structure holds pointers to all of the SQLite API
    26     26   ** routines.
    27     27   **
    28     28   ** WARNING:  In order to maintain backwards compatibility, add new
    29     29   ** interfaces to the end of this structure only.  If you insert new
    30     30   ** interfaces in the middle of this structure, then older different
    31         -** versions of SQLite will not be able to load each others' shared
           31  +** versions of SQLite will not be able to load each other's shared
    32     32   ** libraries!
    33     33   */
    34     34   struct sqlite3_api_routines {
    35     35     void * (*aggregate_context)(sqlite3_context*,int nBytes);
    36     36     int  (*aggregate_count)(sqlite3_context*);
    37     37     int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
    38     38     int  (*bind_double)(sqlite3_stmt*,int,double);
................................................................................
   246    246     int (*stmt_readonly)(sqlite3_stmt*);
   247    247     int (*stricmp)(const char*,const char*);
   248    248     int (*uri_boolean)(const char*,const char*,int);
   249    249     sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
   250    250     const char *(*uri_parameter)(const char*,const char*);
   251    251     char *(*vsnprintf)(int,char*,const char*,va_list);
   252    252     int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
          253  +  /* Version 3.8.7 and later */
          254  +  int (*auto_extension)(void(*)(void));
          255  +  int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
          256  +                     void(*)(void*));
          257  +  int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
          258  +                      void(*)(void*),unsigned char);
          259  +  int (*cancel_auto_extension)(void(*)(void));
          260  +  int (*load_extension)(sqlite3*,const char*,const char*,char**);
          261  +  void *(*malloc64)(sqlite3_uint64);
          262  +  sqlite3_uint64 (*msize)(void*);
          263  +  void *(*realloc64)(void*,sqlite3_uint64);
          264  +  void (*reset_auto_extension)(void);
          265  +  void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
          266  +                        void(*)(void*));
          267  +  void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
          268  +                         void(*)(void*), unsigned char);
          269  +  int (*strglob)(const char*,const char*);
   253    270   };
   254    271   
   255    272   /*
   256    273   ** The following macros redefine the API routines so that they are
   257         -** redirected throught the global sqlite3_api structure.
          274  +** redirected through the global sqlite3_api structure.
   258    275   **
   259    276   ** This header file is also used by the loadext.c source file
   260    277   ** (part of the main SQLite library - not an extension) so that
   261    278   ** it can get access to the sqlite3_api_routines structure
   262    279   ** definition.  But the main library does not want to redefine
   263    280   ** the API.  So the redefinition macros are only valid if the
   264    281   ** SQLITE_CORE macros is undefined.
................................................................................
   463    480   #define sqlite3_stmt_readonly          sqlite3_api->stmt_readonly
   464    481   #define sqlite3_stricmp                sqlite3_api->stricmp
   465    482   #define sqlite3_uri_boolean            sqlite3_api->uri_boolean
   466    483   #define sqlite3_uri_int64              sqlite3_api->uri_int64
   467    484   #define sqlite3_uri_parameter          sqlite3_api->uri_parameter
   468    485   #define sqlite3_uri_vsnprintf          sqlite3_api->vsnprintf
   469    486   #define sqlite3_wal_checkpoint_v2      sqlite3_api->wal_checkpoint_v2
          487  +/* Version 3.8.7 and later */
          488  +#define sqlite3_auto_extension         sqlite3_api->auto_extension
          489  +#define sqlite3_bind_blob64            sqlite3_api->bind_blob64
          490  +#define sqlite3_bind_text64            sqlite3_api->bind_text64
          491  +#define sqlite3_cancel_auto_extension  sqlite3_api->cancel_auto_extension
          492  +#define sqlite3_load_extension         sqlite3_api->load_extension
          493  +#define sqlite3_malloc64               sqlite3_api->malloc64
          494  +#define sqlite3_msize                  sqlite3_api->msize
          495  +#define sqlite3_realloc64              sqlite3_api->realloc64
          496  +#define sqlite3_reset_auto_extension   sqlite3_api->reset_auto_extension
          497  +#define sqlite3_result_blob64          sqlite3_api->result_blob64
          498  +#define sqlite3_result_text64          sqlite3_api->result_text64
          499  +#define sqlite3_strglob                sqlite3_api->strglob
   470    500   #endif /* SQLITE_CORE */
   471    501   
   472    502   #ifndef SQLITE_CORE
   473    503     /* This case when the file really is being compiled as a loadable 
   474    504     ** extension */
   475    505   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
   476    506   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;

Changes to src/sqliteInt.h.

    43     43   # define _LARGE_FILE       1
    44     44   # ifndef _FILE_OFFSET_BITS
    45     45   #   define _FILE_OFFSET_BITS 64
    46     46   # endif
    47     47   # define _LARGEFILE_SOURCE 1
    48     48   #endif
    49     49   
           50  +/* Needed for various definitions... */
           51  +#if defined(__GNUC__) && !defined(_GNU_SOURCE)
           52  +# define _GNU_SOURCE
           53  +#endif
           54  +
           55  +#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
           56  +# define _BSD_SOURCE
           57  +#endif
           58  +
    50     59   /*
    51     60   ** For MinGW, check to see if we can include the header file containing its
    52     61   ** version information, among other things.  Normally, this internal MinGW
    53     62   ** header file would [only] be included automatically by other MinGW header
    54     63   ** files; however, the contained version information is now required by this
    55     64   ** header file to work around binary compatibility issues (see below) and
    56     65   ** this is the only known way to reliably obtain it.  This entire #if block
................................................................................
   100    109   #pragma warn -rch /* unreachable code */
   101    110   #pragma warn -ccc /* Condition is always true or false */
   102    111   #pragma warn -aus /* Assigned value is never used */
   103    112   #pragma warn -csu /* Comparing signed and unsigned */
   104    113   #pragma warn -spa /* Suspicious pointer arithmetic */
   105    114   #endif
   106    115   
   107         -/* Needed for various definitions... */
   108         -#ifndef _GNU_SOURCE
   109         -# define _GNU_SOURCE
   110         -#endif
   111         -
   112         -#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
   113         -# define _BSD_SOURCE
   114         -#endif
   115         -
   116    116   /*
   117    117   ** Include standard header files as necessary
   118    118   */
   119    119   #ifdef HAVE_STDINT_H
   120    120   #include <stdint.h>
   121    121   #endif
   122    122   #ifdef HAVE_INTTYPES_H
................................................................................
   347    347   # define NEVER(X)       ((X)?(assert(0),1):0)
   348    348   #else
   349    349   # define ALWAYS(X)      (X)
   350    350   # define NEVER(X)       (X)
   351    351   #endif
   352    352   
   353    353   /*
   354         -** Return true (non-zero) if the input is a integer that is too large
          354  +** Return true (non-zero) if the input is an integer that is too large
   355    355   ** to fit in 32-bits.  This macro is used inside of various testcase()
   356    356   ** macros to verify that we have tested SQLite for large-file support.
   357    357   */
   358    358   #define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
   359    359   
   360    360   /*
   361    361   ** The macro unlikely() is a hint that surrounds a boolean
................................................................................
   635    635   
   636    636   /*
   637    637   ** Assert that the pointer X is aligned to an 8-byte boundary.  This
   638    638   ** macro is used only within assert() to verify that the code gets
   639    639   ** all alignment restrictions correct.
   640    640   **
   641    641   ** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
   642         -** underlying malloc() implemention might return us 4-byte aligned
          642  +** underlying malloc() implementation might return us 4-byte aligned
   643    643   ** pointers.  In that case, only verify 4-byte alignment.
   644    644   */
   645    645   #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
   646    646   # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
   647    647   #else
   648    648   # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
   649    649   #endif
................................................................................
   702    702   # define SQLITE_ENABLE_STAT3_OR_STAT4 1
   703    703   #elif SQLITE_ENABLE_STAT3
   704    704   # define SQLITE_ENABLE_STAT3_OR_STAT4 1
   705    705   #elif SQLITE_ENABLE_STAT3_OR_STAT4
   706    706   # undef SQLITE_ENABLE_STAT3_OR_STAT4
   707    707   #endif
   708    708   
          709  +/*
          710  +** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
          711  +** the Select query generator tracing logic is turned on.
          712  +*/
          713  +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_SELECTTRACE)
          714  +# define SELECTTRACE_ENABLED 1
          715  +#else
          716  +# define SELECTTRACE_ENABLED 0
          717  +#endif
          718  +
   709    719   /*
   710    720   ** An instance of the following structure is used to store the busy-handler
   711    721   ** callback for a given sqlite handle. 
   712    722   **
   713    723   ** The sqlite.busyHandler member of the sqlite struct contains the busy
   714    724   ** callback for the database handle. Each pager opened via the sqlite
   715    725   ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
................................................................................
   993   1003   **
   994   1004   ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
   995   1005   ** Collisions are on the FuncDef.pHash chain.
   996   1006   */
   997   1007   struct FuncDefHash {
   998   1008     FuncDef *a[23];       /* Hash table for functions */
   999   1009   };
         1010  +
         1011  +#ifdef SQLITE_USER_AUTHENTICATION
         1012  +/*
         1013  +** Information held in the "sqlite3" database connection object and used
         1014  +** to manage user authentication.
         1015  +*/
         1016  +typedef struct sqlite3_userauth sqlite3_userauth;
         1017  +struct sqlite3_userauth {
         1018  +  u8 authLevel;                 /* Current authentication level */
         1019  +  int nAuthPW;                  /* Size of the zAuthPW in bytes */
         1020  +  char *zAuthPW;                /* Password used to authenticate */
         1021  +  char *zAuthUser;              /* User name used to authenticate */
         1022  +};
         1023  +
         1024  +/* Allowed values for sqlite3_userauth.authLevel */
         1025  +#define UAUTH_Unknown     0     /* Authentication not yet checked */
         1026  +#define UAUTH_Fail        1     /* User authentication failed */
         1027  +#define UAUTH_User        2     /* Authenticated as a normal user */
         1028  +#define UAUTH_Admin       3     /* Authenticated as an administrator */
         1029  +
         1030  +/* Functions used only by user authorization logic */
         1031  +int sqlite3UserAuthTable(const char*);
         1032  +int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
         1033  +void sqlite3UserAuthInit(sqlite3*);
         1034  +void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
         1035  +
         1036  +#endif /* SQLITE_USER_AUTHENTICATION */
         1037  +
         1038  +/*
         1039  +** typedef for the authorization callback function.
         1040  +*/
         1041  +#ifdef SQLITE_USER_AUTHENTICATION
         1042  +  typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
         1043  +                               const char*, const char*);
         1044  +#else
         1045  +  typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
         1046  +                               const char*);
         1047  +#endif
         1048  +
  1000   1049   
  1001   1050   /*
  1002   1051   ** Each database connection is an instance of the following structure.
  1003   1052   */
  1004   1053   struct sqlite3 {
  1005   1054     sqlite3_vfs *pVfs;            /* OS Interface */
  1006   1055     struct Vdbe *pVdbe;           /* List of active virtual machines */
................................................................................
  1061   1110     sqlite3_value *pErr;          /* Most recent error message */
  1062   1111     union {
  1063   1112       volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
  1064   1113       double notUsed1;            /* Spacer */
  1065   1114     } u1;
  1066   1115     Lookaside lookaside;          /* Lookaside malloc configuration */
  1067   1116   #ifndef SQLITE_OMIT_AUTHORIZATION
  1068         -  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
  1069         -                                /* Access authorization function */
         1117  +  sqlite3_xauth xAuth;          /* Access authorization function */
  1070   1118     void *pAuthArg;               /* 1st argument to the access auth function */
  1071   1119   #endif
  1072   1120   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  1073   1121     int (*xProgress)(void *);     /* The progress callback */
  1074   1122     void *pProgressArg;           /* Argument to the progress callback */
  1075   1123     unsigned nProgressOps;        /* Number of opcodes for progress callback */
  1076   1124   #endif
................................................................................
  1088   1136     Savepoint *pSavepoint;        /* List of active savepoints */
  1089   1137     int busyTimeout;              /* Busy handler timeout, in msec */
  1090   1138     int nSavepoint;               /* Number of non-transaction savepoints */
  1091   1139     int nStatement;               /* Number of nested statement-transactions  */
  1092   1140     i64 nDeferredCons;            /* Net deferred constraints this transaction. */
  1093   1141     i64 nDeferredImmCons;         /* Net deferred immediate constraints */
  1094   1142     int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
  1095         -
  1096   1143   #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  1097   1144     /* The following variables are all protected by the STATIC_MASTER 
  1098   1145     ** mutex, not by sqlite3.mutex. They are used by code in notify.c. 
  1099   1146     **
  1100   1147     ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
  1101   1148     ** unlock so that it can proceed.
  1102   1149     **
................................................................................
  1106   1153     */
  1107   1154     sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
  1108   1155     sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
  1109   1156     void *pUnlockArg;                     /* Argument to xUnlockNotify */
  1110   1157     void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
  1111   1158     sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
  1112   1159   #endif
         1160  +#ifdef SQLITE_USER_AUTHENTICATION
         1161  +  sqlite3_userauth auth;        /* User authentication information */
         1162  +#endif
  1113   1163   };
  1114   1164   
  1115   1165   /*
  1116   1166   ** A macro to discover the encoding of a database.
  1117   1167   */
  1118   1168   #define ENC(db) ((db)->aDb[0].pSchema->enc)
  1119   1169   
................................................................................
  1165   1215   #define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
  1166   1216   #define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
  1167   1217   #define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
  1168   1218   #define SQLITE_SubqCoroutine  0x0100   /* Evaluate subqueries as coroutines */
  1169   1219   #define SQLITE_Transitive     0x0200   /* Transitive constraints */
  1170   1220   #define SQLITE_OmitNoopJoin   0x0400   /* Omit unused tables in joins */
  1171   1221   #define SQLITE_Stat3          0x0800   /* Use the SQLITE_STAT3 table */
  1172         -#define SQLITE_AdjustOutEst   0x1000   /* Adjust output estimates using WHERE */
  1173   1222   #define SQLITE_AllOpts        0xffff   /* All optimizations */
  1174   1223   
  1175   1224   /*
  1176   1225   ** Macros for testing whether or not optimizations are enabled or disabled.
  1177   1226   */
  1178   1227   #ifndef SQLITE_OMIT_BUILTIN_TEST
  1179   1228   #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
................................................................................
  1252   1301   #define SQLITE_FUNC_NEEDCOLL 0x020 /* sqlite3GetFuncCollSeq() might be called */
  1253   1302   #define SQLITE_FUNC_LENGTH   0x040 /* Built-in length() function */
  1254   1303   #define SQLITE_FUNC_TYPEOF   0x080 /* Built-in typeof() function */
  1255   1304   #define SQLITE_FUNC_COUNT    0x100 /* Built-in count(*) aggregate */
  1256   1305   #define SQLITE_FUNC_COALESCE 0x200 /* Built-in coalesce() or ifnull() */
  1257   1306   #define SQLITE_FUNC_UNLIKELY 0x400 /* Built-in unlikely() function */
  1258   1307   #define SQLITE_FUNC_CONSTANT 0x800 /* Constant inputs give a constant output */
         1308  +#define SQLITE_FUNC_MINMAX  0x1000 /* True for min() and max() aggregates */
  1259   1309   
  1260   1310   /*
  1261   1311   ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
  1262   1312   ** used to create the initializers for the FuncDef structures.
  1263   1313   **
  1264   1314   **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
  1265   1315   **     Used to create a scalar function definition of a function zName 
................................................................................
  1298   1348     {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  1299   1349      pArg, 0, xFunc, 0, 0, #zName, 0, 0}
  1300   1350   #define LIKEFUNC(zName, nArg, arg, flags) \
  1301   1351     {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
  1302   1352      (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
  1303   1353   #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
  1304   1354     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
         1355  +   SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
         1356  +#define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \
         1357  +  {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
  1305   1358      SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
  1306   1359   
  1307   1360   /*
  1308   1361   ** All current savepoints are stored in a linked list starting at
  1309   1362   ** sqlite3.pSavepoint. The first element in the list is the most recently
  1310   1363   ** opened savepoint. Savepoints are added to the list by the vdbe
  1311   1364   ** OP_Savepoint instruction.
................................................................................
  1385   1438   /*
  1386   1439   ** Column affinity types.
  1387   1440   **
  1388   1441   ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
  1389   1442   ** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
  1390   1443   ** the speed a little by numbering the values consecutively.  
  1391   1444   **
  1392         -** But rather than start with 0 or 1, we begin with 'a'.  That way,
         1445  +** But rather than start with 0 or 1, we begin with 'A'.  That way,
  1393   1446   ** when multiple affinity types are concatenated into a string and
  1394   1447   ** used as the P4 operand, they will be more readable.
  1395   1448   **
  1396   1449   ** Note also that the numeric types are grouped together so that testing
  1397         -** for a numeric type is a single comparison.
         1450  +** for a numeric type is a single comparison.  And the NONE type is first.
  1398   1451   */
  1399         -#define SQLITE_AFF_TEXT     'a'
  1400         -#define SQLITE_AFF_NONE     'b'
  1401         -#define SQLITE_AFF_NUMERIC  'c'
  1402         -#define SQLITE_AFF_INTEGER  'd'
  1403         -#define SQLITE_AFF_REAL     'e'
         1452  +#define SQLITE_AFF_NONE     'A'
         1453  +#define SQLITE_AFF_TEXT     'B'
         1454  +#define SQLITE_AFF_NUMERIC  'C'
         1455  +#define SQLITE_AFF_INTEGER  'D'
         1456  +#define SQLITE_AFF_REAL     'E'
  1404   1457   
  1405   1458   #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
  1406   1459   
  1407   1460   /*
  1408   1461   ** The SQLITE_AFF_MASK values masks off the significant bits of an
  1409   1462   ** affinity value. 
  1410   1463   */
  1411         -#define SQLITE_AFF_MASK     0x67
         1464  +#define SQLITE_AFF_MASK     0x47
  1412   1465   
  1413   1466   /*
  1414   1467   ** Additional bit values that can be ORed with an affinity without
  1415   1468   ** changing the affinity.
  1416   1469   **
  1417   1470   ** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
  1418   1471   ** It causes an assert() to fire if either operand to a comparison
  1419   1472   ** operator is NULL.  It is added to certain comparison operators to
  1420   1473   ** prove that the operands are always NOT NULL.
  1421   1474   */
  1422         -#define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
  1423         -#define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
         1475  +#define SQLITE_JUMPIFNULL   0x10  /* jumps if either operand is NULL */
         1476  +#define SQLITE_STOREP2      0x20  /* Store result in reg[P2] rather than jump */
  1424   1477   #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
  1425         -#define SQLITE_NOTNULL      0x88  /* Assert that operands are never NULL */
         1478  +#define SQLITE_NOTNULL      0x90  /* Assert that operands are never NULL */
  1426   1479   
  1427   1480   /*
  1428   1481   ** An object of this type is created for each virtual table present in
  1429   1482   ** the database schema. 
  1430   1483   **
  1431   1484   ** If the database schema is shared, then there is one instance of this
  1432   1485   ** structure for each database connection (sqlite3*) that uses the shared
................................................................................
  2221   2274     Parse *pParse;       /* The parser */
  2222   2275     SrcList *pSrcList;   /* One or more tables used to resolve names */
  2223   2276     ExprList *pEList;    /* Optional list of result-set columns */
  2224   2277     AggInfo *pAggInfo;   /* Information about aggregates at this level */
  2225   2278     NameContext *pNext;  /* Next outer name context.  NULL for outermost */
  2226   2279     int nRef;            /* Number of names resolved by this context */
  2227   2280     int nErr;            /* Number of errors encountered while resolving names */
  2228         -  u8 ncFlags;          /* Zero or more NC_* flags defined below */
         2281  +  u16 ncFlags;         /* Zero or more NC_* flags defined below */
  2229   2282   };
  2230   2283   
  2231   2284   /*
  2232   2285   ** Allowed values for the NameContext, ncFlags field.
         2286  +**
         2287  +** Note:  NC_MinMaxAgg must have the same value as SF_MinMaxAgg and
         2288  +** SQLITE_FUNC_MINMAX.
         2289  +** 
  2233   2290   */
  2234         -#define NC_AllowAgg  0x01    /* Aggregate functions are allowed here */
  2235         -#define NC_HasAgg    0x02    /* One or more aggregate functions seen */
  2236         -#define NC_IsCheck   0x04    /* True if resolving names in a CHECK constraint */
  2237         -#define NC_InAggFunc 0x08    /* True if analyzing arguments to an agg func */
  2238         -#define NC_PartIdx   0x10    /* True if resolving a partial index WHERE */
         2291  +#define NC_AllowAgg  0x0001  /* Aggregate functions are allowed here */
         2292  +#define NC_HasAgg    0x0002  /* One or more aggregate functions seen */
         2293  +#define NC_IsCheck   0x0004  /* True if resolving names in a CHECK constraint */
         2294  +#define NC_InAggFunc 0x0008  /* True if analyzing arguments to an agg func */
         2295  +#define NC_PartIdx   0x0010  /* True if resolving a partial index WHERE */
         2296  +#define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
  2239   2297   
  2240   2298   /*
  2241   2299   ** An instance of the following structure contains all information
  2242   2300   ** needed to generate code for a single SELECT statement.
  2243   2301   **
  2244   2302   ** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
  2245   2303   ** If there is a LIMIT clause, the parser sets nLimit to the value of the
................................................................................
  2258   2316   ** sequences for the ORDER BY clause.
  2259   2317   */
  2260   2318   struct Select {
  2261   2319     ExprList *pEList;      /* The fields of the result */
  2262   2320     u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
  2263   2321     u16 selFlags;          /* Various SF_* values */
  2264   2322     int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
         2323  +#if SELECTTRACE_ENABLED
         2324  +  char zSelName[12];     /* Symbolic name of this SELECT use for debugging */
         2325  +#endif
  2265   2326     int addrOpenEphm[2];   /* OP_OpenEphem opcodes related to this select */
  2266   2327     u64 nSelectRow;        /* Estimated number of result rows */
  2267   2328     SrcList *pSrc;         /* The FROM clause */
  2268   2329     Expr *pWhere;          /* The WHERE clause */
  2269   2330     ExprList *pGroupBy;    /* The GROUP BY clause */
  2270   2331     Expr *pHaving;         /* The HAVING clause */
  2271   2332     ExprList *pOrderBy;    /* The ORDER BY clause */
................................................................................
  2282   2343   */
  2283   2344   #define SF_Distinct        0x0001  /* Output should be DISTINCT */
  2284   2345   #define SF_Resolved        0x0002  /* Identifiers have been resolved */
  2285   2346   #define SF_Aggregate       0x0004  /* Contains aggregate functions */
  2286   2347   #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
  2287   2348   #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
  2288   2349   #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
  2289         -                    /*     0x0040  NOT USED */
         2350  +#define SF_Compound        0x0040  /* Part of a compound query */
  2290   2351   #define SF_Values          0x0080  /* Synthesized from VALUES clause */
  2291   2352                       /*     0x0100  NOT USED */
  2292   2353   #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
  2293   2354   #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
  2294   2355   #define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
  2295         -#define SF_Compound        0x1000  /* Part of a compound query */
         2356  +#define SF_MinMaxAgg       0x1000  /* Aggregate containing min() or max() */
  2296   2357   
  2297   2358   
  2298   2359   /*
  2299   2360   ** The results of a SELECT can be distributed in several ways, as defined
  2300   2361   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
  2301   2362   ** Type".
  2302   2363   **
................................................................................
  2516   2577     Token constraintName;/* Name of the constraint currently being parsed */
  2517   2578     yDbMask writeMask;   /* Start a write transaction on these databases */
  2518   2579     yDbMask cookieMask;  /* Bitmask of schema verified databases */
  2519   2580     int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
  2520   2581     int regRowid;        /* Register holding rowid of CREATE TABLE entry */
  2521   2582     int regRoot;         /* Register holding root page number for new objects */
  2522   2583     int nMaxArg;         /* Max args passed to user function by sub-program */
         2584  +#if SELECTTRACE_ENABLED
         2585  +  int nSelect;         /* Number of SELECT statements seen */
         2586  +  int nSelectIndent;   /* How far to indent SELECTTRACE() output */
         2587  +#endif
  2523   2588   #ifndef SQLITE_OMIT_SHARED_CACHE
  2524   2589     int nTableLock;        /* Number of locks in aTableLock */
  2525   2590     TableLock *aTableLock; /* Required table locks for shared-cache mode */
  2526   2591   #endif
  2527   2592     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
  2528   2593   
  2529   2594     /* Information used while coding trigger programs. */
................................................................................
  2890   2955   #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
  2891   2956   #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
  2892   2957   #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
  2893   2958   
  2894   2959   
  2895   2960   /*
  2896   2961   ** FTS4 is really an extension for FTS3.  It is enabled using the
  2897         -** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
  2898         -** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
         2962  +** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also call
         2963  +** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
  2899   2964   */
  2900   2965   #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
  2901   2966   # define SQLITE_ENABLE_FTS3
  2902   2967   #endif
  2903   2968   
  2904   2969   /*
  2905   2970   ** The ctype.h header is needed for non-ASCII systems.  It is also
................................................................................
  2938   3003   */
  2939   3004   #define sqlite3StrICmp sqlite3_stricmp
  2940   3005   int sqlite3Strlen30(const char*);
  2941   3006   #define sqlite3StrNICmp sqlite3_strnicmp
  2942   3007   
  2943   3008   int sqlite3MallocInit(void);
  2944   3009   void sqlite3MallocEnd(void);
  2945         -void *sqlite3Malloc(int);
  2946         -void *sqlite3MallocZero(int);
  2947         -void *sqlite3DbMallocZero(sqlite3*, int);
  2948         -void *sqlite3DbMallocRaw(sqlite3*, int);
         3010  +void *sqlite3Malloc(u64);
         3011  +void *sqlite3MallocZero(u64);
         3012  +void *sqlite3DbMallocZero(sqlite3*, u64);
         3013  +void *sqlite3DbMallocRaw(sqlite3*, u64);
  2949   3014   char *sqlite3DbStrDup(sqlite3*,const char*);
  2950         -char *sqlite3DbStrNDup(sqlite3*,const char*, int);
  2951         -void *sqlite3Realloc(void*, int);
  2952         -void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
  2953         -void *sqlite3DbRealloc(sqlite3 *, void *, int);
         3015  +char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
         3016  +void *sqlite3Realloc(void*, u64);
         3017  +void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
         3018  +void *sqlite3DbRealloc(sqlite3 *, void *, u64);
  2954   3019   void sqlite3DbFree(sqlite3*, void*);
  2955   3020   int sqlite3MallocSize(void*);
  2956   3021   int sqlite3DbMallocSize(sqlite3*, void*);
  2957   3022   void *sqlite3ScratchMalloc(int);
  2958   3023   void sqlite3ScratchFree(void*);
  2959   3024   void *sqlite3PageMalloc(int);
  2960   3025   void sqlite3PageFree(void*);
................................................................................
  3250   3315   void sqlite3UniqueConstraint(Parse*, int, Index*);
  3251   3316   void sqlite3RowidConstraint(Parse*, int, Table*);
  3252   3317   Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
  3253   3318   ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
  3254   3319   SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
  3255   3320   IdList *sqlite3IdListDup(sqlite3*,IdList*);
  3256   3321   Select *sqlite3SelectDup(sqlite3*,Select*,int);
         3322  +#if SELECTTRACE_ENABLED
         3323  +void sqlite3SelectSetName(Select*,const char*);
         3324  +#else
         3325  +# define sqlite3SelectSetName(A,B)
         3326  +#endif
  3257   3327   void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
  3258   3328   FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
  3259   3329   void sqlite3RegisterBuiltinFunctions(sqlite3*);
  3260   3330   void sqlite3RegisterDateTimeFunctions(void);
  3261   3331   void sqlite3RegisterGlobalFunctions(void);
  3262   3332   int sqlite3SafetyCheckOk(sqlite3*);
  3263   3333   int sqlite3SafetyCheckSickOrOk(sqlite3*);
................................................................................
  3487   3557   void sqlite3Stat4ProbeFree(UnpackedRecord*);
  3488   3558   int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
  3489   3559   #endif
  3490   3560   
  3491   3561   /*
  3492   3562   ** The interface to the LEMON-generated parser
  3493   3563   */
  3494         -void *sqlite3ParserAlloc(void*(*)(size_t));
         3564  +void *sqlite3ParserAlloc(void*(*)(u64));
  3495   3565   void sqlite3ParserFree(void*, void(*)(void*));
  3496   3566   void sqlite3Parser(void*, int, Token, Parse*);
  3497   3567   #ifdef YYTRACKMAXSTACKDEPTH
  3498   3568     int sqlite3ParserStackPeak(void*);
  3499   3569   #endif
  3500   3570   
  3501   3571   void sqlite3AutoLoadExtensions(sqlite3*);

Changes to src/table.c.

    25     25   /*
    26     26   ** This structure is used to pass data from sqlite3_get_table() through
    27     27   ** to the callback function is uses to build the result.
    28     28   */
    29     29   typedef struct TabResult {
    30     30     char **azResult;   /* Accumulated output */
    31     31     char *zErrMsg;     /* Error message text, if an error occurs */
    32         -  int nAlloc;        /* Slots allocated for azResult[] */
    33         -  int nRow;          /* Number of rows in the result */
    34         -  int nColumn;       /* Number of columns in the result */
    35         -  int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
           32  +  u32 nAlloc;        /* Slots allocated for azResult[] */
           33  +  u32 nRow;          /* Number of rows in the result */
           34  +  u32 nColumn;       /* Number of columns in the result */
           35  +  u32 nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
    36     36     int rc;            /* Return code from sqlite3_exec() */
    37     37   } TabResult;
    38     38   
    39     39   /*
    40     40   ** This routine is called once for each row in the result table.  Its job
    41     41   ** is to fill in the TabResult structure appropriately, allocating new
    42     42   ** memory as necessary.
................................................................................
    54     54       need = nCol*2;
    55     55     }else{
    56     56       need = nCol;
    57     57     }
    58     58     if( p->nData + need > p->nAlloc ){
    59     59       char **azNew;
    60     60       p->nAlloc = p->nAlloc*2 + need;
    61         -    azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
           61  +    azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc );
    62     62       if( azNew==0 ) goto malloc_failed;
    63     63       p->azResult = azNew;
    64     64     }
    65     65   
    66     66     /* If this is the first row, then generate an extra row containing
    67     67     ** the names of all columns.
    68     68     */
................................................................................
    69     69     if( p->nRow==0 ){
    70     70       p->nColumn = nCol;
    71     71       for(i=0; i<nCol; i++){
    72     72         z = sqlite3_mprintf("%s", colv[i]);
    73     73         if( z==0 ) goto malloc_failed;
    74     74         p->azResult[p->nData++] = z;
    75     75       }
    76         -  }else if( p->nColumn!=nCol ){
           76  +  }else if( (int)p->nColumn!=nCol ){
    77     77       sqlite3_free(p->zErrMsg);
    78     78       p->zErrMsg = sqlite3_mprintf(
    79     79          "sqlite3_get_table() called with two or more incompatible queries"
    80     80       );
    81     81       p->rc = SQLITE_ERROR;
    82     82       return 1;
    83     83     }
................................................................................
   178    178     return rc;
   179    179   }
   180    180   
   181    181   /*
   182    182   ** This routine frees the space the sqlite3_get_table() malloced.
   183    183   */
   184    184   void sqlite3_free_table(
   185         -  char **azResult            /* Result returned from from sqlite3_get_table() */
          185  +  char **azResult            /* Result returned from sqlite3_get_table() */
   186    186   ){
   187    187     if( azResult ){
   188    188       int i, n;
   189    189       azResult--;
   190    190       assert( azResult!=0 );
   191    191       n = SQLITE_PTR_TO_INT(azResult[0]);
   192    192       for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
   193    193       sqlite3_free(azResult);
   194    194     }
   195    195   }
   196    196   
   197    197   #endif /* SQLITE_OMIT_GET_TABLE */

Changes to src/tclsqlite.c.

   756    756       Tcl_IncrRefCount(pCmd);
   757    757       rc = Tcl_EvalObjEx(p->interp, pCmd, 0);
   758    758       Tcl_DecrRefCount(pCmd);
   759    759     }else{
   760    760       /* If there are arguments to the function, make a shallow copy of the
   761    761       ** script object, lappend the arguments, then evaluate the copy.
   762    762       **
   763         -    ** By "shallow" copy, we mean a only the outer list Tcl_Obj is duplicated.
          763  +    ** By "shallow" copy, we mean only the outer list Tcl_Obj is duplicated.
   764    764       ** The new Tcl_Obj contains pointers to the original list elements. 
   765    765       ** That way, when Tcl_EvalObjv() is run and shimmers the first element
   766    766       ** of the list to tclCmdNameType, that alternate representation will
   767    767       ** be preserved and reused on the next invocation.
   768    768       */
   769    769       Tcl_Obj **aArg;
   770    770       int nArg;
................................................................................
   868    868   static int auth_callback(
   869    869     void *pArg,
   870    870     int code,
   871    871     const char *zArg1,
   872    872     const char *zArg2,
   873    873     const char *zArg3,
   874    874     const char *zArg4
          875  +#ifdef SQLITE_USER_AUTHENTICATION
          876  +  ,const char *zArg5
          877  +#endif
   875    878   ){
   876    879     const char *zCode;
   877    880     Tcl_DString str;
   878    881     int rc;
   879    882     const char *zReply;
   880    883     SqliteDb *pDb = (SqliteDb*)pArg;
   881    884     if( pDb->disableAuth ) return SQLITE_OK;
................................................................................
   920    923     Tcl_DStringInit(&str);
   921    924     Tcl_DStringAppend(&str, pDb->zAuth, -1);
   922    925     Tcl_DStringAppendElement(&str, zCode);
   923    926     Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
   924    927     Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
   925    928     Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
   926    929     Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
          930  +#ifdef SQLITE_USER_AUTHENTICATION
          931  +  Tcl_DStringAppendElement(&str, zArg5 ? zArg5 : "");
          932  +#endif  
   927    933     rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str));
   928    934     Tcl_DStringFree(&str);
   929    935     zReply = rc==TCL_OK ? Tcl_GetStringResult(pDb->interp) : "SQLITE_DENY";
   930    936     if( strcmp(zReply,"SQLITE_OK")==0 ){
   931    937       rc = SQLITE_OK;
   932    938     }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
   933    939       rc = SQLITE_DENY;
................................................................................
  1696   1702         if( zAuth && len>0 ){
  1697   1703           pDb->zAuth = Tcl_Alloc( len + 1 );
  1698   1704           memcpy(pDb->zAuth, zAuth, len+1);
  1699   1705         }else{
  1700   1706           pDb->zAuth = 0;
  1701   1707         }
  1702   1708         if( pDb->zAuth ){
         1709  +        typedef int (*sqlite3_auth_cb)(
         1710  +           void*,int,const char*,const char*,
         1711  +           const char*,const char*);
  1703   1712           pDb->interp = interp;
  1704         -        sqlite3_set_authorizer(pDb->db, auth_callback, pDb);
         1713  +        sqlite3_set_authorizer(pDb->db,(sqlite3_auth_cb)auth_callback,pDb);
  1705   1714         }else{
  1706   1715           sqlite3_set_authorizer(pDb->db, 0, 0);
  1707   1716         }
  1708   1717       }
  1709   1718   #endif
  1710   1719       break;
  1711   1720     }

Changes to src/test1.c.

  2601   2601   **   "test_collate <enc> <lhs> <rhs>"
  2602   2602   **
  2603   2603   ** The <lhs> and <rhs> are the two values being compared, encoded in UTF-8.
  2604   2604   ** The <enc> parameter is the encoding of the collation function that
  2605   2605   ** SQLite selected to call. The TCL test script implements the
  2606   2606   ** "test_collate" proc.
  2607   2607   **
  2608         -** Note that this will only work with one intepreter at a time, as the
         2608  +** Note that this will only work with one interpreter at a time, as the
  2609   2609   ** interp pointer to use when evaluating the TCL script is stored in
  2610   2610   ** pTestCollateInterp.
  2611   2611   */
  2612   2612   static Tcl_Interp* pTestCollateInterp;
  2613   2613   static int test_collate_func(
  2614   2614     void *pCtx, 
  2615   2615     int nA, const void *zA,
................................................................................
  3772   3772     return TCL_OK;
  3773   3773   }
  3774   3774   
  3775   3775   /*
  3776   3776   ** Usage: sqlite3_prepare_tkt3134 DB
  3777   3777   **
  3778   3778   ** Generate a prepared statement for a zero-byte string as a test
  3779         -** for ticket #3134.  The string should be preceeded by a zero byte.
         3779  +** for ticket #3134.  The string should be preceded by a zero byte.
  3780   3780   */
  3781   3781   static int test_prepare_tkt3134(
  3782   3782     void * clientData,
  3783   3783     Tcl_Interp *interp,
  3784   3784     int objc,
  3785   3785     Tcl_Obj *CONST objv[]
  3786   3786   ){
................................................................................
  6685   6685     return TCL_OK;
  6686   6686    sql_error:
  6687   6687     Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), 0);
  6688   6688     return TCL_ERROR;
  6689   6689   }
  6690   6690   
  6691   6691   
         6692  +#ifdef SQLITE_USER_AUTHENTICATION
         6693  +#include "sqlite3userauth.h"
         6694  +/*
         6695  +** tclcmd:  sqlite3_user_authenticate DB USERNAME PASSWORD
         6696  +*/
         6697  +static int test_user_authenticate(
         6698  +  ClientData clientData, /* Unused */
         6699  +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
         6700  +  int objc,              /* Number of arguments */
         6701  +  Tcl_Obj *CONST objv[]  /* Command arguments */
         6702  +){
         6703  +  char *zUser = 0;
         6704  +  char *zPasswd = 0;
         6705  +  int nPasswd = 0;
         6706  +  sqlite3 *db;
         6707  +  int rc;
         6708  +
         6709  +  if( objc!=4 ){
         6710  +    Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD");
         6711  +    return TCL_ERROR;
         6712  +  }
         6713  +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
         6714  +    return TCL_ERROR;
         6715  +  }
         6716  +  zUser = Tcl_GetString(objv[2]);
         6717  +  zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
         6718  +  rc = sqlite3_user_authenticate(db, zUser, zPasswd, nPasswd);
         6719  +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
         6720  +  return TCL_OK;
         6721  +}
         6722  +#endif /* SQLITE_USER_AUTHENTICATION */
         6723  +
         6724  +#ifdef SQLITE_USER_AUTHENTICATION
         6725  +/*
         6726  +** tclcmd:  sqlite3_user_add DB USERNAME PASSWORD ISADMIN
         6727  +*/
         6728  +static int test_user_add(
         6729  +  ClientData clientData, /* Unused */
         6730  +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
         6731  +  int objc,              /* Number of arguments */
         6732  +  Tcl_Obj *CONST objv[]  /* Command arguments */
         6733  +){
         6734  +  char *zUser = 0;
         6735  +  char *zPasswd = 0;
         6736  +  int nPasswd = 0;
         6737  +  int isAdmin = 0;
         6738  +  sqlite3 *db;
         6739  +  int rc;
         6740  +
         6741  +  if( objc!=5 ){
         6742  +    Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
         6743  +    return TCL_ERROR;
         6744  +  }
         6745  +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
         6746  +    return TCL_ERROR;
         6747  +  }
         6748  +  zUser = Tcl_GetString(objv[2]);
         6749  +  zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
         6750  +  Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
         6751  +  rc = sqlite3_user_add(db, zUser, zPasswd, nPasswd, isAdmin);
         6752  +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
         6753  +  return TCL_OK;
         6754  +}
         6755  +#endif /* SQLITE_USER_AUTHENTICATION */
         6756  +
         6757  +#ifdef SQLITE_USER_AUTHENTICATION
         6758  +/*
         6759  +** tclcmd:  sqlite3_user_change DB USERNAME PASSWORD ISADMIN
         6760  +*/
         6761  +static int test_user_change(
         6762  +  ClientData clientData, /* Unused */
         6763  +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
         6764  +  int objc,              /* Number of arguments */
         6765  +  Tcl_Obj *CONST objv[]  /* Command arguments */
         6766  +){
         6767  +  char *zUser = 0;
         6768  +  char *zPasswd = 0;
         6769  +  int nPasswd = 0;
         6770  +  int isAdmin = 0;
         6771  +  sqlite3 *db;
         6772  +  int rc;
         6773  +
         6774  +  if( objc!=5 ){
         6775  +    Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN");
         6776  +    return TCL_ERROR;
         6777  +  }
         6778  +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
         6779  +    return TCL_ERROR;
         6780  +  }
         6781  +  zUser = Tcl_GetString(objv[2]);
         6782  +  zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd);
         6783  +  Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin);
         6784  +  rc = sqlite3_user_change(db, zUser, zPasswd, nPasswd, isAdmin);
         6785  +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
         6786  +  return TCL_OK;
         6787  +}
         6788  +#endif /* SQLITE_USER_AUTHENTICATION */
         6789  +
         6790  +#ifdef SQLITE_USER_AUTHENTICATION
         6791  +/*
         6792  +** tclcmd:  sqlite3_user_delete DB USERNAME
         6793  +*/
         6794  +static int test_user_delete(
         6795  +  ClientData clientData, /* Unused */
         6796  +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
         6797  +  int objc,              /* Number of arguments */
         6798  +  Tcl_Obj *CONST objv[]  /* Command arguments */
         6799  +){
         6800  +  char *zUser = 0;
         6801  +  sqlite3 *db;
         6802  +  int rc;
         6803  +
         6804  +  if( objc!=3 ){
         6805  +    Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME");
         6806  +    return TCL_ERROR;
         6807  +  }
         6808  +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){
         6809  +    return TCL_ERROR;
         6810  +  }
         6811  +  zUser = Tcl_GetString(objv[2]);
         6812  +  rc = sqlite3_user_delete(db, zUser);
         6813  +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
         6814  +  return TCL_OK;
         6815  +}
         6816  +#endif /* SQLITE_USER_AUTHENTICATION */
         6817  +
  6692   6818   /*
  6693   6819   ** Register commands with the TCL interpreter.
  6694   6820   */
  6695   6821   int Sqlitetest1_Init(Tcl_Interp *interp){
  6696   6822     extern int sqlite3_search_count;
  6697   6823     extern int sqlite3_found_count;
  6698   6824     extern int sqlite3_interrupt_count;
................................................................................
  6929   7055        { "sqlite3_test_control", test_test_control },
  6930   7056   #if SQLITE_OS_UNIX
  6931   7057        { "getrusage", test_getrusage },
  6932   7058   #endif
  6933   7059        { "load_static_extension", tclLoadStaticExtensionCmd },
  6934   7060        { "sorter_test_fakeheap", sorter_test_fakeheap },
  6935   7061        { "sorter_test_sort4_helper", sorter_test_sort4_helper },
         7062  +#ifdef SQLITE_USER_AUTHENTICATION
         7063  +     { "sqlite3_user_authenticate", test_user_authenticate, 0 },
         7064  +     { "sqlite3_user_add",          test_user_add,          0 },
         7065  +     { "sqlite3_user_change",       test_user_change,       0 },
         7066  +     { "sqlite3_user_delete",       test_user_delete,       0 },
         7067  +#endif
         7068  +
  6936   7069     };
  6937   7070     static int bitmask_size = sizeof(Bitmask)*8;
  6938   7071     int i;
  6939   7072     extern int sqlite3_sync_count, sqlite3_fullsync_count;
  6940   7073     extern int sqlite3_opentemp_count;
  6941   7074     extern int sqlite3_like_count;
  6942   7075     extern int sqlite3_xferopt_count;

Changes to src/test_config.c.

   618    618   #endif
   619    619   
   620    620   #ifdef SQLITE_SECURE_DELETE
   621    621     Tcl_SetVar2(interp, "sqlite_options", "secure_delete", "1", TCL_GLOBAL_ONLY);
   622    622   #else
   623    623     Tcl_SetVar2(interp, "sqlite_options", "secure_delete", "0", TCL_GLOBAL_ONLY);
   624    624   #endif
          625  +
          626  +#ifdef SQLITE_USER_AUTHENTICATION
          627  +  Tcl_SetVar2(interp, "sqlite_options", "userauth", "1", TCL_GLOBAL_ONLY);
          628  +#else
          629  +  Tcl_SetVar2(interp, "sqlite_options", "userauth", "0", TCL_GLOBAL_ONLY);
          630  +#endif
   625    631   
   626    632   #ifdef SQLITE_MULTIPLEX_EXT_OVWR
   627    633     Tcl_SetVar2(interp, "sqlite_options", "multiplex_ext_overwrite", "1", TCL_GLOBAL_ONLY);
   628    634   #else
   629    635     Tcl_SetVar2(interp, "sqlite_options", "multiplex_ext_overwrite", "0", TCL_GLOBAL_ONLY);
   630    636   #endif
   631    637   

Changes to src/test_func.c.

   500    500       pHdr += sqlite3GetVarint(pHdr, &iSerialType);
   501    501       pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem);
   502    502   
   503    503       if( iCurrent==iIdx ){
   504    504         sqlite3_result_value(context, &mem);
   505    505       }
   506    506   
   507         -    sqlite3DbFree(db, mem.zMalloc);
          507  +    if( mem.szMalloc ) sqlite3DbFree(db, mem.zMalloc);
   508    508     }
   509    509   }
   510    510   
   511    511   /*
   512    512   ** tclcmd: test_decode(record)
   513    513   **
   514    514   ** This function implements an SQL user-function that accepts a blob
................................................................................
   587    587   
   588    588         default:
   589    589           assert( 0 );
   590    590       }
   591    591   
   592    592       Tcl_ListObjAppendElement(0, pRet, pVal);
   593    593   
   594         -    if( mem.zMalloc ){
          594  +    if( mem.szMalloc ){
   595    595         sqlite3DbFree(db, mem.zMalloc);
   596    596       }
   597    597     }
   598    598   
   599    599     sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
   600    600     Tcl_DecrRefCount(pRet);
   601    601   }

Changes to src/test_intarray.c.

    33     33     void (*xFree)(void*);     /* Function used to free a[] */
    34     34   };
    35     35   
    36     36   /* Objects used internally by the virtual table implementation */
    37     37   typedef struct intarray_vtab intarray_vtab;
    38     38   typedef struct intarray_cursor intarray_cursor;
    39     39   
    40         -/* A intarray table object */
           40  +/* An intarray table object */
    41     41   struct intarray_vtab {
    42     42     sqlite3_vtab base;            /* Base class */
    43     43     sqlite3_intarray *pContent;   /* Content of the integer array */
    44     44   };
    45     45   
    46         -/* A intarray cursor object */
           46  +/* An intarray cursor object */
    47     47   struct intarray_cursor {
    48     48     sqlite3_vtab_cursor base;    /* Base class */
    49     49     int i;                       /* Current cursor position */
    50     50   };
    51     51   
    52     52   /*
    53     53   ** None of this works unless we have virtual tables.

Changes to src/test_malloc.c.

   692    692       return TCL_ERROR;
   693    693     }
   694    694     nPending = faultsimPending();
   695    695     Tcl_SetObjResult(interp, Tcl_NewIntObj(nPending));
   696    696     return TCL_OK;
   697    697   }
   698    698   
          699  +/*
          700  +** The following global variable keeps track of the number of tests
          701  +** that have run.  This variable is only useful when running in the
          702  +** debugger.
          703  +*/
          704  +static int sqlite3_memdebug_title_count = 0;
   699    705   
   700    706   /*
   701    707   ** Usage:    sqlite3_memdebug_settitle TITLE
   702    708   **
   703    709   ** Set a title string stored with each allocation.  The TITLE is
   704    710   ** typically the name of the test that was running when the
   705    711   ** allocation occurred.  The TITLE is stored with the allocation
................................................................................
   709    715   */
   710    716   static int test_memdebug_settitle(
   711    717     void * clientData,
   712    718     Tcl_Interp *interp,
   713    719     int objc,
   714    720     Tcl_Obj *CONST objv[]
   715    721   ){
          722  +  sqlite3_memdebug_title_count++;
   716    723     if( objc!=2 ){
   717    724       Tcl_WrongNumArgs(interp, 1, objv, "TITLE");
   718    725       return TCL_ERROR;
   719    726     }
   720    727   #ifdef SQLITE_MEMDEBUG
   721    728     {
   722    729       const char *zTitle;
................................................................................
   876    883   }
   877    884   
   878    885   /*
   879    886   ** Usage:    sqlite3_config_scratch SIZE N
   880    887   **
   881    888   ** Set the scratch memory buffer using SQLITE_CONFIG_SCRATCH.
   882    889   ** The buffer is static and is of limited size.  N might be
   883         -** adjusted downward as needed to accomodate the requested size.
          890  +** adjusted downward as needed to accommodate the requested size.
   884    891   ** The revised value of N is returned.
   885    892   **
   886    893   ** A negative SIZE causes the buffer pointer to be NULL.
   887    894   */
   888    895   static int test_config_scratch(
   889    896     void * clientData,
   890    897     Tcl_Interp *interp,
................................................................................
   916    923   }
   917    924   
   918    925   /*
   919    926   ** Usage:    sqlite3_config_pagecache SIZE N
   920    927   **
   921    928   ** Set the page-cache memory buffer using SQLITE_CONFIG_PAGECACHE.
   922    929   ** The buffer is static and is of limited size.  N might be
   923         -** adjusted downward as needed to accomodate the requested size.
          930  +** adjusted downward as needed to accommodate the requested size.
   924    931   ** The revised value of N is returned.
   925    932   **
   926    933   ** A negative SIZE causes the buffer pointer to be NULL.
   927    934   */
   928    935   static int test_config_pagecache(
   929    936     void * clientData,
   930    937     Tcl_Interp *interp,

Changes to src/test_schema.c.

   185    185         while( SQLITE_ROW!=sqlite3_step(pCur->pDbList) ){
   186    186           rc = finalize(&pCur->pDbList);
   187    187           goto next_exit;
   188    188         }
   189    189   
   190    190         /* Set zSql to the SQL to pull the list of tables from the 
   191    191         ** sqlite_master (or sqlite_temp_master) table of the database
   192         -      ** identfied by the row pointed to by the SQL statement pCur->pDbList
          192  +      ** identified by the row pointed to by the SQL statement pCur->pDbList
   193    193         ** (iterating through a "PRAGMA database_list;" statement).
   194    194         */
   195    195         if( sqlite3_column_int(pCur->pDbList, 0)==1 ){
   196    196           zSql = sqlite3_mprintf(
   197    197               "SELECT name FROM sqlite_temp_master WHERE type='table'"
   198    198           );
   199    199         }else{

Changes to src/tokenize.c.

    73     73   ** allowed in an identifier.  For 7-bit characters, 
    74     74   ** sqlite3IsIdChar[X] must be 1.
    75     75   **
    76     76   ** For EBCDIC, the rules are more complex but have the same
    77     77   ** end result.
    78     78   **
    79     79   ** Ticket #1066.  the SQL standard does not allow '$' in the
    80         -** middle of identfiers.  But many SQL implementations do. 
           80  +** middle of identifiers.  But many SQL implementations do. 
    81     81   ** SQLite will allow '$' in identifiers for compatibility.
    82     82   ** But the feature is undocumented.
    83     83   */
    84     84   #ifdef SQLITE_ASCII
    85     85   #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
    86     86   #endif
    87     87   #ifdef SQLITE_EBCDIC
................................................................................
   394    394     if( db->nVdbeActive==0 ){
   395    395       db->u1.isInterrupted = 0;
   396    396     }
   397    397     pParse->rc = SQLITE_OK;
   398    398     pParse->zTail = zSql;
   399    399     i = 0;
   400    400     assert( pzErrMsg!=0 );
   401         -  pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
          401  +  pEngine = sqlite3ParserAlloc(sqlite3Malloc);
   402    402     if( pEngine==0 ){
   403    403       db->mallocFailed = 1;
   404    404       return SQLITE_NOMEM;
   405    405     }
   406    406     assert( pParse->pNewTable==0 );
   407    407     assert( pParse->pNewTrigger==0 );
   408    408     assert( pParse->nVar==0 );

Changes to src/trigger.c.

   123    123   
   124    124     /* A long-standing parser bug is that this syntax was allowed:
   125    125     **
   126    126     **    CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
   127    127     **                                                 ^^^^^^^^
   128    128     **
   129    129     ** To maintain backwards compatibility, ignore the database
   130         -  ** name on pTableName if we are reparsing our of SQLITE_MASTER.
          130  +  ** name on pTableName if we are reparsing out of SQLITE_MASTER.
   131    131     */
   132    132     if( db->init.busy && iDb!=1 ){
   133    133       sqlite3DbFree(db, pTableName->a[0].zDatabase);
   134    134       pTableName->a[0].zDatabase = 0;
   135    135     }
   136    136   
   137    137     /* If the trigger name was unqualified, and the table is a temp table,

Changes to src/update.c.

   323    323   
   324    324     /* Start the view context. */
   325    325     if( isView ){
   326    326       sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
   327    327     }
   328    328   
   329    329     /* If we are trying to update a view, realize that view into
   330         -  ** a ephemeral table.
          330  +  ** an ephemeral table.
   331    331     */
   332    332   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
   333    333     if( isView ){
   334    334       sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur);
   335    335     }
   336    336   #endif
   337    337   
................................................................................
   484    484       }
   485    485       if( chngRowid==0 && pPk==0 ){
   486    486         sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
   487    487       }
   488    488     }
   489    489   
   490    490     /* Populate the array of registers beginning at regNew with the new
   491         -  ** row data. This array is used to check constaints, create the new
          491  +  ** row data. This array is used to check constants, create the new
   492    492     ** table and index records, and as the values for any new.* references
   493    493     ** made by triggers.
   494    494     **
   495    495     ** If there are one or more BEFORE triggers, then do not populate the
   496    496     ** registers associated with columns that are (a) not modified by
   497    497     ** this UPDATE statement and (b) not accessed by new.* references. The
   498    498     ** values for registers not modified by the UPDATE must be reloaded from 
................................................................................
   664    664     sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
   665    665     sqlite3SrcListDelete(db, pTabList);
   666    666     sqlite3ExprListDelete(db, pChanges);
   667    667     sqlite3ExprDelete(db, pWhere);
   668    668     return;
   669    669   }
   670    670   /* Make sure "isView" and other macros defined above are undefined. Otherwise
   671         -** thely may interfere with compilation of other functions in this file
          671  +** they may interfere with compilation of other functions in this file
   672    672   ** (or in another file, if this file becomes part of the amalgamation).  */
   673    673   #ifdef isView
   674    674    #undef isView
   675    675   #endif
   676    676   #ifdef pTrigger
   677    677    #undef pTrigger
   678    678   #endif
   679    679   
   680    680   #ifndef SQLITE_OMIT_VIRTUALTABLE
   681    681   /*
   682    682   ** Generate code for an UPDATE of a virtual table.
   683    683   **
   684         -** The strategy is that we create an ephemerial table that contains
          684  +** The strategy is that we create an ephemeral table that contains
   685    685   ** for each row to be changed:
   686    686   **
   687    687   **   (A)  The original rowid of that row.
   688    688   **   (B)  The revised rowid for the row. (note1)
   689    689   **   (C)  The content of every column in the row.
   690    690   **
   691    691   ** Then we loop over this ephemeral table and for each row in
   692         -** the ephermeral table call VUpdate.
          692  +** the ephemeral table call VUpdate.
   693    693   **
   694    694   ** When finished, drop the ephemeral table.
   695    695   **
   696    696   ** (note1) Actually, if we know in advance that (A) is always the same
   697    697   ** as (B) we only store (A), then duplicate (A) when pulling
   698    698   ** it out of the ephemeral table before calling VUpdate.
   699    699   */

Changes to src/utf.c.

   310    310         }
   311    311       }
   312    312       pMem->n = (int)(z - zOut);
   313    313     }
   314    314     *z = 0;
   315    315     assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
   316    316   
          317  +  c = pMem->flags;
   317    318     sqlite3VdbeMemRelease(pMem);
   318         -  pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
          319  +  pMem->flags = MEM_Str|MEM_Term|(c&MEM_AffMask);
   319    320     pMem->enc = desiredEnc;
   320         -  pMem->flags |= (MEM_Term);
   321    321     pMem->z = (char*)zOut;
   322    322     pMem->zMalloc = pMem->z;
          323  +  pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
   323    324   
   324    325   translate_out:
   325    326   #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   326    327     {
   327    328       char zBuf[100];
   328    329       sqlite3VdbeMemPrettyPrint(pMem, zBuf);
   329    330       fprintf(stderr, "OUTPUT: %s\n", zBuf);

Changes to src/util.c.

   200    200   ** is added to the dequoted string.
   201    201   **
   202    202   ** The return value is -1 if no dequoting occurs or the length of the
   203    203   ** dequoted string, exclusive of the zero terminator, if dequoting does
   204    204   ** occur.
   205    205   **
   206    206   ** 2002-Feb-14: This routine is extended to remove MS-Access style
   207         -** brackets from around identifers.  For example:  "[a-b-c]" becomes
          207  +** brackets from around identifiers.  For example:  "[a-b-c]" becomes
   208    208   ** "a-b-c".
   209    209   */
   210    210   int sqlite3Dequote(char *z){
   211    211     char quote;
   212    212     int i, j;
   213    213     if( z==0 ) return -1;
   214    214     quote = z[0];

Changes to src/vacuum.c.

    83     83   **        original database.
    84     84   **
    85     85   ** The transient database requires temporary disk space approximately
    86     86   ** equal to the size of the original database.  The copy operation of
    87     87   ** step (3) requires additional temporary disk space approximately equal
    88     88   ** to the size of the original database for the rollback journal.
    89     89   ** Hence, temporary disk space that is approximately 2x the size of the
    90         -** orginal database is required.  Every page of the database is written
           90  +** original database is required.  Every page of the database is written
    91     91   ** approximately 3 times:  Once for step (2) and twice for step (3).
    92     92   ** Two writes per page are required in step (3) because the original
    93     93   ** database content must be written into the rollback journal prior to
    94     94   ** overwriting the database with the vacuumed content.
    95     95   **
    96     96   ** Only 1x temporary space and only 1x writes would be required if
    97     97   ** the copy of step (3) were replace by deleting the original database

Changes to src/vdbe.c.

   205    205         (isBtreeCursor?sqlite3BtreeCursorSize():0);
   206    206   
   207    207     assert( iCur<p->nCursor );
   208    208     if( p->apCsr[iCur] ){
   209    209       sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
   210    210       p->apCsr[iCur] = 0;
   211    211     }
   212         -  if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
          212  +  if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
   213    213       p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
   214    214       memset(pCx, 0, sizeof(VdbeCursor));
   215    215       pCx->iDb = iDb;
   216    216       pCx->nField = nField;
   217    217       if( isBtreeCursor ){
   218    218         pCx->pCursor = (BtCursor*)
   219    219             &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
................................................................................
   238    238   ** point or exponential notation, the result is only MEM_Real, even
   239    239   ** if there is an exact integer representation of the quantity.
   240    240   */
   241    241   static void applyNumericAffinity(Mem *pRec, int bTryForInt){
   242    242     double rValue;
   243    243     i64 iValue;
   244    244     u8 enc = pRec->enc;
   245         -  if( (pRec->flags&MEM_Str)==0 ) return;
          245  +  assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
   246    246     if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
   247    247     if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
   248    248       pRec->u.i = iValue;
   249    249       pRec->flags |= MEM_Int;
   250    250     }else{
   251         -    pRec->r = rValue;
          251  +    pRec->u.r = rValue;
   252    252       pRec->flags |= MEM_Real;
   253    253       if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
   254    254     }
   255    255   }
   256    256   
   257    257   /*
   258    258   ** Processing is determine by the affinity parameter:
................................................................................
   273    273   **    No-op.  pRec is unchanged.
   274    274   */
   275    275   static void applyAffinity(
   276    276     Mem *pRec,          /* The value to apply affinity to */
   277    277     char affinity,      /* The affinity to be applied */
   278    278     u8 enc              /* Use this text encoding */
   279    279   ){
   280         -  if( affinity==SQLITE_AFF_TEXT ){
          280  +  if( affinity>=SQLITE_AFF_NUMERIC ){
          281  +    assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
          282  +             || affinity==SQLITE_AFF_NUMERIC );
          283  +    if( (pRec->flags & MEM_Int)==0 ){
          284  +      if( (pRec->flags & MEM_Real)==0 ){
          285  +        if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
          286  +      }else{
          287  +        sqlite3VdbeIntegerAffinity(pRec);
          288  +      }
          289  +    }
          290  +  }else if( affinity==SQLITE_AFF_TEXT ){
   281    291       /* Only attempt the conversion to TEXT if there is an integer or real
   282    292       ** representation (blob and NULL do not get converted) but no string
   283    293       ** representation.
   284    294       */
   285    295       if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
   286    296         sqlite3VdbeMemStringify(pRec, enc, 1);
   287    297       }
   288         -  }else if( affinity!=SQLITE_AFF_NONE ){
   289         -    assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
   290         -             || affinity==SQLITE_AFF_NUMERIC );
   291         -    if( (pRec->flags & MEM_Int)==0 ){
   292         -      if( (pRec->flags & MEM_Real)==0 ){
   293         -        applyNumericAffinity(pRec,1);
   294         -      }else{
   295         -        sqlite3VdbeIntegerAffinity(pRec);
   296         -      }
   297         -    }
   298    298     }
   299    299   }
   300    300   
   301    301   /*
   302    302   ** Try to convert the type of a function argument or a result column
   303    303   ** into a numeric representation.  Use either INTEGER or REAL whichever
   304    304   ** is appropriate.  But only do the conversion if it is possible without
................................................................................
   325    325   ){
   326    326     applyAffinity((Mem *)pVal, affinity, enc);
   327    327   }
   328    328   
   329    329   /*
   330    330   ** pMem currently only holds a string type (or maybe a BLOB that we can
   331    331   ** interpret as a string if we want to).  Compute its corresponding
   332         -** numeric type, if has one.  Set the pMem->r and pMem->u.i fields
          332  +** numeric type, if has one.  Set the pMem->u.r and pMem->u.i fields
   333    333   ** accordingly.
   334    334   */
   335    335   static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
   336    336     assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
   337    337     assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
   338         -  if( sqlite3AtoF(pMem->z, &pMem->r, pMem->n, pMem->enc)==0 ){
          338  +  if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
   339    339       return 0;
   340    340     }
   341    341     if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
   342    342       return MEM_Int;
   343    343     }
   344    344     return MEM_Real;
   345    345   }
   346    346   
   347    347   /*
   348    348   ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
   349    349   ** none.  
   350    350   **
   351    351   ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
   352         -** But it does set pMem->r and pMem->u.i appropriately.
          352  +** But it does set pMem->u.r and pMem->u.i appropriately.
   353    353   */
   354    354   static u16 numericType(Mem *pMem){
   355    355     if( pMem->flags & (MEM_Int|MEM_Real) ){
   356    356       return pMem->flags & (MEM_Int|MEM_Real);
   357    357     }
   358    358     if( pMem->flags & (MEM_Str|MEM_Blob) ){
   359    359       return computeNumericType(pMem);
................................................................................
   455    455       printf(" NULL");
   456    456     }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
   457    457       printf(" si:%lld", p->u.i);
   458    458     }else if( p->flags & MEM_Int ){
   459    459       printf(" i:%lld", p->u.i);
   460    460   #ifndef SQLITE_OMIT_FLOATING_POINT
   461    461     }else if( p->flags & MEM_Real ){
   462         -    printf(" r:%g", p->r);
          462  +    printf(" r:%g", p->u.r);
   463    463   #endif
   464    464     }else if( p->flags & MEM_RowSet ){
   465    465       printf(" (rowset)");
   466    466     }else{
   467    467       char zBuf[200];
   468    468       sqlite3VdbeMemPrettyPrint(p, zBuf);
   469    469       printf(" %s", zBuf);
................................................................................
   636    636       */
   637    637       assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
   638    638       if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
   639    639         assert( pOp->p2>0 );
   640    640         assert( pOp->p2<=(p->nMem-p->nCursor) );
   641    641         pOut = &aMem[pOp->p2];
   642    642         memAboutToChange(p, pOut);
   643         -      VdbeMemReleaseExtern(pOut);
          643  +      if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
   644    644         pOut->flags = MEM_Int;
   645    645       }
   646    646   
   647    647       /* Sanity checking on other operands */
   648    648   #ifdef SQLITE_DEBUG
   649    649       if( (pOp->opflags & OPFLG_IN1)!=0 ){
   650    650         assert( pOp->p1>0 );
................................................................................
   998    998   **
   999    999   ** P4 is a pointer to a 64-bit floating point value.
  1000   1000   ** Write that value into register P2.
  1001   1001   */
  1002   1002   case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
  1003   1003     pOut->flags = MEM_Real;
  1004   1004     assert( !sqlite3IsNaN(*pOp->p4.pReal) );
  1005         -  pOut->r = *pOp->p4.pReal;
         1005  +  pOut->u.r = *pOp->p4.pReal;
  1006   1006     break;
  1007   1007   }
  1008   1008   #endif
  1009   1009   
  1010   1010   /* Opcode: String8 * P2 * P4 *
  1011   1011   ** Synopsis: r[P2]='P4'
  1012   1012   **
................................................................................
  1021   1021     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
  1022   1022   
  1023   1023   #ifndef SQLITE_OMIT_UTF16
  1024   1024     if( encoding!=SQLITE_UTF8 ){
  1025   1025       rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
  1026   1026       if( rc==SQLITE_TOOBIG ) goto too_big;
  1027   1027       if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
  1028         -    assert( pOut->zMalloc==pOut->z );
         1028  +    assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
  1029   1029       assert( VdbeMemDynamic(pOut)==0 );
  1030         -    pOut->zMalloc = 0;
         1030  +    pOut->szMalloc = 0;
  1031   1031       pOut->flags |= MEM_Static;
  1032   1032       if( pOp->p4type==P4_DYNAMIC ){
  1033   1033         sqlite3DbFree(db, pOp->p4.z);
  1034   1034       }
  1035   1035       pOp->p4type = P4_DYNAMIC;
  1036   1036       pOp->p4.z = pOut->z;
  1037   1037       pOp->p1 = pOut->n;
................................................................................
  1075   1075     u16 nullFlag;
  1076   1076     cnt = pOp->p3-pOp->p2;
  1077   1077     assert( pOp->p3<=(p->nMem-p->nCursor) );
  1078   1078     pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
  1079   1079     while( cnt>0 ){
  1080   1080       pOut++;
  1081   1081       memAboutToChange(p, pOut);
  1082         -    VdbeMemReleaseExtern(pOut);
         1082  +    sqlite3VdbeMemSetNull(pOut);
  1083   1083       pOut->flags = nullFlag;
  1084   1084       cnt--;
  1085   1085     }
  1086   1086     break;
  1087   1087   }
  1088   1088   
  1089   1089   /* Opcode: SoftNull P1 * * * *
................................................................................
  1143   1143   ** Move the P3 values in register P1..P1+P3-1 over into
  1144   1144   ** registers P2..P2+P3-1.  Registers P1..P1+P3-1 are
  1145   1145   ** left holding a NULL.  It is an error for register ranges
  1146   1146   ** P1..P1+P3-1 and P2..P2+P3-1 to overlap.  It is an error
  1147   1147   ** for P3 to be less than 1.
  1148   1148   */
  1149   1149   case OP_Move: {
  1150         -  char *zMalloc;   /* Holding variable for allocated memory */
  1151   1150     int n;           /* Number of registers left to copy */
  1152   1151     int p1;          /* Register to copy from */
  1153   1152     int p2;          /* Register to copy to */
  1154   1153   
  1155   1154     n = pOp->p3;
  1156   1155     p1 = pOp->p1;
  1157   1156     p2 = pOp->p2;
................................................................................
  1161   1160     pIn1 = &aMem[p1];
  1162   1161     pOut = &aMem[p2];
  1163   1162     do{
  1164   1163       assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
  1165   1164       assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
  1166   1165       assert( memIsValid(pIn1) );
  1167   1166       memAboutToChange(p, pOut);
  1168         -    sqlite3VdbeMemRelease(pOut);
  1169         -    zMalloc = pOut->zMalloc;
  1170         -    memcpy(pOut, pIn1, sizeof(Mem));
         1167  +    sqlite3VdbeMemMove(pOut, pIn1);
  1171   1168   #ifdef SQLITE_DEBUG
  1172   1169       if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
  1173   1170         pOut->pScopyFrom += p1 - pOp->p2;
  1174   1171       }
  1175   1172   #endif
  1176         -    pIn1->flags = MEM_Undefined;
  1177         -    pIn1->xDel = 0;
  1178         -    pIn1->zMalloc = zMalloc;
  1179   1173       REGISTER_TRACE(p2++, pOut);
  1180   1174       pIn1++;
  1181   1175       pOut++;
  1182   1176     }while( --n );
  1183   1177     break;
  1184   1178   }
  1185   1179   
................................................................................
  1476   1470   #ifdef SQLITE_OMIT_FLOATING_POINT
  1477   1471       pOut->u.i = rB;
  1478   1472       MemSetTypeFlag(pOut, MEM_Int);
  1479   1473   #else
  1480   1474       if( sqlite3IsNaN(rB) ){
  1481   1475         goto arithmetic_result_is_null;
  1482   1476       }
  1483         -    pOut->r = rB;
         1477  +    pOut->u.r = rB;
  1484   1478       MemSetTypeFlag(pOut, MEM_Real);
  1485   1479       if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
  1486   1480         sqlite3VdbeIntegerAffinity(pOut);
  1487   1481       }
  1488   1482   #endif
  1489   1483     }
  1490   1484     break;
................................................................................
  1751   1745   ** <li value="100"> INTEGER
  1752   1746   ** <li value="101"> REAL
  1753   1747   ** </ul>
  1754   1748   **
  1755   1749   ** A NULL value is not changed by this routine.  It remains NULL.
  1756   1750   */
  1757   1751   case OP_Cast: {                  /* in1 */
  1758         -  assert( pOp->p2>=SQLITE_AFF_TEXT && pOp->p2<=SQLITE_AFF_REAL );
         1752  +  assert( pOp->p2>=SQLITE_AFF_NONE && pOp->p2<=SQLITE_AFF_REAL );
  1759   1753     testcase( pOp->p2==SQLITE_AFF_TEXT );
  1760   1754     testcase( pOp->p2==SQLITE_AFF_NONE );
  1761   1755     testcase( pOp->p2==SQLITE_AFF_NUMERIC );
  1762   1756     testcase( pOp->p2==SQLITE_AFF_INTEGER );
  1763   1757     testcase( pOp->p2==SQLITE_AFF_REAL );
  1764   1758     pIn1 = &aMem[pOp->p1];
  1765   1759     memAboutToChange(p, pIn1);
................................................................................
  1901   1895           }
  1902   1896         }
  1903   1897         break;
  1904   1898       }
  1905   1899     }else{
  1906   1900       /* Neither operand is NULL.  Do a comparison. */
  1907   1901       affinity = pOp->p5 & SQLITE_AFF_MASK;
  1908         -    if( affinity ){
  1909         -      applyAffinity(pIn1, affinity, encoding);
  1910         -      applyAffinity(pIn3, affinity, encoding);
  1911         -      if( db->mallocFailed ) goto no_mem;
         1902  +    if( affinity>=SQLITE_AFF_NUMERIC ){
         1903  +      if( (pIn1->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
         1904  +        applyNumericAffinity(pIn1,0);
         1905  +      }
         1906  +      if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
         1907  +        applyNumericAffinity(pIn3,0);
         1908  +      }
         1909  +    }else if( affinity==SQLITE_AFF_TEXT ){
         1910  +      if( (pIn1->flags & MEM_Str)==0 && (pIn1->flags & (MEM_Int|MEM_Real))!=0 ){
         1911  +        testcase( pIn1->flags & MEM_Int );
         1912  +        testcase( pIn1->flags & MEM_Real );
         1913  +        sqlite3VdbeMemStringify(pIn1, encoding, 1);
         1914  +      }
         1915  +      if( (pIn3->flags & MEM_Str)==0 && (pIn3->flags & (MEM_Int|MEM_Real))!=0 ){
         1916  +        testcase( pIn3->flags & MEM_Int );
         1917  +        testcase( pIn3->flags & MEM_Real );
         1918  +        sqlite3VdbeMemStringify(pIn3, encoding, 1);
         1919  +      }
  1912   1920       }
  1913         -
  1914   1921       assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
  1915         -    ExpandBlob(pIn1);
  1916         -    ExpandBlob(pIn3);
         1922  +    if( pIn1->flags & MEM_Zero ){
         1923  +      sqlite3VdbeMemExpandBlob(pIn1);
         1924  +      flags1 &= ~MEM_Zero;
         1925  +    }
         1926  +    if( pIn3->flags & MEM_Zero ){
         1927  +      sqlite3VdbeMemExpandBlob(pIn3);
         1928  +      flags3 &= ~MEM_Zero;
         1929  +    }
         1930  +    if( db->mallocFailed ) goto no_mem;
  1917   1931       res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
  1918   1932     }
  1919   1933     switch( pOp->opcode ){
  1920   1934       case OP_Eq:    res = res==0;     break;
  1921   1935       case OP_Ne:    res = res!=0;     break;
  1922   1936       case OP_Lt:    res = res<0;      break;
  1923   1937       case OP_Le:    res = res<=0;     break;
................................................................................
  1934   1948     }else{
  1935   1949       VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
  1936   1950       if( res ){
  1937   1951         pc = pOp->p2-1;
  1938   1952       }
  1939   1953     }
  1940   1954     /* Undo any changes made by applyAffinity() to the input registers. */
  1941         -  pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
  1942         -  pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
         1955  +  pIn1->flags = flags1;
         1956  +  pIn3->flags = flags3;
  1943   1957     break;
  1944   1958   }
  1945   1959   
  1946   1960   /* Opcode: Permutation * * * P4 *
  1947   1961   **
  1948   1962   ** Set the permutation used by the OP_Compare operator to be the array
  1949   1963   ** of integers in P4.
................................................................................
  2103   2117   ** Interpret the value in register P1 as a boolean value.  Store the
  2104   2118   ** boolean complement in register P2.  If the value in register P1 is 
  2105   2119   ** NULL, then a NULL is stored in P2.
  2106   2120   */
  2107   2121   case OP_Not: {                /* same as TK_NOT, in1, out2 */
  2108   2122     pIn1 = &aMem[pOp->p1];
  2109   2123     pOut = &aMem[pOp->p2];
  2110         -  if( pIn1->flags & MEM_Null ){
  2111         -    sqlite3VdbeMemSetNull(pOut);
  2112         -  }else{
  2113         -    sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
         2124  +  sqlite3VdbeMemSetNull(pOut);
         2125  +  if( (pIn1->flags & MEM_Null)==0 ){
         2126  +    pOut->flags = MEM_Int;
         2127  +    pOut->u.i = !sqlite3VdbeIntValue(pIn1);
  2114   2128     }
  2115   2129     break;
  2116   2130   }
  2117   2131   
  2118   2132   /* Opcode: BitNot P1 P2 * * *
  2119   2133   ** Synopsis: r[P1]= ~r[P1]
  2120   2134   **
................................................................................
  2121   2135   ** Interpret the content of register P1 as an integer.  Store the
  2122   2136   ** ones-complement of the P1 value into register P2.  If P1 holds
  2123   2137   ** a NULL then store a NULL in P2.
  2124   2138   */
  2125   2139   case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
  2126   2140     pIn1 = &aMem[pOp->p1];
  2127   2141     pOut = &aMem[pOp->p2];
  2128         -  if( pIn1->flags & MEM_Null ){
  2129         -    sqlite3VdbeMemSetNull(pOut);
  2130         -  }else{
  2131         -    sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
         2142  +  sqlite3VdbeMemSetNull(pOut);
         2143  +  if( (pIn1->flags & MEM_Null)==0 ){
         2144  +    pOut->flags = MEM_Int;
         2145  +    pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
  2132   2146     }
  2133   2147     break;
  2134   2148   }
  2135   2149   
  2136   2150   /* Opcode: Once P1 P2 * * *
  2137   2151   **
  2138   2152   ** Check the "once" flag number P1. If it is set, jump to instruction P2. 
................................................................................
  2242   2256   ** skipped for length() and all content loading can be skipped for typeof().
  2243   2257   */
  2244   2258   case OP_Column: {
  2245   2259     i64 payloadSize64; /* Number of bytes in the record */
  2246   2260     int p2;            /* column number to retrieve */
  2247   2261     VdbeCursor *pC;    /* The VDBE cursor */
  2248   2262     BtCursor *pCrsr;   /* The BTree cursor */
  2249         -  u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
  2250   2263     u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
  2251   2264     int len;           /* The length of the serialized data for the column */
  2252   2265     int i;             /* Loop counter */
  2253   2266     Mem *pDest;        /* Where to write the extracted value */
  2254   2267     Mem sMem;          /* For storing the record being decoded */
  2255   2268     const u8 *zData;   /* Part of the record being decoded */
  2256   2269     const u8 *zHdr;    /* Next unparsed byte of the header */
  2257   2270     const u8 *zEndHdr; /* Pointer to first byte after the header */
  2258   2271     u32 offset;        /* Offset into the data */
  2259   2272     u32 szField;       /* Number of bytes in the content of a field */
  2260   2273     u32 avail;         /* Number of bytes of available data */
  2261   2274     u32 t;             /* A type code from the record header */
         2275  +  u16 fx;            /* pDest->flags value */
  2262   2276     Mem *pReg;         /* PseudoTable input register */
  2263   2277   
  2264   2278     p2 = pOp->p2;
  2265   2279     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  2266   2280     pDest = &aMem[pOp->p3];
  2267   2281     memAboutToChange(p, pDest);
  2268   2282     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  2269   2283     pC = p->apCsr[pOp->p1];
  2270   2284     assert( pC!=0 );
  2271   2285     assert( p2<pC->nField );
  2272         -  aType = pC->aType;
  2273         -  aOffset = aType + pC->nField;
         2286  +  aOffset = pC->aType + pC->nField;
  2274   2287   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2275   2288     assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
  2276   2289   #endif
  2277   2290     pCrsr = pC->pCursor;
  2278   2291     assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
  2279   2292     assert( pCrsr!=0 || pC->nullRow );          /* pC->nullRow on PseudoTables */
  2280   2293   
................................................................................
  2347   2360       if( offset > 98307 || offset > pC->payloadSize ){
  2348   2361         rc = SQLITE_CORRUPT_BKPT;
  2349   2362         goto op_column_error;
  2350   2363       }
  2351   2364     }
  2352   2365   
  2353   2366     /* Make sure at least the first p2+1 entries of the header have been
  2354         -  ** parsed and valid information is in aOffset[] and aType[].
         2367  +  ** parsed and valid information is in aOffset[] and pC->aType[].
  2355   2368     */
  2356   2369     if( pC->nHdrParsed<=p2 ){
  2357   2370       /* If there is more header available for parsing in the record, try
  2358   2371       ** to extract additional fields up through the p2+1-th field 
  2359   2372       */
  2360   2373       if( pC->iHdrOffset<aOffset[0] ){
  2361   2374         /* Make sure zData points to enough of the record to cover the header. */
................................................................................
  2367   2380             goto op_column_error;
  2368   2381           }
  2369   2382           zData = (u8*)sMem.z;
  2370   2383         }else{
  2371   2384           zData = pC->aRow;
  2372   2385         }
  2373   2386     
  2374         -      /* Fill in aType[i] and aOffset[i] values through the p2-th field. */
         2387  +      /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
  2375   2388         i = pC->nHdrParsed;
  2376   2389         offset = aOffset[i];
  2377   2390         zHdr = zData + pC->iHdrOffset;
  2378   2391         zEndHdr = zData + aOffset[0];
  2379   2392         assert( i<=p2 && zHdr<zEndHdr );
  2380   2393         do{
  2381   2394           if( zHdr[0]<0x80 ){
  2382   2395             t = zHdr[0];
  2383   2396             zHdr++;
  2384   2397           }else{
  2385   2398             zHdr += sqlite3GetVarint32(zHdr, &t);
  2386   2399           }
  2387         -        aType[i] = t;
         2400  +        pC->aType[i] = t;
  2388   2401           szField = sqlite3VdbeSerialTypeLen(t);
  2389   2402           offset += szField;
  2390   2403           if( offset<szField ){  /* True if offset overflows */
  2391   2404             zHdr = &zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
  2392   2405             break;
  2393   2406           }
  2394   2407           i++;
................................................................................
  2427   2440           MemSetTypeFlag(pDest, MEM_Null);
  2428   2441         }
  2429   2442         goto op_column_out;
  2430   2443       }
  2431   2444     }
  2432   2445   
  2433   2446     /* Extract the content for the p2+1-th column.  Control can only
  2434         -  ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
         2447  +  ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
  2435   2448     ** all valid.
  2436   2449     */
  2437   2450     assert( p2<pC->nHdrParsed );
  2438   2451     assert( rc==SQLITE_OK );
  2439   2452     assert( sqlite3VdbeCheckMemInvariants(pDest) );
         2453  +  if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest);
         2454  +  t = pC->aType[p2];
  2440   2455     if( pC->szRow>=aOffset[p2+1] ){
  2441   2456       /* This is the common case where the desired content fits on the original
  2442   2457       ** page - where the content is not on an overflow page */
  2443         -    VdbeMemReleaseExtern(pDest);
  2444         -    sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
         2458  +    sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], t, pDest);
  2445   2459     }else{
  2446   2460       /* This branch happens only when content is on overflow pages */
  2447         -    t = aType[p2];
  2448   2461       if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
  2449   2462             && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
  2450   2463        || (len = sqlite3VdbeSerialTypeLen(t))==0
  2451   2464       ){
  2452         -      /* Content is irrelevant for the typeof() function and for
  2453         -      ** the length(X) function if X is a blob.  So we might as well use
  2454         -      ** bogus content rather than reading content from disk.  NULL works
  2455         -      ** for text and blob and whatever is in the payloadSize64 variable
  2456         -      ** will work for everything else.  Content is also irrelevant if
  2457         -      ** the content length is 0. */
  2458         -      zData = t<=13 ? (u8*)&payloadSize64 : 0;
  2459         -      sMem.zMalloc = 0;
         2465  +      /* Content is irrelevant for
         2466  +      **    1. the typeof() function,
         2467  +      **    2. the length(X) function if X is a blob, and
         2468  +      **    3. if the content length is zero.
         2469  +      ** So we might as well use bogus content rather than reading
         2470  +      ** content from disk.  NULL will work for the value for strings
         2471  +      ** and blobs and whatever is in the payloadSize64 variable
         2472  +      ** will work for everything else. */
         2473  +      sqlite3VdbeSerialGet(t<=13 ? (u8*)&payloadSize64 : 0, t, pDest);
  2460   2474       }else{
  2461         -      memset(&sMem, 0, sizeof(sMem));
  2462         -      sqlite3VdbeMemMove(&sMem, pDest);
  2463   2475         rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
  2464         -                                   &sMem);
         2476  +                                   pDest);
  2465   2477         if( rc!=SQLITE_OK ){
  2466   2478           goto op_column_error;
  2467   2479         }
  2468         -      zData = (u8*)sMem.z;
  2469         -    }
  2470         -    sqlite3VdbeSerialGet(zData, t, pDest);
  2471         -    /* If we dynamically allocated space to hold the data (in the
  2472         -    ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
  2473         -    ** dynamically allocated space over to the pDest structure.
  2474         -    ** This prevents a memory copy. */
  2475         -    if( sMem.zMalloc ){
  2476         -      assert( sMem.z==sMem.zMalloc );
  2477         -      assert( VdbeMemDynamic(pDest)==0 );
  2478         -      assert( (pDest->flags & (MEM_Blob|MEM_Str))==0 || pDest->z==sMem.z );
  2479         -      pDest->flags &= ~(MEM_Ephem|MEM_Static);
  2480         -      pDest->flags |= MEM_Term;
  2481         -      pDest->z = sMem.z;
  2482         -      pDest->zMalloc = sMem.zMalloc;
         2480  +      sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
         2481  +      pDest->flags &= ~MEM_Ephem;
  2483   2482       }
  2484   2483     }
  2485   2484     pDest->enc = encoding;
  2486   2485   
  2487   2486   op_column_out:
  2488         -  Deephemeralize(pDest);
         2487  +  /* If the column value is an ephemeral string, go ahead and persist
         2488  +  ** that string in case the cursor moves before the column value is
         2489  +  ** used.  The following code does the equivalent of Deephemeralize()
         2490  +  ** but does it faster. */
         2491  +  if( (pDest->flags & MEM_Ephem)!=0 && pDest->z ){
         2492  +    fx = pDest->flags & (MEM_Str|MEM_Blob);
         2493  +    assert( fx!=0 );
         2494  +    zData = (const u8*)pDest->z;
         2495  +    len = pDest->n;
         2496  +    if( sqlite3VdbeMemClearAndResize(pDest, len+2) ) goto no_mem;
         2497  +    memcpy(pDest->z, zData, len);
         2498  +    pDest->z[len] = 0;
         2499  +    pDest->z[len+1] = 0;
         2500  +    pDest->flags = fx|MEM_Term;
         2501  +  }
  2489   2502   op_column_error:
  2490   2503     UPDATE_MAX_BLOBSIZE(pDest);
  2491   2504     REGISTER_TRACE(pOp->p3, pDest);
  2492   2505     break;
  2493   2506   }
  2494   2507   
  2495   2508   /* Opcode: Affinity P1 P2 * P4 *
................................................................................
  2556   2569     ** like this:
  2557   2570     **
  2558   2571     ** ------------------------------------------------------------------------
  2559   2572     ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | 
  2560   2573     ** ------------------------------------------------------------------------
  2561   2574     **
  2562   2575     ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
  2563         -  ** and so froth.
         2576  +  ** and so forth.
  2564   2577     **
  2565   2578     ** Each type field is a varint representing the serial type of the 
  2566   2579     ** corresponding data element (see sqlite3VdbeSerialType()). The
  2567   2580     ** hdr-size field is also a varint which is the offset from the beginning
  2568   2581     ** of the record to data0.
  2569   2582     */
  2570   2583     nData = 0;         /* Number of bytes of data space */
................................................................................
  2632   2645     if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  2633   2646       goto too_big;
  2634   2647     }
  2635   2648   
  2636   2649     /* Make sure the output register has a buffer large enough to store 
  2637   2650     ** the new record. The output register (pOp->p3) is not allowed to
  2638   2651     ** be one of the input registers (because the following call to
  2639         -  ** sqlite3VdbeMemGrow() could clobber the value before it is used).
         2652  +  ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
  2640   2653     */
  2641         -  if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
         2654  +  if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
  2642   2655       goto no_mem;
  2643   2656     }
  2644   2657     zNewRecord = (u8 *)pOut->z;
  2645   2658   
  2646   2659     /* Write the record */
  2647   2660     i = putVarint32(zNewRecord, nHdr);
  2648   2661     j = nHdr;
................................................................................
  2655   2668     }while( (++pRec)<=pLast );
  2656   2669     assert( i==nHdr );
  2657   2670     assert( j==nByte );
  2658   2671   
  2659   2672     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  2660   2673     pOut->n = (int)nByte;
  2661   2674     pOut->flags = MEM_Blob;
  2662         -  pOut->xDel = 0;
  2663   2675     if( nZero ){
  2664   2676       pOut->u.nZero = nZero;
  2665   2677       pOut->flags |= MEM_Zero;
  2666   2678     }
  2667   2679     pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
  2668   2680     REGISTER_TRACE(pOp->p3, pOut);
  2669   2681     UPDATE_MAX_BLOBSIZE(pOut);
................................................................................
  3543   3555     pC->nullRow = 0;
  3544   3556   #ifdef SQLITE_DEBUG
  3545   3557     pC->seekOp = pOp->opcode;
  3546   3558   #endif
  3547   3559     if( pC->isTable ){
  3548   3560       /* The input value in P3 might be of any type: integer, real, string,
  3549   3561       ** blob, or NULL.  But it needs to be an integer before we can do
  3550         -    ** the seek, so covert it. */
         3562  +    ** the seek, so convert it. */
  3551   3563       pIn3 = &aMem[pOp->p3];
  3552         -    if( (pIn3->flags & (MEM_Int|MEM_Real))==0 ){
         3564  +    if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
  3553   3565         applyNumericAffinity(pIn3, 0);
  3554   3566       }
  3555   3567       iKey = sqlite3VdbeIntValue(pIn3);
  3556   3568       pC->rowidIsValid = 0;
  3557   3569   
  3558   3570       /* If the P3 value could not be converted into an integer without
  3559   3571       ** loss of information, then special processing is required... */
................................................................................
  3568   3580         /* If the approximation iKey is larger than the actual real search
  3569   3581         ** term, substitute >= for > and < for <=. e.g. if the search term
  3570   3582         ** is 4.9 and the integer approximation 5:
  3571   3583         **
  3572   3584         **        (x >  4.9)    ->     (x >= 5)
  3573   3585         **        (x <= 4.9)    ->     (x <  5)
  3574   3586         */
  3575         -      if( pIn3->r<(double)iKey ){
         3587  +      if( pIn3->u.r<(double)iKey ){
  3576   3588           assert( OP_SeekGE==(OP_SeekGT-1) );
  3577   3589           assert( OP_SeekLT==(OP_SeekLE-1) );
  3578   3590           assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
  3579   3591           if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
  3580   3592         }
  3581   3593   
  3582   3594         /* If the approximation iKey is smaller than the actual real search
  3583   3595         ** term, substitute <= for < and > for >=.  */
  3584         -      else if( pIn3->r>(double)iKey ){
         3596  +      else if( pIn3->u.r>(double)iKey ){
  3585   3597           assert( OP_SeekLE==(OP_SeekLT+1) );
  3586   3598           assert( OP_SeekGT==(OP_SeekGE+1) );
  3587   3599           assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
  3588   3600           if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
  3589   3601         }
  3590   3602       } 
  3591   3603       rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
................................................................................
  4339   4351     }else{
  4340   4352       VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
  4341   4353       assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
  4342   4354       if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
  4343   4355         goto too_big;
  4344   4356       }
  4345   4357     }
  4346         -  if( sqlite3VdbeMemGrow(pOut, n, 0) ){
         4358  +  if( sqlite3VdbeMemClearAndResize(pOut, n) ){
  4347   4359       goto no_mem;
  4348   4360     }
  4349   4361     pOut->n = n;
  4350   4362     MemSetTypeFlag(pOut, MEM_Blob);
  4351   4363     if( pC->isTable==0 ){
  4352   4364       rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
  4353   4365     }else{
................................................................................
  4849   4861       r.default_rc = 0;
  4850   4862     }
  4851   4863     r.aMem = &aMem[pOp->p3];
  4852   4864   #ifdef SQLITE_DEBUG
  4853   4865     { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  4854   4866   #endif
  4855   4867     res = 0;  /* Not needed.  Only used to silence a warning. */
  4856         -  rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
         4868  +  rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
  4857   4869     assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
  4858   4870     if( (pOp->opcode&1)==(OP_IdxLT&1) ){
  4859   4871       assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
  4860   4872       res = -res;
  4861   4873     }else{
  4862   4874       assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
  4863   4875       res++;
................................................................................
  5619   5631       apVal[i] = pRec;
  5620   5632       memAboutToChange(p, pRec);
  5621   5633     }
  5622   5634     ctx.pFunc = pOp->p4.pFunc;
  5623   5635     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  5624   5636     ctx.pMem = pMem = &aMem[pOp->p3];
  5625   5637     pMem->n++;
  5626         -  t.flags = MEM_Null;
  5627         -  t.z = 0;
  5628         -  t.zMalloc = 0;
  5629         -  t.xDel = 0;
  5630         -  t.db = db;
         5638  +  sqlite3VdbeMemInit(&t, db, MEM_Null);
  5631   5639     ctx.pOut = &t;
  5632   5640     ctx.isError = 0;
  5633   5641     ctx.pColl = 0;
  5634   5642     ctx.skipFlag = 0;
  5635   5643     if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  5636   5644       assert( pOp>p->aOp );
  5637   5645       assert( pOp[-1].p4type==P4_COLLSEQ );

Changes to src/vdbe.h.

   208    208   void sqlite3VdbeSetVarmask(Vdbe*, int);
   209    209   #ifndef SQLITE_OMIT_TRACE
   210    210     char *sqlite3VdbeExpandSql(Vdbe*, const char*);
   211    211   #endif
   212    212   int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
   213    213   
   214    214   void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
   215         -int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*,int);
          215  +int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
   216    216   UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
   217    217   
   218         -typedef int (*RecordCompare)(int,const void*,UnpackedRecord*,int);
          218  +typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
   219    219   RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
   220    220   
   221    221   #ifndef SQLITE_OMIT_TRIGGER
   222    222   void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
   223    223   #endif
   224    224   
   225    225   /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on

Changes to src/vdbeInt.h.

   157    157   
   158    158   /*
   159    159   ** Internally, the vdbe manipulates nearly all SQL values as Mem
   160    160   ** structures. Each Mem struct may cache multiple representations (string,
   161    161   ** integer etc.) of the same value.
   162    162   */
   163    163   struct Mem {
   164         -  sqlite3 *db;        /* The associated database connection */
   165         -  char *z;            /* String or BLOB value */
   166         -  double r;           /* Real value */
   167         -  union {
          164  +  union MemValue {
          165  +    double r;           /* Real value used when MEM_Real is set in flags */
   168    166       i64 i;              /* Integer value used when MEM_Int is set in flags */
   169    167       int nZero;          /* Used when bit MEM_Zero is set in flags */
   170    168       FuncDef *pDef;      /* Used only when flags==MEM_Agg */
   171    169       RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
   172    170       VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
   173    171     } u;
   174         -  int n;              /* Number of characters in string value, excluding '\0' */
   175    172     u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   176    173     u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
          174  +  int n;              /* Number of characters in string value, excluding '\0' */
          175  +  char *z;            /* String or BLOB value */
          176  +  /* ShallowCopy only needs to copy the information above */
          177  +  char *zMalloc;      /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
          178  +  int szMalloc;       /* Size of the zMalloc allocation */
          179  +  int iPadding1;      /* Padding for 8-byte alignment */
          180  +  sqlite3 *db;        /* The associated database connection */
          181  +  void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
   177    182   #ifdef SQLITE_DEBUG
   178    183     Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
   179    184     void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
   180    185   #endif
   181         -  void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
   182         -  char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
   183    186   };
   184    187   
   185    188   /* One or more of the following flags are set to indicate the validOK
   186    189   ** representations of the value stored in the Mem struct.
   187    190   **
   188    191   ** If the MEM_Null flag is set, then the value is an SQL NULL value.
   189    192   ** No other flags may be set in this case.
................................................................................
   234    237   ** is for use inside assert() statements only.
   235    238   */
   236    239   #ifdef SQLITE_DEBUG
   237    240   #define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
   238    241   #endif
   239    242   
   240    243   /*
   241         -** Each auxilliary data pointer stored by a user defined function 
          244  +** Each auxiliary data pointer stored by a user defined function 
   242    245   ** implementation calling sqlite3_set_auxdata() is stored in an instance
   243    246   ** of this structure. All such structures associated with a single VM
   244    247   ** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
   245    248   ** when the VM is halted (if not before).
   246    249   */
   247    250   struct AuxData {
   248    251     int iOp;                        /* Instruction number of OP_Function opcode */
................................................................................
   249    252     int iArg;                       /* Index of function argument. */
   250    253     void *pAux;                     /* Aux data pointer */
   251    254     void (*xDelete)(void *);        /* Destructor for the aux data */
   252    255     AuxData *pNext;                 /* Next element in list */
   253    256   };
   254    257   
   255    258   /*
   256         -** The "context" argument for a installable function.  A pointer to an
          259  +** The "context" argument for an installable function.  A pointer to an
   257    260   ** instance of this structure is the first argument to the routines used
   258    261   ** implement the SQL functions.
   259    262   **
   260    263   ** There is a typedef for this structure in sqlite.h.  So all routines,
   261    264   ** even the public interface to SQLite, can use a pointer to this structure.
   262    265   ** But this file is the only place where the internal details of this
   263    266   ** structure are known.
   264    267   **
   265    268   ** This structure is defined inside of vdbeInt.h because it uses substructures
   266    269   ** (Mem) which are only defined there.
   267    270   */
   268    271   struct sqlite3_context {
   269    272     Mem *pOut;            /* The return value is stored here */
   270         -  FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
          273  +  FuncDef *pFunc;       /* Pointer to function information */
   271    274     Mem *pMem;            /* Memory cell used to store aggregate context */
   272    275     CollSeq *pColl;       /* Collating sequence */
   273    276     Vdbe *pVdbe;          /* The VM that owns this context */
   274    277     int iOp;              /* Instruction number of OP_Function */
   275    278     int isError;          /* Error code returned by the function. */
   276         -  u8 skipFlag;          /* Skip skip accumulator loading if true */
          279  +  u8 skipFlag;          /* Skip accumulator loading if true */
   277    280     u8 fErrorOrAux;       /* isError!=0 or pVdbe->pAuxData modified */
   278    281   };
   279    282   
   280    283   /*
   281    284   ** An Explain object accumulates indented output which is helpful
   282    285   ** in describing recursive data structures.
   283    286   */
................................................................................
   392    395   u32 sqlite3VdbeSerialTypeLen(u32);
   393    396   u32 sqlite3VdbeSerialType(Mem*, int);
   394    397   u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
   395    398   u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
   396    399   void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
   397    400   
   398    401   int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
   399         -int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
   400         -int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
          402  +int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
          403  +int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
   401    404   int sqlite3VdbeExec(Vdbe*);
   402    405   int sqlite3VdbeList(Vdbe*);
   403    406   int sqlite3VdbeHalt(Vdbe*);
   404    407   int sqlite3VdbeChangeEncoding(Mem *, int);
   405    408   int sqlite3VdbeMemTooBig(Mem*);
   406    409   int sqlite3VdbeMemCopy(Mem*, const Mem*);
   407    410   void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
................................................................................
   410    413   int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
   411    414   void sqlite3VdbeMemSetInt64(Mem*, i64);
   412    415   #ifdef SQLITE_OMIT_FLOATING_POINT
   413    416   # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
   414    417   #else
   415    418     void sqlite3VdbeMemSetDouble(Mem*, double);
   416    419   #endif
          420  +void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
   417    421   void sqlite3VdbeMemSetNull(Mem*);
   418    422   void sqlite3VdbeMemSetZeroBlob(Mem*,int);
   419    423   void sqlite3VdbeMemSetRowSet(Mem*);
   420    424   int sqlite3VdbeMemMakeWriteable(Mem*);
   421    425   int sqlite3VdbeMemStringify(Mem*, u8, u8);
   422    426   i64 sqlite3VdbeIntValue(Mem*);
   423    427   int sqlite3VdbeMemIntegerify(Mem*);
................................................................................
   424    428   double sqlite3VdbeRealValue(Mem*);
   425    429   void sqlite3VdbeIntegerAffinity(Mem*);
   426    430   int sqlite3VdbeMemRealify(Mem*);
   427    431   int sqlite3VdbeMemNumerify(Mem*);
   428    432   void sqlite3VdbeMemCast(Mem*,u8,u8);
   429    433   int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
   430    434   void sqlite3VdbeMemRelease(Mem *p);
   431         -void sqlite3VdbeMemReleaseExternal(Mem *p);
   432    435   #define VdbeMemDynamic(X)  \
   433    436     (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
   434         -#define VdbeMemReleaseExtern(X)  \
   435         -  if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
   436    437   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   437    438   const char *sqlite3OpcodeName(int);
   438    439   int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
          440  +int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
   439    441   int sqlite3VdbeCloseStatement(Vdbe *, int);
   440    442   void sqlite3VdbeFrameDelete(VdbeFrame*);
   441    443   int sqlite3VdbeFrameRestore(VdbeFrame *);
   442    444   int sqlite3VdbeTransferError(Vdbe *p);
   443    445   
   444    446   int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
   445    447   void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);

Changes to src/vdbeapi.c.

   224    224     return aType[pVal->flags&MEM_AffMask];
   225    225   }
   226    226   
   227    227   /**************************** sqlite3_result_  *******************************
   228    228   ** The following routines are used by user-defined functions to specify
   229    229   ** the function result.
   230    230   **
   231         -** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
          231  +** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
   232    232   ** result as a string or blob but if the string or blob is too large, it
   233    233   ** then sets the error code to SQLITE_TOOBIG
          234  +**
          235  +** The invokeValueDestructor(P,X) routine invokes destructor function X()
          236  +** on value P is not going to be used and need to be destroyed.
   234    237   */
   235    238   static void setResultStrOrError(
   236    239     sqlite3_context *pCtx,  /* Function context */
   237    240     const char *z,          /* String pointer */
   238    241     int n,                  /* Bytes in string, or negative */
   239    242     u8 enc,                 /* Encoding of z.  0 for BLOBs */
   240    243     void (*xDel)(void*)     /* Destructor function */
   241    244   ){
   242    245     if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
   243    246       sqlite3_result_error_toobig(pCtx);
   244    247     }
          248  +}
          249  +static int invokeValueDestructor(
          250  +  const void *p,             /* Value to destroy */
          251  +  void (*xDel)(void*),       /* The destructor */
          252  +  sqlite3_context *pCtx      /* Set a SQLITE_TOOBIG error if no NULL */
          253  +){
          254  +  assert( xDel!=SQLITE_DYNAMIC );
          255  +  if( xDel==0 ){
          256  +    /* noop */
          257  +  }else if( xDel==SQLITE_TRANSIENT ){
          258  +    /* noop */
          259  +  }else{
          260  +    xDel((void*)p);
          261  +  }
          262  +  if( pCtx ) sqlite3_result_error_toobig(pCtx);
          263  +  return SQLITE_TOOBIG;
   245    264   }
   246    265   void sqlite3_result_blob(
   247    266     sqlite3_context *pCtx, 
   248    267     const void *z, 
   249    268     int n, 
   250    269     void (*xDel)(void *)
   251    270   ){
   252    271     assert( n>=0 );
   253    272     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   254    273     setResultStrOrError(pCtx, z, n, 0, xDel);
          274  +}
          275  +void sqlite3_result_blob64(
          276  +  sqlite3_context *pCtx, 
          277  +  const void *z, 
          278  +  sqlite3_uint64 n,
          279  +  void (*xDel)(void *)
          280  +){
          281  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          282  +  assert( xDel!=SQLITE_DYNAMIC );
          283  +  if( n>0x7fffffff ){
          284  +    (void)invokeValueDestructor(z, xDel, pCtx);
          285  +  }else{
          286  +    setResultStrOrError(pCtx, z, (int)n, 0, xDel);
          287  +  }
   255    288   }
   256    289   void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
   257    290     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   258    291     sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
   259    292   }
   260    293   void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
   261    294     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
................................................................................
   287    320     sqlite3_context *pCtx, 
   288    321     const char *z, 
   289    322     int n,
   290    323     void (*xDel)(void *)
   291    324   ){
   292    325     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   293    326     setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
          327  +}
          328  +void sqlite3_result_text64(
          329  +  sqlite3_context *pCtx, 
          330  +  const char *z, 
          331  +  sqlite3_uint64 n,
          332  +  void (*xDel)(void *),
          333  +  unsigned char enc
          334  +){
          335  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          336  +  assert( xDel!=SQLITE_DYNAMIC );
          337  +  if( n>0x7fffffff ){
          338  +    (void)invokeValueDestructor(z, xDel, pCtx);
          339  +  }else{
          340  +    setResultStrOrError(pCtx, z, (int)n, enc, xDel);
          341  +  }
   294    342   }
   295    343   #ifndef SQLITE_OMIT_UTF16
   296    344   void sqlite3_result_text16(
   297    345     sqlite3_context *pCtx, 
   298    346     const void *z, 
   299    347     int n, 
   300    348     void (*xDel)(void *)
................................................................................
   626    674   ** Create a new aggregate context for p and return a pointer to
   627    675   ** its pMem->z element.
   628    676   */
   629    677   static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
   630    678     Mem *pMem = p->pMem;
   631    679     assert( (pMem->flags & MEM_Agg)==0 );
   632    680     if( nByte<=0 ){
   633         -    sqlite3VdbeMemReleaseExternal(pMem);
   634         -    pMem->flags = MEM_Null;
          681  +    sqlite3VdbeMemSetNull(pMem);
   635    682       pMem->z = 0;
   636    683     }else{
   637         -    sqlite3VdbeMemGrow(pMem, nByte, 0);
          684  +    sqlite3VdbeMemClearAndResize(pMem, nByte);
   638    685       pMem->flags = MEM_Agg;
   639    686       pMem->u.pDef = p->pFunc;
   640    687       if( pMem->z ){
   641    688         memset(pMem->z, 0, nByte);
   642    689       }
   643    690     }
   644    691     return (void*)pMem->z;
................................................................................
   657    704       return createAggContext(p, nByte);
   658    705     }else{
   659    706       return (void*)p->pMem->z;
   660    707     }
   661    708   }
   662    709   
   663    710   /*
   664         -** Return the auxilary data pointer, if any, for the iArg'th argument to
          711  +** Return the auxiliary data pointer, if any, for the iArg'th argument to
   665    712   ** the user-function defined by pCtx.
   666    713   */
   667    714   void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
   668    715     AuxData *pAuxData;
   669    716   
   670    717     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   671    718     for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
................................................................................
   672    719       if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
   673    720     }
   674    721   
   675    722     return (pAuxData ? pAuxData->pAux : 0);
   676    723   }
   677    724   
   678    725   /*
   679         -** Set the auxilary data pointer and delete function, for the iArg'th
          726  +** Set the auxiliary data pointer and delete function, for the iArg'th
   680    727   ** argument to the user-function defined by pCtx. Any previous value is
   681    728   ** deleted by calling the delete function specified when it was set.
   682    729   */
   683    730   void sqlite3_set_auxdata(
   684    731     sqlite3_context *pCtx, 
   685    732     int iArg, 
   686    733     void *pAux, 
................................................................................
   718    765     if( xDelete ){
   719    766       xDelete(pAux);
   720    767     }
   721    768   }
   722    769   
   723    770   #ifndef SQLITE_OMIT_DEPRECATED
   724    771   /*
   725         -** Return the number of times the Step function of a aggregate has been 
          772  +** Return the number of times the Step function of an aggregate has been 
   726    773   ** called.
   727    774   **
   728    775   ** This function is deprecated.  Do not use it for new code.  It is
   729    776   ** provide only to avoid breaking legacy code.  New aggregate function
   730    777   ** implementations should keep their own counts within their aggregate
   731    778   ** context.
   732    779   */
................................................................................
   767    814     ** these assert()s from failing, when building with SQLITE_DEBUG defined
   768    815     ** using gcc, we force nullMem to be 8-byte aligned using the magical
   769    816     ** __attribute__((aligned(8))) macro.  */
   770    817     static const Mem nullMem 
   771    818   #if defined(SQLITE_DEBUG) && defined(__GNUC__)
   772    819       __attribute__((aligned(8))) 
   773    820   #endif
   774         -    = {0, "", (double)0, {0}, 0, MEM_Null, 0,
          821  +    = {
          822  +        /* .u          = */ {0},
          823  +        /* .flags      = */ MEM_Null,
          824  +        /* .enc        = */ 0,
          825  +        /* .n          = */ 0,
          826  +        /* .z          = */ 0,
          827  +        /* .zMalloc    = */ 0,
          828  +        /* .szMalloc   = */ 0,
          829  +        /* .iPadding1  = */ 0,
          830  +        /* .db         = */ 0,
          831  +        /* .xDel       = */ 0,
   775    832   #ifdef SQLITE_DEBUG
   776         -       0, 0,  /* pScopyFrom, pFiller */
          833  +        /* .pScopyFrom = */ 0,
          834  +        /* .pFiller    = */ 0,
   777    835   #endif
   778         -       0, 0 };
          836  +      };
   779    837     return &nullMem;
   780    838   }
   781    839   
   782    840   /*
   783    841   ** Check to see if column iCol of the given statement is valid.  If
   784    842   ** it is, return a pointer to the Mem for the value of that column.
   785    843   ** If iCol is not valid, return a pointer to a Mem which has a value
................................................................................
   988   1046   #endif /* SQLITE_OMIT_UTF16 */
   989   1047   #endif /* SQLITE_OMIT_DECLTYPE */
   990   1048   
   991   1049   #ifdef SQLITE_ENABLE_COLUMN_METADATA
   992   1050   /*
   993   1051   ** Return the name of the database from which a result column derives.
   994   1052   ** NULL is returned if the result column is an expression or constant or
   995         -** anything else which is not an unabiguous reference to a database column.
         1053  +** anything else which is not an unambiguous reference to a database column.
   996   1054   */
   997   1055   const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
   998   1056     return columnName(
   999   1057         pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
  1000   1058   }
  1001   1059   #ifndef SQLITE_OMIT_UTF16
  1002   1060   const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
................................................................................
  1004   1062         pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
  1005   1063   }
  1006   1064   #endif /* SQLITE_OMIT_UTF16 */
  1007   1065   
  1008   1066   /*
  1009   1067   ** Return the name of the table from which a result column derives.
  1010   1068   ** NULL is returned if the result column is an expression or constant or
  1011         -** anything else which is not an unabiguous reference to a database column.
         1069  +** anything else which is not an unambiguous reference to a database column.
  1012   1070   */
  1013   1071   const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
  1014   1072     return columnName(
  1015   1073         pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
  1016   1074   }
  1017   1075   #ifndef SQLITE_OMIT_UTF16
  1018   1076   const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
................................................................................
  1020   1078         pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
  1021   1079   }
  1022   1080   #endif /* SQLITE_OMIT_UTF16 */
  1023   1081   
  1024   1082   /*
  1025   1083   ** Return the name of the table column from which a result column derives.
  1026   1084   ** NULL is returned if the result column is an expression or constant or
  1027         -** anything else which is not an unabiguous reference to a database column.
         1085  +** anything else which is not an unambiguous reference to a database column.
  1028   1086   */
  1029   1087   const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
  1030   1088     return columnName(
  1031   1089         pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
  1032   1090   }
  1033   1091   #ifndef SQLITE_OMIT_UTF16
  1034   1092   const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
................................................................................
  1139   1197     int nData, 
  1140   1198     void (*xDel)(void*)
  1141   1199   ){
  1142   1200   #ifdef SQLITE_ENABLE_SQLRR
  1143   1201     SRRecBindBlob(pStmt, i, zData, nData);
  1144   1202   #endif
  1145   1203     return bindText(pStmt, i, zData, nData, xDel, 0);
         1204  +}
         1205  +int sqlite3_bind_blob64(
         1206  +  sqlite3_stmt *pStmt, 
         1207  +  int i, 
         1208  +  const void *zData, 
         1209  +  sqlite3_uint64 nData, 
         1210  +  void (*xDel)(void*)
         1211  +){
         1212  +  assert( xDel!=SQLITE_DYNAMIC );
         1213  +  if( nData>0x7fffffff ){
         1214  +    return invokeValueDestructor(zData, xDel, 0);
         1215  +  }else{
         1216  +    return bindText(pStmt, i, zData, (int)nData, xDel, 0);
         1217  +  }
  1146   1218   }
  1147   1219   int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
  1148   1220     int rc;
  1149   1221     Vdbe *p = (Vdbe *)pStmt;
  1150   1222   #ifdef SQLITE_ENABLE_SQLRR
  1151   1223     SRRecBindDouble(pStmt, i, rValue);
  1152   1224   #endif
................................................................................
  1195   1267     int nData, 
  1196   1268     void (*xDel)(void*)
  1197   1269   ){
  1198   1270   #ifdef SQLITE_ENABLE_SQLRR
  1199   1271     SRRecBindText(pStmt, i, zData, nData);
  1200   1272   #endif
  1201   1273     return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
         1274  +}
         1275  +int sqlite3_bind_text64( 
         1276  +  sqlite3_stmt *pStmt, 
         1277  +  int i, 
         1278  +  const char *zData, 
         1279  +  sqlite3_uint64 nData, 
         1280  +  void (*xDel)(void*),
         1281  +  unsigned char enc
         1282  +){
         1283  +  assert( xDel!=SQLITE_DYNAMIC );
         1284  +  if( nData>0x7fffffff ){
         1285  +    return invokeValueDestructor(zData, xDel, 0);
         1286  +  }else{
         1287  +    if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
         1288  +    return bindText(pStmt, i, zData, (int)nData, xDel, enc);
         1289  +  }
  1202   1290   }
  1203   1291   #ifndef SQLITE_OMIT_UTF16
  1204   1292   int sqlite3_bind_text16(
  1205   1293     sqlite3_stmt *pStmt, 
  1206   1294     int i, 
  1207   1295     const void *zData, 
  1208   1296     int nData, 
................................................................................
  1218   1306     int rc;
  1219   1307     switch( sqlite3_value_type((sqlite3_value*)pValue) ){
  1220   1308       case SQLITE_INTEGER: {
  1221   1309         rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
  1222   1310         break;
  1223   1311       }
  1224   1312       case SQLITE_FLOAT: {
  1225         -      rc = sqlite3_bind_double(pStmt, i, pValue->r);
         1313  +      rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
  1226   1314         break;
  1227   1315       }
  1228   1316       case SQLITE_BLOB: {
  1229   1317         if( pValue->flags & MEM_Zero ){
  1230   1318           rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
  1231   1319         }else{
  1232   1320           rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
................................................................................
  1327   1415   }
  1328   1416   
  1329   1417   #ifndef SQLITE_OMIT_DEPRECATED
  1330   1418   /*
  1331   1419   ** Deprecated external interface.  Internal/core SQLite code
  1332   1420   ** should call sqlite3TransferBindings.
  1333   1421   **
  1334         -** Is is misuse to call this routine with statements from different
         1422  +** It is misuse to call this routine with statements from different
  1335   1423   ** database connections.  But as this is a deprecated interface, we
  1336   1424   ** will not bother to check for that condition.
  1337   1425   **
  1338   1426   ** If the two statements contain a different number of bindings, then
  1339   1427   ** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
  1340   1428   ** SQLITE_OK is returned.
  1341   1429   */

Changes to src/vdbeaux.c.

     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used for creating, destroying, and populating
    13         -** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)  Prior
    14         -** to version 2.8.7, all this code was combined into the vdbe.c source file.
    15         -** But that file was getting too big so this subroutines were split out.
           13  +** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) 
    16     14   */
    17     15   #include "sqliteInt.h"
    18     16   #include "vdbeInt.h"
    19     17   
    20     18   /*
    21     19   ** Create a new virtual database engine.
    22     20   */
................................................................................
   694    692           break;
   695    693         }
   696    694         case P4_MEM: {
   697    695           if( db->pnBytesFreed==0 ){
   698    696             sqlite3ValueFree((sqlite3_value*)p4);
   699    697           }else{
   700    698             Mem *p = (Mem*)p4;
   701         -          sqlite3DbFree(db, p->zMalloc);
          699  +          if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
   702    700             sqlite3DbFree(db, p);
   703    701           }
   704    702           break;
   705    703         }
   706    704         case P4_VTAB : {
   707    705           if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
   708    706           break;
................................................................................
   891    889   ** Return the opcode for a given address.  If the address is -1, then
   892    890   ** return the most recently inserted opcode.
   893    891   **
   894    892   ** If a memory allocation error has occurred prior to the calling of this
   895    893   ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
   896    894   ** is readable but not writable, though it is cast to a writable value.
   897    895   ** The return of a dummy opcode allows the call to continue functioning
   898         -** after a OOM fault without having to check to see if the return from 
          896  +** after an OOM fault without having to check to see if the return from 
   899    897   ** this routine is a valid pointer.  But because the dummy.opcode is 0,
   900    898   ** dummy will never be written to.  This is verified by code inspection and
   901    899   ** by running with Valgrind.
   902    900   */
   903    901   VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
   904    902     /* C89 specifies that the constant "dummy" will be initialized to all
   905    903     ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
................................................................................
  1072   1070       case P4_MEM: {
  1073   1071         Mem *pMem = pOp->p4.pMem;
  1074   1072         if( pMem->flags & MEM_Str ){
  1075   1073           zP4 = pMem->z;
  1076   1074         }else if( pMem->flags & MEM_Int ){
  1077   1075           sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
  1078   1076         }else if( pMem->flags & MEM_Real ){
  1079         -        sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
         1077  +        sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r);
  1080   1078         }else if( pMem->flags & MEM_Null ){
  1081   1079           sqlite3_snprintf(nTemp, zTemp, "NULL");
  1082   1080         }else{
  1083   1081           assert( pMem->flags & MEM_Blob );
  1084   1082           zP4 = "(blob)";
  1085   1083         }
  1086   1084         break;
................................................................................
  1222   1220   #endif
  1223   1221   
  1224   1222   /*
  1225   1223   ** Release an array of N Mem elements
  1226   1224   */
  1227   1225   static void releaseMemArray(Mem *p, int N){
  1228   1226     if( p && N ){
  1229         -    Mem *pEnd;
         1227  +    Mem *pEnd = &p[N];
  1230   1228       sqlite3 *db = p->db;
  1231   1229       u8 malloc_failed = db->mallocFailed;
  1232   1230       if( db->pnBytesFreed ){
  1233         -      for(pEnd=&p[N]; p<pEnd; p++){
  1234         -        sqlite3DbFree(db, p->zMalloc);
  1235         -      }
         1231  +      do{
         1232  +        if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
         1233  +      }while( (++p)<pEnd );
  1236   1234         return;
  1237   1235       }
  1238         -    for(pEnd=&p[N]; p<pEnd; p++){
         1236  +    do{
  1239   1237         assert( (&p[1])==pEnd || p[0].db==p[1].db );
  1240   1238         assert( sqlite3VdbeCheckMemInvariants(p) );
  1241   1239   
  1242   1240         /* This block is really an inlined version of sqlite3VdbeMemRelease()
  1243   1241         ** that takes advantage of the fact that the memory cell value is 
  1244   1242         ** being set to NULL after releasing any dynamic resources.
  1245   1243         **
................................................................................
  1253   1251         */
  1254   1252         testcase( p->flags & MEM_Agg );
  1255   1253         testcase( p->flags & MEM_Dyn );
  1256   1254         testcase( p->flags & MEM_Frame );
  1257   1255         testcase( p->flags & MEM_RowSet );
  1258   1256         if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
  1259   1257           sqlite3VdbeMemRelease(p);
  1260         -      }else if( p->zMalloc ){
         1258  +      }else if( p->szMalloc ){
  1261   1259           sqlite3DbFree(db, p->zMalloc);
  1262         -        p->zMalloc = 0;
         1260  +        p->szMalloc = 0;
  1263   1261         }
  1264   1262   
  1265   1263         p->flags = MEM_Undefined;
  1266         -    }
         1264  +    }while( (++p)<pEnd );
  1267   1265       db->mallocFailed = malloc_failed;
  1268   1266     }
  1269   1267   }
  1270   1268   
  1271   1269   /*
  1272   1270   ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
  1273   1271   ** allocated by the OP_Program opcode in sqlite3VdbeExec().
................................................................................
  1422   1420       pMem->u.i = pOp->p2;                          /* P2 */
  1423   1421       pMem++;
  1424   1422   
  1425   1423       pMem->flags = MEM_Int;
  1426   1424       pMem->u.i = pOp->p3;                          /* P3 */
  1427   1425       pMem++;
  1428   1426   
  1429         -    if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
         1427  +    if( sqlite3VdbeMemClearAndResize(pMem, 32) ){ /* P4 */
  1430   1428         assert( p->db->mallocFailed );
  1431   1429         return SQLITE_ERROR;
  1432   1430       }
  1433   1431       pMem->flags = MEM_Str|MEM_Term;
  1434   1432       zP4 = displayP4(pOp, pMem->z, 32);
  1435   1433       if( zP4!=pMem->z ){
  1436   1434         sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
................................................................................
  1438   1436         assert( pMem->z!=0 );
  1439   1437         pMem->n = sqlite3Strlen30(pMem->z);
  1440   1438         pMem->enc = SQLITE_UTF8;
  1441   1439       }
  1442   1440       pMem++;
  1443   1441   
  1444   1442       if( p->explain==1 ){
  1445         -      if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
         1443  +      if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
  1446   1444           assert( p->db->mallocFailed );
  1447   1445           return SQLITE_ERROR;
  1448   1446         }
  1449   1447         pMem->flags = MEM_Str|MEM_Term;
  1450   1448         pMem->n = 2;
  1451   1449         sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
  1452   1450         pMem->enc = SQLITE_UTF8;
  1453   1451         pMem++;
  1454   1452     
  1455   1453   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
  1456         -      if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
         1454  +      if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
  1457   1455           assert( p->db->mallocFailed );
  1458   1456           return SQLITE_ERROR;
  1459   1457         }
  1460   1458         pMem->flags = MEM_Str|MEM_Term;
  1461   1459         pMem->n = displayComment(pOp, zP4, pMem->z, 500);
  1462   1460         pMem->enc = SQLITE_UTF8;
  1463   1461   #else
................................................................................
  1602   1600     }
  1603   1601   #endif
  1604   1602   }
  1605   1603   
  1606   1604   /*
  1607   1605   ** Prepare a virtual machine for execution for the first time after
  1608   1606   ** creating the virtual machine.  This involves things such
  1609         -** as allocating stack space and initializing the program counter.
         1607  +** as allocating registers and initializing the program counter.
  1610   1608   ** After the VDBE has be prepped, it can be executed by one or more
  1611   1609   ** calls to sqlite3VdbeExec().  
  1612   1610   **
  1613         -** This function may be called exact once on a each virtual machine.
         1611  +** This function may be called exactly once on each virtual machine.
  1614   1612   ** After this routine is called the VM has been "packaged" and is ready
  1615         -** to run.  After this routine is called, futher calls to 
         1613  +** to run.  After this routine is called, further calls to 
  1616   1614   ** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
  1617   1615   ** the Vdbe from the Parse object that helped generate it so that the
  1618   1616   ** the Vdbe becomes an independent entity and the Parse object can be
  1619   1617   ** destroyed.
  1620   1618   **
  1621   1619   ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
  1622   1620   ** to its initial state after it has been run.
................................................................................
  1816   1814   
  1817   1815     /* Delete any auxdata allocations made by the VM */
  1818   1816     sqlite3VdbeDeleteAuxData(p, -1, 0);
  1819   1817     assert( p->pAuxData==0 );
  1820   1818   }
  1821   1819   
  1822   1820   /*
  1823         -** Clean up the VM after execution.
  1824         -**
  1825         -** This routine will automatically close any cursors, lists, and/or
  1826         -** sorters that were left open.  It also deletes the values of
  1827         -** variables in the aVar[] array.
         1821  +** Clean up the VM after a single run.
  1828   1822   */
  1829   1823   static void Cleanup(Vdbe *p){
  1830   1824     sqlite3 *db = p->db;
  1831   1825   
  1832   1826   #ifdef SQLITE_DEBUG
  1833   1827     /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
  1834   1828     ** Vdbe.aMem[] arrays have already been cleaned up.  */
................................................................................
  1988   1982       if( rc==SQLITE_OK ){
  1989   1983         sqlite3VtabCommit(db);
  1990   1984       }
  1991   1985     }
  1992   1986   
  1993   1987     /* The complex case - There is a multi-file write-transaction active.
  1994   1988     ** This requires a master journal file to ensure the transaction is
  1995         -  ** committed atomicly.
         1989  +  ** committed atomically.
  1996   1990     */
  1997   1991   #ifndef SQLITE_OMIT_DISKIO
  1998   1992     else{
  1999   1993       sqlite3_vfs *pVfs = db->pVfs;
  2000   1994       int needSync = 0;
  2001   1995       char *zMaster = 0;   /* File-name for the master journal */
  2002   1996       char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
................................................................................
  2636   2630   ** function invoked by the OP_Function opcode at instruction iOp of 
  2637   2631   ** VM pVdbe, and only then if:
  2638   2632   **
  2639   2633   **    * the associated function parameter is the 32nd or later (counting
  2640   2634   **      from left to right), or
  2641   2635   **
  2642   2636   **    * the corresponding bit in argument mask is clear (where the first
  2643         -**      function parameter corrsponds to bit 0 etc.).
         2637  +**      function parameter corresponds to bit 0 etc.).
  2644   2638   */
  2645   2639   void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
  2646   2640     AuxData **pp = &pVdbe->pAuxData;
  2647   2641     while( *pp ){
  2648   2642       AuxData *pAux = *pp;
  2649   2643       if( (iOp<0)
  2650   2644        || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
................................................................................
  2740   2734     return SQLITE_OK;
  2741   2735   }
  2742   2736   
  2743   2737   /*
  2744   2738   ** Something has moved cursor "p" out of place.  Maybe the row it was
  2745   2739   ** pointed to was deleted out from under it.  Or maybe the btree was
  2746   2740   ** rebalanced.  Whatever the cause, try to restore "p" to the place it
  2747         -** is suppose to be pointing.  If the row was deleted out from under the
         2741  +** is supposed to be pointing.  If the row was deleted out from under the
  2748   2742   ** cursor, set the cursor to point to a NULL row.
  2749   2743   */
  2750   2744   static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
  2751   2745     int isDifferentRow, rc;
  2752   2746     assert( p->pCursor!=0 );
  2753   2747     assert( sqlite3BtreeCursorHasMoved(p->pCursor) );
  2754   2748     rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow);
................................................................................
  2947   2941     u32 len;
  2948   2942   
  2949   2943     /* Integer and Real */
  2950   2944     if( serial_type<=7 && serial_type>0 ){
  2951   2945       u64 v;
  2952   2946       u32 i;
  2953   2947       if( serial_type==7 ){
  2954         -      assert( sizeof(v)==sizeof(pMem->r) );
  2955         -      memcpy(&v, &pMem->r, sizeof(v));
         2948  +      assert( sizeof(v)==sizeof(pMem->u.r) );
         2949  +      memcpy(&v, &pMem->u.r, sizeof(v));
  2956   2950         swapMixedEndianFloat(v);
  2957   2951       }else{
  2958   2952         v = pMem->u.i;
  2959   2953       }
  2960   2954       len = i = sqlite3VdbeSerialTypeLen(serial_type);
  2961   2955       assert( i>0 );
  2962   2956       do{
................................................................................
  3018   3012       */
  3019   3013       static const u64 t1 = ((u64)0x3ff00000)<<32;
  3020   3014       static const double r1 = 1.0;
  3021   3015       u64 t2 = t1;
  3022   3016       swapMixedEndianFloat(t2);
  3023   3017       assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
  3024   3018   #endif
  3025         -    assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
         3019  +    assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
  3026   3020       swapMixedEndianFloat(x);
  3027         -    memcpy(&pMem->r, &x, sizeof(x));
  3028         -    pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
         3021  +    memcpy(&pMem->u.r, &x, sizeof(x));
         3022  +    pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
  3029   3023     }
  3030   3024     return 8;
  3031   3025   }
  3032   3026   u32 sqlite3VdbeSerialGet(
  3033   3027     const unsigned char *buf,     /* Buffer to deserialize from */
  3034   3028     u32 serial_type,              /* Serial type to deserialize */
  3035   3029     Mem *pMem                     /* Memory cell to write value into */
................................................................................
  3083   3077         pMem->flags = MEM_Int;
  3084   3078         return 0;
  3085   3079       }
  3086   3080       default: {
  3087   3081         static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
  3088   3082         pMem->z = (char *)buf;
  3089   3083         pMem->n = (serial_type-12)/2;
  3090         -      pMem->xDel = 0;
  3091   3084         pMem->flags = aFlag[serial_type&1];
  3092   3085         return pMem->n;
  3093   3086       }
  3094   3087     }
  3095   3088     return 0;
  3096   3089   }
  3097   3090   /*
................................................................................
  3159   3152     Mem *pMem = p->aMem;
  3160   3153   
  3161   3154     p->default_rc = 0;
  3162   3155     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
  3163   3156     idx = getVarint32(aKey, szHdr);
  3164   3157     d = szHdr;
  3165   3158     u = 0;
  3166         -  while( idx<szHdr && u<p->nField && d<=nKey ){
         3159  +  while( idx<szHdr && d<=nKey ){
  3167   3160       u32 serial_type;
  3168   3161   
  3169   3162       idx += getVarint32(&aKey[idx], serial_type);
  3170   3163       pMem->enc = pKeyInfo->enc;
  3171   3164       pMem->db = pKeyInfo->db;
  3172   3165       /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
  3173         -    pMem->zMalloc = 0;
         3166  +    pMem->szMalloc = 0;
  3174   3167       d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
  3175   3168       pMem++;
  3176         -    u++;
         3169  +    if( (++u)>=p->nField ) break;
  3177   3170     }
  3178   3171     assert( u<=pKeyInfo->nField + 1 );
  3179   3172     p->nField = u;
  3180   3173   }
  3181   3174   
  3182   3175   #if SQLITE_DEBUG
  3183   3176   /*
................................................................................
  3206   3199     Mem mem1;
  3207   3200   
  3208   3201     pKeyInfo = pPKey2->pKeyInfo;
  3209   3202     if( pKeyInfo->db==0 ) return 1;
  3210   3203     mem1.enc = pKeyInfo->enc;
  3211   3204     mem1.db = pKeyInfo->db;
  3212   3205     /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
  3213         -  VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
         3206  +  VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
  3214   3207   
  3215   3208     /* Compilers may complain that mem1.u.i is potentially uninitialized.
  3216   3209     ** We could initialize it, as shown here, to silence those complaints.
  3217   3210     ** But in fact, mem1.u.i will never actually be used uninitialized, and doing 
  3218   3211     ** the unnecessary initialization has a measurable negative performance
  3219   3212     ** impact, since this routine is a very high runner.  And so, we choose
  3220   3213     ** to ignore the compiler warnings and leave this variable uninitialized.
................................................................................
  3249   3242       */
  3250   3243       d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
  3251   3244   
  3252   3245       /* Do the comparison
  3253   3246       */
  3254   3247       rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
  3255   3248       if( rc!=0 ){
  3256         -      assert( mem1.zMalloc==0 );  /* See comment below */
         3249  +      assert( mem1.szMalloc==0 );  /* See comment below */
  3257   3250         if( pKeyInfo->aSortOrder[i] ){
  3258   3251           rc = -rc;  /* Invert the result for DESC sort order. */
  3259   3252         }
  3260   3253         goto debugCompareEnd;
  3261   3254       }
  3262   3255       i++;
  3263   3256     }while( idx1<szHdr1 && i<pPKey2->nField );
  3264   3257   
  3265   3258     /* No memory allocation is ever used on mem1.  Prove this using
  3266   3259     ** the following assert().  If the assert() fails, it indicates a
  3267   3260     ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
  3268   3261     */
  3269         -  assert( mem1.zMalloc==0 );
         3262  +  assert( mem1.szMalloc==0 );
  3270   3263   
  3271   3264     /* rc==0 here means that one of the keys ran out of fields and
  3272         -  ** all the fields up to that point were equal. Return the the default_rc
         3265  +  ** all the fields up to that point were equal. Return the default_rc
  3273   3266     ** value.  */
  3274   3267     rc = pPKey2->default_rc;
  3275   3268   
  3276   3269   debugCompareEnd:
  3277   3270     if( desiredResult==0 && rc==0 ) return 1;
  3278   3271     if( desiredResult<0 && rc<0 ) return 1;
  3279   3272     if( desiredResult>0 && rc>0 ) return 1;
................................................................................
  3301   3294       return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
  3302   3295     }else{
  3303   3296       int rc;
  3304   3297       const void *v1, *v2;
  3305   3298       int n1, n2;
  3306   3299       Mem c1;
  3307   3300       Mem c2;
  3308         -    memset(&c1, 0, sizeof(c1));
  3309         -    memset(&c2, 0, sizeof(c2));
         3301  +    sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
         3302  +    sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
  3310   3303       sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
  3311   3304       sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
  3312   3305       v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
  3313   3306       n1 = v1==0 ? 0 : c1.n;
  3314   3307       v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
  3315   3308       n2 = v2==0 ? 0 : c2.n;
  3316   3309       rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
  3317   3310       sqlite3VdbeMemRelease(&c1);
  3318   3311       sqlite3VdbeMemRelease(&c2);
  3319   3312       if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM;
  3320   3313       return rc;
  3321   3314     }
  3322   3315   }
         3316  +
         3317  +/*
         3318  +** Compare two blobs.  Return negative, zero, or positive if the first
         3319  +** is less than, equal to, or greater than the second, respectively.
         3320  +** If one blob is a prefix of the other, then the shorter is the lessor.
         3321  +*/
         3322  +static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
         3323  +  int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n);
         3324  +  if( c ) return c;
         3325  +  return pB1->n - pB2->n;
         3326  +}
         3327  +
  3323   3328   
  3324   3329   /*
  3325   3330   ** Compare the values contained by the two memory cells, returning
  3326   3331   ** negative, zero or positive if pMem1 is less than, equal to, or greater
  3327   3332   ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
  3328   3333   ** and reals) sorted numerically, followed by text ordered by the collating
  3329   3334   ** sequence pColl and finally blob's ordered by memcmp().
  3330   3335   **
  3331   3336   ** Two NULL values are considered equal by this function.
  3332   3337   */
  3333   3338   int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
  3334         -  int rc;
  3335   3339     int f1, f2;
  3336   3340     int combined_flags;
  3337   3341   
  3338   3342     f1 = pMem1->flags;
  3339   3343     f2 = pMem2->flags;
  3340   3344     combined_flags = f1|f2;
  3341   3345     assert( (combined_flags & MEM_RowSet)==0 );
................................................................................
  3355   3359       double r1, r2;
  3356   3360       if( (f1 & f2 & MEM_Int)!=0 ){
  3357   3361         if( pMem1->u.i < pMem2->u.i ) return -1;
  3358   3362         if( pMem1->u.i > pMem2->u.i ) return 1;
  3359   3363         return 0;
  3360   3364       }
  3361   3365       if( (f1&MEM_Real)!=0 ){
  3362         -      r1 = pMem1->r;
         3366  +      r1 = pMem1->u.r;
  3363   3367       }else if( (f1&MEM_Int)!=0 ){
  3364   3368         r1 = (double)pMem1->u.i;
  3365   3369       }else{
  3366   3370         return 1;
  3367   3371       }
  3368   3372       if( (f2&MEM_Real)!=0 ){
  3369         -      r2 = pMem2->r;
         3373  +      r2 = pMem2->u.r;
  3370   3374       }else if( (f2&MEM_Int)!=0 ){
  3371   3375         r2 = (double)pMem2->u.i;
  3372   3376       }else{
  3373   3377         return -1;
  3374   3378       }
  3375   3379       if( r1<r2 ) return -1;
  3376   3380       if( r1>r2 ) return 1;
................................................................................
  3402   3406         return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
  3403   3407       }
  3404   3408       /* If a NULL pointer was passed as the collate function, fall through
  3405   3409       ** to the blob case and use memcmp().  */
  3406   3410     }
  3407   3411    
  3408   3412     /* Both values must be blobs.  Compare using memcmp().  */
  3409         -  rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
  3410         -  if( rc==0 ){
  3411         -    rc = pMem1->n - pMem2->n;
  3412         -  }
  3413         -  return rc;
         3413  +  return sqlite3BlobCompare(pMem1, pMem2);
  3414   3414   }
  3415   3415   
  3416   3416   
  3417   3417   /*
  3418   3418   ** The first argument passed to this function is a serial-type that
  3419   3419   ** corresponds to an integer - all values between 1 and 9 inclusive 
  3420   3420   ** except 7. The second points to a buffer containing an integer value
................................................................................
  3456   3456   }
  3457   3457   
  3458   3458   /*
  3459   3459   ** This function compares the two table rows or index records
  3460   3460   ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
  3461   3461   ** or positive integer if key1 is less than, equal to or 
  3462   3462   ** greater than key2.  The {nKey1, pKey1} key must be a blob
  3463         -** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
         3463  +** created by the OP_MakeRecord opcode of the VDBE.  The pPKey2
  3464   3464   ** key must be a parsed key such as obtained from
  3465   3465   ** sqlite3VdbeParseRecord.
  3466   3466   **
  3467   3467   ** If argument bSkip is non-zero, it is assumed that the caller has already
  3468   3468   ** determined that the first fields of the keys are equal.
  3469   3469   **
  3470   3470   ** Key1 and Key2 do not have to contain the same number of fields. If all 
................................................................................
  3472   3472   ** returned.
  3473   3473   **
  3474   3474   ** If database corruption is discovered, set pPKey2->errCode to 
  3475   3475   ** SQLITE_CORRUPT and return 0. If an OOM error is encountered, 
  3476   3476   ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
  3477   3477   ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
  3478   3478   */
  3479         -int sqlite3VdbeRecordCompare(
         3479  +static int vdbeRecordCompareWithSkip(
  3480   3480     int nKey1, const void *pKey1,   /* Left key */
  3481   3481     UnpackedRecord *pPKey2,         /* Right key */
  3482   3482     int bSkip                       /* If true, skip the first field */
  3483   3483   ){
  3484   3484     u32 d1;                         /* Offset into aKey[] of next data element */
  3485   3485     int i;                          /* Index of next field to compare */
  3486   3486     u32 szHdr1;                     /* Size of record header in bytes */
................................................................................
  3507   3507       if( d1>(unsigned)nKey1 ){ 
  3508   3508         pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
  3509   3509         return 0;  /* Corruption */
  3510   3510       }
  3511   3511       i = 0;
  3512   3512     }
  3513   3513   
  3514         -  VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
         3514  +  VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
  3515   3515     assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField 
  3516   3516          || CORRUPT_DB );
  3517   3517     assert( pPKey2->pKeyInfo->aSortOrder!=0 );
  3518   3518     assert( pPKey2->pKeyInfo->nField>0 );
  3519   3519     assert( idx1<=szHdr1 || CORRUPT_DB );
  3520   3520     do{
  3521   3521       u32 serial_type;
................................................................................
  3527   3527         if( serial_type>=12 ){
  3528   3528           rc = +1;
  3529   3529         }else if( serial_type==0 ){
  3530   3530           rc = -1;
  3531   3531         }else if( serial_type==7 ){
  3532   3532           double rhs = (double)pRhs->u.i;
  3533   3533           sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
  3534         -        if( mem1.r<rhs ){
         3534  +        if( mem1.u.r<rhs ){
  3535   3535             rc = -1;
  3536         -        }else if( mem1.r>rhs ){
         3536  +        }else if( mem1.u.r>rhs ){
  3537   3537             rc = +1;
  3538   3538           }
  3539   3539         }else{
  3540   3540           i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
  3541   3541           i64 rhs = pRhs->u.i;
  3542   3542           if( lhs<rhs ){
  3543   3543             rc = -1;
................................................................................
  3551   3551       else if( pRhs->flags & MEM_Real ){
  3552   3552         serial_type = aKey1[idx1];
  3553   3553         if( serial_type>=12 ){
  3554   3554           rc = +1;
  3555   3555         }else if( serial_type==0 ){
  3556   3556           rc = -1;
  3557   3557         }else{
  3558         -        double rhs = pRhs->r;
         3558  +        double rhs = pRhs->u.r;
  3559   3559           double lhs;
  3560   3560           sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
  3561   3561           if( serial_type==7 ){
  3562         -          lhs = mem1.r;
         3562  +          lhs = mem1.u.r;
  3563   3563           }else{
  3564   3564             lhs = (double)mem1.u.i;
  3565   3565           }
  3566   3566           if( lhs<rhs ){
  3567   3567             rc = -1;
  3568   3568           }else if( lhs>rhs ){
  3569   3569             rc = +1;
................................................................................
  3630   3630       }
  3631   3631   
  3632   3632       if( rc!=0 ){
  3633   3633         if( pKeyInfo->aSortOrder[i] ){
  3634   3634           rc = -rc;
  3635   3635         }
  3636   3636         assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
  3637         -      assert( mem1.zMalloc==0 );  /* See comment below */
         3637  +      assert( mem1.szMalloc==0 );  /* See comment below */
  3638   3638         return rc;
  3639   3639       }
  3640   3640   
  3641   3641       i++;
  3642   3642       pRhs++;
  3643   3643       d1 += sqlite3VdbeSerialTypeLen(serial_type);
  3644   3644       idx1 += sqlite3VarintLen(serial_type);
  3645   3645     }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
  3646   3646   
  3647   3647     /* No memory allocation is ever used on mem1.  Prove this using
  3648   3648     ** the following assert().  If the assert() fails, it indicates a
  3649   3649     ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).  */
  3650         -  assert( mem1.zMalloc==0 );
         3650  +  assert( mem1.szMalloc==0 );
  3651   3651   
  3652   3652     /* rc==0 here means that one or both of the keys ran out of fields and
  3653         -  ** all the fields up to that point were equal. Return the the default_rc
         3653  +  ** all the fields up to that point were equal. Return the default_rc
  3654   3654     ** value.  */
  3655   3655     assert( CORRUPT_DB 
  3656   3656          || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) 
  3657   3657          || pKeyInfo->db->mallocFailed
  3658   3658     );
  3659   3659     return pPKey2->default_rc;
  3660   3660   }
         3661  +int sqlite3VdbeRecordCompare(
         3662  +  int nKey1, const void *pKey1,   /* Left key */
         3663  +  UnpackedRecord *pPKey2          /* Right key */
         3664  +){
         3665  +  return vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
         3666  +}
         3667  +
  3661   3668   
  3662   3669   /*
  3663   3670   ** This function is an optimized version of sqlite3VdbeRecordCompare() 
  3664   3671   ** that (a) the first field of pPKey2 is an integer, and (b) the 
  3665   3672   ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
  3666   3673   ** byte (i.e. is less than 128).
  3667   3674   **
  3668   3675   ** To avoid concerns about buffer overreads, this routine is only used
  3669   3676   ** on schemas where the maximum valid header size is 63 bytes or less.
  3670   3677   */
  3671   3678   static int vdbeRecordCompareInt(
  3672   3679     int nKey1, const void *pKey1, /* Left key */
  3673         -  UnpackedRecord *pPKey2,       /* Right key */
  3674         -  int bSkip                     /* Ignored */
         3680  +  UnpackedRecord *pPKey2        /* Right key */
  3675   3681   ){
  3676   3682     const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
  3677   3683     int serial_type = ((const u8*)pKey1)[1];
  3678   3684     int res;
  3679   3685     u32 y;
  3680   3686     u64 x;
  3681   3687     i64 v = pPKey2->aMem[0].u.i;
  3682   3688     i64 lhs;
  3683         -  UNUSED_PARAMETER(bSkip);
  3684   3689   
  3685         -  assert( bSkip==0 );
  3686   3690     assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
  3687   3691     switch( serial_type ){
  3688   3692       case 1: { /* 1-byte signed integer */
  3689   3693         lhs = ONE_BYTE_INT(aKey);
  3690   3694         testcase( lhs<0 );
  3691   3695         break;
  3692   3696       }
................................................................................
  3728   3732       /* This case could be removed without changing the results of running
  3729   3733       ** this code. Including it causes gcc to generate a faster switch 
  3730   3734       ** statement (since the range of switch targets now starts at zero and
  3731   3735       ** is contiguous) but does not cause any duplicate code to be generated
  3732   3736       ** (as gcc is clever enough to combine the two like cases). Other 
  3733   3737       ** compilers might be similar.  */ 
  3734   3738       case 0: case 7:
  3735         -      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
         3739  +      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
  3736   3740   
  3737   3741       default:
  3738         -      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
         3742  +      return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
  3739   3743     }
  3740   3744   
  3741   3745     if( v>lhs ){
  3742   3746       res = pPKey2->r1;
  3743   3747     }else if( v<lhs ){
  3744   3748       res = pPKey2->r2;
  3745   3749     }else if( pPKey2->nField>1 ){
  3746   3750       /* The first fields of the two keys are equal. Compare the trailing 
  3747   3751       ** fields.  */
  3748         -    res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
         3752  +    res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
  3749   3753     }else{
  3750   3754       /* The first fields of the two keys are equal and there are no trailing
  3751   3755       ** fields. Return pPKey2->default_rc in this case. */
  3752   3756       res = pPKey2->default_rc;
  3753   3757     }
  3754   3758   
  3755   3759     assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
................................................................................
  3760   3764   ** This function is an optimized version of sqlite3VdbeRecordCompare() 
  3761   3765   ** that (a) the first field of pPKey2 is a string, that (b) the first field
  3762   3766   ** uses the collation sequence BINARY and (c) that the size-of-header varint 
  3763   3767   ** at the start of (pKey1/nKey1) fits in a single byte.
  3764   3768   */
  3765   3769   static int vdbeRecordCompareString(
  3766   3770     int nKey1, const void *pKey1, /* Left key */
  3767         -  UnpackedRecord *pPKey2,       /* Right key */
  3768         -  int bSkip
         3771  +  UnpackedRecord *pPKey2        /* Right key */
  3769   3772   ){
  3770   3773     const u8 *aKey1 = (const u8*)pKey1;
  3771   3774     int serial_type;
  3772   3775     int res;
  3773         -  UNUSED_PARAMETER(bSkip);
  3774   3776   
  3775         -  assert( bSkip==0 );
  3776   3777     getVarint32(&aKey1[1], serial_type);
  3777         -
  3778   3778     if( serial_type<12 ){
  3779   3779       res = pPKey2->r1;      /* (pKey1/nKey1) is a number or a null */
  3780   3780     }else if( !(serial_type & 0x01) ){ 
  3781   3781       res = pPKey2->r2;      /* (pKey1/nKey1) is a blob */
  3782   3782     }else{
  3783   3783       int nCmp;
  3784   3784       int nStr;
................................................................................
  3792   3792       nCmp = MIN( pPKey2->aMem[0].n, nStr );
  3793   3793       res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
  3794   3794   
  3795   3795       if( res==0 ){
  3796   3796         res = nStr - pPKey2->aMem[0].n;
  3797   3797         if( res==0 ){
  3798   3798           if( pPKey2->nField>1 ){
  3799         -          res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
         3799  +          res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
  3800   3800           }else{
  3801   3801             res = pPKey2->default_rc;
  3802   3802           }
  3803   3803         }else if( res>0 ){
  3804   3804           res = pPKey2->r2;
  3805   3805         }else{
  3806   3806           res = pPKey2->r1;
................................................................................
  3874   3874     i64 nCellKey = 0;
  3875   3875     int rc;
  3876   3876     u32 szHdr;        /* Size of the header */
  3877   3877     u32 typeRowid;    /* Serial type of the rowid */
  3878   3878     u32 lenRowid;     /* Size of the rowid */
  3879   3879     Mem m, v;
  3880   3880   
  3881         -  UNUSED_PARAMETER(db);
  3882         -
  3883   3881     /* Get the size of the index entry.  Only indices entries of less
  3884   3882     ** than 2GiB are support - anything large must be database corruption.
  3885   3883     ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
  3886   3884     ** this code can safely assume that nCellKey is 32-bits  
  3887   3885     */
  3888   3886     assert( sqlite3BtreeCursorIsValid(pCur) );
  3889   3887     VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
  3890   3888     assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
  3891   3889     assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
  3892   3890   
  3893   3891     /* Read in the complete content of the index entry */
  3894         -  memset(&m, 0, sizeof(m));
         3892  +  sqlite3VdbeMemInit(&m, db, 0);
  3895   3893     rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
  3896   3894     if( rc ){
  3897   3895       return rc;
  3898   3896     }
  3899   3897   
  3900   3898     /* The index entry must begin with a header size */
  3901   3899     (void)getVarint32((u8*)m.z, szHdr);
................................................................................
  3930   3928     *rowid = v.u.i;
  3931   3929     sqlite3VdbeMemRelease(&m);
  3932   3930     return SQLITE_OK;
  3933   3931   
  3934   3932     /* Jump here if database corruption is detected after m has been
  3935   3933     ** allocated.  Free the m object and return SQLITE_CORRUPT. */
  3936   3934   idx_rowid_corruption:
  3937         -  testcase( m.zMalloc!=0 );
         3935  +  testcase( m.szMalloc!=0 );
  3938   3936     sqlite3VdbeMemRelease(&m);
  3939   3937     return SQLITE_CORRUPT_BKPT;
  3940   3938   }
  3941   3939   
  3942   3940   /*
  3943   3941   ** Compare the key of the index entry that cursor pC is pointing to against
  3944   3942   ** the key string in pUnpacked.  Write into *pRes a number
................................................................................
  3947   3945   **
  3948   3946   ** pUnpacked is either created without a rowid or is truncated so that it
  3949   3947   ** omits the rowid at the end.  The rowid at the end of the index entry
  3950   3948   ** is ignored as well.  Hence, this routine only compares the prefixes 
  3951   3949   ** of the keys prior to the final rowid, not the entire key.
  3952   3950   */
  3953   3951   int sqlite3VdbeIdxKeyCompare(
         3952  +  sqlite3 *db,                     /* Database connection */
  3954   3953     VdbeCursor *pC,                  /* The cursor to compare against */
  3955   3954     UnpackedRecord *pUnpacked,       /* Unpacked version of key */
  3956   3955     int *res                         /* Write the comparison result here */
  3957   3956   ){
  3958   3957     i64 nCellKey = 0;
  3959   3958     int rc;
  3960   3959     BtCursor *pCur = pC->pCursor;
................................................................................
  3965   3964     assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
  3966   3965     /* nCellKey will always be between 0 and 0xffffffff because of the way
  3967   3966     ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
  3968   3967     if( nCellKey<=0 || nCellKey>0x7fffffff ){
  3969   3968       *res = 0;
  3970   3969       return SQLITE_CORRUPT_BKPT;
  3971   3970     }
  3972         -  memset(&m, 0, sizeof(m));
         3971  +  sqlite3VdbeMemInit(&m, db, 0);
  3973   3972     rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
  3974   3973     if( rc ){
  3975   3974       return rc;
  3976   3975     }
  3977         -  *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0);
         3976  +  *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
  3978   3977     sqlite3VdbeMemRelease(&m);
  3979   3978     return SQLITE_OK;
  3980   3979   }
  3981   3980   
  3982   3981   /*
  3983   3982   ** This routine sets the value to be returned by subsequent calls to
  3984   3983   ** sqlite3_changes() on the database handle 'db'. 

Changes to src/vdbemem.c.

    22     22   /*
    23     23   ** Check invariants on a Mem object.
    24     24   **
    25     25   ** This routine is intended for use inside of assert() statements, like
    26     26   ** this:    assert( sqlite3VdbeCheckMemInvariants(pMem) );
    27     27   */
    28     28   int sqlite3VdbeCheckMemInvariants(Mem *p){
    29         -  /* The MEM_Dyn bit is set if and only if Mem.xDel is a non-NULL destructor
    30         -  ** function for Mem.z 
           29  +  /* If MEM_Dyn is set then Mem.xDel!=0.  
           30  +  ** Mem.xDel is might not be initialized if MEM_Dyn is clear.
    31     31     */
    32     32     assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
    33         -  assert( (p->flags & MEM_Dyn)!=0 || p->xDel==0 );
           33  +
           34  +  /* MEM_Dyn may only be set if Mem.szMalloc==0 */
           35  +  assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
           36  +
           37  +  /* Cannot be both MEM_Int and MEM_Real at the same time */
           38  +  assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
           39  +
           40  +  /* The szMalloc field holds the correct memory allocation size */
           41  +  assert( p->szMalloc==0
           42  +       || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
    34     43   
    35     44     /* If p holds a string or blob, the Mem.z must point to exactly
    36     45     ** one of the following:
    37     46     **
    38     47     **   (1) Memory in Mem.zMalloc and managed by the Mem object
    39     48     **   (2) Memory to be freed using Mem.xDel
    40         -  **   (3) An ephermal string or blob
           49  +  **   (3) An ephemeral string or blob
    41     50     **   (4) A static string or blob
    42     51     */
    43         -  if( (p->flags & (MEM_Str|MEM_Blob)) && p->z!=0 ){
           52  +  if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
    44     53       assert( 
    45         -      ((p->z==p->zMalloc)? 1 : 0) +
           54  +      ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
    46     55         ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
    47     56         ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
    48     57         ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
    49     58       );
    50     59     }
    51         -
    52     60     return 1;
    53     61   }
    54     62   #endif
    55     63   
    56     64   
    57     65   /*
    58     66   ** If pMem is an object with a valid string representation, this routine
................................................................................
    98    106   ** min(n,32) bytes.
    99    107   **
   100    108   ** If the bPreserve argument is true, then copy of the content of
   101    109   ** pMem->z into the new allocation.  pMem must be either a string or
   102    110   ** blob if bPreserve is true.  If bPreserve is false, any prior content
   103    111   ** in pMem->z is discarded.
   104    112   */
   105         -int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
          113  +SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
   106    114     assert( sqlite3VdbeCheckMemInvariants(pMem) );
   107    115     assert( (pMem->flags&MEM_RowSet)==0 );
   108    116   
   109    117     /* If the bPreserve flag is set to true, then the memory cell must already
   110    118     ** contain a valid string or blob value.  */
   111    119     assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
   112    120     testcase( bPreserve && pMem->z==0 );
   113    121   
   114         -  if( pMem->zMalloc==0 || sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
          122  +  assert( pMem->szMalloc==0
          123  +       || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
          124  +  if( pMem->szMalloc<n ){
   115    125       if( n<32 ) n = 32;
   116         -    if( bPreserve && pMem->z==pMem->zMalloc ){
          126  +    if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){
   117    127         pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
   118    128         bPreserve = 0;
   119    129       }else{
   120         -      sqlite3DbFree(pMem->db, pMem->zMalloc);
          130  +      if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
   121    131         pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
   122    132       }
   123    133       if( pMem->zMalloc==0 ){
   124         -      VdbeMemReleaseExtern(pMem);
          134  +      sqlite3VdbeMemSetNull(pMem);
   125    135         pMem->z = 0;
   126         -      pMem->flags = MEM_Null;  
          136  +      pMem->szMalloc = 0;
   127    137         return SQLITE_NOMEM;
          138  +    }else{
          139  +      pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
   128    140       }
   129    141     }
   130    142   
   131    143     if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
   132    144       memcpy(pMem->zMalloc, pMem->z, pMem->n);
   133    145     }
   134    146     if( (pMem->flags&MEM_Dyn)!=0 ){
   135    147       assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
   136    148       pMem->xDel((void *)(pMem->z));
   137    149     }
   138    150   
   139    151     pMem->z = pMem->zMalloc;
   140    152     pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
   141         -  pMem->xDel = 0;
          153  +  return SQLITE_OK;
          154  +}
          155  +
          156  +/*
          157  +** Change the pMem->zMalloc allocation to be at least szNew bytes.
          158  +** If pMem->zMalloc already meets or exceeds the requested size, this
          159  +** routine is a no-op.
          160  +**
          161  +** Any prior string or blob content in the pMem object may be discarded.
          162  +** The pMem->xDel destructor is called, if it exists.  Though MEM_Str
          163  +** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null
          164  +** values are preserved.
          165  +**
          166  +** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
          167  +** if unable to complete the resizing.
          168  +*/
          169  +int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
          170  +  assert( szNew>=0 );
          171  +  if( pMem->szMalloc<szNew ){
          172  +    return sqlite3VdbeMemGrow(pMem, szNew, 0);
          173  +  }
          174  +  assert( (pMem->flags & MEM_Dyn)==0 );
          175  +  pMem->z = pMem->zMalloc;
          176  +  pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
   142    177     return SQLITE_OK;
   143    178   }
   144    179   
   145    180   /*
   146         -** Make the given Mem object MEM_Dyn.  In other words, make it so
   147         -** that any TEXT or BLOB content is stored in memory obtained from
   148         -** malloc().  In this way, we know that the memory is safe to be
   149         -** overwritten or altered.
          181  +** Change pMem so that its MEM_Str or MEM_Blob value is stored in
          182  +** MEM.zMalloc, where it can be safely written.
   150    183   **
   151    184   ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
   152    185   */
   153    186   int sqlite3VdbeMemMakeWriteable(Mem *pMem){
   154    187     int f;
   155    188     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   156    189     assert( (pMem->flags&MEM_RowSet)==0 );
   157    190     ExpandBlob(pMem);
   158    191     f = pMem->flags;
   159         -  if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
          192  +  if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){
   160    193       if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
   161    194         return SQLITE_NOMEM;
   162    195       }
   163    196       pMem->z[pMem->n] = 0;
   164    197       pMem->z[pMem->n+1] = 0;
   165    198       pMem->flags |= MEM_Term;
   166    199   #ifdef SQLITE_DEBUG
................................................................................
   236    269   ** Existing representations MEM_Int and MEM_Real are invalidated if
   237    270   ** bForce is true but are retained if bForce is false.
   238    271   **
   239    272   ** A MEM_Null value will never be passed to this function. This function is
   240    273   ** used for converting values to text for returning to the user (i.e. via
   241    274   ** sqlite3_value_text()), or for ensuring that values to be used as btree
   242    275   ** keys are strings. In the former case a NULL pointer is returned the
   243         -** user and the later is an internal programming error.
          276  +** user and the latter is an internal programming error.
   244    277   */
   245    278   int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
   246    279     int fg = pMem->flags;
   247    280     const int nByte = 32;
   248    281   
   249    282     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   250    283     assert( !(fg&MEM_Zero) );
   251    284     assert( !(fg&(MEM_Str|MEM_Blob)) );
   252    285     assert( fg&(MEM_Int|MEM_Real) );
   253    286     assert( (pMem->flags&MEM_RowSet)==0 );
   254    287     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   255    288   
   256    289   
   257         -  if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
          290  +  if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
   258    291       return SQLITE_NOMEM;
   259    292     }
   260    293   
   261    294     /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
   262    295     ** string representation of the value. Then, if the required encoding
   263    296     ** is UTF-16le or UTF-16be do a translation.
   264    297     ** 
   265    298     ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
   266    299     */
   267    300     if( fg & MEM_Int ){
   268    301       sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
   269    302     }else{
   270    303       assert( fg & MEM_Real );
   271         -    sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
          304  +    sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
   272    305     }
   273    306     pMem->n = sqlite3Strlen30(pMem->z);
   274    307     pMem->enc = SQLITE_UTF8;
   275    308     pMem->flags |= MEM_Str|MEM_Term;
   276    309     if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
   277    310     sqlite3VdbeChangeEncoding(pMem, enc);
   278    311     return SQLITE_OK;
................................................................................
   297    330       memset(&t, 0, sizeof(t));
   298    331       t.flags = MEM_Null;
   299    332       t.db = pMem->db;
   300    333       ctx.pOut = &t;
   301    334       ctx.pMem = pMem;
   302    335       ctx.pFunc = pFunc;
   303    336       pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
   304         -    assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
   305         -    sqlite3DbFree(pMem->db, pMem->zMalloc);
          337  +    assert( (pMem->flags & MEM_Dyn)==0 );
          338  +    if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
   306    339       memcpy(pMem, &t, sizeof(t));
   307    340       rc = ctx.isError;
   308    341     }
   309    342     return rc;
   310    343   }
   311    344   
   312    345   /*
   313         -** If the memory cell contains a string value that must be freed by
   314         -** invoking an external callback, free it now. Calling this function
   315         -** does not free any Mem.zMalloc buffer.
          346  +** If the memory cell contains a value that must be freed by
          347  +** invoking the external callback in Mem.xDel, then this routine
          348  +** will free that value.  It also sets Mem.flags to MEM_Null.
   316    349   **
   317         -** The VdbeMemReleaseExtern() macro invokes this routine if only if there
   318         -** is work for this routine to do.
          350  +** This is a helper routine for sqlite3VdbeMemSetNull() and
          351  +** for sqlite3VdbeMemRelease().  Use those other routines as the
          352  +** entry point for releasing Mem resources.
   319    353   */
   320         -void sqlite3VdbeMemReleaseExternal(Mem *p){
          354  +static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
   321    355     assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
          356  +  assert( VdbeMemDynamic(p) );
   322    357     if( p->flags&MEM_Agg ){
   323    358       sqlite3VdbeMemFinalize(p, p->u.pDef);
   324    359       assert( (p->flags & MEM_Agg)==0 );
   325         -    sqlite3VdbeMemRelease(p);
   326         -  }else if( p->flags&MEM_Dyn ){
          360  +    testcase( p->flags & MEM_Dyn );
          361  +  }
          362  +  if( p->flags&MEM_Dyn ){
   327    363       assert( (p->flags&MEM_RowSet)==0 );
   328    364       assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
   329    365       p->xDel((void *)p->z);
   330         -    p->xDel = 0;
   331    366     }else if( p->flags&MEM_RowSet ){
   332    367       sqlite3RowSetClear(p->u.pRowSet);
   333    368     }else if( p->flags&MEM_Frame ){
   334         -    sqlite3VdbeMemSetNull(p);
          369  +    VdbeFrame *pFrame = p->u.pFrame;
          370  +    pFrame->pParent = pFrame->v->pDelFrame;
          371  +    pFrame->v->pDelFrame = pFrame;
   335    372     }
          373  +  p->flags = MEM_Null;
   336    374   }
   337    375   
   338    376   /*
   339    377   ** Release memory held by the Mem p, both external memory cleared
   340    378   ** by p->xDel and memory in p->zMalloc.
   341    379   **
   342    380   ** This is a helper routine invoked by sqlite3VdbeMemRelease() in
   343         -** the uncommon case when there really is memory in p that is
   344         -** need of freeing.
          381  +** the unusual case where there really is memory in p that needs
          382  +** to be freed.
   345    383   */
   346         -static SQLITE_NOINLINE void vdbeMemRelease(Mem *p){
          384  +static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
   347    385     if( VdbeMemDynamic(p) ){
   348         -    sqlite3VdbeMemReleaseExternal(p);
          386  +    vdbeMemClearExternAndSetNull(p);
   349    387     }
   350         -  if( p->zMalloc ){
          388  +  if( p->szMalloc ){
   351    389       sqlite3DbFree(p->db, p->zMalloc);
   352         -    p->zMalloc = 0;
          390  +    p->szMalloc = 0;
   353    391     }
   354    392     p->z = 0;
   355    393   }
   356    394   
   357    395   /*
   358         -** Release any memory held by the Mem. This may leave the Mem in an
   359         -** inconsistent state, for example with (Mem.z==0) and
   360         -** (Mem.flags==MEM_Str).
          396  +** Release any memory resources held by the Mem.  Both the memory that is
          397  +** free by Mem.xDel and the Mem.zMalloc allocation are freed.
          398  +**
          399  +** Use this routine prior to clean up prior to abandoning a Mem, or to
          400  +** reset a Mem back to its minimum memory utilization.
          401  +**
          402  +** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
          403  +** prior to inserting new content into the Mem.
   361    404   */
   362    405   void sqlite3VdbeMemRelease(Mem *p){
   363    406     assert( sqlite3VdbeCheckMemInvariants(p) );
   364         -  if( VdbeMemDynamic(p) || p->zMalloc ){
   365         -    vdbeMemRelease(p);
   366         -  }else{
   367         -    p->z = 0;
          407  +  if( VdbeMemDynamic(p) || p->szMalloc ){
          408  +    vdbeMemClear(p);
   368    409     }
   369         -  assert( p->xDel==0 );
   370    410   }
   371    411   
   372    412   /*
   373    413   ** Convert a 64-bit IEEE double into a 64-bit signed integer.
   374    414   ** If the double is out of range of a 64-bit signed integer then
   375    415   ** return the closest available 64-bit signed integer.
   376    416   */
................................................................................
   401    441   
   402    442   /*
   403    443   ** Return some kind of integer value which is the best we can do
   404    444   ** at representing the value that *pMem describes as an integer.
   405    445   ** If pMem is an integer, then the value is exact.  If pMem is
   406    446   ** a floating-point then the value returned is the integer part.
   407    447   ** If pMem is a string or blob, then we make an attempt to convert
   408         -** it into a integer and return that.  If pMem represents an
          448  +** it into an integer and return that.  If pMem represents an
   409    449   ** an SQL-NULL value, return 0.
   410    450   **
   411    451   ** If pMem represents a string value, its encoding might be changed.
   412    452   */
   413    453   i64 sqlite3VdbeIntValue(Mem *pMem){
   414    454     int flags;
   415    455     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   416    456     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   417    457     flags = pMem->flags;
   418    458     if( flags & MEM_Int ){
   419    459       return pMem->u.i;
   420    460     }else if( flags & MEM_Real ){
   421         -    return doubleToInt64(pMem->r);
          461  +    return doubleToInt64(pMem->u.r);
   422    462     }else if( flags & (MEM_Str|MEM_Blob) ){
   423    463       i64 value = 0;
   424    464       assert( pMem->z || pMem->n==0 );
   425    465       sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
   426    466       return value;
   427    467     }else{
   428    468       return 0;
................................................................................
   435    475   ** value.  If it is a string or blob, try to convert it to a double.
   436    476   ** If it is a NULL, return 0.0.
   437    477   */
   438    478   double sqlite3VdbeRealValue(Mem *pMem){
   439    479     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   440    480     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   441    481     if( pMem->flags & MEM_Real ){
   442         -    return pMem->r;
          482  +    return pMem->u.r;
   443    483     }else if( pMem->flags & MEM_Int ){
   444    484       return (double)pMem->u.i;
   445    485     }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
   446    486       /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
   447    487       double val = (double)0;
   448    488       sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
   449    489       return val;
................................................................................
   454    494   }
   455    495   
   456    496   /*
   457    497   ** The MEM structure is already a MEM_Real.  Try to also make it a
   458    498   ** MEM_Int if we can.
   459    499   */
   460    500   void sqlite3VdbeIntegerAffinity(Mem *pMem){
          501  +  i64 ix;
   461    502     assert( pMem->flags & MEM_Real );
   462    503     assert( (pMem->flags & MEM_RowSet)==0 );
   463    504     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   464    505     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   465    506   
   466         -  pMem->u.i = doubleToInt64(pMem->r);
          507  +  ix = doubleToInt64(pMem->u.r);
   467    508   
   468    509     /* Only mark the value as an integer if
   469    510     **
   470    511     **    (1) the round-trip conversion real->int->real is a no-op, and
   471    512     **    (2) The integer is neither the largest nor the smallest
   472    513     **        possible integer (ticket #3922)
   473    514     **
   474    515     ** The second and third terms in the following conditional enforces
   475    516     ** the second condition under the assumption that addition overflow causes
   476    517     ** values to wrap around.
   477    518     */
   478         -  if( pMem->r==(double)pMem->u.i
   479         -   && pMem->u.i>SMALLEST_INT64
   480         -   && pMem->u.i<LARGEST_INT64
   481         -  ){
   482         -    pMem->flags |= MEM_Int;
          519  +  if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
          520  +    pMem->u.i = ix;
          521  +    MemSetTypeFlag(pMem, MEM_Int);
   483    522     }
   484    523   }
   485    524   
   486    525   /*
   487    526   ** Convert pMem to type integer.  Invalidate any prior representations.
   488    527   */
   489    528   int sqlite3VdbeMemIntegerify(Mem *pMem){
................................................................................
   500    539   ** Convert pMem so that it is of type MEM_Real.
   501    540   ** Invalidate any prior representations.
   502    541   */
   503    542   int sqlite3VdbeMemRealify(Mem *pMem){
   504    543     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   505    544     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   506    545   
   507         -  pMem->r = sqlite3VdbeRealValue(pMem);
          546  +  pMem->u.r = sqlite3VdbeRealValue(pMem);
   508    547     MemSetTypeFlag(pMem, MEM_Real);
   509    548     return SQLITE_OK;
   510    549   }
   511    550   
   512    551   /*
   513    552   ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
   514    553   ** Invalidate any prior representations.
................................................................................
   520    559   int sqlite3VdbeMemNumerify(Mem *pMem){
   521    560     if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
   522    561       assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
   523    562       assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   524    563       if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
   525    564         MemSetTypeFlag(pMem, MEM_Int);
   526    565       }else{
   527         -      pMem->r = sqlite3VdbeRealValue(pMem);
          566  +      pMem->u.r = sqlite3VdbeRealValue(pMem);
   528    567         MemSetTypeFlag(pMem, MEM_Real);
   529    568         sqlite3VdbeIntegerAffinity(pMem);
   530    569       }
   531    570     }
   532    571     assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
   533    572     pMem->flags &= ~(MEM_Str|MEM_Blob);
   534    573     return SQLITE_OK;
................................................................................
   574    613         assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
   575    614         pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
   576    615         break;
   577    616       }
   578    617     }
   579    618   }
   580    619   
          620  +/*
          621  +** Initialize bulk memory to be a consistent Mem object.
          622  +**
          623  +** The minimum amount of initialization feasible is performed.
          624  +*/
          625  +void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
          626  +  assert( (flags & ~MEM_TypeMask)==0 );
          627  +  pMem->flags = flags;
          628  +  pMem->db = db;
          629  +  pMem->szMalloc = 0;
          630  +}
          631  +
   581    632   
   582    633   /*
   583    634   ** Delete any previous value and set the value stored in *pMem to NULL.
          635  +**
          636  +** This routine calls the Mem.xDel destructor to dispose of values that
          637  +** require the destructor.  But it preserves the Mem.zMalloc memory allocation.
          638  +** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
          639  +** routine to invoke the destructor and deallocates Mem.zMalloc.
          640  +**
          641  +** Use this routine to reset the Mem prior to insert a new value.
          642  +**
          643  +** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
   584    644   */
   585    645   void sqlite3VdbeMemSetNull(Mem *pMem){
   586         -  if( pMem->flags & MEM_Frame ){
   587         -    VdbeFrame *pFrame = pMem->u.pFrame;
   588         -    pFrame->pParent = pFrame->v->pDelFrame;
   589         -    pFrame->v->pDelFrame = pFrame;
          646  +  if( VdbeMemDynamic(pMem) ){
          647  +    vdbeMemClearExternAndSetNull(pMem);
          648  +  }else{
          649  +    pMem->flags = MEM_Null;
   590    650     }
   591         -  if( pMem->flags & MEM_RowSet ){
   592         -    sqlite3RowSetClear(pMem->u.pRowSet);
   593         -  }
   594         -  MemSetTypeFlag(pMem, MEM_Null);
   595    651   }
   596    652   void sqlite3ValueSetNull(sqlite3_value *p){
   597    653     sqlite3VdbeMemSetNull((Mem*)p); 
   598    654   }
   599    655   
   600    656   /*
   601    657   ** Delete any previous value and set the value to be a BLOB of length
................................................................................
   604    660   void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
   605    661     sqlite3VdbeMemRelease(pMem);
   606    662     pMem->flags = MEM_Blob|MEM_Zero;
   607    663     pMem->n = 0;
   608    664     if( n<0 ) n = 0;
   609    665     pMem->u.nZero = n;
   610    666     pMem->enc = SQLITE_UTF8;
   611         -
   612         -#ifdef SQLITE_OMIT_INCRBLOB
   613         -  sqlite3VdbeMemGrow(pMem, n, 0);
   614         -  if( pMem->z ){
   615         -    pMem->n = n;
   616         -    memset(pMem->z, 0, n);
   617         -  }
   618         -#endif
          667  +  pMem->z = 0;
   619    668   }
   620    669   
   621    670   /*
   622    671   ** The pMem is known to contain content that needs to be destroyed prior
   623    672   ** to a value change.  So invoke the destructor, then set the value to
   624    673   ** a 64-bit integer.
   625    674   */
   626    675   static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
   627         -  sqlite3VdbeMemReleaseExternal(pMem);
          676  +  sqlite3VdbeMemSetNull(pMem);
   628    677     pMem->u.i = val;
   629    678     pMem->flags = MEM_Int;
   630    679   }
   631    680   
   632    681   /*
   633    682   ** Delete any previous value and set the value stored in *pMem to val,
   634    683   ** manifest type INTEGER.
................................................................................
   644    693   
   645    694   #ifndef SQLITE_OMIT_FLOATING_POINT
   646    695   /*
   647    696   ** Delete any previous value and set the value stored in *pMem to val,
   648    697   ** manifest type REAL.
   649    698   */
   650    699   void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
   651         -  if( sqlite3IsNaN(val) ){
   652         -    sqlite3VdbeMemSetNull(pMem);
   653         -  }else{
   654         -    sqlite3VdbeMemRelease(pMem);
   655         -    pMem->r = val;
          700  +  sqlite3VdbeMemSetNull(pMem);
          701  +  if( !sqlite3IsNaN(val) ){
          702  +    pMem->u.r = val;
   656    703       pMem->flags = MEM_Real;
   657    704     }
   658    705   }
   659    706   #endif
   660    707   
   661    708   /*
   662    709   ** Delete any previous value and set the value of pMem to be an
................................................................................
   666    713     sqlite3 *db = pMem->db;
   667    714     assert( db!=0 );
   668    715     assert( (pMem->flags & MEM_RowSet)==0 );
   669    716     sqlite3VdbeMemRelease(pMem);
   670    717     pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
   671    718     if( db->mallocFailed ){
   672    719       pMem->flags = MEM_Null;
          720  +    pMem->szMalloc = 0;
   673    721     }else{
   674    722       assert( pMem->zMalloc );
   675         -    pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, 
   676         -                                       sqlite3DbMallocSize(db, pMem->zMalloc));
          723  +    pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc);
          724  +    pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc);
   677    725       assert( pMem->u.pRowSet!=0 );
   678    726       pMem->flags = MEM_RowSet;
   679    727     }
   680    728   }
   681    729   
   682    730   /*
   683    731   ** Return true if the Mem object contains a TEXT or BLOB that is
................................................................................
   693    741       return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
   694    742     }
   695    743     return 0; 
   696    744   }
   697    745   
   698    746   #ifdef SQLITE_DEBUG
   699    747   /*
   700         -** This routine prepares a memory cell for modication by breaking
          748  +** This routine prepares a memory cell for modification by breaking
   701    749   ** its link to a shallow copy and by marking any current shallow
   702    750   ** copies of this cell as invalid.
   703    751   **
   704    752   ** This is used for testing and debugging only - to make sure shallow
   705    753   ** copies are not misused.
   706    754   */
   707    755   void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
................................................................................
   726    774   ** Make an shallow copy of pFrom into pTo.  Prior contents of
   727    775   ** pTo are freed.  The pFrom->z field is not duplicated.  If
   728    776   ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
   729    777   ** and flags gets srcType (either MEM_Ephem or MEM_Static).
   730    778   */
   731    779   void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
   732    780     assert( (pFrom->flags & MEM_RowSet)==0 );
   733         -  VdbeMemReleaseExtern(pTo);
          781  +  assert( pTo->db==pFrom->db );
          782  +  if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
   734    783     memcpy(pTo, pFrom, MEMCELLSIZE);
   735         -  pTo->xDel = 0;
   736    784     if( (pFrom->flags&MEM_Static)==0 ){
   737    785       pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
   738    786       assert( srcType==MEM_Ephem || srcType==MEM_Static );
   739    787       pTo->flags |= srcType;
   740    788     }
   741    789   }
   742    790   
................................................................................
   743    791   /*
   744    792   ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
   745    793   ** freed before the copy is made.
   746    794   */
   747    795   int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
   748    796     int rc = SQLITE_OK;
   749    797   
          798  +  assert( pTo->db==pFrom->db );
   750    799     assert( (pFrom->flags & MEM_RowSet)==0 );
   751         -  VdbeMemReleaseExtern(pTo);
          800  +  if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
   752    801     memcpy(pTo, pFrom, MEMCELLSIZE);
   753    802     pTo->flags &= ~MEM_Dyn;
   754         -  pTo->xDel = 0;
   755         -
   756    803     if( pTo->flags&(MEM_Str|MEM_Blob) ){
   757    804       if( 0==(pFrom->flags&MEM_Static) ){
   758    805         pTo->flags |= MEM_Ephem;
   759    806         rc = sqlite3VdbeMemMakeWriteable(pTo);
   760    807       }
   761    808     }
   762    809   
................................................................................
   773    820     assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
   774    821     assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
   775    822     assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
   776    823   
   777    824     sqlite3VdbeMemRelease(pTo);
   778    825     memcpy(pTo, pFrom, sizeof(Mem));
   779    826     pFrom->flags = MEM_Null;
   780         -  pFrom->xDel = 0;
   781         -  pFrom->zMalloc = 0;
          827  +  pFrom->szMalloc = 0;
   782    828   }
   783    829   
   784    830   /*
   785    831   ** Change the value of a Mem to be a string or a BLOB.
   786    832   **
   787    833   ** The memory management strategy depends on the value of the xDel
   788    834   ** parameter. If the value passed is SQLITE_TRANSIENT, then the 
................................................................................
   821    867     }else{
   822    868       iLimit = SQLITE_MAX_LENGTH;
   823    869     }
   824    870     flags = (enc==0?MEM_Blob:MEM_Str);
   825    871     if( nByte<0 ){
   826    872       assert( enc!=0 );
   827    873       if( enc==SQLITE_UTF8 ){
   828         -      for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
          874  +      nByte = sqlite3Strlen30(z);
          875  +      if( nByte>iLimit ) nByte = iLimit+1;
   829    876       }else{
   830    877         for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
   831    878       }
   832    879       flags |= MEM_Term;
   833    880     }
   834    881   
   835    882     /* The following block sets the new values of Mem.z and Mem.xDel. It
................................................................................
   840    887       int nAlloc = nByte;
   841    888       if( flags&MEM_Term ){
   842    889         nAlloc += (enc==SQLITE_UTF8?1:2);
   843    890       }
   844    891       if( nByte>iLimit ){
   845    892         return SQLITE_TOOBIG;
   846    893       }
   847         -    if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
          894  +    if( sqlite3VdbeMemClearAndResize(pMem, nAlloc) ){
   848    895         return SQLITE_NOMEM;
   849    896       }
   850    897       memcpy(pMem->z, z, nAlloc);
   851    898     }else if( xDel==SQLITE_DYNAMIC ){
   852    899       sqlite3VdbeMemRelease(pMem);
   853    900       pMem->zMalloc = pMem->z = (char *)z;
   854         -    pMem->xDel = 0;
          901  +    pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
   855    902     }else{
   856    903       sqlite3VdbeMemRelease(pMem);
   857    904       pMem->z = (char *)z;
   858    905       pMem->xDel = xDel;
   859    906       flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
   860    907     }
   861    908   
................................................................................
   879    926   /*
   880    927   ** Move data out of a btree key or data field and into a Mem structure.
   881    928   ** The data or key is taken from the entry that pCur is currently pointing
   882    929   ** to.  offset and amt determine what portion of the data or key to retrieve.
   883    930   ** key is true to get the key or false to get data.  The result is written
   884    931   ** into the pMem element.
   885    932   **
   886         -** The pMem structure is assumed to be uninitialized.  Any prior content
   887         -** is overwritten without being freed.
          933  +** The pMem object must have been initialized.  This routine will use
          934  +** pMem->zMalloc to hold the content from the btree, if possible.  New
          935  +** pMem->zMalloc space will be allocated if necessary.  The calling routine
          936  +** is responsible for making sure that the pMem object is eventually
          937  +** destroyed.
   888    938   **
   889    939   ** If this routine fails for any reason (malloc returns NULL or unable
   890    940   ** to read from the disk) then the pMem is left in an inconsistent state.
   891    941   */
   892    942   int sqlite3VdbeMemFromBtree(
   893    943     BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
   894    944     u32 offset,       /* Offset from the start of data to return bytes from. */
................................................................................
   897    947     Mem *pMem         /* OUT: Return data in this Mem structure. */
   898    948   ){
   899    949     char *zData;        /* Data from the btree layer */
   900    950     u32 available = 0;  /* Number of bytes available on the local btree page */
   901    951     int rc = SQLITE_OK; /* Return code */
   902    952   
   903    953     assert( sqlite3BtreeCursorIsValid(pCur) );
          954  +  assert( !VdbeMemDynamic(pMem) );
   904    955   
   905    956     /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 
   906    957     ** that both the BtShared and database handle mutexes are held. */
   907    958     assert( (pMem->flags & MEM_RowSet)==0 );
   908    959     if( key ){
   909    960       zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
   910    961     }else{
   911    962       zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
   912    963     }
   913    964     assert( zData!=0 );
   914    965   
   915    966     if( offset+amt<=available ){
   916         -    sqlite3VdbeMemRelease(pMem);
   917    967       pMem->z = &zData[offset];
   918    968       pMem->flags = MEM_Blob|MEM_Ephem;
   919    969       pMem->n = (int)amt;
   920         -  }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
   921         -    if( key ){
   922         -      rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
   923         -    }else{
   924         -      rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
   925         -    }
   926         -    if( rc==SQLITE_OK ){
   927         -      pMem->z[amt] = 0;
   928         -      pMem->z[amt+1] = 0;
   929         -      pMem->flags = MEM_Blob|MEM_Term;
   930         -      pMem->n = (int)amt;
   931         -    }else{
   932         -      sqlite3VdbeMemRelease(pMem);
          970  +  }else{
          971  +    pMem->flags = MEM_Null;
          972  +    if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
          973  +      if( key ){
          974  +        rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
          975  +      }else{
          976  +        rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
          977  +      }
          978  +      if( rc==SQLITE_OK ){
          979  +        pMem->z[amt] = 0;
          980  +        pMem->z[amt+1] = 0;
          981  +        pMem->flags = MEM_Blob|MEM_Term;
          982  +        pMem->n = (int)amt;
          983  +      }else{
          984  +        sqlite3VdbeMemRelease(pMem);
          985  +      }
   933    986       }
   934    987     }
   935    988   
   936    989     return rc;
   937    990   }
   938    991   
   939    992   /*
................................................................................
  1149   1202       }
  1150   1203     }else if( op==TK_UMINUS ) {
  1151   1204       /* This branch happens for multiple negative signs.  Ex: -(-5) */
  1152   1205       if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) 
  1153   1206        && pVal!=0
  1154   1207       ){
  1155   1208         sqlite3VdbeMemNumerify(pVal);
  1156         -      if( pVal->u.i==SMALLEST_INT64 ){
  1157         -        pVal->flags &= ~MEM_Int;
  1158         -        pVal->flags |= MEM_Real;
  1159         -        pVal->r = (double)SMALLEST_INT64;
         1209  +      if( pVal->flags & MEM_Real ){
         1210  +        pVal->u.r = -pVal->u.r;
         1211  +      }else if( pVal->u.i==SMALLEST_INT64 ){
         1212  +        pVal->u.r = -(double)SMALLEST_INT64;
         1213  +        MemSetTypeFlag(pVal, MEM_Real);
  1160   1214         }else{
  1161   1215           pVal->u.i = -pVal->u.i;
  1162   1216         }
  1163         -      pVal->r = -pVal->r;
  1164   1217         sqlite3ValueApplyAffinity(pVal, affinity, enc);
  1165   1218       }
  1166   1219     }else if( op==TK_NULL ){
  1167   1220       pVal = valueNew(db, pCtx);
  1168   1221       if( pVal==0 ) goto no_mem;
  1169   1222     }
  1170   1223   #ifndef SQLITE_OMIT_BLOB_LITERAL
................................................................................
  1464   1517   void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
  1465   1518     if( pRec ){
  1466   1519       int i;
  1467   1520       int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
  1468   1521       Mem *aMem = pRec->aMem;
  1469   1522       sqlite3 *db = aMem[0].db;
  1470   1523       for(i=0; i<nCol; i++){
  1471         -      sqlite3DbFree(db, aMem[i].zMalloc);
         1524  +      if( aMem[i].szMalloc ) sqlite3DbFree(db, aMem[i].zMalloc);
  1472   1525       }
  1473   1526       sqlite3KeyInfoUnref(pRec->pKeyInfo);
  1474   1527       sqlite3DbFree(db, pRec);
  1475   1528     }
  1476   1529   }
  1477   1530   #endif /* ifdef SQLITE_ENABLE_STAT4 */
  1478   1531   

Changes to src/vdbesort.c.

   598    598   **
   599    599   ** Or, if an error occurs, return an SQLite error code. The final value of
   600    600   ** *pp is undefined in this case.
   601    601   */
   602    602   static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
   603    603     int rc = SQLITE_OK;
   604    604     if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
   605         -    rc = sqlite3OsFetch(pFile->pFd, 0, (int)pFile->iEof, (void**)pp);
   606         -    testcase( rc!=SQLITE_OK );
          605  +    sqlite3_file *pFd = pFile->pFd;
          606  +    if( pFd->pMethods->iVersion>=3 ){
          607  +      rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
          608  +      testcase( rc!=SQLITE_OK );
          609  +    }
   607    610     }
   608    611     return rc;
   609    612   }
   610    613   
   611    614   /*
   612    615   ** Attach PmaReader pReadr to file pFile (if it is not already attached to
   613    616   ** that file) and seek it to offset iOff within the file.  Return SQLITE_OK 
................................................................................
   754    757     const void *pKey1, int nKey1,   /* Left side of comparison */
   755    758     const void *pKey2, int nKey2    /* Right side of comparison */
   756    759   ){
   757    760     UnpackedRecord *r2 = pTask->pUnpacked;
   758    761     if( pKey2 ){
   759    762       sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
   760    763     }
   761         -  return sqlite3VdbeRecordCompare(nKey1, pKey1, r2, 0);
          764  +  return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
   762    765   }
   763    766   
   764    767   /*
   765    768   ** Initialize the temporary index cursor just opened as a sorter cursor.
   766    769   **
   767    770   ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField)
   768    771   ** to determine the number of fields that should be compared from the
................................................................................
  1117   1120   ** attempts to extend the file to nByte bytes in size and to ensure that
  1118   1121   ** the VFS has memory mapped it.
  1119   1122   **
  1120   1123   ** Whether or not the file does end up memory mapped of course depends on
  1121   1124   ** the specific VFS implementation.
  1122   1125   */
  1123   1126   static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
  1124         -  if( nByte<=(i64)(db->nMaxSorterMmap) ){
         1127  +  if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
  1125   1128       int rc = sqlite3OsTruncate(pFd, nByte);
  1126   1129       if( rc==SQLITE_OK ){
  1127   1130         void *p = 0;
  1128   1131         sqlite3OsFetch(pFd, 0, (int)nByte, &p);
  1129   1132         sqlite3OsUnfetch(pFd, 0, p);
  1130   1133       }
  1131   1134     }
................................................................................
  2055   2058                  );
  2056   2059     pReader->pIncr->pTask->bDone = 1;
  2057   2060     return pRet;
  2058   2061   }
  2059   2062   
  2060   2063   /*
  2061   2064   ** Use a background thread to invoke vdbePmaReaderIncrMergeInit(INCRINIT_TASK) 
  2062         -** on the the PmaReader object passed as the first argument.
         2065  +** on the PmaReader object passed as the first argument.
  2063   2066   **
  2064   2067   ** This call will initialize the various fields of the pReadr->pIncr 
  2065   2068   ** structure and, if it is a multi-threaded IncrMerger, launch a 
  2066   2069   ** background thread to populate aFile[1].
  2067   2070   */
  2068   2071   static int vdbePmaReaderBgIncrInit(PmaReader *pReadr){
  2069   2072     void *pCtx = (void*)pReadr;
................................................................................
  2454   2457   ** Copy the current sorter key into the memory cell pOut.
  2455   2458   */
  2456   2459   int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
  2457   2460     VdbeSorter *pSorter = pCsr->pSorter;
  2458   2461     void *pKey; int nKey;           /* Sorter key to copy into pOut */
  2459   2462   
  2460   2463     pKey = vdbeSorterRowkey(pSorter, &nKey);
  2461         -  if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
         2464  +  if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
  2462   2465       return SQLITE_NOMEM;
  2463   2466     }
  2464   2467     pOut->n = nKey;
  2465   2468     MemSetTypeFlag(pOut, MEM_Blob);
  2466   2469     memcpy(pOut->z, pKey, nKey);
  2467   2470   
  2468   2471     return SQLITE_OK;
................................................................................
  2510   2513     for(i=0; i<nKeyCol; i++){
  2511   2514       if( r2->aMem[i].flags & MEM_Null ){
  2512   2515         *pRes = -1;
  2513   2516         return SQLITE_OK;
  2514   2517       }
  2515   2518     }
  2516   2519   
  2517         -  *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2, 0);
         2520  +  *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
  2518   2521     return SQLITE_OK;
  2519   2522   }

Changes to src/vdbetrace.c.

    60     60   **
    61     61   ** The calling function is responsible for making sure the memory returned
    62     62   ** is eventually freed.
    63     63   **
    64     64   ** ALGORITHM:  Scan the input string looking for host parameters in any of
    65     65   ** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
    66     66   ** string literals, quoted identifier names, and comments.  For text forms,
    67         -** the host parameter index is found by scanning the perpared
           67  +** the host parameter index is found by scanning the prepared
    68     68   ** statement for the corresponding OP_Variable opcode.  Once the host
    69     69   ** parameter index is known, locate the value in p->aVar[].  Then render
    70     70   ** the value as a literal in place of the host parameter name.
    71     71   */
    72     72   char *sqlite3VdbeExpandSql(
    73     73     Vdbe *p,                 /* The prepared statement being evaluated */
    74     74     const char *zRawSql      /* Raw text of the SQL statement */
................................................................................
   123    123         assert( idx>0 && idx<=p->nVar );
   124    124         pVar = &p->aVar[idx-1];
   125    125         if( pVar->flags & MEM_Null ){
   126    126           sqlite3StrAccumAppend(&out, "NULL", 4);
   127    127         }else if( pVar->flags & MEM_Int ){
   128    128           sqlite3XPrintf(&out, 0, "%lld", pVar->u.i);
   129    129         }else if( pVar->flags & MEM_Real ){
   130         -        sqlite3XPrintf(&out, 0, "%!.15g", pVar->r);
          130  +        sqlite3XPrintf(&out, 0, "%!.15g", pVar->u.r);
   131    131         }else if( pVar->flags & MEM_Str ){
   132    132           int nOut;  /* Number of bytes of the string text to include in output */
   133    133   #ifndef SQLITE_OMIT_UTF16
   134    134           u8 enc = ENC(db);
   135    135           Mem utf8;
   136    136           if( enc!=SQLITE_UTF8 ){
   137    137             memset(&utf8, 0, sizeof(utf8));

Changes to src/wal.c.

   570    570     return (volatile WalIndexHdr*)pWal->apWiData[0];
   571    571   }
   572    572   
   573    573   /*
   574    574   ** The argument to this macro must be of type u32. On a little-endian
   575    575   ** architecture, it returns the u32 value that results from interpreting
   576    576   ** the 4 bytes as a big-endian value. On a big-endian architecture, it
   577         -** returns the value that would be produced by intepreting the 4 bytes
          577  +** returns the value that would be produced by interpreting the 4 bytes
   578    578   ** of the input value as a little-endian integer.
   579    579   */
   580    580   #define BYTESWAP32(x) ( \
   581    581       (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)  \
   582    582     + (((x)&0x00FF0000)>>8)  + (((x)&0xFF000000)>>24) \
   583    583   )
   584    584   
................................................................................
   984    984       int idx;                      /* Value to write to hash-table slot */
   985    985       int nCollide;                 /* Number of hash collisions */
   986    986   
   987    987       idx = iFrame - iZero;
   988    988       assert( idx <= HASHTABLE_NSLOT/2 + 1 );
   989    989       
   990    990       /* If this is the first entry to be added to this hash-table, zero the
   991         -    ** entire hash table and aPgno[] array before proceding. 
          991  +    ** entire hash table and aPgno[] array before proceeding. 
   992    992       */
   993    993       if( idx==1 ){
   994    994         int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
   995    995         memset((void*)&aPgno[1], 0, nByte);
   996    996       }
   997    997   
   998    998       /* If the entry in aPgno[] is already set, then the previous writer
................................................................................
  1647   1647   **
  1648   1648   ** Fsync is also called on the database file if (and only if) the entire
  1649   1649   ** WAL content is copied into the database file.  This second fsync makes
  1650   1650   ** it safe to delete the WAL since the new content will persist in the
  1651   1651   ** database file.
  1652   1652   **
  1653   1653   ** This routine uses and updates the nBackfill field of the wal-index header.
  1654         -** This is the only routine tha will increase the value of nBackfill.  
         1654  +** This is the only routine that will increase the value of nBackfill.  
  1655   1655   ** (A WAL reset or recovery will revert nBackfill to zero, but not increase
  1656   1656   ** its value.)
  1657   1657   **
  1658   1658   ** The caller must be holding sufficient locks to ensure that no other
  1659   1659   ** checkpoint is running (in any other thread or process) at the same
  1660   1660   ** time.
  1661   1661   */
................................................................................
  1954   1954   
  1955   1955   /*
  1956   1956   ** Read the wal-index header from the wal-index and into pWal->hdr.
  1957   1957   ** If the wal-header appears to be corrupt, try to reconstruct the
  1958   1958   ** wal-index from the WAL before returning.
  1959   1959   **
  1960   1960   ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
  1961         -** changed by this opertion.  If pWal->hdr is unchanged, set *pChanged
         1961  +** changed by this operation.  If pWal->hdr is unchanged, set *pChanged
  1962   1962   ** to 0.
  1963   1963   **
  1964   1964   ** If the wal-index header is successfully read, return SQLITE_OK. 
  1965   1965   ** Otherwise an SQLite error code.
  1966   1966   */
  1967   1967   static int walIndexReadHdr(Wal *pWal, int *pChanged){
  1968   1968     int rc;                         /* Return code */
................................................................................
  2158   2158       walShmBarrier(pWal);
  2159   2159       if( rc==SQLITE_OK ){
  2160   2160         if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
  2161   2161           /* It is not safe to allow the reader to continue here if frames
  2162   2162           ** may have been appended to the log before READ_LOCK(0) was obtained.
  2163   2163           ** When holding READ_LOCK(0), the reader ignores the entire log file,
  2164   2164           ** which implies that the database file contains a trustworthy
  2165         -        ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
         2165  +        ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
  2166   2166           ** happening, this is usually correct.
  2167   2167           **
  2168   2168           ** However, if frames have been appended to the log (or if the log 
  2169   2169           ** is wrapped and written for that matter) before the READ_LOCK(0)
  2170   2170           ** is obtained, that is not necessarily true. A checkpointer may
  2171   2171           ** have started to backfill the appended frames but crashed before
  2172   2172           ** it finished. Leaving a corrupt image in the database file.
................................................................................
  2860   2860     }
  2861   2861   
  2862   2862     /* If this is the end of a transaction, then we might need to pad
  2863   2863     ** the transaction and/or sync the WAL file.
  2864   2864     **
  2865   2865     ** Padding and syncing only occur if this set of frames complete a
  2866   2866     ** transaction and if PRAGMA synchronous=FULL.  If synchronous==NORMAL
  2867         -  ** or synchonous==OFF, then no padding or syncing are needed.
         2867  +  ** or synchronous==OFF, then no padding or syncing are needed.
  2868   2868     **
  2869   2869     ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
  2870   2870     ** needed and only the sync is done.  If padding is needed, then the
  2871   2871     ** final frame is repeated (with its commit mark) until the next sector
  2872   2872     ** boundary is crossed.  Only the part of the WAL prior to the last
  2873   2873     ** sector boundary is synced; the part of the last frame that extends
  2874   2874     ** past the sector boundary is written after the sync.

Changes to src/walker.c.

    15     15   #include "sqliteInt.h"
    16     16   #include <stdlib.h>
    17     17   #include <string.h>
    18     18   
    19     19   
    20     20   /*
    21     21   ** Walk an expression tree.  Invoke the callback once for each node
    22         -** of the expression, while decending.  (In other words, the callback
           22  +** of the expression, while descending.  (In other words, the callback
    23     23   ** is invoked before visiting children.)
    24     24   **
    25     25   ** The return value from the callback should be one of the WRC_*
    26     26   ** constants to specify how to proceed with the walk.
    27     27   **
    28     28   **    WRC_Continue      Continue descending down the tree.
    29     29   **

Changes to src/where.c.

   697    697           Vdbe *v = pParse->pVdbe;
   698    698           sqlite3VdbeSetVarmask(v, pRight->iColumn);
   699    699           if( *pisComplete && pRight->u.zToken[1] ){
   700    700             /* If the rhs of the LIKE expression is a variable, and the current
   701    701             ** value of the variable means there is no need to invoke the LIKE
   702    702             ** function, then no OP_Variable will be added to the program.
   703    703             ** This causes problems for the sqlite3_bind_parameter_name()
   704         -          ** API. To workaround them, add a dummy OP_Variable here.
          704  +          ** API. To work around them, add a dummy OP_Variable here.
   705    705             */ 
   706    706             int r1 = sqlite3GetTempReg(pParse);
   707    707             sqlite3ExprCodeTarget(pParse, pRight, r1);
   708    708             sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
   709    709             sqlite3ReleaseTempReg(pParse, r1);
   710    710           }
   711    711         }
................................................................................
   817    817   ** From another point of view, "indexable" means that the subterm could
   818    818   ** potentially be used with an index if an appropriate index exists.
   819    819   ** This analysis does not consider whether or not the index exists; that
   820    820   ** is decided elsewhere.  This analysis only looks at whether subterms
   821    821   ** appropriate for indexing exist.
   822    822   **
   823    823   ** All examples A through E above satisfy case 2.  But if a term
   824         -** also statisfies case 1 (such as B) we know that the optimizer will
          824  +** also satisfies case 1 (such as B) we know that the optimizer will
   825    825   ** always prefer case 1, so in that case we pretend that case 2 is not
   826    826   ** satisfied.
   827    827   **
   828    828   ** It might be the case that multiple tables are indexable.  For example,
   829    829   ** (E) above is indexable on tables P, Q, and R.
   830    830   **
   831    831   ** Terms that satisfy case 2 are candidates for lookup by using
................................................................................
   975    975             /* This is the 2-bit case and we are on the second iteration and
   976    976             ** current term is from the first iteration.  So skip this term. */
   977    977             assert( j==1 );
   978    978             continue;
   979    979           }
   980    980           if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
   981    981             /* This term must be of the form t1.a==t2.b where t2 is in the
   982         -          ** chngToIN set but t1 is not.  This term will be either preceeded
          982  +          ** chngToIN set but t1 is not.  This term will be either preceded
   983    983             ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
   984    984             ** and use its inversion. */
   985    985             testcase( pOrTerm->wtFlags & TERM_COPIED );
   986    986             testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
   987    987             assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
   988    988             continue;
   989    989           }
................................................................................
  1386   1386     /* Prevent ON clause terms of a LEFT JOIN from being used to drive
  1387   1387     ** an index for tables to the left of the join.
  1388   1388     */
  1389   1389     pTerm->prereqRight |= extraRight;
  1390   1390   }
  1391   1391   
  1392   1392   /*
  1393         -** This function searches pList for a entry that matches the iCol-th column
         1393  +** This function searches pList for an entry that matches the iCol-th column
  1394   1394   ** of index pIdx.
  1395   1395   **
  1396   1396   ** If such an expression is found, its index in pList->a[] is returned. If
  1397   1397   ** no expression is found, -1 is returned.
  1398   1398   */
  1399   1399   static int findIndexCol(
  1400   1400     Parse *pParse,                  /* Parse context */
................................................................................
  1909   1909   #endif
  1910   1910     assert( pRec!=0 );
  1911   1911     iCol = pRec->nField - 1;
  1912   1912     assert( pIdx->nSample>0 );
  1913   1913     assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
  1914   1914     do{
  1915   1915       iTest = (iMin+i)/2;
  1916         -    res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec, 0);
         1916  +    res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
  1917   1917       if( res<0 ){
  1918   1918         iMin = iTest+1;
  1919   1919       }else{
  1920   1920         i = iTest;
  1921   1921       }
  1922   1922     }while( res && iMin<i );
  1923   1923   
................................................................................
  1924   1924   #ifdef SQLITE_DEBUG
  1925   1925     /* The following assert statements check that the binary search code
  1926   1926     ** above found the right answer. This block serves no purpose other
  1927   1927     ** than to invoke the asserts.  */
  1928   1928     if( res==0 ){
  1929   1929       /* If (res==0) is true, then sample $i must be equal to pRec */
  1930   1930       assert( i<pIdx->nSample );
  1931         -    assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)
         1931  +    assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
  1932   1932            || pParse->db->mallocFailed );
  1933   1933     }else{
  1934   1934       /* Otherwise, pRec must be smaller than sample $i and larger than
  1935   1935       ** sample ($i-1).  */
  1936   1936       assert( i==pIdx->nSample 
  1937         -         || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)>0
         1937  +         || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
  1938   1938            || pParse->db->mallocFailed );
  1939   1939       assert( i==0
  1940         -         || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec, 0)<0
         1940  +         || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
  1941   1941            || pParse->db->mallocFailed );
  1942   1942     }
  1943   1943   #endif /* ifdef SQLITE_DEBUG */
  1944   1944   
  1945   1945     /* At this point, aSample[i] is the first sample that is greater than
  1946   1946     ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less
  1947   1947     ** than pVal.  If aSample[i]==pVal, then res==0.
................................................................................
  2136   2136   **
  2137   2137   ** then nEq is set to 0.
  2138   2138   **
  2139   2139   ** When this function is called, *pnOut is set to the sqlite3LogEst() of the
  2140   2140   ** number of rows that the index scan is expected to visit without 
  2141   2141   ** considering the range constraints. If nEq is 0, this is the number of 
  2142   2142   ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
  2143         -** to account for the range contraints pLower and pUpper.
         2143  +** to account for the range constraints pLower and pUpper.
  2144   2144   ** 
  2145   2145   ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
  2146   2146   ** used, a single range inequality reduces the search space by a factor of 4. 
  2147   2147   ** and a pair of constraints (x>? AND x<?) reduces the expected number of
  2148   2148   ** rows visited by a factor of 64.
  2149   2149   */
  2150   2150   static int whereRangeScanEst(
................................................................................
  3413   3413       **       A: <loop body>                 # Return data, whatever.
  3414   3414       **
  3415   3415       **          Return     2                # Jump back to the Gosub
  3416   3416       **
  3417   3417       **       B: <after the loop>
  3418   3418       **
  3419   3419       ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
  3420         -    ** use an ephermeral index instead of a RowSet to record the primary
         3420  +    ** use an ephemeral index instead of a RowSet to record the primary
  3421   3421       ** keys of the rows we have already seen.
  3422   3422       **
  3423   3423       */
  3424   3424       WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
  3425   3425       SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
  3426   3426       Index *pCov = 0;             /* Potential covering index (or NULL) */
  3427   3427       int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
................................................................................
  3464   3464           memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
  3465   3465         }
  3466   3466       }else{
  3467   3467         pOrTab = pWInfo->pTabList;
  3468   3468       }
  3469   3469   
  3470   3470       /* Initialize the rowset register to contain NULL. An SQL NULL is 
  3471         -    ** equivalent to an empty rowset.  Or, create an ephermeral index
         3471  +    ** equivalent to an empty rowset.  Or, create an ephemeral index
  3472   3472       ** capable of holding primary keys in the case of a WITHOUT ROWID.
  3473   3473       **
  3474   3474       ** Also initialize regReturn to contain the address of the instruction 
  3475   3475       ** immediately following the OP_Return at the bottom of the loop. This
  3476   3476       ** is required in a few obscure LEFT JOIN cases where control jumps
  3477   3477       ** over the top of the loop into the body of it. In this case the 
  3478   3478       ** correct response for the end-of-loop code (the OP_Return) is to 
................................................................................
  4217   4217   ** WHERE clause that reference the loop but which are not used by an
  4218   4218   ** index.
  4219   4219   **
  4220   4220   ** In the current implementation, the first extra WHERE clause term reduces
  4221   4221   ** the number of output rows by a factor of 10 and each additional term
  4222   4222   ** reduces the number of output rows by sqrt(2).
  4223   4223   */
  4224         -static void whereLoopOutputAdjust(WhereClause *pWC, WhereLoop *pLoop){
         4224  +static void whereLoopOutputAdjust(
         4225  +  WhereClause *pWC,      /* The WHERE clause */
         4226  +  WhereLoop *pLoop,      /* The loop to adjust downward */
         4227  +  LogEst nRow            /* Number of rows in the entire table */
         4228  +){
  4225   4229     WhereTerm *pTerm, *pX;
  4226   4230     Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
  4227   4231     int i, j;
         4232  +  int nEq = 0;    /* Number of = constraints not within likely()/unlikely() */
  4228   4233   
  4229         -  if( !OptimizationEnabled(pWC->pWInfo->pParse->db, SQLITE_AdjustOutEst) ){
  4230         -    return;
  4231         -  }
  4232   4234     for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
  4233   4235       if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
  4234   4236       if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
  4235   4237       if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
  4236   4238       for(j=pLoop->nLTerm-1; j>=0; j--){
  4237   4239         pX = pLoop->aLTerm[j];
  4238   4240         if( pX==0 ) continue;
  4239   4241         if( pX==pTerm ) break;
  4240   4242         if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
  4241   4243       }
  4242   4244       if( j<0 ){
  4243         -      pLoop->nOut += (pTerm->truthProb<=0 ? pTerm->truthProb : -1);
         4245  +      if( pTerm->truthProb<=0 ){
         4246  +        pLoop->nOut += pTerm->truthProb;
         4247  +      }else{
         4248  +        pLoop->nOut--;
         4249  +        if( pTerm->eOperator&WO_EQ ) nEq++;
         4250  +      }
  4244   4251       }
         4252  +  }
         4253  +  /* TUNING:  If there is at least one equality constraint in the WHERE
         4254  +  ** clause that does not have a likelihood() explicitly assigned to it
         4255  +  ** then do not let the estimated number of output rows exceed half 
         4256  +  ** the number of rows in the table. */
         4257  +  if( nEq && pLoop->nOut>nRow-10 ){
         4258  +    pLoop->nOut = nRow - 10;
  4245   4259     }
  4246   4260   }
  4247   4261   
  4248   4262   /*
  4249   4263   ** Adjust the cost C by the costMult facter T.  This only occurs if
  4250   4264   ** compiled with -DSQLITE_ENABLE_COSTMULT
  4251   4265   */
................................................................................
  4284   4298     u16 saved_nLTerm;               /* Original value of pNew->nLTerm */
  4285   4299     u16 saved_nEq;                  /* Original value of pNew->u.btree.nEq */
  4286   4300     u16 saved_nSkip;                /* Original value of pNew->u.btree.nSkip */
  4287   4301     u32 saved_wsFlags;              /* Original value of pNew->wsFlags */
  4288   4302     LogEst saved_nOut;              /* Original value of pNew->nOut */
  4289   4303     int iCol;                       /* Index of the column in the table */
  4290   4304     int rc = SQLITE_OK;             /* Return code */
         4305  +  LogEst rSize;                   /* Number of rows in the table */
  4291   4306     LogEst rLogSize;                /* Logarithm of table size */
  4292   4307     WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
  4293   4308   
  4294   4309     pNew = pBuilder->pNew;
  4295   4310     if( db->mallocFailed ) return SQLITE_NOMEM;
  4296   4311   
  4297   4312     assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
................................................................................
  4313   4328     saved_nEq = pNew->u.btree.nEq;
  4314   4329     saved_nSkip = pNew->u.btree.nSkip;
  4315   4330     saved_nLTerm = pNew->nLTerm;
  4316   4331     saved_wsFlags = pNew->wsFlags;
  4317   4332     saved_prereq = pNew->prereq;
  4318   4333     saved_nOut = pNew->nOut;
  4319   4334     pNew->rSetup = 0;
  4320         -  rLogSize = estLog(pProbe->aiRowLogEst[0]);
         4335  +  rSize = pProbe->aiRowLogEst[0];
         4336  +  rLogSize = estLog(rSize);
  4321   4337   
  4322   4338     /* Consider using a skip-scan if there are no WHERE clause constraints
  4323   4339     ** available for the left-most terms of the index, and if the average
  4324   4340     ** number of repeats in the left-most terms is at least 18. 
  4325   4341     **
  4326   4342     ** The magic number 18 is selected on the basis that scanning 17 rows
  4327   4343     ** is almost always quicker than an index seek (even though if the index
................................................................................
  4490   4506         pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
  4491   4507       }
  4492   4508       ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
  4493   4509   
  4494   4510       nOutUnadjusted = pNew->nOut;
  4495   4511       pNew->rRun += nInMul + nIn;
  4496   4512       pNew->nOut += nInMul + nIn;
  4497         -    whereLoopOutputAdjust(pBuilder->pWC, pNew);
         4513  +    whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
  4498   4514       rc = whereLoopInsert(pBuilder, pNew);
  4499   4515   
  4500   4516       if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
  4501   4517         pNew->nOut = saved_nOut;
  4502   4518       }else{
  4503   4519         pNew->nOut = nOutUnadjusted;
  4504   4520       }
................................................................................
  4540   4556   
  4541   4557     if( pIndex->bUnordered ) return 0;
  4542   4558     if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
  4543   4559     for(ii=0; ii<pOB->nExpr; ii++){
  4544   4560       Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
  4545   4561       if( pExpr->op!=TK_COLUMN ) return 0;
  4546   4562       if( pExpr->iTable==iCursor ){
         4563  +      if( pExpr->iColumn<0 ) return 1;
  4547   4564         for(jj=0; jj<pIndex->nKeyCol; jj++){
  4548   4565           if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
  4549   4566         }
  4550   4567       }
  4551   4568     }
  4552   4569     return 0;
  4553   4570   }
................................................................................
  4703   4720           /* TUNING: One-time cost for computing the automatic index is
  4704   4721           ** approximately 7*N*log2(N) where N is the number of rows in
  4705   4722           ** the table being indexed. */
  4706   4723           pNew->rSetup = rLogSize + rSize + 28;  assert( 28==sqlite3LogEst(7) );
  4707   4724           ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
  4708   4725           /* TUNING: Each index lookup yields 20 rows in the table.  This
  4709   4726           ** is more than the usual guess of 10 rows, since we have no way
  4710         -        ** of knowning how selective the index will ultimately be.  It would
         4727  +        ** of knowing how selective the index will ultimately be.  It would
  4711   4728           ** not be unreasonable to make this value much larger. */
  4712   4729           pNew->nOut = 43;  assert( 43==sqlite3LogEst(20) );
  4713   4730           pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
  4714   4731           pNew->wsFlags = WHERE_AUTO_INDEX;
  4715   4732           pNew->prereq = mExtra | pTerm->prereqRight;
  4716   4733           rc = whereLoopInsert(pBuilder, pNew);
  4717   4734         }
................................................................................
  4744   4761         pNew->wsFlags = WHERE_IPK;
  4745   4762   
  4746   4763         /* Full table scan */
  4747   4764         pNew->iSortIdx = b ? iSortIdx : 0;
  4748   4765         /* TUNING: Cost of full table scan is (N*3.0). */
  4749   4766         pNew->rRun = rSize + 16;
  4750   4767         ApplyCostMultiplier(pNew->rRun, pTab->costMult);
  4751         -      whereLoopOutputAdjust(pWC, pNew);
         4768  +      whereLoopOutputAdjust(pWC, pNew, rSize);
  4752   4769         rc = whereLoopInsert(pBuilder, pNew);
  4753   4770         pNew->nOut = rSize;
  4754   4771         if( rc ) break;
  4755   4772       }else{
  4756   4773         Bitmask m;
  4757   4774         if( pProbe->isCovering ){
  4758   4775           pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
................................................................................
  4780   4797           ** index and table rows. If this is a non-covering index scan,
  4781   4798           ** also add the cost of visiting table rows (N*3.0).  */
  4782   4799           pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
  4783   4800           if( m!=0 ){
  4784   4801             pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
  4785   4802           }
  4786   4803           ApplyCostMultiplier(pNew->rRun, pTab->costMult);
  4787         -        whereLoopOutputAdjust(pWC, pNew);
         4804  +        whereLoopOutputAdjust(pWC, pNew, rSize);
  4788   4805           rc = whereLoopInsert(pBuilder, pNew);
  4789   4806           pNew->nOut = rSize;
  4790   4807           if( rc ) break;
  4791   4808         }
  4792   4809       }
  4793   4810   
  4794   4811       rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
................................................................................
  5133   5150   **   N==0:  No terms of the ORDER BY clause are satisfied
  5134   5151   **   N<0:   Unknown yet how many terms of ORDER BY might be satisfied.   
  5135   5152   **
  5136   5153   ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
  5137   5154   ** strict.  With GROUP BY and DISTINCT the only requirement is that
  5138   5155   ** equivalent rows appear immediately adjacent to one another.  GROUP BY
  5139   5156   ** and DISTINCT do not require rows to appear in any particular order as long
  5140         -** as equivelent rows are grouped together.  Thus for GROUP BY and DISTINCT
         5157  +** as equivalent rows are grouped together.  Thus for GROUP BY and DISTINCT
  5141   5158   ** the pOrderBy terms can be matched in any order.  With ORDER BY, the 
  5142   5159   ** pOrderBy terms must be matched in strict left-to-right order.
  5143   5160   */
  5144   5161   static i8 wherePathSatisfiesOrderBy(
  5145   5162     WhereInfo *pWInfo,    /* The WHERE clause */
  5146   5163     ExprList *pOrderBy,   /* ORDER BY or GROUP BY or DISTINCT clause to check */
  5147   5164     WherePath *pPath,     /* The WherePath to check */

Changes to src/whereInt.h.

   172    172   ** Then a WherePath object is a path through the graph that visits some
   173    173   ** or all of the WhereLoop objects once.
   174    174   **
   175    175   ** The "solver" works by creating the N best WherePath objects of length
   176    176   ** 1.  Then using those as a basis to compute the N best WherePath objects
   177    177   ** of length 2.  And so forth until the length of WherePaths equals the
   178    178   ** number of nodes in the FROM clause.  The best (lowest cost) WherePath
   179         -** at the end is the choosen query plan.
          179  +** at the end is the chosen query plan.
   180    180   */
   181    181   struct WherePath {
   182    182     Bitmask maskLoop;     /* Bitmask of all WhereLoop objects in this path */
   183    183     Bitmask revLoop;      /* aLoop[]s that should be reversed for ORDER BY */
   184    184     LogEst nRow;          /* Estimated number of rows generated by this path */
   185    185     LogEst rCost;         /* Total cost of this path */
   186    186     LogEst rUnsorted;     /* Total cost of this path ignoring sorting costs */

Changes to test/aggnested.test.

   152    152       INSERT INTO t2 VALUES(1);
   153    153       SELECT
   154    154        (SELECT sum(value2==xyz) FROM t2)
   155    155       FROM
   156    156        (SELECT value1 as xyz, max(x1) AS pqr
   157    157           FROM t1
   158    158          GROUP BY id1);
          159  +    SELECT
          160  +     (SELECT sum(value2<>xyz) FROM t2)
          161  +    FROM
          162  +     (SELECT value1 as xyz, max(x1) AS pqr
          163  +        FROM t1
          164  +       GROUP BY id1);
   159    165     }
   160         -} {0}
          166  +} {1 0}
   161    167   do_test aggnested-3.3 {
   162    168     db eval {
   163    169       DROP TABLE IF EXISTS t1;
   164    170       DROP TABLE IF EXISTS t2;
   165    171       CREATE TABLE t1(id1, value1);
   166    172       INSERT INTO t1 VALUES(4469,2),(4469,1);
   167    173       CREATE TABLE t2 (value2);

Changes to test/auth.test.

    32     32       db authorizer ::auth
    33     33     }
    34     34   }
    35     35   
    36     36   do_test auth-1.1.1 {
    37     37     db close
    38     38     set ::DB [sqlite3 db test.db]
    39         -  proc auth {code arg1 arg2 arg3 arg4} {
           39  +  proc auth {code arg1 arg2 arg3 arg4 args} {
    40     40       if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
    41     41         return SQLITE_DENY
    42     42       }
    43     43       return SQLITE_OK
    44     44     }
    45     45     db authorizer ::auth
    46     46     catchsql {CREATE TABLE t1(a,b,c)}
................................................................................
    57     57       SELECT x;
    58     58     }
    59     59   } {1 {no such column: x}}
    60     60   do_test auth-1.2 {
    61     61     execsql {SELECT name FROM sqlite_master}
    62     62   } {}
    63     63   do_test auth-1.3.1 {
    64         -  proc auth {code arg1 arg2 arg3 arg4} {
           64  +  proc auth {code arg1 arg2 arg3 arg4 args} {
    65     65       if {$code=="SQLITE_CREATE_TABLE"} {
    66     66         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
    67     67         return SQLITE_DENY
    68     68       }
    69     69       return SQLITE_OK
    70     70     }
    71     71     catchsql {CREATE TABLE t1(a,b,c)}
................................................................................
    78     78   } {t1 {} main {}}
    79     79   do_test auth-1.4 {
    80     80     execsql {SELECT name FROM sqlite_master}
    81     81   } {}
    82     82   
    83     83   ifcapable tempdb {
    84     84     do_test auth-1.5 {
    85         -    proc auth {code arg1 arg2 arg3 arg4} {
           85  +    proc auth {code arg1 arg2 arg3 arg4 args} {
    86     86         if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
    87     87           return SQLITE_DENY
    88     88         }
    89     89         return SQLITE_OK
    90     90       }
    91     91       catchsql {CREATE TEMP TABLE t1(a,b,c)}
    92     92     } {1 {not authorized}}
    93     93     do_test auth-1.6 {
    94     94       execsql {SELECT name FROM sqlite_temp_master}
    95     95     } {}
    96     96     do_test auth-1.7.1 {
    97         -    proc auth {code arg1 arg2 arg3 arg4} {
           97  +    proc auth {code arg1 arg2 arg3 arg4 args} {
    98     98         if {$code=="SQLITE_CREATE_TEMP_TABLE"} {
    99     99           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   100    100           return SQLITE_DENY
   101    101         }
   102    102         return SQLITE_OK
   103    103       }
   104    104       catchsql {CREATE TEMP TABLE t1(a,b,c)}
................................................................................
   108    108     } {t1 {} temp {}}
   109    109     do_test auth-1.8 {
   110    110       execsql {SELECT name FROM sqlite_temp_master}
   111    111     } {}
   112    112   }
   113    113   
   114    114   do_test auth-1.9 {
   115         -  proc auth {code arg1 arg2 arg3 arg4} {
          115  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   116    116       if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
   117    117         return SQLITE_IGNORE
   118    118       }
   119    119       return SQLITE_OK
   120    120     }
   121    121     catchsql {CREATE TABLE t1(a,b,c)}
   122    122   } {0 {}}
   123    123   do_test auth-1.10 {
   124    124     execsql {SELECT name FROM sqlite_master}
   125    125   } {}
   126    126   do_test auth-1.11 {
   127         -  proc auth {code arg1 arg2 arg3 arg4} {
          127  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   128    128       if {$code=="SQLITE_CREATE_TABLE"} {
   129    129         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   130    130         return SQLITE_IGNORE
   131    131       }
   132    132       return SQLITE_OK
   133    133     }
   134    134     catchsql {CREATE TABLE t1(a,b,c)}
................................................................................
   135    135   } {0 {}}
   136    136   do_test auth-1.12 {
   137    137     execsql {SELECT name FROM sqlite_master}
   138    138   } {}
   139    139   
   140    140   ifcapable tempdb {
   141    141     do_test auth-1.13 {
   142         -    proc auth {code arg1 arg2 arg3 arg4} {
          142  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   143    143         if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
   144    144           return SQLITE_IGNORE
   145    145         }
   146    146         return SQLITE_OK
   147    147       }
   148    148       catchsql {CREATE TEMP TABLE t1(a,b,c)}
   149    149     } {0 {}}
   150    150     do_test auth-1.14 {
   151    151       execsql {SELECT name FROM sqlite_temp_master}
   152    152     } {}
   153    153     do_test auth-1.15 {
   154         -    proc auth {code arg1 arg2 arg3 arg4} {
          154  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   155    155         if {$code=="SQLITE_CREATE_TEMP_TABLE"} {
   156    156           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   157    157           return SQLITE_IGNORE
   158    158         }
   159    159         return SQLITE_OK
   160    160       }
   161    161       catchsql {CREATE TEMP TABLE t1(a,b,c)}
   162    162     } {0 {}}
   163    163     do_test auth-1.16 {
   164    164       execsql {SELECT name FROM sqlite_temp_master}
   165    165     } {}
   166    166     
   167    167     do_test auth-1.17 {
   168         -    proc auth {code arg1 arg2 arg3 arg4} {
          168  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   169    169         if {$code=="SQLITE_CREATE_TABLE"} {
   170    170           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   171    171           return SQLITE_DENY
   172    172         }
   173    173         return SQLITE_OK
   174    174       }
   175    175       catchsql {CREATE TEMP TABLE t1(a,b,c)}
................................................................................
   177    177     do_test auth-1.18 {
   178    178       execsql {SELECT name FROM sqlite_temp_master}
   179    179     } {t1}
   180    180   }
   181    181   
   182    182   do_test auth-1.19.1 {
   183    183     set ::authargs {}
   184         -  proc auth {code arg1 arg2 arg3 arg4} {
          184  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   185    185       if {$code=="SQLITE_CREATE_TEMP_TABLE"} {
   186    186         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   187    187         return SQLITE_DENY
   188    188       }
   189    189       return SQLITE_OK
   190    190     }
   191    191     catchsql {CREATE TABLE t2(a,b,c)}
................................................................................
   194    194     set ::authargs
   195    195   } {}
   196    196   do_test auth-1.20 {
   197    197     execsql {SELECT name FROM sqlite_master}
   198    198   } {t2}
   199    199   
   200    200   do_test auth-1.21.1 {
   201         -  proc auth {code arg1 arg2 arg3 arg4} {
          201  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   202    202       if {$code=="SQLITE_DROP_TABLE"} {
   203    203         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   204    204         return SQLITE_DENY
   205    205       }
   206    206       return SQLITE_OK
   207    207     }
   208    208     catchsql {DROP TABLE t2}
................................................................................
   210    210   do_test auth-1.21.2 {
   211    211     set ::authargs
   212    212   } {t2 {} main {}}
   213    213   do_test auth-1.22 {
   214    214     execsql {SELECT name FROM sqlite_master}
   215    215   } {t2}
   216    216   do_test auth-1.23.1 {
   217         -  proc auth {code arg1 arg2 arg3 arg4} {
          217  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   218    218       if {$code=="SQLITE_DROP_TABLE"} {
   219    219         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   220    220         return SQLITE_IGNORE
   221    221       }
   222    222       return SQLITE_OK
   223    223     }
   224    224     catchsql {DROP TABLE t2}
................................................................................
   228    228   } {t2 {} main {}}
   229    229   do_test auth-1.24 {
   230    230     execsql {SELECT name FROM sqlite_master}
   231    231   } {t2}
   232    232   
   233    233   ifcapable tempdb {
   234    234     do_test auth-1.25 {
   235         -    proc auth {code arg1 arg2 arg3 arg4} {
          235  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   236    236         if {$code=="SQLITE_DROP_TEMP_TABLE"} {
   237    237           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   238    238           return SQLITE_DENY
   239    239         }
   240    240         return SQLITE_OK
   241    241       }
   242    242       catchsql {DROP TABLE t1}
   243    243     } {1 {not authorized}}
   244    244     do_test auth-1.26 {
   245    245       execsql {SELECT name FROM sqlite_temp_master}
   246    246     } {t1}
   247    247     do_test auth-1.27 {
   248         -    proc auth {code arg1 arg2 arg3 arg4} {
          248  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   249    249         if {$code=="SQLITE_DROP_TEMP_TABLE"} {
   250    250           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   251    251           return SQLITE_IGNORE
   252    252         }
   253    253         return SQLITE_OK
   254    254       }
   255    255       catchsql {DROP TABLE t1}
................................................................................
   256    256     } {0 {}}
   257    257     do_test auth-1.28 {
   258    258       execsql {SELECT name FROM sqlite_temp_master}
   259    259     } {t1}
   260    260   }
   261    261   
   262    262   do_test auth-1.29 {
   263         -  proc auth {code arg1 arg2 arg3 arg4} {
          263  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   264    264       if {$code=="SQLITE_INSERT" && $arg1=="t2"} {
   265    265         return SQLITE_DENY
   266    266       }
   267    267       return SQLITE_OK
   268    268     }
   269    269     catchsql {INSERT INTO t2 VALUES(1,2,3)}
   270    270   } {1 {not authorized}}
   271    271   do_test auth-1.30 {
   272    272     execsql {SELECT * FROM t2}
   273    273   } {}
   274    274   do_test auth-1.31 {
   275         -  proc auth {code arg1 arg2 arg3 arg4} {
          275  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   276    276       if {$code=="SQLITE_INSERT" && $arg1=="t2"} {
   277    277         return SQLITE_IGNORE
   278    278       }
   279    279       return SQLITE_OK
   280    280     }
   281    281     catchsql {INSERT INTO t2 VALUES(1,2,3)}
   282    282   } {0 {}}
   283    283   do_test auth-1.32 {
   284    284     execsql {SELECT * FROM t2}
   285    285   } {}
   286    286   do_test auth-1.33 {
   287         -  proc auth {code arg1 arg2 arg3 arg4} {
          287  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   288    288       if {$code=="SQLITE_INSERT" && $arg1=="t1"} {
   289    289         return SQLITE_IGNORE
   290    290       }
   291    291       return SQLITE_OK
   292    292     }
   293    293     catchsql {INSERT INTO t2 VALUES(1,2,3)}
   294    294   } {0 {}}
   295    295   do_test auth-1.34 {
   296    296     execsql {SELECT * FROM t2}
   297    297   } {1 2 3}
   298    298   
   299    299   do_test auth-1.35.1 {
   300         -  proc auth {code arg1 arg2 arg3 arg4} {
          300  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   301    301       if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="b"} {
   302    302         return SQLITE_DENY
   303    303       }
   304    304       return SQLITE_OK
   305    305     }
   306    306     catchsql {SELECT * FROM t2}
   307    307   } {1 {access to t2.b is prohibited}}
................................................................................
   309    309     do_test auth-1.35.2 {
   310    310       execsql {ATTACH DATABASE 'test.db' AS two}
   311    311       catchsql {SELECT * FROM two.t2}
   312    312     } {1 {access to two.t2.b is prohibited}}
   313    313     execsql {DETACH DATABASE two}
   314    314   }
   315    315   do_test auth-1.36 {
   316         -  proc auth {code arg1 arg2 arg3 arg4} {
          316  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   317    317       if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="b"} {
   318    318         return SQLITE_IGNORE
   319    319       }
   320    320       return SQLITE_OK
   321    321     }
   322    322     catchsql {SELECT * FROM t2}
   323    323   } {0 {1 {} 3}}
   324    324   do_test auth-1.37 {
   325         -  proc auth {code arg1 arg2 arg3 arg4} {
          325  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   326    326       if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="b"} {
   327    327         return SQLITE_IGNORE
   328    328       }
   329    329       return SQLITE_OK
   330    330     }
   331    331     catchsql {SELECT * FROM t2 WHERE b=2}
   332    332   } {0 {}}
   333    333   do_test auth-1.38 {
   334         -  proc auth {code arg1 arg2 arg3 arg4} {
          334  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   335    335       if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="a"} {
   336    336         return SQLITE_IGNORE
   337    337       }
   338    338       return SQLITE_OK
   339    339     }
   340    340     catchsql {SELECT * FROM t2 WHERE b=2}
   341    341   } {0 {{} 2 3}}
   342    342   do_test auth-1.39 {
   343         -  proc auth {code arg1 arg2 arg3 arg4} {
          343  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   344    344       if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="b"} {
   345    345         return SQLITE_IGNORE
   346    346       }
   347    347       return SQLITE_OK
   348    348     }
   349    349     catchsql {SELECT * FROM t2 WHERE b IS NULL}
   350    350   } {0 {1 {} 3}}
   351    351   do_test auth-1.40 {
   352         -  proc auth {code arg1 arg2 arg3 arg4} {
          352  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   353    353       if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="b"} {
   354    354         return SQLITE_DENY
   355    355       }
   356    356       return SQLITE_OK
   357    357     }
   358    358     catchsql {SELECT a,c FROM t2 WHERE b IS NULL}
   359    359   } {1 {access to t2.b is prohibited}}
   360    360     
   361    361   do_test auth-1.41 {
   362         -  proc auth {code arg1 arg2 arg3 arg4} {
          362  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   363    363       if {$code=="SQLITE_UPDATE" && $arg1=="t2" && $arg2=="b"} {
   364    364         return SQLITE_DENY
   365    365       }
   366    366       return SQLITE_OK
   367    367     }
   368    368     catchsql {UPDATE t2 SET a=11}
   369    369   } {0 {}}
   370    370   do_test auth-1.42 {
   371    371     execsql {SELECT * FROM t2}
   372    372   } {11 2 3}
   373    373   do_test auth-1.43 {
   374         -  proc auth {code arg1 arg2 arg3 arg4} {
          374  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   375    375       if {$code=="SQLITE_UPDATE" && $arg1=="t2" && $arg2=="b"} {
   376    376         return SQLITE_DENY
   377    377       }
   378    378       return SQLITE_OK
   379    379     }
   380    380     catchsql {UPDATE t2 SET b=22, c=33}
   381    381   } {1 {not authorized}}
   382    382   do_test auth-1.44 {
   383    383     execsql {SELECT * FROM t2}
   384    384   } {11 2 3}
   385    385   do_test auth-1.45 {
   386         -  proc auth {code arg1 arg2 arg3 arg4} {
          386  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   387    387       if {$code=="SQLITE_UPDATE" && $arg1=="t2" && $arg2=="b"} {
   388    388         return SQLITE_IGNORE
   389    389       }
   390    390       return SQLITE_OK
   391    391     }
   392    392     catchsql {UPDATE t2 SET b=22, c=33}
   393    393   } {0 {}}
   394    394   do_test auth-1.46 {
   395    395     execsql {SELECT * FROM t2}
   396    396   } {11 2 33}
   397    397   
   398    398   do_test auth-1.47 {
   399         -  proc auth {code arg1 arg2 arg3 arg4} {
          399  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   400    400       if {$code=="SQLITE_DELETE" && $arg1=="t2"} {
   401    401         return SQLITE_DENY
   402    402       }
   403    403       return SQLITE_OK
   404    404     }
   405    405     catchsql {DELETE FROM t2 WHERE a=11}
   406    406   } {1 {not authorized}}
   407    407   do_test auth-1.48 {
   408    408     execsql {SELECT * FROM t2}
   409    409   } {11 2 33}
   410    410   do_test auth-1.49 {
   411         -  proc auth {code arg1 arg2 arg3 arg4} {
          411  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   412    412       if {$code=="SQLITE_DELETE" && $arg1=="t2"} {
   413    413         return SQLITE_IGNORE
   414    414       }
   415    415       return SQLITE_OK
   416    416     }
   417    417     catchsql {DELETE FROM t2 WHERE a=11}
   418    418   } {0 {}}
................................................................................
   420    420     execsql {SELECT * FROM t2}
   421    421   } {}
   422    422   do_test auth-1.50.2 {
   423    423     execsql {INSERT INTO t2 VALUES(11, 2, 33)}
   424    424   } {}
   425    425   
   426    426   do_test auth-1.51 {
   427         -  proc auth {code arg1 arg2 arg3 arg4} {
          427  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   428    428       if {$code=="SQLITE_SELECT"} {
   429    429         return SQLITE_DENY
   430    430       }
   431    431       return SQLITE_OK
   432    432     }
   433    433     catchsql {SELECT * FROM t2}
   434    434   } {1 {not authorized}}
   435    435   do_test auth-1.52 {
   436         -  proc auth {code arg1 arg2 arg3 arg4} {
          436  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   437    437       if {$code=="SQLITE_SELECT"} {
   438    438         return SQLITE_IGNORE
   439    439       }
   440    440       return SQLITE_OK
   441    441     }
   442    442     catchsql {SELECT * FROM t2}
   443    443   } {0 {}}
   444    444   do_test auth-1.53 {
   445         -  proc auth {code arg1 arg2 arg3 arg4} {
          445  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   446    446       if {$code=="SQLITE_SELECT"} {
   447    447         return SQLITE_OK
   448    448       }
   449    449       return SQLITE_OK
   450    450     }
   451    451     catchsql {SELECT * FROM t2}
   452    452   } {0 {11 2 33}}
................................................................................
   458    458     execsql {INSERT INTO t2 VALUES(7, 8, 9);}
   459    459   } {}
   460    460   do_test auth-1.55 {
   461    461     execsql {SELECT * FROM t2}
   462    462   } {11 2 33 7 8 9}
   463    463   
   464    464   do_test auth-1.63 {
   465         -  proc auth {code arg1 arg2 arg3 arg4} {
          465  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   466    466       if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
   467    467          return SQLITE_DENY
   468    468       }
   469    469       return SQLITE_OK
   470    470     }
   471    471     catchsql {DROP TABLE t2}
   472    472   } {1 {not authorized}}
   473    473   do_test auth-1.64 {
   474    474     execsql {SELECT name FROM sqlite_master}
   475    475   } {t2}
   476    476   do_test auth-1.65 {
   477         -  proc auth {code arg1 arg2 arg3 arg4} {
          477  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   478    478       if {$code=="SQLITE_DELETE" && $arg1=="t2"} {
   479    479          return SQLITE_DENY
   480    480       }
   481    481       return SQLITE_OK
   482    482     }
   483    483     catchsql {DROP TABLE t2}
   484    484   } {1 {not authorized}}
   485    485   do_test auth-1.66 {
   486    486     execsql {SELECT name FROM sqlite_master}
   487    487   } {t2}
   488    488   
   489    489   ifcapable tempdb {
   490    490     do_test auth-1.67 {
   491         -    proc auth {code arg1 arg2 arg3 arg4} {
          491  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   492    492         if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
   493    493            return SQLITE_DENY
   494    494         }
   495    495         return SQLITE_OK
   496    496       }
   497    497       catchsql {DROP TABLE t1}
   498    498     } {1 {not authorized}}
   499    499     do_test auth-1.68 {
   500    500       execsql {SELECT name FROM sqlite_temp_master}
   501    501     } {t1}
   502    502     do_test auth-1.69 {
   503         -    proc auth {code arg1 arg2 arg3 arg4} {
          503  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   504    504         if {$code=="SQLITE_DELETE" && $arg1=="t1"} {
   505    505            return SQLITE_DENY
   506    506         }
   507    507         return SQLITE_OK
   508    508       }
   509    509       catchsql {DROP TABLE t1}
   510    510     } {1 {not authorized}}
   511    511     do_test auth-1.70 {
   512    512       execsql {SELECT name FROM sqlite_temp_master}
   513    513     } {t1}
   514    514   }
   515    515   
   516    516   do_test auth-1.71 {
   517         -  proc auth {code arg1 arg2 arg3 arg4} {
          517  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   518    518       if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
   519    519          return SQLITE_IGNORE
   520    520       }
   521    521       return SQLITE_OK
   522    522     }
   523    523     catchsql {DROP TABLE t2}
   524    524   } {0 {}}
   525    525   do_test auth-1.72 {
   526    526     execsql {SELECT name FROM sqlite_master}
   527    527   } {t2}
   528    528   do_test auth-1.73 {
   529         -  proc auth {code arg1 arg2 arg3 arg4} {
          529  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   530    530       if {$code=="SQLITE_DELETE" && $arg1=="t2"} {
   531    531          return SQLITE_IGNORE
   532    532       }
   533    533       return SQLITE_OK
   534    534     }
   535    535     catchsql {DROP TABLE t2}
   536    536   } {0 {}}
   537    537   do_test auth-1.74 {
   538    538     execsql {SELECT name FROM sqlite_master}
   539    539   } {t2}
   540    540   
   541    541   ifcapable tempdb {
   542    542     do_test auth-1.75 {
   543         -    proc auth {code arg1 arg2 arg3 arg4} {
          543  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   544    544         if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
   545    545            return SQLITE_IGNORE
   546    546         }
   547    547         return SQLITE_OK
   548    548       }
   549    549       catchsql {DROP TABLE t1}
   550    550     } {0 {}}
   551    551     do_test auth-1.76 {
   552    552       execsql {SELECT name FROM sqlite_temp_master}
   553    553     } {t1}
   554    554     do_test auth-1.77 {
   555         -    proc auth {code arg1 arg2 arg3 arg4} {
          555  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   556    556         if {$code=="SQLITE_DELETE" && $arg1=="t1"} {
   557    557            return SQLITE_IGNORE
   558    558         }
   559    559         return SQLITE_OK
   560    560       }
   561    561       catchsql {DROP TABLE t1}
   562    562     } {0 {}}
................................................................................
   565    565     } {t1}
   566    566   }
   567    567   
   568    568   # Test cases auth-1.79 to auth-1.124 test creating and dropping views.
   569    569   # Omit these if the library was compiled with views omitted.
   570    570   ifcapable view {
   571    571   do_test auth-1.79 {
   572         -  proc auth {code arg1 arg2 arg3 arg4} {
          572  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   573    573       if {$code=="SQLITE_CREATE_VIEW"} {
   574    574         set ::authargs [list $arg1 $arg2 $arg3 $arg4] 
   575    575         return SQLITE_DENY
   576    576       }
   577    577       return SQLITE_OK
   578    578     }
   579    579     catchsql {CREATE VIEW v1 AS SELECT a+1,b+1 FROM t2}
................................................................................
   581    581   do_test auth-1.80 {
   582    582     set ::authargs
   583    583   } {v1 {} main {}}
   584    584   do_test auth-1.81 {
   585    585     execsql {SELECT name FROM sqlite_master}
   586    586   } {t2}
   587    587   do_test auth-1.82 {
   588         -  proc auth {code arg1 arg2 arg3 arg4} {
          588  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   589    589       if {$code=="SQLITE_CREATE_VIEW"} {
   590    590         set ::authargs [list $arg1 $arg2 $arg3 $arg4] 
   591    591         return SQLITE_IGNORE
   592    592       }
   593    593       return SQLITE_OK
   594    594     }
   595    595     catchsql {CREATE VIEW v1 AS SELECT a+1,b+1 FROM t2}
................................................................................
   599    599   } {v1 {} main {}}
   600    600   do_test auth-1.84 {
   601    601     execsql {SELECT name FROM sqlite_master}
   602    602   } {t2}
   603    603   
   604    604   ifcapable tempdb {
   605    605     do_test auth-1.85 {
   606         -    proc auth {code arg1 arg2 arg3 arg4} {
          606  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   607    607         if {$code=="SQLITE_CREATE_TEMP_VIEW"} {
   608    608           set ::authargs [list $arg1 $arg2 $arg3 $arg4] 
   609    609           return SQLITE_DENY
   610    610         }
   611    611         return SQLITE_OK
   612    612       }
   613    613       catchsql {CREATE TEMPORARY VIEW v1 AS SELECT a+1,b+1 FROM t2}
................................................................................
   615    615     do_test auth-1.86 {
   616    616       set ::authargs
   617    617     } {v1 {} temp {}}
   618    618     do_test auth-1.87 {
   619    619       execsql {SELECT name FROM sqlite_temp_master}
   620    620     } {t1}
   621    621     do_test auth-1.88 {
   622         -    proc auth {code arg1 arg2 arg3 arg4} {
          622  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   623    623         if {$code=="SQLITE_CREATE_TEMP_VIEW"} {
   624    624           set ::authargs [list $arg1 $arg2 $arg3 $arg4] 
   625    625           return SQLITE_IGNORE
   626    626         }
   627    627         return SQLITE_OK
   628    628       }
   629    629       catchsql {CREATE TEMPORARY VIEW v1 AS SELECT a+1,b+1 FROM t2}
................................................................................
   633    633     } {v1 {} temp {}}
   634    634     do_test auth-1.90 {
   635    635       execsql {SELECT name FROM sqlite_temp_master}
   636    636     } {t1}
   637    637   }
   638    638   
   639    639   do_test auth-1.91 {
   640         -  proc auth {code arg1 arg2 arg3 arg4} {
          640  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   641    641       if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
   642    642         return SQLITE_DENY
   643    643       }
   644    644       return SQLITE_OK
   645    645     }
   646    646     catchsql {CREATE VIEW v1 AS SELECT a+1,b+1 FROM t2}
   647    647   } {1 {not authorized}}
   648    648   do_test auth-1.92 {
   649    649     execsql {SELECT name FROM sqlite_master}
   650    650   } {t2}
   651    651   do_test auth-1.93 {
   652         -  proc auth {code arg1 arg2 arg3 arg4} {
          652  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   653    653       if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
   654    654         return SQLITE_IGNORE
   655    655       }
   656    656       return SQLITE_OK
   657    657     }
   658    658     catchsql {CREATE VIEW v1 AS SELECT a+1,b+1 FROM t2}
   659    659   } {0 {}}
   660    660   do_test auth-1.94 {
   661    661     execsql {SELECT name FROM sqlite_master}
   662    662   } {t2}
   663    663   
   664    664   ifcapable tempdb {
   665    665     do_test auth-1.95 {
   666         -    proc auth {code arg1 arg2 arg3 arg4} {
          666  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   667    667         if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
   668    668           return SQLITE_DENY
   669    669         }
   670    670         return SQLITE_OK
   671    671       }
   672    672       catchsql {CREATE TEMPORARY VIEW v1 AS SELECT a+1,b+1 FROM t2}
   673    673     } {1 {not authorized}}
   674    674     do_test auth-1.96 {
   675    675       execsql {SELECT name FROM sqlite_temp_master}
   676    676     } {t1}
   677    677     do_test auth-1.97 {
   678         -    proc auth {code arg1 arg2 arg3 arg4} {
          678  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   679    679         if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
   680    680           return SQLITE_IGNORE
   681    681         }
   682    682         return SQLITE_OK
   683    683       }
   684    684       catchsql {CREATE TEMPORARY VIEW v1 AS SELECT a+1,b+1 FROM t2}
   685    685     } {0 {}}
   686    686     do_test auth-1.98 {
   687    687       execsql {SELECT name FROM sqlite_temp_master}
   688    688     } {t1}
   689    689   }
   690    690   
   691    691   do_test auth-1.99 {
   692         -  proc auth {code arg1 arg2 arg3 arg4} {
          692  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   693    693       if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
   694    694         return SQLITE_DENY
   695    695       }
   696    696       return SQLITE_OK
   697    697     }
   698    698     catchsql {
   699    699       CREATE VIEW v2 AS SELECT a+1,b+1 FROM t2;
................................................................................
   700    700       DROP VIEW v2
   701    701     }
   702    702   } {1 {not authorized}}
   703    703   do_test auth-1.100 {
   704    704     execsql {SELECT name FROM sqlite_master}
   705    705   } {t2 v2}
   706    706   do_test auth-1.101 {
   707         -  proc auth {code arg1 arg2 arg3 arg4} {
          707  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   708    708       if {$code=="SQLITE_DROP_VIEW"} {
   709    709         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   710    710         return SQLITE_DENY
   711    711       }
   712    712       return SQLITE_OK
   713    713     }
   714    714     catchsql {DROP VIEW v2}
................................................................................
   716    716   do_test auth-1.102 {
   717    717     set ::authargs
   718    718   } {v2 {} main {}}
   719    719   do_test auth-1.103 {
   720    720     execsql {SELECT name FROM sqlite_master}
   721    721   } {t2 v2}
   722    722   do_test auth-1.104 {
   723         -  proc auth {code arg1 arg2 arg3 arg4} {
          723  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   724    724       if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
   725    725         return SQLITE_IGNORE
   726    726       }
   727    727       return SQLITE_OK
   728    728     }
   729    729     catchsql {DROP VIEW v2}
   730    730   } {0 {}}
   731    731   do_test auth-1.105 {
   732    732     execsql {SELECT name FROM sqlite_master}
   733    733   } {t2 v2}
   734    734   do_test auth-1.106 {
   735         -  proc auth {code arg1 arg2 arg3 arg4} {
          735  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   736    736       if {$code=="SQLITE_DROP_VIEW"} {
   737    737         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   738    738         return SQLITE_IGNORE
   739    739       }
   740    740       return SQLITE_OK
   741    741     }
   742    742     catchsql {DROP VIEW v2}
................................................................................
   744    744   do_test auth-1.107 {
   745    745     set ::authargs
   746    746   } {v2 {} main {}}
   747    747   do_test auth-1.108 {
   748    748     execsql {SELECT name FROM sqlite_master}
   749    749   } {t2 v2}
   750    750   do_test auth-1.109 {
   751         -  proc auth {code arg1 arg2 arg3 arg4} {
          751  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   752    752       if {$code=="SQLITE_DROP_VIEW"} {
   753    753         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   754    754         return SQLITE_OK
   755    755       }
   756    756       return SQLITE_OK
   757    757     }
   758    758     catchsql {DROP VIEW v2}
................................................................................
   763    763   do_test auth-1.111 {
   764    764     execsql {SELECT name FROM sqlite_master}
   765    765   } {t2}
   766    766   
   767    767   
   768    768   ifcapable tempdb {
   769    769     do_test auth-1.112 {
   770         -    proc auth {code arg1 arg2 arg3 arg4} {
          770  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   771    771         if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
   772    772           return SQLITE_DENY
   773    773         }
   774    774         return SQLITE_OK
   775    775       }
   776    776       catchsql {
   777    777         CREATE TEMP VIEW v1 AS SELECT a+1,b+1 FROM t1;
................................................................................
   778    778         DROP VIEW v1
   779    779       }
   780    780     } {1 {not authorized}}
   781    781     do_test auth-1.113 {
   782    782       execsql {SELECT name FROM sqlite_temp_master}
   783    783     } {t1 v1}
   784    784     do_test auth-1.114 {
   785         -    proc auth {code arg1 arg2 arg3 arg4} {
          785  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   786    786         if {$code=="SQLITE_DROP_TEMP_VIEW"} {
   787    787           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   788    788           return SQLITE_DENY
   789    789         }
   790    790         return SQLITE_OK
   791    791       }
   792    792       catchsql {DROP VIEW v1}
................................................................................
   794    794     do_test auth-1.115 {
   795    795       set ::authargs
   796    796     } {v1 {} temp {}}
   797    797     do_test auth-1.116 {
   798    798       execsql {SELECT name FROM sqlite_temp_master}
   799    799     } {t1 v1}
   800    800     do_test auth-1.117 {
   801         -    proc auth {code arg1 arg2 arg3 arg4} {
          801  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   802    802         if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
   803    803           return SQLITE_IGNORE
   804    804         }
   805    805         return SQLITE_OK
   806    806       }
   807    807       catchsql {DROP VIEW v1}
   808    808     } {0 {}}
   809    809     do_test auth-1.118 {
   810    810       execsql {SELECT name FROM sqlite_temp_master}
   811    811     } {t1 v1}
   812    812     do_test auth-1.119 {
   813         -    proc auth {code arg1 arg2 arg3 arg4} {
          813  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   814    814         if {$code=="SQLITE_DROP_TEMP_VIEW"} {
   815    815           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   816    816           return SQLITE_IGNORE
   817    817         }
   818    818         return SQLITE_OK
   819    819       }
   820    820       catchsql {DROP VIEW v1}
................................................................................
   822    822     do_test auth-1.120 {
   823    823       set ::authargs
   824    824     } {v1 {} temp {}}
   825    825     do_test auth-1.121 {
   826    826       execsql {SELECT name FROM sqlite_temp_master}
   827    827     } {t1 v1}
   828    828     do_test auth-1.122 {
   829         -    proc auth {code arg1 arg2 arg3 arg4} {
          829  +    proc auth {code arg1 arg2 arg3 arg4 args} {
   830    830         if {$code=="SQLITE_DROP_TEMP_VIEW"} {
   831    831           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   832    832           return SQLITE_OK
   833    833         }
   834    834         return SQLITE_OK
   835    835       }
   836    836       catchsql {DROP VIEW v1}
................................................................................
   845    845   } ;# ifcapable view
   846    846   
   847    847   # Test cases auth-1.125 to auth-1.176 test creating and dropping triggers.
   848    848   # Omit these if the library was compiled with triggers omitted.
   849    849   #
   850    850   ifcapable trigger&&tempdb {
   851    851   do_test auth-1.125 {
   852         -  proc auth {code arg1 arg2 arg3 arg4} {
          852  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   853    853       if {$code=="SQLITE_CREATE_TRIGGER"} {
   854    854         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   855    855         return SQLITE_DENY
   856    856       }
   857    857       return SQLITE_OK
   858    858     }
   859    859     catchsql {
................................................................................
   865    865   do_test auth-1.126 {
   866    866     set ::authargs
   867    867   } {r2 t2 main {}}
   868    868   do_test auth-1.127 {
   869    869     execsql {SELECT name FROM sqlite_master}
   870    870   } {t2}
   871    871   do_test auth-1.128 {
   872         -  proc auth {code arg1 arg2 arg3 arg4} {
          872  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   873    873       if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
   874    874         return SQLITE_DENY
   875    875       }
   876    876       return SQLITE_OK
   877    877     }
   878    878     catchsql {
   879    879       CREATE TRIGGER r2 DELETE on t2 BEGIN
................................................................................
   881    881       END;
   882    882     }
   883    883   } {1 {not authorized}}
   884    884   do_test auth-1.129 {
   885    885     execsql {SELECT name FROM sqlite_master}
   886    886   } {t2}
   887    887   do_test auth-1.130 {
   888         -  proc auth {code arg1 arg2 arg3 arg4} {
          888  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   889    889       if {$code=="SQLITE_CREATE_TRIGGER"} {
   890    890         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   891    891         return SQLITE_IGNORE
   892    892       }
   893    893       return SQLITE_OK
   894    894     }
   895    895     catchsql {
................................................................................
   901    901   do_test auth-1.131 {
   902    902     set ::authargs
   903    903   } {r2 t2 main {}}
   904    904   do_test auth-1.132 {
   905    905     execsql {SELECT name FROM sqlite_master}
   906    906   } {t2}
   907    907   do_test auth-1.133 {
   908         -  proc auth {code arg1 arg2 arg3 arg4} {
          908  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   909    909       if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
   910    910         return SQLITE_IGNORE
   911    911       }
   912    912       return SQLITE_OK
   913    913     }
   914    914     catchsql {
   915    915       CREATE TRIGGER r2 DELETE on t2 BEGIN
................................................................................
   917    917       END;
   918    918     }
   919    919   } {0 {}}
   920    920   do_test auth-1.134 {
   921    921     execsql {SELECT name FROM sqlite_master}
   922    922   } {t2}
   923    923   do_test auth-1.135 {
   924         -  proc auth {code arg1 arg2 arg3 arg4} {
          924  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   925    925       if {$code=="SQLITE_CREATE_TRIGGER"} {
   926    926         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   927    927         return SQLITE_OK
   928    928       }
   929    929       return SQLITE_OK
   930    930     }
   931    931     catchsql {
................................................................................
   940    940   } {r2 t2 main {}}
   941    941   do_test auth-1.136.2 {
   942    942     execsql {
   943    943       SELECT name FROM sqlite_master WHERE type='trigger'
   944    944     }
   945    945   } {r2}
   946    946   do_test auth-1.136.3 {
   947         -  proc auth {code arg1 arg2 arg3 arg4} {
          947  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   948    948       lappend ::authargs $code $arg1 $arg2 $arg3 $arg4
   949    949       return SQLITE_OK
   950    950     }
   951    951     set ::authargs {}
   952    952     execsql {
   953    953       INSERT INTO t2 VALUES(1,2,3);
   954    954     }
................................................................................
   959    959       SELECT * FROM tx;
   960    960     }
   961    961   } {3}
   962    962   do_test auth-1.137 {
   963    963     execsql {SELECT name FROM sqlite_master}
   964    964   } {t2 tx r2}
   965    965   do_test auth-1.138 {
   966         -  proc auth {code arg1 arg2 arg3 arg4} {
          966  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   967    967       if {$code=="SQLITE_CREATE_TEMP_TRIGGER"} {
   968    968         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
   969    969         return SQLITE_DENY
   970    970       }
   971    971       return SQLITE_OK
   972    972     }
   973    973     catchsql {
................................................................................
   979    979   do_test auth-1.139 {
   980    980     set ::authargs
   981    981   } {r1 t1 temp {}}
   982    982   do_test auth-1.140 {
   983    983     execsql {SELECT name FROM sqlite_temp_master}
   984    984   } {t1}
   985    985   do_test auth-1.141 {
   986         -  proc auth {code arg1 arg2 arg3 arg4} {
          986  +  proc auth {code arg1 arg2 arg3 arg4 args} {
   987    987       if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
   988    988         return SQLITE_DENY
   989    989       }
   990    990       return SQLITE_OK
   991    991     }
   992    992     catchsql {
   993    993       CREATE TRIGGER r1 DELETE on t1 BEGIN
................................................................................
   995    995       END;
   996    996     }
   997    997   } {1 {not authorized}}
   998    998   do_test auth-1.142 {
   999    999     execsql {SELECT name FROM sqlite_temp_master}
  1000   1000   } {t1}
  1001   1001   do_test auth-1.143 {
  1002         -  proc auth {code arg1 arg2 arg3 arg4} {
         1002  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1003   1003       if {$code=="SQLITE_CREATE_TEMP_TRIGGER"} {
  1004   1004         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1005   1005         return SQLITE_IGNORE
  1006   1006       }
  1007   1007       return SQLITE_OK
  1008   1008     }
  1009   1009     catchsql {
................................................................................
  1015   1015   do_test auth-1.144 {
  1016   1016     set ::authargs
  1017   1017   } {r1 t1 temp {}}
  1018   1018   do_test auth-1.145 {
  1019   1019     execsql {SELECT name FROM sqlite_temp_master}
  1020   1020   } {t1}
  1021   1021   do_test auth-1.146 {
  1022         -  proc auth {code arg1 arg2 arg3 arg4} {
         1022  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1023   1023       if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
  1024   1024         return SQLITE_IGNORE
  1025   1025       }
  1026   1026       return SQLITE_OK
  1027   1027     }
  1028   1028     catchsql {
  1029   1029       CREATE TRIGGER r1 DELETE on t1 BEGIN
................................................................................
  1031   1031       END;
  1032   1032     }
  1033   1033   } {0 {}}
  1034   1034   do_test auth-1.147 {
  1035   1035     execsql {SELECT name FROM sqlite_temp_master}
  1036   1036   } {t1}
  1037   1037   do_test auth-1.148 {
  1038         -  proc auth {code arg1 arg2 arg3 arg4} {
         1038  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1039   1039       if {$code=="SQLITE_CREATE_TEMP_TRIGGER"} {
  1040   1040         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1041   1041         return SQLITE_OK
  1042   1042       }
  1043   1043       return SQLITE_OK
  1044   1044     }
  1045   1045     catchsql {
................................................................................
  1052   1052     set ::authargs
  1053   1053   } {r1 t1 temp {}}
  1054   1054   do_test auth-1.150 {
  1055   1055     execsql {SELECT name FROM sqlite_temp_master}
  1056   1056   } {t1 r1}
  1057   1057   
  1058   1058   do_test auth-1.151 {
  1059         -  proc auth {code arg1 arg2 arg3 arg4} {
         1059  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1060   1060       if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
  1061   1061         return SQLITE_DENY
  1062   1062       }
  1063   1063       return SQLITE_OK
  1064   1064     }
  1065   1065     catchsql {DROP TRIGGER r2}
  1066   1066   } {1 {not authorized}}
  1067   1067   do_test auth-1.152 {
  1068   1068     execsql {SELECT name FROM sqlite_master}
  1069   1069   } {t2 tx r2}
  1070   1070   do_test auth-1.153 {
  1071         -  proc auth {code arg1 arg2 arg3 arg4} {
         1071  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1072   1072       if {$code=="SQLITE_DROP_TRIGGER"} {
  1073   1073         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1074   1074         return SQLITE_DENY
  1075   1075       }
  1076   1076       return SQLITE_OK
  1077   1077     }
  1078   1078     catchsql {DROP TRIGGER r2}
................................................................................
  1080   1080   do_test auth-1.154 {
  1081   1081     set ::authargs
  1082   1082   } {r2 t2 main {}}
  1083   1083   do_test auth-1.155 {
  1084   1084     execsql {SELECT name FROM sqlite_master}
  1085   1085   } {t2 tx r2}
  1086   1086   do_test auth-1.156 {
  1087         -  proc auth {code arg1 arg2 arg3 arg4} {
         1087  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1088   1088       if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
  1089   1089         return SQLITE_IGNORE
  1090   1090       }
  1091   1091       return SQLITE_OK
  1092   1092     }
  1093   1093     catchsql {DROP TRIGGER r2}
  1094   1094   } {0 {}}
  1095   1095   do_test auth-1.157 {
  1096   1096     execsql {SELECT name FROM sqlite_master}
  1097   1097   } {t2 tx r2}
  1098   1098   do_test auth-1.158 {
  1099         -  proc auth {code arg1 arg2 arg3 arg4} {
         1099  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1100   1100       if {$code=="SQLITE_DROP_TRIGGER"} {
  1101   1101         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1102   1102         return SQLITE_IGNORE
  1103   1103       }
  1104   1104       return SQLITE_OK
  1105   1105     }
  1106   1106     catchsql {DROP TRIGGER r2}
................................................................................
  1108   1108   do_test auth-1.159 {
  1109   1109     set ::authargs
  1110   1110   } {r2 t2 main {}}
  1111   1111   do_test auth-1.160 {
  1112   1112     execsql {SELECT name FROM sqlite_master}
  1113   1113   } {t2 tx r2}
  1114   1114   do_test auth-1.161 {
  1115         -  proc auth {code arg1 arg2 arg3 arg4} {
         1115  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1116   1116       if {$code=="SQLITE_DROP_TRIGGER"} {
  1117   1117         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1118   1118         return SQLITE_OK
  1119   1119       }
  1120   1120       return SQLITE_OK
  1121   1121     }
  1122   1122     catchsql {DROP TRIGGER r2}
................................................................................
  1129   1129       DROP TABLE tx;
  1130   1130       DELETE FROM t2 WHERE a=1 AND b=2 AND c=3;
  1131   1131       SELECT name FROM sqlite_master;
  1132   1132     }
  1133   1133   } {t2}
  1134   1134   
  1135   1135   do_test auth-1.164 {
  1136         -  proc auth {code arg1 arg2 arg3 arg4} {
         1136  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1137   1137       if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
  1138   1138         return SQLITE_DENY
  1139   1139       }
  1140   1140       return SQLITE_OK
  1141   1141     }
  1142   1142     catchsql {DROP TRIGGER r1}
  1143   1143   } {1 {not authorized}}
  1144   1144   do_test auth-1.165 {
  1145   1145     execsql {SELECT name FROM sqlite_temp_master}
  1146   1146   } {t1 r1}
  1147   1147   do_test auth-1.166 {
  1148         -  proc auth {code arg1 arg2 arg3 arg4} {
         1148  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1149   1149       if {$code=="SQLITE_DROP_TEMP_TRIGGER"} {
  1150   1150         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1151   1151         return SQLITE_DENY
  1152   1152       }
  1153   1153       return SQLITE_OK
  1154   1154     }
  1155   1155     catchsql {DROP TRIGGER r1}
................................................................................
  1157   1157   do_test auth-1.167 {
  1158   1158     set ::authargs
  1159   1159   } {r1 t1 temp {}}
  1160   1160   do_test auth-1.168 {
  1161   1161     execsql {SELECT name FROM sqlite_temp_master}
  1162   1162   } {t1 r1}
  1163   1163   do_test auth-1.169 {
  1164         -  proc auth {code arg1 arg2 arg3 arg4} {
         1164  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1165   1165       if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
  1166   1166         return SQLITE_IGNORE
  1167   1167       }
  1168   1168       return SQLITE_OK
  1169   1169     }
  1170   1170     catchsql {DROP TRIGGER r1}
  1171   1171   } {0 {}}
  1172   1172   do_test auth-1.170 {
  1173   1173     execsql {SELECT name FROM sqlite_temp_master}
  1174   1174   } {t1 r1}
  1175   1175   do_test auth-1.171 {
  1176         -  proc auth {code arg1 arg2 arg3 arg4} {
         1176  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1177   1177       if {$code=="SQLITE_DROP_TEMP_TRIGGER"} {
  1178   1178         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1179   1179         return SQLITE_IGNORE
  1180   1180       }
  1181   1181       return SQLITE_OK
  1182   1182     }
  1183   1183     catchsql {DROP TRIGGER r1}
................................................................................
  1185   1185   do_test auth-1.172 {
  1186   1186     set ::authargs
  1187   1187   } {r1 t1 temp {}}
  1188   1188   do_test auth-1.173 {
  1189   1189     execsql {SELECT name FROM sqlite_temp_master}
  1190   1190   } {t1 r1}
  1191   1191   do_test auth-1.174 {
  1192         -  proc auth {code arg1 arg2 arg3 arg4} {
         1192  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1193   1193       if {$code=="SQLITE_DROP_TEMP_TRIGGER"} {
  1194   1194         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1195   1195         return SQLITE_OK
  1196   1196       }
  1197   1197       return SQLITE_OK
  1198   1198     }
  1199   1199     catchsql {DROP TRIGGER r1}
................................................................................
  1203   1203   } {r1 t1 temp {}}
  1204   1204   do_test auth-1.176 {
  1205   1205     execsql {SELECT name FROM sqlite_temp_master}
  1206   1206   } {t1}
  1207   1207   } ;# ifcapable trigger
  1208   1208   
  1209   1209   do_test auth-1.177 {
  1210         -  proc auth {code arg1 arg2 arg3 arg4} {
         1210  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1211   1211       if {$code=="SQLITE_CREATE_INDEX"} {
  1212   1212         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1213   1213         return SQLITE_DENY
  1214   1214       }
  1215   1215       return SQLITE_OK
  1216   1216     }
  1217   1217     catchsql {CREATE INDEX i2 ON t2(a)}
................................................................................
  1219   1219   do_test auth-1.178 {
  1220   1220     set ::authargs
  1221   1221   } {i2 t2 main {}}
  1222   1222   do_test auth-1.179 {
  1223   1223     execsql {SELECT name FROM sqlite_master}
  1224   1224   } {t2}
  1225   1225   do_test auth-1.180 {
  1226         -  proc auth {code arg1 arg2 arg3 arg4} {
         1226  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1227   1227       if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
  1228   1228         return SQLITE_DENY
  1229   1229       }
  1230   1230       return SQLITE_OK
  1231   1231     }
  1232   1232     catchsql {CREATE INDEX i2 ON t2(a)}
  1233   1233   } {1 {not authorized}}
  1234   1234   do_test auth-1.181 {
  1235   1235     execsql {SELECT name FROM sqlite_master}
  1236   1236   } {t2}
  1237   1237   do_test auth-1.182 {
  1238         -  proc auth {code arg1 arg2 arg3 arg4} {
         1238  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1239   1239       if {$code=="SQLITE_CREATE_INDEX"} {
  1240   1240         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1241   1241         return SQLITE_IGNORE
  1242   1242       }
  1243   1243       return SQLITE_OK
  1244   1244     }
  1245   1245     catchsql {CREATE INDEX i2 ON t2(b)}
................................................................................
  1247   1247   do_test auth-1.183 {
  1248   1248     set ::authargs
  1249   1249   } {i2 t2 main {}}
  1250   1250   do_test auth-1.184 {
  1251   1251     execsql {SELECT name FROM sqlite_master}
  1252   1252   } {t2}
  1253   1253   do_test auth-1.185 {
  1254         -  proc auth {code arg1 arg2 arg3 arg4} {
         1254  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1255   1255       if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
  1256   1256         return SQLITE_IGNORE
  1257   1257       }
  1258   1258       return SQLITE_OK
  1259   1259     }
  1260   1260     catchsql {CREATE INDEX i2 ON t2(b)}
  1261   1261   } {0 {}}
  1262   1262   do_test auth-1.186 {
  1263   1263     execsql {SELECT name FROM sqlite_master}
  1264   1264   } {t2}
  1265   1265   do_test auth-1.187 {
  1266         -  proc auth {code arg1 arg2 arg3 arg4} {
         1266  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1267   1267       if {$code=="SQLITE_CREATE_INDEX"} {
  1268   1268         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1269   1269         return SQLITE_OK
  1270   1270       }
  1271   1271       return SQLITE_OK
  1272   1272     }
  1273   1273     catchsql {CREATE INDEX i2 ON t2(a)}
................................................................................
  1277   1277   } {i2 t2 main {}}
  1278   1278   do_test auth-1.189 {
  1279   1279     execsql {SELECT name FROM sqlite_master}
  1280   1280   } {t2 i2}
  1281   1281   
  1282   1282   ifcapable tempdb {
  1283   1283     do_test auth-1.190 {
  1284         -    proc auth {code arg1 arg2 arg3 arg4} {
         1284  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1285   1285         if {$code=="SQLITE_CREATE_TEMP_INDEX"} {
  1286   1286           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1287   1287           return SQLITE_DENY
  1288   1288         }
  1289   1289         return SQLITE_OK
  1290   1290       }
  1291   1291       catchsql {CREATE INDEX i1 ON t1(a)}
................................................................................
  1293   1293     do_test auth-1.191 {
  1294   1294       set ::authargs
  1295   1295     } {i1 t1 temp {}}
  1296   1296     do_test auth-1.192 {
  1297   1297       execsql {SELECT name FROM sqlite_temp_master}
  1298   1298     } {t1}
  1299   1299     do_test auth-1.193 {
  1300         -    proc auth {code arg1 arg2 arg3 arg4} {
         1300  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1301   1301         if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
  1302   1302           return SQLITE_DENY
  1303   1303         }
  1304   1304         return SQLITE_OK
  1305   1305       }
  1306   1306       catchsql {CREATE INDEX i1 ON t1(b)}
  1307   1307     } {1 {not authorized}}
  1308   1308     do_test auth-1.194 {
  1309   1309       execsql {SELECT name FROM sqlite_temp_master}
  1310   1310     } {t1}
  1311   1311     do_test auth-1.195 {
  1312         -    proc auth {code arg1 arg2 arg3 arg4} {
         1312  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1313   1313         if {$code=="SQLITE_CREATE_TEMP_INDEX"} {
  1314   1314           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1315   1315           return SQLITE_IGNORE
  1316   1316         }
  1317   1317         return SQLITE_OK
  1318   1318       }
  1319   1319       catchsql {CREATE INDEX i1 ON t1(b)}
................................................................................
  1321   1321     do_test auth-1.196 {
  1322   1322       set ::authargs
  1323   1323     } {i1 t1 temp {}}
  1324   1324     do_test auth-1.197 {
  1325   1325       execsql {SELECT name FROM sqlite_temp_master}
  1326   1326     } {t1}
  1327   1327     do_test auth-1.198 {
  1328         -    proc auth {code arg1 arg2 arg3 arg4} {
         1328  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1329   1329         if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
  1330   1330           return SQLITE_IGNORE
  1331   1331         }
  1332   1332         return SQLITE_OK
  1333   1333       }
  1334   1334       catchsql {CREATE INDEX i1 ON t1(c)}
  1335   1335     } {0 {}}
  1336   1336     do_test auth-1.199 {
  1337   1337       execsql {SELECT name FROM sqlite_temp_master}
  1338   1338     } {t1}
  1339   1339     do_test auth-1.200 {
  1340         -    proc auth {code arg1 arg2 arg3 arg4} {
         1340  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1341   1341         if {$code=="SQLITE_CREATE_TEMP_INDEX"} {
  1342   1342           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1343   1343           return SQLITE_OK
  1344   1344         }
  1345   1345         return SQLITE_OK
  1346   1346       }
  1347   1347       catchsql {CREATE INDEX i1 ON t1(a)}
................................................................................
  1351   1351     } {i1 t1 temp {}}
  1352   1352     do_test auth-1.202 {
  1353   1353       execsql {SELECT name FROM sqlite_temp_master}
  1354   1354     } {t1 i1}
  1355   1355   }
  1356   1356   
  1357   1357   do_test auth-1.203 {
  1358         -  proc auth {code arg1 arg2 arg3 arg4} {
         1358  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1359   1359       if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
  1360   1360         return SQLITE_DENY
  1361   1361       }
  1362   1362       return SQLITE_OK
  1363   1363     }
  1364   1364     catchsql {DROP INDEX i2}
  1365   1365   } {1 {not authorized}}
  1366   1366   do_test auth-1.204 {
  1367   1367     execsql {SELECT name FROM sqlite_master}
  1368   1368   } {t2 i2}
  1369   1369   do_test auth-1.205 {
  1370         -  proc auth {code arg1 arg2 arg3 arg4} {
         1370  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1371   1371       if {$code=="SQLITE_DROP_INDEX"} {
  1372   1372         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1373   1373         return SQLITE_DENY
  1374   1374       }
  1375   1375       return SQLITE_OK
  1376   1376     }
  1377   1377     catchsql {DROP INDEX i2}
................................................................................
  1379   1379   do_test auth-1.206 {
  1380   1380     set ::authargs
  1381   1381   } {i2 t2 main {}}
  1382   1382   do_test auth-1.207 {
  1383   1383     execsql {SELECT name FROM sqlite_master}
  1384   1384   } {t2 i2}
  1385   1385   do_test auth-1.208 {
  1386         -  proc auth {code arg1 arg2 arg3 arg4} {
         1386  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1387   1387       if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
  1388   1388         return SQLITE_IGNORE
  1389   1389       }
  1390   1390       return SQLITE_OK
  1391   1391     }
  1392   1392     catchsql {DROP INDEX i2}
  1393   1393   } {0 {}}
  1394   1394   do_test auth-1.209 {
  1395   1395     execsql {SELECT name FROM sqlite_master}
  1396   1396   } {t2 i2}
  1397   1397   do_test auth-1.210 {
  1398         -  proc auth {code arg1 arg2 arg3 arg4} {
         1398  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1399   1399       if {$code=="SQLITE_DROP_INDEX"} {
  1400   1400         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1401   1401         return SQLITE_IGNORE
  1402   1402       }
  1403   1403       return SQLITE_OK
  1404   1404     }
  1405   1405     catchsql {DROP INDEX i2}
................................................................................
  1407   1407   do_test auth-1.211 {
  1408   1408     set ::authargs
  1409   1409   } {i2 t2 main {}}
  1410   1410   do_test auth-1.212 {
  1411   1411     execsql {SELECT name FROM sqlite_master}
  1412   1412   } {t2 i2}
  1413   1413   do_test auth-1.213 {
  1414         -  proc auth {code arg1 arg2 arg3 arg4} {
         1414  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1415   1415       if {$code=="SQLITE_DROP_INDEX"} {
  1416   1416         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1417   1417         return SQLITE_OK
  1418   1418       }
  1419   1419       return SQLITE_OK
  1420   1420     }
  1421   1421     catchsql {DROP INDEX i2}
................................................................................
  1425   1425   } {i2 t2 main {}}
  1426   1426   do_test auth-1.215 {
  1427   1427     execsql {SELECT name FROM sqlite_master}
  1428   1428   } {t2}
  1429   1429   
  1430   1430   ifcapable tempdb {
  1431   1431     do_test auth-1.216 {
  1432         -    proc auth {code arg1 arg2 arg3 arg4} {
         1432  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1433   1433         if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
  1434   1434           return SQLITE_DENY
  1435   1435         }
  1436   1436         return SQLITE_OK
  1437   1437       }
  1438   1438       catchsql {DROP INDEX i1}
  1439   1439     } {1 {not authorized}}
  1440   1440     do_test auth-1.217 {
  1441   1441       execsql {SELECT name FROM sqlite_temp_master}
  1442   1442     } {t1 i1}
  1443   1443     do_test auth-1.218 {
  1444         -    proc auth {code arg1 arg2 arg3 arg4} {
         1444  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1445   1445         if {$code=="SQLITE_DROP_TEMP_INDEX"} {
  1446   1446           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1447   1447           return SQLITE_DENY
  1448   1448         }
  1449   1449         return SQLITE_OK
  1450   1450       }
  1451   1451       catchsql {DROP INDEX i1}
................................................................................
  1453   1453     do_test auth-1.219 {
  1454   1454       set ::authargs
  1455   1455     } {i1 t1 temp {}}
  1456   1456     do_test auth-1.220 {
  1457   1457       execsql {SELECT name FROM sqlite_temp_master}
  1458   1458     } {t1 i1}
  1459   1459     do_test auth-1.221 {
  1460         -    proc auth {code arg1 arg2 arg3 arg4} {
         1460  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1461   1461         if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
  1462   1462           return SQLITE_IGNORE
  1463   1463         }
  1464   1464         return SQLITE_OK
  1465   1465       }
  1466   1466       catchsql {DROP INDEX i1}
  1467   1467     } {0 {}}
  1468   1468     do_test auth-1.222 {
  1469   1469       execsql {SELECT name FROM sqlite_temp_master}
  1470   1470     } {t1 i1}
  1471   1471     do_test auth-1.223 {
  1472         -    proc auth {code arg1 arg2 arg3 arg4} {
         1472  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1473   1473         if {$code=="SQLITE_DROP_TEMP_INDEX"} {
  1474   1474           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1475   1475           return SQLITE_IGNORE
  1476   1476         }
  1477   1477         return SQLITE_OK
  1478   1478       }
  1479   1479       catchsql {DROP INDEX i1}
................................................................................
  1481   1481     do_test auth-1.224 {
  1482   1482       set ::authargs
  1483   1483     } {i1 t1 temp {}}
  1484   1484     do_test auth-1.225 {
  1485   1485       execsql {SELECT name FROM sqlite_temp_master}
  1486   1486     } {t1 i1}
  1487   1487     do_test auth-1.226 {
  1488         -    proc auth {code arg1 arg2 arg3 arg4} {
         1488  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1489   1489         if {$code=="SQLITE_DROP_TEMP_INDEX"} {
  1490   1490           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1491   1491           return SQLITE_OK
  1492   1492         }
  1493   1493         return SQLITE_OK
  1494   1494       }
  1495   1495       catchsql {DROP INDEX i1}
................................................................................
  1499   1499     } {i1 t1 temp {}}
  1500   1500     do_test auth-1.228 {
  1501   1501       execsql {SELECT name FROM sqlite_temp_master}
  1502   1502     } {t1}
  1503   1503   }
  1504   1504   
  1505   1505   do_test auth-1.229 {
  1506         -  proc auth {code arg1 arg2 arg3 arg4} {
         1506  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1507   1507       if {$code=="SQLITE_PRAGMA"} {
  1508   1508         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1509   1509         return SQLITE_DENY
  1510   1510       }
  1511   1511       return SQLITE_OK
  1512   1512     }
  1513   1513     catchsql {PRAGMA full_column_names=on}
................................................................................
  1515   1515   do_test auth-1.230 {
  1516   1516     set ::authargs
  1517   1517   } {full_column_names on {} {}}
  1518   1518   do_test auth-1.231 {
  1519   1519     execsql2 {SELECT a FROM t2}
  1520   1520   } {a 11 a 7}
  1521   1521   do_test auth-1.232 {
  1522         -  proc auth {code arg1 arg2 arg3 arg4} {
         1522  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1523   1523       if {$code=="SQLITE_PRAGMA"} {
  1524   1524         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1525   1525         return SQLITE_IGNORE
  1526   1526       }
  1527   1527       return SQLITE_OK
  1528   1528     }
  1529   1529     catchsql {PRAGMA full_column_names=on}
................................................................................
  1531   1531   do_test auth-1.233 {
  1532   1532     set ::authargs
  1533   1533   } {full_column_names on {} {}}
  1534   1534   do_test auth-1.234 {
  1535   1535     execsql2 {SELECT a FROM t2}
  1536   1536   } {a 11 a 7}
  1537   1537   do_test auth-1.235 {
  1538         -  proc auth {code arg1 arg2 arg3 arg4} {
         1538  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1539   1539       if {$code=="SQLITE_PRAGMA"} {
  1540   1540         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1541   1541         return SQLITE_OK
  1542   1542       }
  1543   1543       return SQLITE_OK
  1544   1544     }
  1545   1545     catchsql {PRAGMA full_column_names=on}
  1546   1546   } {0 {}}
  1547   1547   do_test auth-1.236 {
  1548   1548     execsql2 {SELECT a FROM t2}
  1549   1549   } {t2.a 11 t2.a 7}
  1550   1550   do_test auth-1.237 {
  1551         -  proc auth {code arg1 arg2 arg3 arg4} {
         1551  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1552   1552       if {$code=="SQLITE_PRAGMA"} {
  1553   1553         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1554   1554         return SQLITE_OK
  1555   1555       }
  1556   1556       return SQLITE_OK
  1557   1557     }
  1558   1558     catchsql {PRAGMA full_column_names=OFF}
................................................................................
  1561   1561     set ::authargs
  1562   1562   } {full_column_names OFF {} {}}
  1563   1563   do_test auth-1.239 {
  1564   1564     execsql2 {SELECT a FROM t2}
  1565   1565   } {a 11 a 7}
  1566   1566   
  1567   1567   do_test auth-1.240 {
  1568         -  proc auth {code arg1 arg2 arg3 arg4} {
         1568  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1569   1569       if {$code=="SQLITE_TRANSACTION"} {
  1570   1570         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1571   1571         return SQLITE_DENY
  1572   1572       }
  1573   1573       return SQLITE_OK
  1574   1574     }
  1575   1575     catchsql {BEGIN}
  1576   1576   } {1 {not authorized}}
  1577   1577   do_test auth-1.241 {
  1578   1578     set ::authargs
  1579   1579   } {BEGIN {} {} {}}
  1580   1580   do_test auth-1.242 {
  1581         -  proc auth {code arg1 arg2 arg3 arg4} {
         1581  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1582   1582       if {$code=="SQLITE_TRANSACTION" && $arg1!="BEGIN"} {
  1583   1583         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1584   1584         return SQLITE_DENY
  1585   1585       }
  1586   1586       return SQLITE_OK
  1587   1587     }
  1588   1588     catchsql {BEGIN; INSERT INTO t2 VALUES(44,55,66); COMMIT}
................................................................................
  1614   1614   } {11 2 33 7 8 9}
  1615   1615   
  1616   1616   # ticket #340 - authorization for ATTACH and DETACH.
  1617   1617   #
  1618   1618   ifcapable attach {
  1619   1619     do_test auth-1.251 {
  1620   1620       db authorizer ::auth
  1621         -    proc auth {code arg1 arg2 arg3 arg4} {
         1621  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1622   1622         if {$code=="SQLITE_ATTACH"} {
  1623   1623           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1624   1624         }
  1625   1625         return SQLITE_OK
  1626   1626       }
  1627   1627       catchsql {
  1628   1628         ATTACH DATABASE ':memory:' AS test1
................................................................................
  1640   1640     do_test auth-1.252c {
  1641   1641       db eval {DETACH test1}
  1642   1642       db eval {ATTACH ':mem' || 'ory:' AS test1}
  1643   1643       set ::authargs
  1644   1644     } {{} {} {} {}}
  1645   1645     do_test auth-1.253 {
  1646   1646       catchsql {DETACH DATABASE test1}
  1647         -    proc auth {code arg1 arg2 arg3 arg4} {
         1647  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1648   1648         if {$code=="SQLITE_ATTACH"} {
  1649   1649           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1650   1650           return SQLITE_DENY
  1651   1651         }
  1652   1652         return SQLITE_OK
  1653   1653       }
  1654   1654       catchsql {
................................................................................
  1656   1656       }
  1657   1657     } {1 {not authorized}}
  1658   1658     do_test auth-1.254 {
  1659   1659       lindex [execsql {PRAGMA database_list}] 7
  1660   1660     } {}
  1661   1661     do_test auth-1.255 {
  1662   1662       catchsql {DETACH DATABASE test1}
  1663         -    proc auth {code arg1 arg2 arg3 arg4} {
         1663  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1664   1664         if {$code=="SQLITE_ATTACH"} {
  1665   1665           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1666   1666           return SQLITE_IGNORE
  1667   1667         }
  1668   1668         return SQLITE_OK
  1669   1669       }
  1670   1670       catchsql {
................................................................................
  1671   1671         ATTACH DATABASE ':memory:' AS test1;
  1672   1672       }
  1673   1673     } {0 {}}
  1674   1674     do_test auth-1.256 {
  1675   1675       lindex [execsql {PRAGMA database_list}] 7
  1676   1676     } {}
  1677   1677     do_test auth-1.257 {
  1678         -    proc auth {code arg1 arg2 arg3 arg4} {
         1678  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1679   1679         if {$code=="SQLITE_DETACH"} {
  1680   1680           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1681   1681           return SQLITE_OK
  1682   1682         }
  1683   1683         return SQLITE_OK
  1684   1684       }
  1685   1685       execsql {ATTACH DATABASE ':memory:' AS test1}
................................................................................
  1688   1688       }
  1689   1689     } {0 {}}
  1690   1690     do_test auth-1.258 {
  1691   1691       lindex [execsql {PRAGMA database_list}] 7
  1692   1692     } {}
  1693   1693     do_test auth-1.259 {
  1694   1694       execsql {ATTACH DATABASE ':memory:' AS test1}
  1695         -    proc auth {code arg1 arg2 arg3 arg4} {
         1695  +    proc auth {code arg1 arg2 arg3 arg4 args} {
  1696   1696         if {$code=="SQLITE_DETACH"} {
  1697   1697           set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1698   1698           return SQLITE_IGNORE
  1699   1699         }
  1700   1700         return SQLITE_OK
  1701   1701       }
  1702   1702       catchsql {
................................................................................
  1706   1706     ifcapable tempdb {
  1707   1707       ifcapable schema_pragmas {
  1708   1708       do_test auth-1.260 {
  1709   1709         lindex [execsql {PRAGMA database_list}] 7
  1710   1710       } {test1}
  1711   1711       } ;# ifcapable schema_pragmas
  1712   1712       do_test auth-1.261 {
  1713         -      proc auth {code arg1 arg2 arg3 arg4} {
         1713  +      proc auth {code arg1 arg2 arg3 arg4 args} {
  1714   1714           if {$code=="SQLITE_DETACH"} {
  1715   1715             set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1716   1716             return SQLITE_DENY
  1717   1717           }
  1718   1718           return SQLITE_OK
  1719   1719         }
  1720   1720         catchsql {
................................................................................
  1731   1731       db authorizer ::auth
  1732   1732       
  1733   1733       # Authorization for ALTER TABLE. These tests are omitted if the library
  1734   1734       # was built without ALTER TABLE support.
  1735   1735       ifcapable altertable {
  1736   1736       
  1737   1737         do_test auth-1.263 {
  1738         -        proc auth {code arg1 arg2 arg3 arg4} {
         1738  +        proc auth {code arg1 arg2 arg3 arg4 args} {
  1739   1739             if {$code=="SQLITE_ALTER_TABLE"} {
  1740   1740               set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1741   1741               return SQLITE_OK
  1742   1742             }
  1743   1743             return SQLITE_OK
  1744   1744           }
  1745   1745           catchsql {
................................................................................
  1749   1749         do_test auth-1.264 {
  1750   1750           execsql {SELECT name FROM sqlite_temp_master WHERE type='table'}
  1751   1751         } {t1x}
  1752   1752         do_test auth-1.265 {
  1753   1753           set authargs
  1754   1754         } {temp t1 {} {}}
  1755   1755         do_test auth-1.266 {
  1756         -        proc auth {code arg1 arg2 arg3 arg4} {
         1756  +        proc auth {code arg1 arg2 arg3 arg4 args} {
  1757   1757             if {$code=="SQLITE_ALTER_TABLE"} {
  1758   1758               set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1759   1759               return SQLITE_IGNORE
  1760   1760             }
  1761   1761             return SQLITE_OK
  1762   1762           }
  1763   1763           catchsql {
................................................................................
  1767   1767         do_test auth-1.267 {
  1768   1768           execsql {SELECT name FROM sqlite_temp_master WHERE type='table'}
  1769   1769         } {t1x}
  1770   1770         do_test auth-1.268 {
  1771   1771           set authargs
  1772   1772         } {temp t1x {} {}}
  1773   1773         do_test auth-1.269 {
  1774         -        proc auth {code arg1 arg2 arg3 arg4} {
         1774  +        proc auth {code arg1 arg2 arg3 arg4 args} {
  1775   1775             if {$code=="SQLITE_ALTER_TABLE"} {
  1776   1776               set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1777   1777               return SQLITE_DENY
  1778   1778             }
  1779   1779             return SQLITE_OK
  1780   1780           }
  1781   1781           catchsql {
................................................................................
  1800   1800   }
  1801   1801   
  1802   1802   ifcapable  altertable {
  1803   1803   db authorizer {}
  1804   1804   catchsql {ALTER TABLE t1x RENAME TO t1}
  1805   1805   db authorizer ::auth
  1806   1806   do_test auth-1.272 {
  1807         -  proc auth {code arg1 arg2 arg3 arg4} {
         1807  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1808   1808       if {$code=="SQLITE_ALTER_TABLE"} {
  1809   1809         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1810   1810         return SQLITE_OK
  1811   1811       }
  1812   1812       return SQLITE_OK
  1813   1813     }
  1814   1814     catchsql {
................................................................................
  1818   1818   do_test auth-1.273 {
  1819   1819     execsql {SELECT name FROM sqlite_master WHERE type='table'}
  1820   1820   } {t2x}
  1821   1821   do_test auth-1.274 {
  1822   1822     set authargs
  1823   1823   } {main t2 {} {}}
  1824   1824   do_test auth-1.275 {
  1825         -  proc auth {code arg1 arg2 arg3 arg4} {
         1825  +  proc auth {code arg1 arg2 arg3 arg4 args} {
  1826   1826       if {$code=="SQLITE_ALTER_TABLE"} {
  1827   1827         set ::authargs [list $arg1 $arg2 $arg3 $arg4]
  1828   1828         return SQLITE_IGNORE
  1829   1829       }
  1830   1830       return SQLITE_OK
  1831   1831     }
  1832   1832     catchsql {
................................................................................
  1836   1836   do_test auth-1.276 {
  1837   1837     execsql {SELECT name FROM sqlite_master WHERE type='table'}
  1838   1838   } {