@cryptotaxi247 / netdata / commits / 103a4b2f0

Update SQLITE to version 3.50.1 (#20539)

Update sqlite to version 3.50.1

Stelios Fragkakis committed Jun 23, 2025 at 08:32 UTC 103a4b2f0926832de393d5595cfed01e991911d6
2 files changed +2970 -1557
src/database/sqlite/vendored/sqlite3.c
+2868 -1513
@@ -1,6 +1,6 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 -** version 3.49.1. By combining all the individual C code files into this
3 +** version 3.50.1. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
@@ -18,7 +18,7 @@
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 -** 873d4e274b4988d260ba8354a9718324a1c2 with changes in files:
21 +** b77dc5e0f596d2140d9ac682b2893ff65d3a with changes in files:
22 **
23 **
24 */
@@ -460,7 +460,7 @@ extern "C" {
460 **
461 ** Since [version 3.6.18] ([dateof:3.6.18]),
462 ** SQLite source code has been stored in the
463 -** <a href="http://www.fossil-scm.org/">Fossil configuration management
463 +** <a href="http://fossil-scm.org/">Fossil configuration management
464 ** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
465 ** a string which identifies a particular check-in of SQLite
466 ** within its configuration management system. ^The SQLITE_SOURCE_ID
@@ -473,9 +473,9 @@ extern "C" {
473 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
474 ** [sqlite_version()] and [sqlite_source_id()].
475 */
476 -#define SQLITE_VERSION "3.49.1"
477 -#define SQLITE_VERSION_NUMBER 3049001
478 -#define SQLITE_SOURCE_ID "2025-02-18 13:38:58 873d4e274b4988d260ba8354a9718324a1c26187a4ab4c1cc0227c03d0f10e70"
476 +#define SQLITE_VERSION "3.50.1"
477 +#define SQLITE_VERSION_NUMBER 3050001
478 +#define SQLITE_SOURCE_ID "2025-06-06 14:52:32 b77dc5e0f596d2140d9ac682b2893ff65d3a4140aa86067a3efebe29dc914c95"
479
480 /*
481 ** CAPI3REF: Run-Time Library Version Numbers
@@ -1490,6 +1490,12 @@ struct sqlite3_io_methods {
1490 ** the value that M is to be set to. Before returning, the 32-bit signed
1491 ** integer is overwritten with the previous value of M.
1492 **
1493 +** <li>[[SQLITE_FCNTL_BLOCK_ON_CONNECT]]
1494 +** The [SQLITE_FCNTL_BLOCK_ON_CONNECT] opcode is used to configure the
1495 +** VFS to block when taking a SHARED lock to connect to a wal mode database.
1496 +** This is used to implement the functionality associated with
1497 +** SQLITE_SETLK_BLOCK_ON_CONNECT.
1498 +**
1499 ** <li>[[SQLITE_FCNTL_DATA_VERSION]]
1500 ** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
1501 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
@@ -1586,6 +1592,7 @@ struct sqlite3_io_methods {
1592 #define SQLITE_FCNTL_CKSM_FILE 41
1593 #define SQLITE_FCNTL_RESET_CACHE 42
1594 #define SQLITE_FCNTL_NULL_IO 43
1595 +#define SQLITE_FCNTL_BLOCK_ON_CONNECT 44
1596
1597 /* deprecated names */
1598 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
@@ -2316,13 +2323,16 @@ struct sqlite3_mem_methods {
2323 **
2324 ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
2325 ** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
2319 -** the default size of lookaside memory on each [database connection].
2326 +** the default size of [lookaside memory] on each [database connection].
2327 ** The first argument is the
2321 -** size of each lookaside buffer slot and the second is the number of
2322 -** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
2323 -** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
2324 -** option to [sqlite3_db_config()] can be used to change the lookaside
2325 -** configuration on individual connections.)^ </dd>
2328 +** size of each lookaside buffer slot ("sz") and the second is the number of
2329 +** slots allocated to each database connection ("cnt").)^
2330 +** ^(SQLITE_CONFIG_LOOKASIDE sets the <i>default</i> lookaside size.
2331 +** The [SQLITE_DBCONFIG_LOOKASIDE] option to [sqlite3_db_config()] can
2332 +** be used to change the lookaside configuration on individual connections.)^
2333 +** The [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to change the
2334 +** default lookaside configuration at compile-time.
2335 +** </dd>
2336 **
2337 ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2338 ** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
@@ -2559,31 +2569,50 @@ struct sqlite3_mem_methods {
2569 ** [[SQLITE_DBCONFIG_LOOKASIDE]]
2570 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
2571 ** <dd> The SQLITE_DBCONFIG_LOOKASIDE option is used to adjust the
2562 -** configuration of the lookaside memory allocator within a database
2572 +** configuration of the [lookaside memory allocator] within a database
2573 ** connection.
2574 ** The arguments to the SQLITE_DBCONFIG_LOOKASIDE option are <i>not</i>
2575 ** in the [DBCONFIG arguments|usual format].
2576 ** The SQLITE_DBCONFIG_LOOKASIDE option takes three arguments, not two,
2577 ** so that a call to [sqlite3_db_config()] that uses SQLITE_DBCONFIG_LOOKASIDE
2578 ** should have a total of five parameters.
2569 -** ^The first argument (the third parameter to [sqlite3_db_config()] is a
2579 +** <ol>
2580 +** <li><p>The first argument ("buf") is a
2581 ** pointer to a memory buffer to use for lookaside memory.
2571 -** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
2572 -** may be NULL in which case SQLite will allocate the
2573 -** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
2574 -** size of each lookaside buffer slot. ^The third argument is the number of
2575 -** slots. The size of the buffer in the first argument must be greater than
2576 -** or equal to the product of the second and third arguments. The buffer
2577 -** must be aligned to an 8-byte boundary. ^If the second argument to
2578 -** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
2579 -** rounded down to the next smaller multiple of 8. ^(The lookaside memory
2582 +** The first argument may be NULL in which case SQLite will allocate the
2583 +** lookaside buffer itself using [sqlite3_malloc()].
2584 +** <li><P>The second argument ("sz") is the
2585 +** size of each lookaside buffer slot. Lookaside is disabled if "sz"
2586 +** is less than 8. The "sz" argument should be a multiple of 8 less than
2587 +** 65536. If "sz" does not meet this constraint, it is reduced in size until
2588 +** it does.
2589 +** <li><p>The third argument ("cnt") is the number of slots. Lookaside is disabled
2590 +** if "cnt"is less than 1. The "cnt" value will be reduced, if necessary, so
2591 +** that the product of "sz" and "cnt" does not exceed 2,147,418,112. The "cnt"
2592 +** parameter is usually chosen so that the product of "sz" and "cnt" is less
2593 +** than 1,000,000.
2594 +** </ol>
2595 +** <p>If the "buf" argument is not NULL, then it must
2596 +** point to a memory buffer with a size that is greater than
2597 +** or equal to the product of "sz" and "cnt".
2598 +** The buffer must be aligned to an 8-byte boundary.
2599 +** The lookaside memory
2600 ** configuration for a database connection can only be changed when that
2601 ** connection is not currently using lookaside memory, or in other words
2582 -** when the "current value" returned by
2583 -** [sqlite3_db_status](D,[SQLITE_DBSTATUS_LOOKASIDE_USED],...) is zero.
2602 +** when the value returned by [SQLITE_DBSTATUS_LOOKASIDE_USED] is zero.
2603 ** Any attempt to change the lookaside memory configuration when lookaside
2604 ** memory is in use leaves the configuration unchanged and returns
2586 -** [SQLITE_BUSY].)^</dd>
2605 +** [SQLITE_BUSY].
2606 +** If the "buf" argument is NULL and an attempt
2607 +** to allocate memory based on "sz" and "cnt" fails, then
2608 +** lookaside is silently disabled.
2609 +** <p>
2610 +** The [SQLITE_CONFIG_LOOKASIDE] configuration option can be used to set the
2611 +** default lookaside configuration at initialization. The
2612 +** [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to set the default lookaside
2613 +** configuration at compile-time. Typical values for lookaside are 1200 for
2614 +** "sz" and 40 to 100 for "cnt".
2615 +** </dd>
2616 **
2617 ** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
2618 ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
@@ -3320,6 +3349,44 @@ SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3349 */
3350 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3351
3352 +/*
3353 +** CAPI3REF: Set the Setlk Timeout
3354 +** METHOD: sqlite3
3355 +**
3356 +** This routine is only useful in SQLITE_ENABLE_SETLK_TIMEOUT builds. If
3357 +** the VFS supports blocking locks, it sets the timeout in ms used by
3358 +** eligible locks taken on wal mode databases by the specified database
3359 +** handle. In non-SQLITE_ENABLE_SETLK_TIMEOUT builds, or if the VFS does
3360 +** not support blocking locks, this function is a no-op.
3361 +**
3362 +** Passing 0 to this function disables blocking locks altogether. Passing
3363 +** -1 to this function requests that the VFS blocks for a long time -
3364 +** indefinitely if possible. The results of passing any other negative value
3365 +** are undefined.
3366 +**
3367 +** Internally, each SQLite database handle store two timeout values - the
3368 +** busy-timeout (used for rollback mode databases, or if the VFS does not
3369 +** support blocking locks) and the setlk-timeout (used for blocking locks
3370 +** on wal-mode databases). The sqlite3_busy_timeout() method sets both
3371 +** values, this function sets only the setlk-timeout value. Therefore,
3372 +** to configure separate busy-timeout and setlk-timeout values for a single
3373 +** database handle, call sqlite3_busy_timeout() followed by this function.
3374 +**
3375 +** Whenever the number of connections to a wal mode database falls from
3376 +** 1 to 0, the last connection takes an exclusive lock on the database,
3377 +** then checkpoints and deletes the wal file. While it is doing this, any
3378 +** new connection that tries to read from the database fails with an
3379 +** SQLITE_BUSY error. Or, if the SQLITE_SETLK_BLOCK_ON_CONNECT flag is
3380 +** passed to this API, the new connection blocks until the exclusive lock
3381 +** has been released.
3382 +*/
3383 +SQLITE_API int sqlite3_setlk_timeout(sqlite3*, int ms, int flags);
3384 +
3385 +/*
3386 +** CAPI3REF: Flags for sqlite3_setlk_timeout()
3387 +*/
3388 +#define SQLITE_SETLK_BLOCK_ON_CONNECT 0x01
3389 +
3390 /*
3391 ** CAPI3REF: Convenience Routines For Running Queries
3392 ** METHOD: sqlite3
@@ -5435,7 +5502,7 @@ SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5502 ** other than [SQLITE_ROW] before any subsequent invocation of
5503 ** sqlite3_step(). Failure to reset the prepared statement using
5504 ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
5438 -** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1],
5505 +** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1]),
5506 ** sqlite3_step() began
5507 ** calling [sqlite3_reset()] automatically in this circumstance rather
5508 ** than returning [SQLITE_MISUSE]. This is not considered a compatibility
@@ -7331,6 +7398,8 @@ SQLITE_API int sqlite3_autovacuum_pages(
7398 **
7399 ** ^The second argument is a pointer to the function to invoke when a
7400 ** row is updated, inserted or deleted in a rowid table.
7401 +** ^The update hook is disabled by invoking sqlite3_update_hook()
7402 +** with a NULL pointer as the second parameter.
7403 ** ^The first argument to the callback is a copy of the third argument
7404 ** to sqlite3_update_hook().
7405 ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
@@ -11813,9 +11882,10 @@ SQLITE_API void sqlite3session_table_filter(
11882 ** is inserted while a session object is enabled, then later deleted while
11883 ** the same session object is disabled, no INSERT record will appear in the
11884 ** changeset, even though the delete took place while the session was disabled.
11816 -** Or, if one field of a row is updated while a session is disabled, and
11817 -** another field of the same row is updated while the session is enabled, the
11818 -** resulting changeset will contain an UPDATE change that updates both fields.
11885 +** Or, if one field of a row is updated while a session is enabled, and
11886 +** then another field of the same row is updated while the session is disabled,
11887 +** the resulting changeset will contain an UPDATE change that updates both
11888 +** fields.
11889 */
11890 SQLITE_API int sqlite3session_changeset(
11891 sqlite3_session *pSession, /* Session object */
@@ -11887,8 +11957,9 @@ SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession
11957 ** database zFrom the contents of the two compatible tables would be
11958 ** identical.
11959 **
11890 -** It an error if database zFrom does not exist or does not contain the
11891 -** required compatible table.
11960 +** Unless the call to this function is a no-op as described above, it is an
11961 +** error if database zFrom does not exist or does not contain the required
11962 +** compatible table.
11963 **
11964 ** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
11965 ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
@@ -12023,7 +12094,7 @@ SQLITE_API int sqlite3changeset_start_v2(
12094 ** The following flags may passed via the 4th parameter to
12095 ** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
12096 **
12026 -** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
12097 +** <dt>SQLITE_CHANGESETSTART_INVERT <dd>
12098 ** Invert the changeset while iterating through it. This is equivalent to
12099 ** inverting a changeset using sqlite3changeset_invert() before applying it.
12100 ** It is an error to specify this flag with a patchset.
@@ -12338,19 +12409,6 @@ SQLITE_API int sqlite3changeset_concat(
12409 void **ppOut /* OUT: Buffer containing output changeset */
12410 );
12411
12341 -
12342 -/*
12343 -** CAPI3REF: Upgrade the Schema of a Changeset/Patchset
12344 -*/
12345 -SQLITE_API int sqlite3changeset_upgrade(
12346 - sqlite3 *db,
12347 - const char *zDb,
12348 - int nIn, const void *pIn, /* Input changeset */
12349 - int *pnOut, void **ppOut /* OUT: Inverse of input */
12350 -);
12351 -
12352 -
12353 -
12412 /*
12413 ** CAPI3REF: Changegroup Handle
12414 **
@@ -14098,14 +14156,22 @@ struct fts5_api {
14156 ** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
14157 ** * Terms in the VALUES clause of an INSERT statement
14158 **
14101 -** The hard upper limit here is 32676. Most database people will
14159 +** The hard upper limit here is 32767. Most database people will
14160 ** tell you that in a well-normalized database, you usually should
14161 ** not have more than a dozen or so columns in any table. And if
14162 ** that is the case, there is no point in having more than a few
14163 ** dozen values in any of the other situations described above.
14164 +**
14165 +** An index can only have SQLITE_MAX_COLUMN columns from the user
14166 +** point of view, but the underlying b-tree that implements the index
14167 +** might have up to twice as many columns in a WITHOUT ROWID table,
14168 +** since must also store the primary key at the end. Hence the
14169 +** column count for Index is u16 instead of i16.
14170 */
14107 -#ifndef SQLITE_MAX_COLUMN
14171 +#if !defined(SQLITE_MAX_COLUMN)
14172 # define SQLITE_MAX_COLUMN 2000
14173 +#elif SQLITE_MAX_COLUMN>32767
14174 +# error SQLITE_MAX_COLUMN may not exceed 32767
14175 #endif
14176
14177 /*
@@ -14757,6 +14823,7 @@ struct HashElem {
14823 HashElem *next, *prev; /* Next and previous elements in the table */
14824 void *data; /* Data associated with this element */
14825 const char *pKey; /* Key associated with this element */
14826 + unsigned int h; /* hash for pKey */
14827 };
14828
14829 /*
@@ -15117,7 +15184,17 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*);
15184 ** ourselves.
15185 */
15186 #ifndef offsetof
15120 -#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
15187 +#define offsetof(STRUCTURE,FIELD) ((size_t)((char*)&((STRUCTURE*)0)->FIELD))
15188 +#endif
15189 +
15190 +/*
15191 +** Work around C99 "flex-array" syntax for pre-C99 compilers, so as
15192 +** to avoid complaints from -fsanitize=strict-bounds.
15193 +*/
15194 +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
15195 +# define FLEXARRAY
15196 +#else
15197 +# define FLEXARRAY 1
15198 #endif
15199
15200 /*
@@ -15195,6 +15272,11 @@ typedef INT16_TYPE i16; /* 2-byte signed integer */
15272 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
15273 typedef INT8_TYPE i8; /* 1-byte signed integer */
15274
15275 +/* A bitfield type for use inside of structures. Always follow with :N where
15276 +** N is the number of bits.
15277 +*/
15278 +typedef unsigned bft; /* Bit Field Type */
15279 +
15280 /*
15281 ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
15282 ** that can be stored in a u32 without loss of data. The value
@@ -15363,6 +15445,14 @@ typedef INT16_TYPE LogEst;
15445 #define LARGEST_UINT64 (0xffffffff|(((u64)0xffffffff)<<32))
15446 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
15447
15448 +/*
15449 +** Macro SMXV(n) return the maximum value that can be held in variable n,
15450 +** assuming n is a signed integer type. UMXV(n) is similar for unsigned
15451 +** integer types.
15452 +*/
15453 +#define SMXV(n) ((((i64)1)<<(sizeof(n)-1))-1)
15454 +#define UMXV(n) ((((i64)1)<<(sizeof(n)))-1)
15455 +
15456 /*
15457 ** Round up a number to the next larger multiple of 8. This is used
15458 ** to force 8-byte alignment on 64-bit architectures.
@@ -17339,8 +17429,8 @@ SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*);
17429 SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3*);
17430 #endif
17431
17342 -/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
17343 -** each VDBE opcode.
17432 +/* Use SQLITE_ENABLE_EXPLAIN_COMMENTS to enable generation of extra
17433 +** comments on each VDBE opcode.
17434 **
17435 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
17436 ** comments in VDBE programs that show key decision points in the code
@@ -18063,6 +18153,10 @@ struct sqlite3 {
18153 Savepoint *pSavepoint; /* List of active savepoints */
18154 int nAnalysisLimit; /* Number of index rows to ANALYZE */
18155 int busyTimeout; /* Busy handler timeout, in msec */
18156 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
18157 + int setlkTimeout; /* Blocking lock timeout, in msec. -1 -> inf. */
18158 + int setlkFlags; /* Flags passed to setlk_timeout() */
18159 +#endif
18160 int nSavepoint; /* Number of non-transaction savepoints */
18161 int nStatement; /* Number of nested statement-transactions */
18162 i64 nDeferredCons; /* Net deferred constraints this transaction. */
@@ -18617,6 +18711,7 @@ struct CollSeq {
18711 #define SQLITE_AFF_INTEGER 0x44 /* 'D' */
18712 #define SQLITE_AFF_REAL 0x45 /* 'E' */
18713 #define SQLITE_AFF_FLEXNUM 0x46 /* 'F' */
18714 +#define SQLITE_AFF_DEFER 0x58 /* 'X' - defer computation until later */
18715
18716 #define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
18717
@@ -18741,6 +18836,7 @@ struct Table {
18836 } u;
18837 Trigger *pTrigger; /* List of triggers on this object */
18838 Schema *pSchema; /* Schema that contains this table */
18839 + u8 aHx[16]; /* Column aHt[K%sizeof(aHt)] might have hash K */
18840 };
18841
18842 /*
@@ -18874,9 +18970,13 @@ struct FKey {
18970 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
18971 int iFrom; /* Index of column in pFrom */
18972 char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
18877 - } aCol[1]; /* One entry for each of nCol columns */
18973 + } aCol[FLEXARRAY]; /* One entry for each of nCol columns */
18974 };
18975
18976 +/* The size (in bytes) of an FKey object holding N columns. The answer
18977 +** does NOT include space to hold the zTo name. */
18978 +#define SZ_FKEY(N) (offsetof(FKey,aCol)+(N)*sizeof(struct sColMap))
18979 +
18980 /*
18981 ** SQLite supports many different ways to resolve a constraint
18982 ** error. ROLLBACK processing means that a constraint violation
@@ -18938,9 +19038,12 @@ struct KeyInfo {
19038 u16 nAllField; /* Total columns, including key plus others */
19039 sqlite3 *db; /* The database connection */
19040 u8 *aSortFlags; /* Sort order for each column. */
18941 - CollSeq *aColl[1]; /* Collating sequence for each term of the key */
19041 + CollSeq *aColl[FLEXARRAY]; /* Collating sequence for each term of the key */
19042 };
19043
19044 +/* The size (in bytes) of a KeyInfo object with up to N fields */
19045 +#define SZ_KEYINFO(N) (offsetof(KeyInfo,aColl) + (N)*sizeof(CollSeq*))
19046 +
19047 /*
19048 ** Allowed bit values for entries in the KeyInfo.aSortFlags[] array.
19049 */
@@ -19060,7 +19163,7 @@ struct Index {
19163 Pgno tnum; /* DB Page containing root of this index */
19164 LogEst szIdxRow; /* Estimated average row size in bytes */
19165 u16 nKeyCol; /* Number of columns forming the key */
19063 - u16 nColumn; /* Number of columns stored in the index */
19166 + u16 nColumn; /* Nr columns in btree. Can be 2*Table.nCol */
19167 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
19168 unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
19169 unsigned bUnordered:1; /* Use this index for == or IN queries only */
@@ -19069,9 +19172,9 @@ struct Index {
19172 unsigned isCovering:1; /* True if this is a covering index */
19173 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
19174 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
19072 - unsigned bLowQual:1; /* sqlite_stat1 says this is a low-quality index */
19175 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
19176 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
19177 + unsigned bIdxRowid:1; /* One or more of the index keys is the ROWID */
19178 unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
19179 unsigned bHasExpr:1; /* Index contains an expression, either a literal
19180 ** expression, or a reference to a VIRTUAL column */
@@ -19398,10 +19501,10 @@ struct Expr {
19501 /* Macros can be used to test, set, or clear bits in the
19502 ** Expr.flags field.
19503 */
19401 -#define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
19402 -#define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
19403 -#define ExprSetProperty(E,P) (E)->flags|=(P)
19404 -#define ExprClearProperty(E,P) (E)->flags&=~(P)
19504 +#define ExprHasProperty(E,P) (((E)->flags&(u32)(P))!=0)
19505 +#define ExprHasAllProperty(E,P) (((E)->flags&(u32)(P))==(u32)(P))
19506 +#define ExprSetProperty(E,P) (E)->flags|=(u32)(P)
19507 +#define ExprClearProperty(E,P) (E)->flags&=~(u32)(P)
19508 #define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
19509 #define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
19510 #define ExprIsFullSize(E) (((E)->flags&(EP_Reduced|EP_TokenOnly))==0)
@@ -19513,9 +19616,14 @@ struct ExprList {
19616 int iConstExprReg; /* Register in which Expr value is cached. Used only
19617 ** by Parse.pConstExpr */
19618 } u;
19516 - } a[1]; /* One slot for each expression in the list */
19619 + } a[FLEXARRAY]; /* One slot for each expression in the list */
19620 };
19621
19622 +/* The size (in bytes) of an ExprList object that is big enough to hold
19623 +** as many as N expressions. */
19624 +#define SZ_EXPRLIST(N) \
19625 + (offsetof(ExprList,a) + (N)*sizeof(struct ExprList_item))
19626 +
19627 /*
19628 ** Allowed values for Expr.a.eEName
19629 */
@@ -19543,9 +19651,12 @@ struct IdList {
19651 int nId; /* Number of identifiers on the list */
19652 struct IdList_item {
19653 char *zName; /* Name of the identifier */
19546 - } a[1];
19654 + } a[FLEXARRAY];
19655 };
19656
19657 +/* The size (in bytes) of an IdList object that can hold up to N IDs. */
19658 +#define SZ_IDLIST(N) (offsetof(IdList,a)+(N)*sizeof(struct IdList_item))
19659 +
19660 /*
19661 ** Allowed values for IdList.eType, which determines which value of the a.u4
19662 ** is valid.
@@ -19665,11 +19776,19 @@ struct OnOrUsing {
19776 **
19777 */
19778 struct SrcList {
19668 - int nSrc; /* Number of tables or subqueries in the FROM clause */
19669 - u32 nAlloc; /* Number of entries allocated in a[] below */
19670 - SrcItem a[1]; /* One entry for each identifier on the list */
19779 + int nSrc; /* Number of tables or subqueries in the FROM clause */
19780 + u32 nAlloc; /* Number of entries allocated in a[] below */
19781 + SrcItem a[FLEXARRAY]; /* One entry for each identifier on the list */
19782 };
19783
19784 +/* Size (in bytes) of a SrcList object that can hold as many as N
19785 +** SrcItem objects. */
19786 +#define SZ_SRCLIST(N) (offsetof(SrcList,a)+(N)*sizeof(SrcItem))
19787 +
19788 +/* Size (in bytes( of a SrcList object that holds 1 SrcItem. This is a
19789 +** special case of SZ_SRCITEM(1) that comes up often. */
19790 +#define SZ_SRCLIST_1 (offsetof(SrcList,a)+sizeof(SrcItem))
19791 +
19792 /*
19793 ** Permitted values of the SrcList.a.jointype field
19794 */
@@ -20138,25 +20257,32 @@ struct Parse {
20257 char *zErrMsg; /* An error message */
20258 Vdbe *pVdbe; /* An engine for executing database bytecode */
20259 int rc; /* Return code from execution */
20141 - u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
20142 - u8 checkSchema; /* Causes schema cookie check after an error */
20260 + LogEst nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
20261 u8 nested; /* Number of nested calls to the parser/code generator */
20262 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
20263 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
20264 u8 mayAbort; /* True if statement may throw an ABORT exception */
20265 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
20148 - u8 okConstFactor; /* OK to factor out constants */
20266 u8 disableLookaside; /* Number of times lookaside has been disabled */
20267 u8 prepFlags; /* SQLITE_PREPARE_* flags */
20268 u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
20152 - u8 bHasWith; /* True if statement contains WITH */
20269 u8 mSubrtnSig; /* mini Bloom filter on available SubrtnSig.selId */
20270 + u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
20271 + u8 bReturning; /* Coding a RETURNING trigger */
20272 + u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
20273 + u8 disableTriggers; /* True to disable triggers */
20274 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
20275 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
20276 #endif
20277 #ifdef SQLITE_DEBUG
20278 u8 ifNotExists; /* Might be true if IF NOT EXISTS. Assert()s only */
20279 + u8 isCreate; /* CREATE TABLE, INDEX, or VIEW (but not TRIGGER)
20280 + ** and ALTER TABLE ADD COLUMN. */
20281 #endif
20282 + bft colNamesSet :1; /* TRUE after OP_ColumnName has been issued to pVdbe */
20283 + bft bHasWith :1; /* True if statement contains WITH */
20284 + bft okConstFactor :1; /* OK to factor out constants */
20285 + bft checkSchema :1; /* Causes schema cookie check after an error */
20286 int nRangeReg; /* Size of the temporary register block */
20287 int iRangeReg; /* First register in temporary register block */
20288 int nErr; /* Number of errors seen */
@@ -20171,12 +20297,9 @@ struct Parse {
20297 ExprList *pConstExpr;/* Constant expressions */
20298 IndexedExpr *pIdxEpr;/* List of expressions used by active indexes */
20299 IndexedExpr *pIdxPartExpr; /* Exprs constrained by index WHERE clauses */
20174 - Token constraintName;/* Name of the constraint currently being parsed */
20300 yDbMask writeMask; /* Start a write transaction on these databases */
20301 yDbMask cookieMask; /* Bitmask of schema verified databases */
20177 - int regRowid; /* Register holding rowid of CREATE TABLE entry */
20178 - int regRoot; /* Register holding root page number for new objects */
20179 - int nMaxArg; /* Max args passed to user function by sub-program */
20302 + int nMaxArg; /* Max args to xUpdate and xFilter vtab methods */
20303 int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
20304 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
20305 u32 nProgressSteps; /* xProgress steps taken during sqlite3_prepare() */
@@ -20190,17 +20313,6 @@ struct Parse {
20313 Table *pTriggerTab; /* Table triggers are being coded for */
20314 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
20315 ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */
20193 - union {
20194 - int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
20195 - Returning *pReturning; /* The RETURNING clause */
20196 - } u1;
20197 - u32 oldmask; /* Mask of old.* columns referenced */
20198 - u32 newmask; /* Mask of new.* columns referenced */
20199 - LogEst nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
20200 - u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
20201 - u8 bReturning; /* Coding a RETURNING trigger */
20202 - u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
20203 - u8 disableTriggers; /* True to disable triggers */
20316
20317 /**************************************************************************
20318 ** Fields above must be initialized to zero. The fields that follow,
@@ -20212,6 +20324,19 @@ struct Parse {
20324 int aTempReg[8]; /* Holding area for temporary registers */
20325 Parse *pOuterParse; /* Outer Parse object when nested */
20326 Token sNameToken; /* Token with unqualified schema object name */
20327 + u32 oldmask; /* Mask of old.* columns referenced */
20328 + u32 newmask; /* Mask of new.* columns referenced */
20329 + union {
20330 + struct { /* These fields available when isCreate is true */
20331 + int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
20332 + int regRowid; /* Register holding rowid of CREATE TABLE entry */
20333 + int regRoot; /* Register holding root page for new objects */
20334 + Token constraintName; /* Name of the constraint currently being parsed */
20335 + } cr;
20336 + struct { /* These fields available to all other statements */
20337 + Returning *pReturning; /* The RETURNING clause */
20338 + } d;
20339 + } u1;
20340
20341 /************************************************************************
20342 ** Above is constant between recursions. Below is reset before and after
@@ -20727,9 +20852,13 @@ struct With {
20852 int nCte; /* Number of CTEs in the WITH clause */
20853 int bView; /* Belongs to the outermost Select of a view */
20854 With *pOuter; /* Containing WITH clause, or NULL */
20730 - Cte a[1]; /* For each CTE in the WITH clause.... */
20855 + Cte a[FLEXARRAY]; /* For each CTE in the WITH clause.... */
20856 };
20857
20858 +/* The size (in bytes) of a With object that can hold as many
20859 +** as N different CTEs. */
20860 +#define SZ_WITH(N) (offsetof(With,a) + (N)*sizeof(Cte))
20861 +
20862 /*
20863 ** The Cte object is not guaranteed to persist for the entire duration
20864 ** of code generation. (The query flattener or other parser tree
@@ -20758,9 +20887,13 @@ struct DbClientData {
20887 DbClientData *pNext; /* Next in a linked list */
20888 void *pData; /* The data */
20889 void (*xDestructor)(void*); /* Destructor. Might be NULL */
20761 - char zName[1]; /* Name of this client data. MUST BE LAST */
20890 + char zName[FLEXARRAY]; /* Name of this client data. MUST BE LAST */
20891 };
20892
20893 +/* The size (in bytes) of a DbClientData object that can has a name
20894 +** that is N bytes long, including the zero-terminator. */
20895 +#define SZ_DBCLIENTDATA(N) (offsetof(DbClientData,zName)+(N))
20896 +
20897 #ifdef SQLITE_DEBUG
20898 /*
20899 ** An instance of the TreeView object is used for printing the content of
@@ -21203,7 +21336,7 @@ SQLITE_PRIVATE void sqlite3SubqueryColumnTypes(Parse*,Table*,Select*,char);
21336 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
21337 SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *, int);
21338 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
21206 -SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index*, i16);
21339 +SQLITE_PRIVATE int sqlite3TableColumnToIndex(Index*, int);
21340 #ifdef SQLITE_OMIT_GENERATED_COLUMNS
21341 # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
21342 # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
@@ -21301,7 +21434,7 @@ SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
21434 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
21435 SQLITE_PRIVATE void sqlite3ClearOnOrUsing(sqlite3*, OnOrUsing*);
21436 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
21304 -SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
21437 +SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,int,int,char**);
21438 SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
21439 Expr*, int, int, u8);
21440 SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
@@ -21437,7 +21570,8 @@ SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,const Select*,int);
21570 SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
21571 SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
21572 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
21440 -SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum*,sqlite3_value*);
21573 +SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum*,sqlite3_value*,int);
21574 +SQLITE_PRIVATE int sqlite3AppendOneUtf8Character(char*, u32);
21575 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
21576 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
21577 SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void);
@@ -22302,6 +22436,9 @@ static const char * const sqlite3azCompileOpt[] = {
22436 #ifdef SQLITE_BUG_COMPATIBLE_20160819
22437 "BUG_COMPATIBLE_20160819",
22438 #endif
22439 +#ifdef SQLITE_BUG_COMPATIBLE_20250510
22440 + "BUG_COMPATIBLE_20250510",
22441 +#endif
22442 #ifdef SQLITE_CASE_SENSITIVE_LIKE
22443 "CASE_SENSITIVE_LIKE",
22444 #endif
@@ -22538,6 +22675,9 @@ static const char * const sqlite3azCompileOpt[] = {
22675 #ifdef SQLITE_ENABLE_SESSION
22676 "ENABLE_SESSION",
22677 #endif
22678 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
22679 + "ENABLE_SETLK_TIMEOUT",
22680 +#endif
22681 #ifdef SQLITE_ENABLE_SNAPSHOT
22682 "ENABLE_SNAPSHOT",
22683 #endif
@@ -22592,6 +22732,9 @@ static const char * const sqlite3azCompileOpt[] = {
22732 #ifdef SQLITE_EXTRA_INIT
22733 "EXTRA_INIT=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT),
22734 #endif
22735 +#ifdef SQLITE_EXTRA_INIT_MUTEXED
22736 + "EXTRA_INIT_MUTEXED=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT_MUTEXED),
22737 +#endif
22738 #ifdef SQLITE_EXTRA_SHUTDOWN
22739 "EXTRA_SHUTDOWN=" CTIMEOPT_VAL(SQLITE_EXTRA_SHUTDOWN),
22740 #endif
@@ -23576,12 +23719,19 @@ struct VdbeCursor {
23719 #endif
23720 VdbeTxtBlbCache *pCache; /* Cache of large TEXT or BLOB values */
23721
23579 - /* 2*nField extra array elements allocated for aType[], beyond the one
23580 - ** static element declared in the structure. nField total array slots for
23581 - ** aType[] and nField+1 array slots for aOffset[] */
23582 - u32 aType[1]; /* Type values record decode. MUST BE LAST */
23722 + /* Space is allocated for aType to hold at least 2*nField+1 entries:
23723 + ** nField slots for aType[] and nField+1 array slots for aOffset[] */
23724 + u32 aType[FLEXARRAY]; /* Type values record decode. MUST BE LAST */
23725 };
23726
23727 +/*
23728 +** The size (in bytes) of a VdbeCursor object that has an nField value of N
23729 +** or less. The value of SZ_VDBECURSOR(n) is guaranteed to be a multiple
23730 +** of 8.
23731 +*/
23732 +#define SZ_VDBECURSOR(N) \
23733 + (ROUND8(offsetof(VdbeCursor,aType)) + ((N)+1)*sizeof(u64))
23734 +
23735 /* Return true if P is a null-only cursor
23736 */
23737 #define IsNullCursor(P) \
@@ -23838,13 +23988,16 @@ struct sqlite3_context {
23988 u8 enc; /* Encoding to use for results */
23989 u8 skipFlag; /* Skip accumulator loading if true */
23990 u16 argc; /* Number of arguments */
23841 - sqlite3_value *argv[1]; /* Argument set */
23991 + sqlite3_value *argv[FLEXARRAY]; /* Argument set */
23992 };
23993
23844 -/* A bitfield type for use inside of structures. Always follow with :N where
23845 -** N is the number of bits.
23994 +/*
23995 +** The size (in bytes) of an sqlite3_context object that holds N
23996 +** argv[] arguments.
23997 */
23847 -typedef unsigned bft; /* Bit Field Type */
23998 +#define SZ_CONTEXT(N) \
23999 + (offsetof(sqlite3_context,argv)+(N)*sizeof(sqlite3_value*))
24000 +
24001
24002 /* The ScanStatus object holds a single value for the
24003 ** sqlite3_stmt_scanstatus() interface.
@@ -23905,7 +24058,7 @@ struct Vdbe {
24058 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
24059 i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
24060 Mem *aMem; /* The memory locations */
23908 - Mem **apArg; /* Arguments to currently executing user function */
24061 + Mem **apArg; /* Arguments xUpdate and xFilter vtab methods */
24062 VdbeCursor **apCsr; /* One element of this array for each open cursor */
24063 Mem *aVar; /* Values for the OP_Variable opcode. */
24064
@@ -23925,6 +24078,7 @@ struct Vdbe {
24078 #ifdef SQLITE_DEBUG
24079 int rcApp; /* errcode set by sqlite3_result_error_code() */
24080 u32 nWrite; /* Number of write operations that have occurred */
24081 + int napArg; /* Size of the apArg[] array */
24082 #endif
24083 u16 nResColumn; /* Number of columns in one row of the result set */
24084 u16 nResAlloc; /* Column slots allocated to aColName[] */
@@ -23977,7 +24131,7 @@ struct PreUpdate {
24131 VdbeCursor *pCsr; /* Cursor to read old values from */
24132 int op; /* One of SQLITE_INSERT, UPDATE, DELETE */
24133 u8 *aRecord; /* old.* database record */
23980 - KeyInfo keyinfo;
24134 + KeyInfo *pKeyinfo; /* Key information */
24135 UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */
24136 UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */
24137 int iNewReg; /* Register for new.* values */
@@ -23989,6 +24143,7 @@ struct PreUpdate {
24143 Table *pTab; /* Schema object being updated */
24144 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
24145 sqlite3_value **apDflt; /* Array of default values, if required */
24146 + u8 keyinfoSpace[SZ_KEYINFO(0)]; /* Space to hold pKeyinfo[0] content */
24147 };
24148
24149 /*
@@ -24355,8 +24510,9 @@ SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
24510 nInit += countLookasideSlots(db->lookaside.pSmallInit);
24511 nFree += countLookasideSlots(db->lookaside.pSmallFree);
24512 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
24358 - if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
24359 - return db->lookaside.nSlot - (nInit+nFree);
24513 + assert( db->lookaside.nSlot >= nInit+nFree );
24514 + if( pHighwater ) *pHighwater = (int)(db->lookaside.nSlot - nInit);
24515 + return (int)(db->lookaside.nSlot - (nInit+nFree));
24516 }
24517
24518 /*
@@ -24409,7 +24565,7 @@ SQLITE_API int sqlite3_db_status(
24565 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
24566 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
24567 *pCurrent = 0;
24412 - *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
24568 + *pHighwater = (int)db->lookaside.anStat[op-SQLITE_DBSTATUS_LOOKASIDE_HIT];
24569 if( resetFlag ){
24570 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
24571 }
@@ -25921,7 +26077,7 @@ static int daysAfterMonday(DateTime *pDate){
26077 ** In other words, return the day of the week according
26078 ** to this code:
26079 **
25924 -** 0=Sunday, 1=Monday, 2=Tues, ..., 6=Saturday
26080 +** 0=Sunday, 1=Monday, 2=Tuesday, ..., 6=Saturday
26081 */
26082 static int daysAfterSunday(DateTime *pDate){
26083 assert( pDate->validJD );
@@ -30130,6 +30286,8 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
30286
30287 #ifdef __CYGWIN__
30288 # include <sys/cygwin.h>
30289 +# include <sys/stat.h> /* amalgamator: dontcache */
30290 +# include <unistd.h> /* amalgamator: dontcache */
30291 # include <errno.h> /* amalgamator: dontcache */
30292 #endif
30293
@@ -31524,17 +31682,17 @@ SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
31682 #define etPERCENT 7 /* Percent symbol. %% */
31683 #define etCHARX 8 /* Characters. %c */
31684 /* The rest are extensions, not normally found in printf() */
31527 -#define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */
31528 -#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
31529 - NULL pointers replaced by SQL NULL. %Q */
31530 -#define etTOKEN 11 /* a pointer to a Token structure */
31531 -#define etSRCITEM 12 /* a pointer to a SrcItem */
31532 -#define etPOINTER 13 /* The %p conversion */
31533 -#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
31534 -#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
31535 -#define etDECIMAL 16 /* %d or %u, but not %x, %o */
31685 +#define etESCAPE_q 9 /* Strings with '\'' doubled. %q */
31686 +#define etESCAPE_Q 10 /* Strings with '\'' doubled and enclosed in '',
31687 + NULL pointers replaced by SQL NULL. %Q */
31688 +#define etTOKEN 11 /* a pointer to a Token structure */
31689 +#define etSRCITEM 12 /* a pointer to a SrcItem */
31690 +#define etPOINTER 13 /* The %p conversion */
31691 +#define etESCAPE_w 14 /* %w -> Strings with '\"' doubled */
31692 +#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
31693 +#define etDECIMAL 16 /* %d or %u, but not %x, %o */
31694
31537 -#define etINVALID 17 /* Any unrecognized conversion type */
31695 +#define etINVALID 17 /* Any unrecognized conversion type */
31696
31697
31698 /*
@@ -31573,9 +31731,9 @@ static const et_info fmtinfo[] = {
31731 { 's', 0, 4, etSTRING, 0, 0 },
31732 { 'g', 0, 1, etGENERIC, 30, 0 },
31733 { 'z', 0, 4, etDYNSTRING, 0, 0 },
31576 - { 'q', 0, 4, etSQLESCAPE, 0, 0 },
31577 - { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
31578 - { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
31734 + { 'q', 0, 4, etESCAPE_q, 0, 0 },
31735 + { 'Q', 0, 4, etESCAPE_Q, 0, 0 },
31736 + { 'w', 0, 4, etESCAPE_w, 0, 0 },
31737 { 'c', 0, 0, etCHARX, 0, 0 },
31738 { 'o', 8, 0, etRADIX, 0, 2 },
31739 { 'u', 10, 0, etDECIMAL, 0, 0 },
@@ -32172,25 +32330,7 @@ SQLITE_API void sqlite3_str_vappendf(
32330 }
32331 }else{
32332 unsigned int ch = va_arg(ap,unsigned int);
32175 - if( ch<0x00080 ){
32176 - buf[0] = ch & 0xff;
32177 - length = 1;
32178 - }else if( ch<0x00800 ){
32179 - buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
32180 - buf[1] = 0x80 + (u8)(ch & 0x3f);
32181 - length = 2;
32182 - }else if( ch<0x10000 ){
32183 - buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
32184 - buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
32185 - buf[2] = 0x80 + (u8)(ch & 0x3f);
32186 - length = 3;
32187 - }else{
32188 - buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
32189 - buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
32190 - buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
32191 - buf[3] = 0x80 + (u8)(ch & 0x3f);
32192 - length = 4;
32193 - }
32333 + length = sqlite3AppendOneUtf8Character(buf, ch);
32334 }
32335 if( precision>1 ){
32336 i64 nPrior = 1;
@@ -32270,22 +32410,31 @@ SQLITE_API void sqlite3_str_vappendf(
32410 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
32411 }
32412 break;
32273 - case etSQLESCAPE: /* %q: Escape ' characters */
32274 - case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
32275 - case etSQLESCAPE3: { /* %w: Escape " characters */
32413 + case etESCAPE_q: /* %q: Escape ' characters */
32414 + case etESCAPE_Q: /* %Q: Escape ' and enclose in '...' */
32415 + case etESCAPE_w: { /* %w: Escape " characters */
32416 i64 i, j, k, n;
32277 - int needQuote, isnull;
32417 + int needQuote = 0;
32418 char ch;
32279 - char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
32419 char *escarg;
32420 + char q;
32421
32422 if( bArgList ){
32423 escarg = getTextArg(pArgList);
32424 }else{
32425 escarg = va_arg(ap,char*);
32426 }
32287 - isnull = escarg==0;
32288 - if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
32427 + if( escarg==0 ){
32428 + escarg = (xtype==etESCAPE_Q ? "NULL" : "(NULL)");
32429 + }else if( xtype==etESCAPE_Q ){
32430 + needQuote = 1;
32431 + }
32432 + if( xtype==etESCAPE_w ){
32433 + q = '"';
32434 + flag_alternateform = 0;
32435 + }else{
32436 + q = '\'';
32437 + }
32438 /* For %q, %Q, and %w, the precision is the number of bytes (or
32439 ** characters if the ! flags is present) to use from the input.
32440 ** Because of the extra quoting characters inserted, the number
@@ -32298,7 +32447,30 @@ SQLITE_API void sqlite3_str_vappendf(
32447 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
32448 }
32449 }
32301 - needQuote = !isnull && xtype==etSQLESCAPE2;
32450 + if( flag_alternateform ){
32451 + /* For %#q, do unistr()-style backslash escapes for
32452 + ** all control characters, and for backslash itself.
32453 + ** For %#Q, do the same but only if there is at least
32454 + ** one control character. */
32455 + u32 nBack = 0;
32456 + u32 nCtrl = 0;
32457 + for(k=0; k<i; k++){
32458 + if( escarg[k]=='\\' ){
32459 + nBack++;
32460 + }else if( ((u8*)escarg)[k]<=0x1f ){
32461 + nCtrl++;
32462 + }
32463 + }
32464 + if( nCtrl || xtype==etESCAPE_q ){
32465 + n += nBack + 5*nCtrl;
32466 + if( xtype==etESCAPE_Q ){
32467 + n += 10;
32468 + needQuote = 2;
32469 + }
32470 + }else{
32471 + flag_alternateform = 0;
32472 + }
32473 + }
32474 n += i + 3;
32475 if( n>etBUFSIZE ){
32476 bufpt = zExtra = printfTempBuf(pAccum, n);
@@ -32307,13 +32479,41 @@ SQLITE_API void sqlite3_str_vappendf(
32479 bufpt = buf;
32480 }
32481 j = 0;
32310 - if( needQuote ) bufpt[j++] = q;
32482 + if( needQuote ){
32483 + if( needQuote==2 ){
32484 + memcpy(&bufpt[j], "unistr('", 8);
32485 + j += 8;
32486 + }else{
32487 + bufpt[j++] = '\'';
32488 + }
32489 + }
32490 k = i;
32312 - for(i=0; i<k; i++){
32313 - bufpt[j++] = ch = escarg[i];
32314 - if( ch==q ) bufpt[j++] = ch;
32491 + if( flag_alternateform ){
32492 + for(i=0; i<k; i++){
32493 + bufpt[j++] = ch = escarg[i];
32494 + if( ch==q ){
32495 + bufpt[j++] = ch;
32496 + }else if( ch=='\\' ){
32497 + bufpt[j++] = '\\';
32498 + }else if( ((unsigned char)ch)<=0x1f ){
32499 + bufpt[j-1] = '\\';
32500 + bufpt[j++] = 'u';
32501 + bufpt[j++] = '0';
32502 + bufpt[j++] = '0';
32503 + bufpt[j++] = ch>=0x10 ? '1' : '0';
32504 + bufpt[j++] = "0123456789abcdef"[ch&0xf];
32505 + }
32506 + }
32507 + }else{
32508 + for(i=0; i<k; i++){
32509 + bufpt[j++] = ch = escarg[i];
32510 + if( ch==q ) bufpt[j++] = ch;
32511 + }
32512 + }
32513 + if( needQuote ){
32514 + bufpt[j++] = '\'';
32515 + if( needQuote==2 ) bufpt[j++] = ')';
32516 }
32316 - if( needQuote ) bufpt[j++] = q;
32517 bufpt[j] = 0;
32518 length = j;
32519 goto adjust_width_for_utf8;
@@ -32556,7 +32756,7 @@ SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
32756 static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
32757 char *zText;
32758 assert( p->mxAlloc>0 && !isMalloced(p) );
32559 - zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
32759 + zText = sqlite3DbMallocRaw(p->db, 1+(u64)p->nChar );
32760 if( zText ){
32761 memcpy(zText, p->zText, p->nChar+1);
32762 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
@@ -32801,6 +33001,15 @@ SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
33001 return zBuf;
33002 }
33003
33004 +/* Maximum size of an sqlite3_log() message. */
33005 +#if defined(SQLITE_MAX_LOG_MESSAGE)
33006 + /* Leave the definition as supplied */
33007 +#elif SQLITE_PRINT_BUF_SIZE*10>10000
33008 +# define SQLITE_MAX_LOG_MESSAGE 10000
33009 +#else
33010 +# define SQLITE_MAX_LOG_MESSAGE (SQLITE_PRINT_BUF_SIZE*10)
33011 +#endif
33012 +
33013 /*
33014 ** This is the routine that actually formats the sqlite3_log() message.
33015 ** We house it in a separate routine from sqlite3_log() to avoid using
@@ -32817,7 +33026,7 @@ SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
33026 */
33027 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
33028 StrAccum acc; /* String accumulator */
32820 - char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
33029 + char zMsg[SQLITE_MAX_LOG_MESSAGE]; /* Complete log message */
33030
33031 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
33032 sqlite3_str_vappendf(&acc, zFormat, ap);
@@ -34812,6 +35021,35 @@ static const unsigned char sqlite3Utf8Trans1[] = {
35021 } \
35022 }
35023
35024 +/*
35025 +** Write a single UTF8 character whose value is v into the
35026 +** buffer starting at zOut. zOut must be sized to hold at
35027 +** least four bytes. Return the number of bytes needed
35028 +** to encode the new character.
35029 +*/
35030 +SQLITE_PRIVATE int sqlite3AppendOneUtf8Character(char *zOut, u32 v){
35031 + if( v<0x00080 ){
35032 + zOut[0] = (u8)(v & 0xff);
35033 + return 1;
35034 + }
35035 + if( v<0x00800 ){
35036 + zOut[0] = 0xc0 + (u8)((v>>6) & 0x1f);
35037 + zOut[1] = 0x80 + (u8)(v & 0x3f);
35038 + return 2;
35039 + }
35040 + if( v<0x10000 ){
35041 + zOut[0] = 0xe0 + (u8)((v>>12) & 0x0f);
35042 + zOut[1] = 0x80 + (u8)((v>>6) & 0x3f);
35043 + zOut[2] = 0x80 + (u8)(v & 0x3f);
35044 + return 3;
35045 + }
35046 + zOut[0] = 0xf0 + (u8)((v>>18) & 0x07);
35047 + zOut[1] = 0x80 + (u8)((v>>12) & 0x3f);
35048 + zOut[2] = 0x80 + (u8)((v>>6) & 0x3f);
35049 + zOut[3] = 0x80 + (u8)(v & 0x3f);
35050 + return 4;
35051 +}
35052 +
35053 /*
35054 ** Translate a single UTF-8 character. Return the unicode value.
35055 **
@@ -35233,7 +35471,7 @@ SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nByte, int nChar){
35471 int n = 0;
35472
35473 if( SQLITE_UTF16NATIVE==SQLITE_UTF16LE ) z++;
35236 - while( n<nChar && ALWAYS(z<=zEnd) ){
35474 + while( n<nChar && z<=zEnd ){
35475 c = z[0];
35476 z += 2;
35477 if( c>=0xd8 && c<0xdc && z<=zEnd && z[0]>=0xdc && z[0]<0xe0 ) z += 2;
@@ -36408,7 +36646,11 @@ SQLITE_PRIVATE void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRou
36646 }
36647 p->z = &p->zBuf[i+1];
36648 assert( i+p->n < sizeof(p->zBuf) );
36411 - while( ALWAYS(p->n>0) && p->z[p->n-1]=='0' ){ p->n--; }
36649 + assert( p->n>0 );
36650 + while( p->z[p->n-1]=='0' ){
36651 + p->n--;
36652 + assert( p->n>0 );
36653 + }
36654 }
36655
36656 /*
@@ -36913,7 +37155,7 @@ SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
37155 }
37156
37157 /*
36916 -** Compute the absolute value of a 32-bit signed integer, of possible. Or
37158 +** Compute the absolute value of a 32-bit signed integer, if possible. Or
37159 ** if the integer has a value of -2147483648, return +2147483647
37160 */
37161 SQLITE_PRIVATE int sqlite3AbsInt32(int x){
@@ -37194,12 +37436,19 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
37436 */
37437 static unsigned int strHash(const char *z){
37438 unsigned int h = 0;
37197 - unsigned char c;
37198 - while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
37439 + while( z[0] ){ /*OPTIMIZATION-IF-TRUE*/
37440 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
37441 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
37201 - ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
37202 - h += sqlite3UpperToLower[c];
37442 + ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2.
37443 + **
37444 + ** Only bits 0xdf for ASCII and bits 0xbf for EBCDIC each octet are
37445 + ** hashed since the omitted bits determine the upper/lower case difference.
37446 + */
37447 +#ifdef SQLITE_EBCDIC
37448 + h += 0xbf & (unsigned char)*(z++);
37449 +#else
37450 + h += 0xdf & (unsigned char)*(z++);
37451 +#endif
37452 h *= 0x9e3779b1;
37453 }
37454 return h;
@@ -37272,9 +37521,8 @@ static int rehash(Hash *pH, unsigned int new_size){
37521 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
37522 memset(new_ht, 0, new_size*sizeof(struct _ht));
37523 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
37275 - unsigned int h = strHash(elem->pKey) % new_size;
37524 next_elem = elem->next;
37277 - insertElement(pH, &new_ht[h], elem);
37525 + insertElement(pH, &new_ht[elem->h % new_size], elem);
37526 }
37527 return 1;
37528 }
@@ -37292,23 +37540,22 @@ static HashElem *findElementWithHash(
37540 HashElem *elem; /* Used to loop thru the element list */
37541 unsigned int count; /* Number of elements left to test */
37542 unsigned int h; /* The computed hash */
37295 - static HashElem nullElement = { 0, 0, 0, 0 };
37543 + static HashElem nullElement = { 0, 0, 0, 0, 0 };
37544
37545 + h = strHash(pKey);
37546 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
37547 struct _ht *pEntry;
37299 - h = strHash(pKey) % pH->htsize;
37300 - pEntry = &pH->ht[h];
37548 + pEntry = &pH->ht[h % pH->htsize];
37549 elem = pEntry->chain;
37550 count = pEntry->count;
37551 }else{
37304 - h = 0;
37552 elem = pH->first;
37553 count = pH->count;
37554 }
37555 if( pHash ) *pHash = h;
37556 while( count ){
37557 assert( elem!=0 );
37311 - if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
37558 + if( h==elem->h && sqlite3StrICmp(elem->pKey,pKey)==0 ){
37559 return elem;
37560 }
37561 elem = elem->next;
@@ -37320,10 +37567,9 @@ static HashElem *findElementWithHash(
37567 /* Remove a single entry from the hash table given a pointer to that
37568 ** element and a hash on the element's key.
37569 */
37323 -static void removeElementGivenHash(
37570 +static void removeElement(
37571 Hash *pH, /* The pH containing "elem" */
37325 - HashElem* elem, /* The element to be removed from the pH */
37326 - unsigned int h /* Hash value for the element */
37572 + HashElem *elem /* The element to be removed from the pH */
37573 ){
37574 struct _ht *pEntry;
37575 if( elem->prev ){
@@ -37335,7 +37581,7 @@ static void removeElementGivenHash(
37581 elem->next->prev = elem->prev;
37582 }
37583 if( pH->ht ){
37338 - pEntry = &pH->ht[h];
37584 + pEntry = &pH->ht[elem->h % pH->htsize];
37585 if( pEntry->chain==elem ){
37586 pEntry->chain = elem->next;
37587 }
@@ -37386,7 +37632,7 @@ SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
37632 if( elem->data ){
37633 void *old_data = elem->data;
37634 if( data==0 ){
37389 - removeElementGivenHash(pH,elem,h);
37635 + removeElement(pH,elem);
37636 }else{
37637 elem->data = data;
37638 elem->pKey = pKey;
@@ -37397,15 +37643,13 @@ SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
37643 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
37644 if( new_elem==0 ) return data;
37645 new_elem->pKey = pKey;
37646 + new_elem->h = h;
37647 new_elem->data = data;
37648 pH->count++;
37402 - if( pH->count>=10 && pH->count > 2*pH->htsize ){
37403 - if( rehash(pH, pH->count*2) ){
37404 - assert( pH->htsize>0 );
37405 - h = strHash(pKey) % pH->htsize;
37406 - }
37649 + if( pH->count>=5 && pH->count > 2*pH->htsize ){
37650 + rehash(pH, pH->count*3);
37651 }
37408 - insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
37652 + insertElement(pH, pH->ht ? &pH->ht[new_elem->h % pH->htsize] : 0, new_elem);
37653 return 0;
37654 }
37655
@@ -38888,6 +39132,7 @@ struct unixFile {
39132 #endif
39133 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
39134 unsigned iBusyTimeout; /* Wait this many millisec on locks */
39135 + int bBlockOnConnect; /* True to block for SHARED locks */
39136 #endif
39137 #if OS_VXWORKS
39138 struct vxworksFileId *pId; /* Unique file ID */
@@ -40281,6 +40526,13 @@ static int unixFileLock(unixFile *pFile, struct flock *pLock){
40526 rc = 0;
40527 }
40528 }else{
40529 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
40530 + if( pFile->bBlockOnConnect && pLock->l_type==F_RDLCK
40531 + && pLock->l_start==SHARED_FIRST && pLock->l_len==SHARED_SIZE
40532 + ){
40533 + rc = osFcntl(pFile->h, F_SETLKW, pLock);
40534 + }else
40535 +#endif
40536 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
40537 }
40538 return rc;
@@ -42642,8 +42894,9 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){
42894 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42895 case SQLITE_FCNTL_LOCK_TIMEOUT: {
42896 int iOld = pFile->iBusyTimeout;
42897 + int iNew = *(int*)pArg;
42898 #if SQLITE_ENABLE_SETLK_TIMEOUT==1
42646 - pFile->iBusyTimeout = *(int*)pArg;
42899 + pFile->iBusyTimeout = iNew<0 ? 0x7FFFFFFF : (unsigned)iNew;
42900 #elif SQLITE_ENABLE_SETLK_TIMEOUT==2
42901 pFile->iBusyTimeout = !!(*(int*)pArg);
42902 #else
@@ -42652,7 +42905,12 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){
42905 *(int*)pArg = iOld;
42906 return SQLITE_OK;
42907 }
42655 -#endif
42908 + case SQLITE_FCNTL_BLOCK_ON_CONNECT: {
42909 + int iNew = *(int*)pArg;
42910 + pFile->bBlockOnConnect = iNew;
42911 + return SQLITE_OK;
42912 + }
42913 +#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
42914 #if SQLITE_MAX_MMAP_SIZE>0
42915 case SQLITE_FCNTL_MMAP_SIZE: {
42916 i64 newLimit = *(i64*)pArg;
@@ -43625,21 +43883,20 @@ static int unixShmLock(
43883 /* Check that, if this to be a blocking lock, no locks that occur later
43884 ** in the following list than the lock being obtained are already held:
43885 **
43628 - ** 1. Checkpointer lock (ofst==1).
43629 - ** 2. Write lock (ofst==0).
43630 - ** 3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
43886 + ** 1. Recovery lock (ofst==2).
43887 + ** 2. Checkpointer lock (ofst==1).
43888 + ** 3. Write lock (ofst==0).
43889 + ** 4. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
43890 **
43891 ** In other words, if this is a blocking lock, none of the locks that
43892 ** occur later in the above list than the lock being obtained may be
43893 ** held.
43635 - **
43636 - ** It is not permitted to block on the RECOVER lock.
43894 */
43638 -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43895 +#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && defined(SQLITE_DEBUG)
43896 {
43897 u16 lockMask = (p->exclMask|p->sharedMask);
43898 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
43642 - (ofst!=2) /* not RECOVER */
43899 + (ofst!=2 || lockMask==0)
43900 && (ofst!=1 || lockMask==0 || lockMask==2)
43901 && (ofst!=0 || lockMask<3)
43902 && (ofst<3 || lockMask<(1<<ofst))
@@ -45444,7 +45701,7 @@ static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
45701
45702 /* Almost all modern unix systems support nanosleep(). But if you are
45703 ** compiling for one of the rare exceptions, you can use
45447 - ** -DHAVE_NANOSLEEP=0 (perhaps in conjuction with -DHAVE_USLEEP if
45704 + ** -DHAVE_NANOSLEEP=0 (perhaps in conjunction with -DHAVE_USLEEP if
45705 ** usleep() is available) in order to bypass the use of nanosleep() */
45706 nanosleep(&sp, NULL);
45707
@@ -47165,8 +47422,18 @@ struct winFile {
47422 sqlite3_int64 mmapSize; /* Size of mapped region */
47423 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
47424 #endif
47425 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
47426 + DWORD iBusyTimeout; /* Wait this many millisec on locks */
47427 + int bBlockOnConnect;
47428 +#endif
47429 };
47430
47431 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
47432 +# define winFileBusyTimeout(pDbFd) pDbFd->iBusyTimeout
47433 +#else
47434 +# define winFileBusyTimeout(pDbFd) 0
47435 +#endif
47436 +
47437 /*
47438 ** The winVfsAppData structure is used for the pAppData member for all of the
47439 ** Win32 VFS variants.
@@ -47485,7 +47752,7 @@ static struct win_syscall {
47752 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
47753 #endif
47754
47488 -#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
47755 +#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(const FILETIME*, \
47756 LPFILETIME))aSyscall[11].pCurrent)
47757
47758 #if SQLITE_OS_WINCE
@@ -47494,7 +47761,7 @@ static struct win_syscall {
47761 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
47762 #endif
47763
47497 -#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
47764 +#define osFileTimeToSystemTime ((BOOL(WINAPI*)(const FILETIME*, \
47765 LPSYSTEMTIME))aSyscall[12].pCurrent)
47766
47767 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
@@ -47600,6 +47867,12 @@ static struct win_syscall {
47867 #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
47868 LPWSTR*))aSyscall[25].pCurrent)
47869
47870 +/*
47871 +** For GetLastError(), MSDN says:
47872 +**
47873 +** Minimum supported client: Windows XP [desktop apps | UWP apps]
47874 +** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
47875 +*/
47876 { "GetLastError", (SYSCALL)GetLastError, 0 },
47877
47878 #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
@@ -47768,7 +48041,7 @@ static struct win_syscall {
48041 { "LockFile", (SYSCALL)0, 0 },
48042 #endif
48043
47771 -#ifndef osLockFile
48044 +#if !defined(osLockFile) && defined(SQLITE_WIN32_HAS_ANSI)
48045 #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
48046 DWORD))aSyscall[47].pCurrent)
48047 #endif
@@ -47832,7 +48105,7 @@ static struct win_syscall {
48105
48106 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
48107
47835 -#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
48108 +#define osSystemTimeToFileTime ((BOOL(WINAPI*)(const SYSTEMTIME*, \
48109 LPFILETIME))aSyscall[56].pCurrent)
48110
48111 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
@@ -47841,7 +48114,7 @@ static struct win_syscall {
48114 { "UnlockFile", (SYSCALL)0, 0 },
48115 #endif
48116
47844 -#ifndef osUnlockFile
48117 +#if !defined(osUnlockFile) && defined(SQLITE_WIN32_HAS_ANSI)
48118 #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
48119 DWORD))aSyscall[57].pCurrent)
48120 #endif
@@ -47882,11 +48155,13 @@ static struct win_syscall {
48155 #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
48156 DWORD,DWORD))aSyscall[62].pCurrent)
48157
47885 -#if !SQLITE_OS_WINRT
48158 +/*
48159 +** For WaitForSingleObject(), MSDN says:
48160 +**
48161 +** Minimum supported client: Windows XP [desktop apps | UWP apps]
48162 +** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
48163 +*/
48164 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
47887 -#else
47888 - { "WaitForSingleObject", (SYSCALL)0, 0 },
47889 -#endif
48165
48166 #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
48167 DWORD))aSyscall[63].pCurrent)
@@ -48033,6 +48308,97 @@ static struct win_syscall {
48308 #define osFlushViewOfFile \
48309 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
48310
48311 +/*
48312 +** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CreateEvent()
48313 +** to implement blocking locks with timeouts. MSDN says:
48314 +**
48315 +** Minimum supported client: Windows XP [desktop apps | UWP apps]
48316 +** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
48317 +*/
48318 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
48319 + { "CreateEvent", (SYSCALL)CreateEvent, 0 },
48320 +#else
48321 + { "CreateEvent", (SYSCALL)0, 0 },
48322 +#endif
48323 +
48324 +#define osCreateEvent ( \
48325 + (HANDLE(WINAPI*) (LPSECURITY_ATTRIBUTES,BOOL,BOOL,LPCSTR)) \
48326 + aSyscall[80].pCurrent \
48327 +)
48328 +
48329 +/*
48330 +** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CancelIo()
48331 +** for the case where a timeout expires and a lock request must be
48332 +** cancelled.
48333 +**
48334 +** Minimum supported client: Windows XP [desktop apps | UWP apps]
48335 +** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
48336 +*/
48337 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
48338 + { "CancelIo", (SYSCALL)CancelIo, 0 },
48339 +#else
48340 + { "CancelIo", (SYSCALL)0, 0 },
48341 +#endif
48342 +
48343 +#define osCancelIo ((BOOL(WINAPI*)(HANDLE))aSyscall[81].pCurrent)
48344 +
48345 +#if defined(SQLITE_WIN32_HAS_WIDE) && defined(_WIN32)
48346 + { "GetModuleHandleW", (SYSCALL)GetModuleHandleW, 0 },
48347 +#else
48348 + { "GetModuleHandleW", (SYSCALL)0, 0 },
48349 +#endif
48350 +
48351 +#define osGetModuleHandleW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[82].pCurrent)
48352 +
48353 +#ifndef _WIN32
48354 + { "getenv", (SYSCALL)getenv, 0 },
48355 +#else
48356 + { "getenv", (SYSCALL)0, 0 },
48357 +#endif
48358 +
48359 +#define osGetenv ((const char *(*)(const char *))aSyscall[83].pCurrent)
48360 +
48361 +#ifndef _WIN32
48362 + { "getcwd", (SYSCALL)getcwd, 0 },
48363 +#else
48364 + { "getcwd", (SYSCALL)0, 0 },
48365 +#endif
48366 +
48367 +#define osGetcwd ((char*(*)(char*,size_t))aSyscall[84].pCurrent)
48368 +
48369 +#ifndef _WIN32
48370 + { "readlink", (SYSCALL)readlink, 0 },
48371 +#else
48372 + { "readlink", (SYSCALL)0, 0 },
48373 +#endif
48374 +
48375 +#define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[85].pCurrent)
48376 +
48377 +#ifndef _WIN32
48378 + { "lstat", (SYSCALL)lstat, 0 },
48379 +#else
48380 + { "lstat", (SYSCALL)0, 0 },
48381 +#endif
48382 +
48383 +#define osLstat ((int(*)(const char*,struct stat*))aSyscall[86].pCurrent)
48384 +
48385 +#ifndef _WIN32
48386 + { "__errno", (SYSCALL)__errno, 0 },
48387 +#else
48388 + { "__errno", (SYSCALL)0, 0 },
48389 +#endif
48390 +
48391 +#define osErrno (*((int*(*)(void))aSyscall[87].pCurrent)())
48392 +
48393 +#ifndef _WIN32
48394 + { "cygwin_conv_path", (SYSCALL)cygwin_conv_path, 0 },
48395 +#else
48396 + { "cygwin_conv_path", (SYSCALL)0, 0 },
48397 +#endif
48398 +
48399 +#define osCygwin_conv_path ((size_t(*)(unsigned int, \
48400 + const void *, void *, size_t))aSyscall[88].pCurrent)
48401 +
48402 }; /* End of the overrideable system calls */
48403
48404 /*
@@ -48206,6 +48572,7 @@ SQLITE_API int sqlite3_win32_reset_heap(){
48572 }
48573 #endif /* SQLITE_WIN32_MALLOC */
48574
48575 +#ifdef _WIN32
48576 /*
48577 ** This function outputs the specified (ANSI) string to the Win32 debugger
48578 ** (if available).
@@ -48248,6 +48615,7 @@ SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
48615 }
48616 #endif
48617 }
48618 +#endif /* _WIN32 */
48619
48620 /*
48621 ** The following routine suspends the current thread for at least ms
@@ -48331,7 +48699,9 @@ SQLITE_API int sqlite3_win32_is_nt(void){
48699 }
48700 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
48701 #elif SQLITE_TEST
48334 - return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
48702 + return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2
48703 + || osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0
48704 + ;
48705 #else
48706 /*
48707 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
@@ -48546,6 +48916,7 @@ SQLITE_PRIVATE void sqlite3MemSetDefault(void){
48916 }
48917 #endif /* SQLITE_WIN32_MALLOC */
48918
48919 +#ifdef _WIN32
48920 /*
48921 ** Convert a UTF-8 string to Microsoft Unicode.
48922 **
@@ -48571,6 +48942,7 @@ static LPWSTR winUtf8ToUnicode(const char *zText){
48942 }
48943 return zWideText;
48944 }
48945 +#endif /* _WIN32 */
48946
48947 /*
48948 ** Convert a Microsoft Unicode string to UTF-8.
@@ -48605,28 +48977,29 @@ static char *winUnicodeToUtf8(LPCWSTR zWideText){
48977 ** Space to hold the returned string is obtained from sqlite3_malloc().
48978 */
48979 static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
48608 - int nByte;
48980 + int nWideChar;
48981 LPWSTR zMbcsText;
48982 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
48983
48612 - nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
48613 - 0)*sizeof(WCHAR);
48614 - if( nByte==0 ){
48984 + nWideChar = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
48985 + 0);
48986 + if( nWideChar==0 ){
48987 return 0;
48988 }
48617 - zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
48989 + zMbcsText = sqlite3MallocZero( nWideChar*sizeof(WCHAR) );
48990 if( zMbcsText==0 ){
48991 return 0;
48992 }
48621 - nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
48622 - nByte);
48623 - if( nByte==0 ){
48993 + nWideChar = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
48994 + nWideChar);
48995 + if( nWideChar==0 ){
48996 sqlite3_free(zMbcsText);
48997 zMbcsText = 0;
48998 }
48999 return zMbcsText;
49000 }
49001
49002 +#ifdef _WIN32
49003 /*
49004 ** Convert a Microsoft Unicode string to a multi-byte character string,
49005 ** using the ANSI or OEM code page.
@@ -48654,6 +49027,7 @@ static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
49027 }
49028 return zText;
49029 }
49030 +#endif /* _WIN32 */
49031
49032 /*
49033 ** Convert a multi-byte character string to UTF-8.
@@ -48673,6 +49047,7 @@ static char *winMbcsToUtf8(const char *zText, int useAnsi){
49047 return zTextUtf8;
49048 }
49049
49050 +#ifdef _WIN32
49051 /*
49052 ** Convert a UTF-8 string to a multi-byte character string.
49053 **
@@ -48722,6 +49097,7 @@ SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
49097 #endif
49098 return winUnicodeToUtf8(zWideText);
49099 }
49100 +#endif /* _WIN32 */
49101
49102 /*
49103 ** This is a public wrapper for the winMbcsToUtf8() function.
@@ -48739,6 +49115,7 @@ SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
49115 return winMbcsToUtf8(zText, osAreFileApisANSI());
49116 }
49117
49118 +#ifdef _WIN32
49119 /*
49120 ** This is a public wrapper for the winMbcsToUtf8() function.
49121 */
@@ -48863,6 +49240,7 @@ SQLITE_API int sqlite3_win32_set_directory(
49240 ){
49241 return sqlite3_win32_set_directory16(type, zValue);
49242 }
49243 +#endif /* _WIN32 */
49244
49245 /*
49246 ** The return value of winGetLastErrorMsg
@@ -49411,13 +49789,98 @@ static BOOL winLockFile(
49789 ovlp.Offset = offsetLow;
49790 ovlp.OffsetHigh = offsetHigh;
49791 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
49792 +#ifdef SQLITE_WIN32_HAS_ANSI
49793 }else{
49794 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
49795 numBytesHigh);
49796 +#endif
49797 }
49798 #endif
49799 }
49800
49801 +/*
49802 +** Lock a region of nByte bytes starting at offset offset of file hFile.
49803 +** Take an EXCLUSIVE lock if parameter bExclusive is true, or a SHARED lock
49804 +** otherwise. If nMs is greater than zero and the lock cannot be obtained
49805 +** immediately, block for that many ms before giving up.
49806 +**
49807 +** This function returns SQLITE_OK if the lock is obtained successfully. If
49808 +** some other process holds the lock, SQLITE_BUSY is returned if nMs==0, or
49809 +** SQLITE_BUSY_TIMEOUT otherwise. Or, if an error occurs, SQLITE_IOERR.
49810 +*/
49811 +static int winHandleLockTimeout(
49812 + HANDLE hFile,
49813 + DWORD offset,
49814 + DWORD nByte,
49815 + int bExcl,
49816 + DWORD nMs
49817 +){
49818 + DWORD flags = LOCKFILE_FAIL_IMMEDIATELY | (bExcl?LOCKFILE_EXCLUSIVE_LOCK:0);
49819 + int rc = SQLITE_OK;
49820 + BOOL ret;
49821 +
49822 + if( !osIsNT() ){
49823 + ret = winLockFile(&hFile, flags, offset, 0, nByte, 0);
49824 + }else{
49825 + OVERLAPPED ovlp;
49826 + memset(&ovlp, 0, sizeof(OVERLAPPED));
49827 + ovlp.Offset = offset;
49828 +
49829 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
49830 + if( nMs!=0 ){
49831 + flags &= ~LOCKFILE_FAIL_IMMEDIATELY;
49832 + }
49833 + ovlp.hEvent = osCreateEvent(NULL, TRUE, FALSE, NULL);
49834 + if( ovlp.hEvent==NULL ){
49835 + return SQLITE_IOERR_LOCK;
49836 + }
49837 +#endif
49838 +
49839 + ret = osLockFileEx(hFile, flags, 0, nByte, 0, &ovlp);
49840 +
49841 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
49842 + /* If SQLITE_ENABLE_SETLK_TIMEOUT is defined, then the file-handle was
49843 + ** opened with FILE_FLAG_OVERHEAD specified. In this case, the call to
49844 + ** LockFileEx() may fail because the request is still pending. This can
49845 + ** happen even if LOCKFILE_FAIL_IMMEDIATELY was specified.
49846 + **
49847 + ** If nMs is 0, then LOCKFILE_FAIL_IMMEDIATELY was set in the flags
49848 + ** passed to LockFileEx(). In this case, if the operation is pending,
49849 + ** block indefinitely until it is finished.
49850 + **
49851 + ** Otherwise, wait for up to nMs ms for the operation to finish. nMs
49852 + ** may be set to INFINITE.
49853 + */
49854 + if( !ret && GetLastError()==ERROR_IO_PENDING ){
49855 + DWORD nDelay = (nMs==0 ? INFINITE : nMs);
49856 + DWORD res = osWaitForSingleObject(ovlp.hEvent, nDelay);
49857 + if( res==WAIT_OBJECT_0 ){
49858 + ret = TRUE;
49859 + }else if( res==WAIT_TIMEOUT ){
49860 +#if SQLITE_ENABLE_SETLK_TIMEOUT==1
49861 + rc = SQLITE_BUSY_TIMEOUT;
49862 +#else
49863 + rc = SQLITE_BUSY;
49864 +#endif
49865 + }else{
49866 + /* Some other error has occurred */
49867 + rc = SQLITE_IOERR_LOCK;
49868 + }
49869 +
49870 + /* If it is still pending, cancel the LockFileEx() call. */
49871 + osCancelIo(hFile);
49872 + }
49873 +
49874 + osCloseHandle(ovlp.hEvent);
49875 +#endif
49876 + }
49877 +
49878 + if( rc==SQLITE_OK && !ret ){
49879 + rc = SQLITE_BUSY;
49880 + }
49881 + return rc;
49882 +}
49883 +
49884 /*
49885 ** Unlock a file region.
49886 */
@@ -49442,13 +49905,23 @@ static BOOL winUnlockFile(
49905 ovlp.Offset = offsetLow;
49906 ovlp.OffsetHigh = offsetHigh;
49907 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
49908 +#ifdef SQLITE_WIN32_HAS_ANSI
49909 }else{
49910 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
49911 numBytesHigh);
49912 +#endif
49913 }
49914 #endif
49915 }
49916
49917 +/*
49918 +** Remove an nByte lock starting at offset iOff from HANDLE h.
49919 +*/
49920 +static int winHandleUnlock(HANDLE h, int iOff, int nByte){
49921 + BOOL ret = winUnlockFile(&h, iOff, 0, nByte, 0);
49922 + return (ret ? SQLITE_OK : SQLITE_IOERR_UNLOCK);
49923 +}
49924 +
49925 /*****************************************************************************
49926 ** The next group of routines implement the I/O methods specified
49927 ** by the sqlite3_io_methods object.
@@ -49462,66 +49935,70 @@ static BOOL winUnlockFile(
49935 #endif
49936
49937 /*
49465 -** Move the current position of the file handle passed as the first
49466 -** argument to offset iOffset within the file. If successful, return 0.
49467 -** Otherwise, set pFile->lastErrno and return non-zero.
49938 +** Seek the file handle h to offset nByte of the file.
49939 +**
49940 +** If successful, return SQLITE_OK. Or, if an error occurs, return an SQLite
49941 +** error code.
49942 */
49469 -static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
49943 +static int winHandleSeek(HANDLE h, sqlite3_int64 iOffset){
49944 + int rc = SQLITE_OK; /* Return value */
49945 +
49946 #if !SQLITE_OS_WINRT
49947 LONG upperBits; /* Most sig. 32 bits of new offset */
49948 LONG lowerBits; /* Least sig. 32 bits of new offset */
49949 DWORD dwRet; /* Value returned by SetFilePointer() */
49474 - DWORD lastErrno; /* Value returned by GetLastError() */
49475 -
49476 - OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
49950
49951 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
49952 lowerBits = (LONG)(iOffset & 0xffffffff);
49953
49954 + dwRet = osSetFilePointer(h, lowerBits, &upperBits, FILE_BEGIN);
49955 +
49956 /* API oddity: If successful, SetFilePointer() returns a dword
49957 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
49958 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
49959 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
49960 ** whether an error has actually occurred, it is also necessary to call
49486 - ** GetLastError().
49487 - */
49488 - dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
49489 -
49490 - if( (dwRet==INVALID_SET_FILE_POINTER
49491 - && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
49492 - pFile->lastErrno = lastErrno;
49493 - winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
49494 - "winSeekFile", pFile->zPath);
49495 - OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
49496 - return 1;
49961 + ** GetLastError(). */
49962 + if( dwRet==INVALID_SET_FILE_POINTER ){
49963 + DWORD lastErrno = osGetLastError();
49964 + if( lastErrno!=NO_ERROR ){
49965 + rc = SQLITE_IOERR_SEEK;
49966 + }
49967 }
49498 -
49499 - OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
49500 - return 0;
49968 #else
49502 - /*
49503 - ** Same as above, except that this implementation works for WinRT.
49504 - */
49505 -
49969 + /* This implementation works for WinRT. */
49970 LARGE_INTEGER x; /* The new offset */
49971 BOOL bRet; /* Value returned by SetFilePointerEx() */
49972
49973 x.QuadPart = iOffset;
49510 - bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
49974 + bRet = osSetFilePointerEx(h, x, 0, FILE_BEGIN);
49975
49976 if(!bRet){
49513 - pFile->lastErrno = osGetLastError();
49514 - winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
49515 - "winSeekFile", pFile->zPath);
49516 - OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
49517 - return 1;
49977 + rc = SQLITE_IOERR_SEEK;
49978 }
49519 -
49520 - OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
49521 - return 0;
49979 #endif
49980 +
49981 + OSTRACE(("SEEK file=%p, offset=%lld rc=%s\n", h, iOffset, sqlite3ErrName(rc)));
49982 + return rc;
49983 +}
49984 +
49985 +/*
49986 +** Move the current position of the file handle passed as the first
49987 +** argument to offset iOffset within the file. If successful, return 0.
49988 +** Otherwise, set pFile->lastErrno and return non-zero.
49989 +*/
49990 +static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
49991 + int rc;
49992 +
49993 + rc = winHandleSeek(pFile->h, iOffset);
49994 + if( rc!=SQLITE_OK ){
49995 + pFile->lastErrno = osGetLastError();
49996 + winLogError(rc, pFile->lastErrno, "winSeekFile", pFile->zPath);
49997 + }
49998 + return rc;
49999 }
50000
50001 +
50002 #if SQLITE_MAX_MMAP_SIZE>0
50003 /* Forward references to VFS helper methods used for memory mapped files */
50004 static int winMapfile(winFile*, sqlite3_int64);
@@ -49781,6 +50258,60 @@ static int winWrite(
50258 return SQLITE_OK;
50259 }
50260
50261 +/*
50262 +** Truncate the file opened by handle h to nByte bytes in size.
50263 +*/
50264 +static int winHandleTruncate(HANDLE h, sqlite3_int64 nByte){
50265 + int rc = SQLITE_OK; /* Return code */
50266 + rc = winHandleSeek(h, nByte);
50267 + if( rc==SQLITE_OK ){
50268 + if( 0==osSetEndOfFile(h) ){
50269 + rc = SQLITE_IOERR_TRUNCATE;
50270 + }
50271 + }
50272 + return rc;
50273 +}
50274 +
50275 +/*
50276 +** Determine the size in bytes of the file opened by the handle passed as
50277 +** the first argument.
50278 +*/
50279 +static int winHandleSize(HANDLE h, sqlite3_int64 *pnByte){
50280 + int rc = SQLITE_OK;
50281 +
50282 +#if SQLITE_OS_WINRT
50283 + FILE_STANDARD_INFO info;
50284 + BOOL b;
50285 + b = osGetFileInformationByHandleEx(h, FileStandardInfo, &info, sizeof(info));
50286 + if( b ){
50287 + *pnByte = info.EndOfFile.QuadPart;
50288 + }else{
50289 + rc = SQLITE_IOERR_FSTAT;
50290 + }
50291 +#else
50292 + DWORD upperBits = 0;
50293 + DWORD lowerBits = 0;
50294 +
50295 + assert( pnByte );
50296 + lowerBits = osGetFileSize(h, &upperBits);
50297 + *pnByte = (((sqlite3_int64)upperBits)<<32) + lowerBits;
50298 + if( lowerBits==INVALID_FILE_SIZE && osGetLastError()!=NO_ERROR ){
50299 + rc = SQLITE_IOERR_FSTAT;
50300 + }
50301 +#endif
50302 +
50303 + return rc;
50304 +}
50305 +
50306 +/*
50307 +** Close the handle passed as the only argument.
50308 +*/
50309 +static void winHandleClose(HANDLE h){
50310 + if( h!=INVALID_HANDLE_VALUE ){
50311 + osCloseHandle(h);
50312 + }
50313 +}
50314 +
50315 /*
50316 ** Truncate an open file to a specified size
50317 */
@@ -50036,8 +50567,9 @@ static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
50567 ** Different API routines are called depending on whether or not this
50568 ** is Win9x or WinNT.
50569 */
50039 -static int winGetReadLock(winFile *pFile){
50570 +static int winGetReadLock(winFile *pFile, int bBlock){
50571 int res;
50572 + DWORD mask = ~(bBlock ? LOCKFILE_FAIL_IMMEDIATELY : 0);
50573 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
50574 if( osIsNT() ){
50575 #if SQLITE_OS_WINCE
@@ -50047,7 +50579,7 @@ static int winGetReadLock(winFile *pFile){
50579 */
50580 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
50581 #else
50050 - res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
50582 + res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS&mask, SHARED_FIRST, 0,
50583 SHARED_SIZE, 0);
50584 #endif
50585 }
@@ -50056,7 +50588,7 @@ static int winGetReadLock(winFile *pFile){
50588 int lk;
50589 sqlite3_randomness(sizeof(lk), &lk);
50590 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
50059 - res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
50591 + res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS&mask,
50592 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
50593 }
50594 #endif
@@ -50151,46 +50683,62 @@ static int winLock(sqlite3_file *id, int locktype){
50683 assert( locktype!=PENDING_LOCK );
50684 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
50685
50154 - /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
50686 + /* Lock the PENDING_LOCK byte if we need to acquire an EXCLUSIVE lock or
50687 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
50688 ** the PENDING_LOCK byte is temporary.
50689 */
50690 newLocktype = pFile->locktype;
50159 - if( pFile->locktype==NO_LOCK
50160 - || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
50691 + if( locktype==SHARED_LOCK
50692 + || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
50693 ){
50694 int cnt = 3;
50163 - while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
50164 - PENDING_BYTE, 0, 1, 0))==0 ){
50695 +
50696 + /* Flags for the LockFileEx() call. This should be an exclusive lock if
50697 + ** this call is to obtain EXCLUSIVE, or a shared lock if this call is to
50698 + ** obtain SHARED. */
50699 + int flags = LOCKFILE_FAIL_IMMEDIATELY;
50700 + if( locktype==EXCLUSIVE_LOCK ){
50701 + flags |= LOCKFILE_EXCLUSIVE_LOCK;
50702 + }
50703 + while( cnt>0 ){
50704 /* Try 3 times to get the pending lock. This is needed to work
50705 ** around problems caused by indexing and/or anti-virus software on
50706 ** Windows systems.
50707 + **
50708 ** If you are using this code as a model for alternative VFSes, do not
50169 - ** copy this retry logic. It is a hack intended for Windows only.
50170 - */
50709 + ** copy this retry logic. It is a hack intended for Windows only. */
50710 + res = winLockFile(&pFile->h, flags, PENDING_BYTE, 0, 1, 0);
50711 + if( res ) break;
50712 +
50713 lastErrno = osGetLastError();
50714 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
50173 - pFile->h, cnt, res));
50715 + pFile->h, cnt, res
50716 + ));
50717 +
50718 if( lastErrno==ERROR_INVALID_HANDLE ){
50719 pFile->lastErrno = lastErrno;
50720 rc = SQLITE_IOERR_LOCK;
50721 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
50178 - pFile->h, cnt, sqlite3ErrName(rc)));
50722 + pFile->h, cnt, sqlite3ErrName(rc)
50723 + ));
50724 return rc;
50725 }
50181 - if( cnt ) sqlite3_win32_sleep(1);
50726 +
50727 + cnt--;
50728 + if( cnt>0 ) sqlite3_win32_sleep(1);
50729 }
50730 gotPendingLock = res;
50184 - if( !res ){
50185 - lastErrno = osGetLastError();
50186 - }
50731 }
50732
50733 /* Acquire a shared lock
50734 */
50735 if( locktype==SHARED_LOCK && res ){
50736 assert( pFile->locktype==NO_LOCK );
50193 - res = winGetReadLock(pFile);
50737 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
50738 + res = winGetReadLock(pFile, pFile->bBlockOnConnect);
50739 +#else
50740 + res = winGetReadLock(pFile, 0);
50741 +#endif
50742 if( res ){
50743 newLocktype = SHARED_LOCK;
50744 }else{
@@ -50228,7 +50776,7 @@ static int winLock(sqlite3_file *id, int locktype){
50776 newLocktype = EXCLUSIVE_LOCK;
50777 }else{
50778 lastErrno = osGetLastError();
50231 - winGetReadLock(pFile);
50779 + winGetReadLock(pFile, 0);
50780 }
50781 }
50782
@@ -50308,7 +50856,7 @@ static int winUnlock(sqlite3_file *id, int locktype){
50856 type = pFile->locktype;
50857 if( type>=EXCLUSIVE_LOCK ){
50858 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
50311 - if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
50859 + if( locktype==SHARED_LOCK && !winGetReadLock(pFile, 0) ){
50860 /* This should never happen. We should always be able to
50861 ** reacquire the read lock */
50862 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
@@ -50518,6 +51066,28 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){
51066 return rc;
51067 }
51068 #endif
51069 +
51070 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
51071 + case SQLITE_FCNTL_LOCK_TIMEOUT: {
51072 + int iOld = pFile->iBusyTimeout;
51073 + int iNew = *(int*)pArg;
51074 +#if SQLITE_ENABLE_SETLK_TIMEOUT==1
51075 + pFile->iBusyTimeout = (iNew < 0) ? INFINITE : (DWORD)iNew;
51076 +#elif SQLITE_ENABLE_SETLK_TIMEOUT==2
51077 + pFile->iBusyTimeout = (DWORD)(!!iNew);
51078 +#else
51079 +# error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2"
51080 +#endif
51081 + *(int*)pArg = iOld;
51082 + return SQLITE_OK;
51083 + }
51084 + case SQLITE_FCNTL_BLOCK_ON_CONNECT: {
51085 + int iNew = *(int*)pArg;
51086 + pFile->bBlockOnConnect = iNew;
51087 + return SQLITE_OK;
51088 + }
51089 +#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
51090 +
51091 }
51092 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
51093 return SQLITE_NOTFOUND;
@@ -50598,23 +51168,27 @@ static int winShmMutexHeld(void) {
51168 **
51169 ** The following fields are read-only after the object is created:
51170 **
50601 -** fid
51171 ** zFilename
51172 **
51173 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
51174 ** winShmMutexHeld() is true when reading or writing any other field
51175 ** in this structure.
51176 **
51177 +** File-handle hSharedShm is used to (a) take the DMS lock, (b) truncate
51178 +** the *-shm file if the DMS-locking protocol demands it, and (c) map
51179 +** regions of the *-shm file into memory using MapViewOfFile() or
51180 +** similar. Other locks are taken by individual clients using the
51181 +** winShm.hShm handles.
51182 */
51183 struct winShmNode {
51184 sqlite3_mutex *mutex; /* Mutex to access this object */
51185 char *zFilename; /* Name of the file */
50612 - winFile hFile; /* File handle from winOpen */
51186 + HANDLE hSharedShm; /* File handle open on zFilename */
51187
51188 + int isUnlocked; /* DMS lock has not yet been obtained */
51189 + int isReadonly; /* True if read-only */
51190 int szRegion; /* Size of shared-memory regions */
51191 int nRegion; /* Size of array apRegion */
50616 - u8 isReadonly; /* True if read-only */
50617 - u8 isUnlocked; /* True if no DMS lock held */
51192
51193 struct ShmRegion {
51194 HANDLE hMap; /* File handle from CreateFileMapping */
@@ -50623,7 +51197,6 @@ struct winShmNode {
51197 DWORD lastErrno; /* The Windows errno from the last I/O error */
51198
51199 int nRef; /* Number of winShm objects pointing to this */
50626 - winShm *pFirst; /* All winShm objects pointing to this */
51200 winShmNode *pNext; /* Next in list of all winShmNode objects */
51201 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
51202 u8 nextShmId; /* Next available winShm.id value */
@@ -50639,23 +51212,15 @@ static winShmNode *winShmNodeList = 0;
51212
51213 /*
51214 ** Structure used internally by this VFS to record the state of an
50642 -** open shared memory connection.
50643 -**
50644 -** The following fields are initialized when this object is created and
50645 -** are read-only thereafter:
50646 -**
50647 -** winShm.pShmNode
50648 -** winShm.id
50649 -**
50650 -** All other fields are read/write. The winShm.pShmNode->mutex must be held
50651 -** while accessing any read/write fields.
51215 +** open shared memory connection. There is one such structure for each
51216 +** winFile open on a wal mode database.
51217 */
51218 struct winShm {
51219 winShmNode *pShmNode; /* The underlying winShmNode object */
50655 - winShm *pNext; /* Next winShm with the same winShmNode */
50656 - u8 hasMutex; /* True if holding the winShmNode mutex */
51220 u16 sharedMask; /* Mask of shared locks held */
51221 u16 exclMask; /* Mask of exclusive locks held */
51222 + HANDLE hShm; /* File-handle on *-shm file. For locking. */
51223 + int bReadonly; /* True if hShm is opened read-only */
51224 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
51225 u8 id; /* Id of this connection with its winShmNode */
51226 #endif
@@ -50667,50 +51232,6 @@ struct winShm {
51232 #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
51233 #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
51234
50670 -/*
50671 -** Apply advisory locks for all n bytes beginning at ofst.
50672 -*/
50673 -#define WINSHM_UNLCK 1
50674 -#define WINSHM_RDLCK 2
50675 -#define WINSHM_WRLCK 3
50676 -static int winShmSystemLock(
50677 - winShmNode *pFile, /* Apply locks to this open shared-memory segment */
50678 - int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
50679 - int ofst, /* Offset to first byte to be locked/unlocked */
50680 - int nByte /* Number of bytes to lock or unlock */
50681 -){
50682 - int rc = 0; /* Result code form Lock/UnlockFileEx() */
50683 -
50684 - /* Access to the winShmNode object is serialized by the caller */
50685 - assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
50686 -
50687 - OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
50688 - pFile->hFile.h, lockType, ofst, nByte));
50689 -
50690 - /* Release/Acquire the system-level lock */
50691 - if( lockType==WINSHM_UNLCK ){
50692 - rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
50693 - }else{
50694 - /* Initialize the locking parameters */
50695 - DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
50696 - if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
50697 - rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
50698 - }
50699 -
50700 - if( rc!= 0 ){
50701 - rc = SQLITE_OK;
50702 - }else{
50703 - pFile->lastErrno = osGetLastError();
50704 - rc = SQLITE_BUSY;
50705 - }
50706 -
50707 - OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
50708 - pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
50709 - "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
50710 -
50711 - return rc;
50712 -}
50713 -
51235 /* Forward references to VFS methods */
51236 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
51237 static int winDelete(sqlite3_vfs *,const char*,int);
@@ -50742,11 +51263,7 @@ static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
51263 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
51264 UNUSED_VARIABLE_VALUE(bRc);
51265 }
50745 - if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
50746 - SimulateIOErrorBenign(1);
50747 - winClose((sqlite3_file *)&p->hFile);
50748 - SimulateIOErrorBenign(0);
50749 - }
51266 + winHandleClose(p->hSharedShm);
51267 if( deleteFlag ){
51268 SimulateIOErrorBenign(1);
51269 sqlite3BeginBenignMalloc();
@@ -50764,42 +51281,239 @@ static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
51281 }
51282
51283 /*
50767 -** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
50768 -** take it now. Return SQLITE_OK if successful, or an SQLite error
50769 -** code otherwise.
50770 -**
50771 -** If the DMS cannot be locked because this is a readonly_shm=1
50772 -** connection and no other process already holds a lock, return
50773 -** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
51284 +** The DMS lock has not yet been taken on the shm file associated with
51285 +** pShmNode. Take the lock. Truncate the *-shm file if required.
51286 +** Return SQLITE_OK if successful, or an SQLite error code otherwise.
51287 */
50775 -static int winLockSharedMemory(winShmNode *pShmNode){
50776 - int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
51288 +static int winLockSharedMemory(winShmNode *pShmNode, DWORD nMs){
51289 + HANDLE h = pShmNode->hSharedShm;
51290 + int rc = SQLITE_OK;
51291
51292 + assert( sqlite3_mutex_held(pShmNode->mutex) );
51293 + rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 1, 0);
51294 if( rc==SQLITE_OK ){
51295 + /* We have an EXCLUSIVE lock on the DMS byte. This means that this
51296 + ** is the first process to open the file. Truncate it to zero bytes
51297 + ** in this case. */
51298 if( pShmNode->isReadonly ){
50780 - pShmNode->isUnlocked = 1;
50781 - winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50782 - return SQLITE_READONLY_CANTINIT;
50783 - }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
50784 - winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50785 - return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
50786 - "winLockSharedMemory", pShmNode->zFilename);
51299 + rc = SQLITE_READONLY_CANTINIT;
51300 + }else{
51301 + rc = winHandleTruncate(h, 0);
51302 }
51303 +
51304 + /* Release the EXCLUSIVE lock acquired above. */
51305 + winUnlockFile(&h, WIN_SHM_DMS, 0, 1, 0);
51306 + }else if( (rc & 0xFF)==SQLITE_BUSY ){
51307 + rc = SQLITE_OK;
51308 }
51309
51310 if( rc==SQLITE_OK ){
50791 - winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
51311 + /* Take a SHARED lock on the DMS byte. */
51312 + rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 0, nMs);
51313 + if( rc==SQLITE_OK ){
51314 + pShmNode->isUnlocked = 0;
51315 + }
51316 }
51317
50794 - return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
51318 + return rc;
51319 }
51320
51321 +
51322 /*
50798 -** Open the shared-memory area associated with database file pDbFd.
51323 +** Convert a UTF-8 filename into whatever form the underlying
51324 +** operating system wants filenames in. Space to hold the result
51325 +** is obtained from malloc and must be freed by the calling
51326 +** function
51327 +**
51328 +** On Cygwin, 3 possible input forms are accepted:
51329 +** - If the filename starts with "<drive>:/" or "<drive>:\",
51330 +** it is converted to UTF-16 as-is.
51331 +** - If the filename contains '/', it is assumed to be a
51332 +** Cygwin absolute path, it is converted to a win32
51333 +** absolute path in UTF-16.
51334 +** - Otherwise it must be a filename only, the win32 filename
51335 +** is returned in UTF-16.
51336 +** Note: If the function cygwin_conv_path() fails, only
51337 +** UTF-8 -> UTF-16 conversion will be done. This can only
51338 +** happen when the file path >32k, in which case winUtf8ToUnicode()
51339 +** will fail too.
51340 +*/
51341 +static void *winConvertFromUtf8Filename(const char *zFilename){
51342 + void *zConverted = 0;
51343 + if( osIsNT() ){
51344 +#ifdef __CYGWIN__
51345 + int nChar;
51346 + LPWSTR zWideFilename;
51347 +
51348 + if( osCygwin_conv_path && !(winIsDriveLetterAndColon(zFilename)
51349 + && winIsDirSep(zFilename[2])) ){
51350 + i64 nByte;
51351 + int convertflag = CCP_POSIX_TO_WIN_W;
51352 + if( !strchr(zFilename, '/') ) convertflag |= CCP_RELATIVE;
51353 + nByte = (i64)osCygwin_conv_path(convertflag,
51354 + zFilename, 0, 0);
51355 + if( nByte>0 ){
51356 + zConverted = sqlite3MallocZero(12+(u64)nByte);
51357 + if ( zConverted==0 ){
51358 + return zConverted;
51359 + }
51360 + zWideFilename = zConverted;
51361 + /* Filenames should be prefixed, except when converted
51362 + * full path already starts with "\\?\". */
51363 + if( osCygwin_conv_path(convertflag, zFilename,
51364 + zWideFilename+4, nByte)==0 ){
51365 + if( (convertflag&CCP_RELATIVE) ){
51366 + memmove(zWideFilename, zWideFilename+4, nByte);
51367 + }else if( memcmp(zWideFilename+4, L"\\\\", 4) ){
51368 + memcpy(zWideFilename, L"\\\\?\\", 8);
51369 + }else if( zWideFilename[6]!='?' ){
51370 + memmove(zWideFilename+6, zWideFilename+4, nByte);
51371 + memcpy(zWideFilename, L"\\\\?\\UNC", 14);
51372 + }else{
51373 + memmove(zWideFilename, zWideFilename+4, nByte);
51374 + }
51375 + return zConverted;
51376 + }
51377 + sqlite3_free(zConverted);
51378 + }
51379 + }
51380 + nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
51381 + if( nChar==0 ){
51382 + return 0;
51383 + }
51384 + zWideFilename = sqlite3MallocZero( nChar*sizeof(WCHAR)+12 );
51385 + if( zWideFilename==0 ){
51386 + return 0;
51387 + }
51388 + nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1,
51389 + zWideFilename, nChar);
51390 + if( nChar==0 ){
51391 + sqlite3_free(zWideFilename);
51392 + zWideFilename = 0;
51393 + }else if( nChar>MAX_PATH
51394 + && winIsDriveLetterAndColon(zFilename)
51395 + && winIsDirSep(zFilename[2]) ){
51396 + memmove(zWideFilename+4, zWideFilename, nChar*sizeof(WCHAR));
51397 + zWideFilename[2] = '\\';
51398 + memcpy(zWideFilename, L"\\\\?\\", 8);
51399 + }else if( nChar>MAX_PATH
51400 + && winIsDirSep(zFilename[0]) && winIsDirSep(zFilename[1])
51401 + && zFilename[2] != '?' ){
51402 + memmove(zWideFilename+6, zWideFilename, nChar*sizeof(WCHAR));
51403 + memcpy(zWideFilename, L"\\\\?\\UNC", 14);
51404 + }
51405 + zConverted = zWideFilename;
51406 +#else
51407 + zConverted = winUtf8ToUnicode(zFilename);
51408 +#endif /* __CYGWIN__ */
51409 + }
51410 +#if defined(SQLITE_WIN32_HAS_ANSI) && defined(_WIN32)
51411 + else{
51412 + zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
51413 + }
51414 +#endif
51415 + /* caller will handle out of memory */
51416 + return zConverted;
51417 +}
51418 +
51419 +/*
51420 +** This function is used to open a handle on a *-shm file.
51421 **
50800 -** When opening a new shared-memory file, if no other instances of that
50801 -** file are currently open, in this process or in other processes, then
50802 -** the file must be truncated to zero length or have its header cleared.
51422 +** If SQLITE_ENABLE_SETLK_TIMEOUT is defined at build time, then the file
51423 +** is opened with FILE_FLAG_OVERLAPPED specified. If not, it is not.
51424 +*/
51425 +static int winHandleOpen(
51426 + const char *zUtf8, /* File to open */
51427 + int *pbReadonly, /* IN/OUT: True for readonly handle */
51428 + HANDLE *ph /* OUT: New HANDLE for file */
51429 +){
51430 + int rc = SQLITE_OK;
51431 + void *zConverted = 0;
51432 + int bReadonly = *pbReadonly;
51433 + HANDLE h = INVALID_HANDLE_VALUE;
51434 +
51435 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
51436 + const DWORD flag_overlapped = FILE_FLAG_OVERLAPPED;
51437 +#else
51438 + const DWORD flag_overlapped = 0;
51439 +#endif
51440 +
51441 + /* Convert the filename to the system encoding. */
51442 + zConverted = winConvertFromUtf8Filename(zUtf8);
51443 + if( zConverted==0 ){
51444 + OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8));
51445 + rc = SQLITE_IOERR_NOMEM_BKPT;
51446 + goto winopenfile_out;
51447 + }
51448 +
51449 + /* Ensure the file we are trying to open is not actually a directory. */
51450 + if( winIsDir(zConverted) ){
51451 + OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8));
51452 + rc = SQLITE_CANTOPEN_ISDIR;
51453 + goto winopenfile_out;
51454 + }
51455 +
51456 + /* TODO: platforms.
51457 + ** TODO: retry-on-ioerr.
51458 + */
51459 + if( osIsNT() ){
51460 +#if SQLITE_OS_WINRT
51461 + CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
51462 + memset(&extendedParameters, 0, sizeof(extendedParameters));
51463 + extendedParameters.dwSize = sizeof(extendedParameters);
51464 + extendedParameters.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
51465 + extendedParameters.dwFileFlags = flag_overlapped;
51466 + extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
51467 + h = osCreateFile2((LPCWSTR)zConverted,
51468 + (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)),/* dwDesiredAccess */
51469 + FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */
51470 + OPEN_ALWAYS, /* dwCreationDisposition */
51471 + &extendedParameters
51472 + );
51473 +#else
51474 + h = osCreateFileW((LPCWSTR)zConverted, /* lpFileName */
51475 + (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)), /* dwDesiredAccess */
51476 + FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */
51477 + NULL, /* lpSecurityAttributes */
51478 + OPEN_ALWAYS, /* dwCreationDisposition */
51479 + FILE_ATTRIBUTE_NORMAL|flag_overlapped,
51480 + NULL
51481 + );
51482 +#endif
51483 + }else{
51484 + /* Due to pre-processor directives earlier in this file,
51485 + ** SQLITE_WIN32_HAS_ANSI is always defined if osIsNT() is false. */
51486 +#ifdef SQLITE_WIN32_HAS_ANSI
51487 + h = osCreateFileA((LPCSTR)zConverted,
51488 + (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)), /* dwDesiredAccess */
51489 + FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */
51490 + NULL, /* lpSecurityAttributes */
51491 + OPEN_ALWAYS, /* dwCreationDisposition */
51492 + FILE_ATTRIBUTE_NORMAL|flag_overlapped,
51493 + NULL
51494 + );
51495 +#endif
51496 + }
51497 +
51498 + if( h==INVALID_HANDLE_VALUE ){
51499 + if( bReadonly==0 ){
51500 + bReadonly = 1;
51501 + rc = winHandleOpen(zUtf8, &bReadonly, &h);
51502 + }else{
51503 + rc = SQLITE_CANTOPEN_BKPT;
51504 + }
51505 + }
51506 +
51507 + winopenfile_out:
51508 + sqlite3_free(zConverted);
51509 + *pbReadonly = bReadonly;
51510 + *ph = h;
51511 + return rc;
51512 +}
51513 +
51514 +
51515 +/*
51516 +** Open the shared-memory area associated with database file pDbFd.
51517 */
51518 static int winOpenSharedMemory(winFile *pDbFd){
51519 struct winShm *p; /* The connection to be opened */
@@ -50811,98 +51525,83 @@ static int winOpenSharedMemory(winFile *pDbFd){
51525 assert( pDbFd->pShm==0 ); /* Not previously opened */
51526
51527 /* Allocate space for the new sqlite3_shm object. Also speculatively
50814 - ** allocate space for a new winShmNode and filename.
50815 - */
51528 + ** allocate space for a new winShmNode and filename. */
51529 p = sqlite3MallocZero( sizeof(*p) );
51530 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
51531 nName = sqlite3Strlen30(pDbFd->zPath);
50819 - pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
51532 + pNew = sqlite3MallocZero( sizeof(*pShmNode) + (i64)nName + 17 );
51533 if( pNew==0 ){
51534 sqlite3_free(p);
51535 return SQLITE_IOERR_NOMEM_BKPT;
51536 }
51537 pNew->zFilename = (char*)&pNew[1];
51538 + pNew->hSharedShm = INVALID_HANDLE_VALUE;
51539 + pNew->isUnlocked = 1;
51540 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
51541 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
51542
51543 + /* Open a file-handle on the *-shm file for this connection. This file-handle
51544 + ** is only used for locking. The mapping of the *-shm file is created using
51545 + ** the shared file handle in winShmNode.hSharedShm. */
51546 + p->bReadonly = sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0);
51547 + rc = winHandleOpen(pNew->zFilename, &p->bReadonly, &p->hShm);
51548 +
51549 /* Look to see if there is an existing winShmNode that can be used.
50829 - ** If no matching winShmNode currently exists, create a new one.
50830 - */
51550 + ** If no matching winShmNode currently exists, then create a new one. */
51551 winShmEnterMutex();
51552 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
51553 /* TBD need to come up with better match here. Perhaps
50834 - ** use FILE_ID_BOTH_DIR_INFO Structure.
50835 - */
51554 + ** use FILE_ID_BOTH_DIR_INFO Structure. */
51555 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
51556 }
50838 - if( pShmNode ){
50839 - sqlite3_free(pNew);
50840 - }else{
50841 - int inFlags = SQLITE_OPEN_WAL;
50842 - int outFlags = 0;
50843 -
51557 + if( pShmNode==0 ){
51558 pShmNode = pNew;
50845 - pNew = 0;
50846 - ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
50847 - pShmNode->pNext = winShmNodeList;
50848 - winShmNodeList = pShmNode;
51559
51560 + /* Allocate a mutex for this winShmNode object, if one is required. */
51561 if( sqlite3GlobalConfig.bCoreMutex ){
51562 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
50852 - if( pShmNode->mutex==0 ){
50853 - rc = SQLITE_IOERR_NOMEM_BKPT;
50854 - goto shm_open_err;
50855 - }
51563 + if( pShmNode->mutex==0 ) rc = SQLITE_IOERR_NOMEM_BKPT;
51564 }
51565
50858 - if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
50859 - inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
50860 - }else{
50861 - inFlags |= SQLITE_OPEN_READONLY;
50862 - }
50863 - rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
50864 - (sqlite3_file*)&pShmNode->hFile,
50865 - inFlags, &outFlags);
50866 - if( rc!=SQLITE_OK ){
50867 - rc = winLogError(rc, osGetLastError(), "winOpenShm",
50868 - pShmNode->zFilename);
50869 - goto shm_open_err;
51566 + /* Open a file-handle to use for mappings, and for the DMS lock. */
51567 + if( rc==SQLITE_OK ){
51568 + HANDLE h = INVALID_HANDLE_VALUE;
51569 + pShmNode->isReadonly = p->bReadonly;
51570 + rc = winHandleOpen(pNew->zFilename, &pShmNode->isReadonly, &h);
51571 + pShmNode->hSharedShm = h;
51572 }
50871 - if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
51573
50873 - rc = winLockSharedMemory(pShmNode);
50874 - if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
51574 + /* If successful, link the new winShmNode into the global list. If an
51575 + ** error occurred, free the object. */
51576 + if( rc==SQLITE_OK ){
51577 + pShmNode->pNext = winShmNodeList;
51578 + winShmNodeList = pShmNode;
51579 + pNew = 0;
51580 + }else{
51581 + sqlite3_mutex_free(pShmNode->mutex);
51582 + if( pShmNode->hSharedShm!=INVALID_HANDLE_VALUE ){
51583 + osCloseHandle(pShmNode->hSharedShm);
51584 + }
51585 + }
51586 }
51587
50877 - /* Make the new connection a child of the winShmNode */
50878 - p->pShmNode = pShmNode;
51588 + /* If no error has occurred, link the winShm object to the winShmNode and
51589 + ** the winShm to pDbFd. */
51590 + if( rc==SQLITE_OK ){
51591 + p->pShmNode = pShmNode;
51592 + pShmNode->nRef++;
51593 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
50880 - p->id = pShmNode->nextShmId++;
51594 + p->id = pShmNode->nextShmId++;
51595 #endif
50882 - pShmNode->nRef++;
50883 - pDbFd->pShm = p;
50884 - winShmLeaveMutex();
50885 -
50886 - /* The reference count on pShmNode has already been incremented under
50887 - ** the cover of the winShmEnterMutex() mutex and the pointer from the
50888 - ** new (struct winShm) object to the pShmNode has been set. All that is
50889 - ** left to do is to link the new object into the linked list starting
50890 - ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
50891 - ** mutex.
50892 - */
50893 - sqlite3_mutex_enter(pShmNode->mutex);
50894 - p->pNext = pShmNode->pFirst;
50895 - pShmNode->pFirst = p;
50896 - sqlite3_mutex_leave(pShmNode->mutex);
50897 - return rc;
51596 + pDbFd->pShm = p;
51597 + }else if( p ){
51598 + winHandleClose(p->hShm);
51599 + sqlite3_free(p);
51600 + }
51601
50899 - /* Jump here on any error */
50900 -shm_open_err:
50901 - winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50902 - winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
50903 - sqlite3_free(p);
50904 - sqlite3_free(pNew);
51602 + assert( rc!=SQLITE_OK || pShmNode->isUnlocked==0 || pShmNode->nRegion==0 );
51603 winShmLeaveMutex();
51604 + sqlite3_free(pNew);
51605 return rc;
51606 }
51607
@@ -50917,27 +51616,19 @@ static int winShmUnmap(
51616 winFile *pDbFd; /* Database holding shared-memory */
51617 winShm *p; /* The connection to be closed */
51618 winShmNode *pShmNode; /* The underlying shared-memory file */
50920 - winShm **pp; /* For looping over sibling connections */
51619
51620 pDbFd = (winFile*)fd;
51621 p = pDbFd->pShm;
51622 if( p==0 ) return SQLITE_OK;
50925 - pShmNode = p->pShmNode;
50926 -
50927 - /* Remove connection p from the set of connections associated
50928 - ** with pShmNode */
50929 - sqlite3_mutex_enter(pShmNode->mutex);
50930 - for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
50931 - *pp = p->pNext;
51623 + if( p->hShm!=INVALID_HANDLE_VALUE ){
51624 + osCloseHandle(p->hShm);
51625 + }
51626
50933 - /* Free the connection p */
50934 - sqlite3_free(p);
50935 - pDbFd->pShm = 0;
50936 - sqlite3_mutex_leave(pShmNode->mutex);
51627 + pShmNode = p->pShmNode;
51628 + winShmEnterMutex();
51629
51630 /* If pShmNode->nRef has reached 0, then close the underlying
50939 - ** shared-memory file, too */
50940 - winShmEnterMutex();
51631 + ** shared-memory file, too. */
51632 assert( pShmNode->nRef>0 );
51633 pShmNode->nRef--;
51634 if( pShmNode->nRef==0 ){
@@ -50945,6 +51636,9 @@ static int winShmUnmap(
51636 }
51637 winShmLeaveMutex();
51638
51639 + /* Free the connection p */
51640 + sqlite3_free(p);
51641 + pDbFd->pShm = 0;
51642 return SQLITE_OK;
51643 }
51644
@@ -50959,10 +51653,9 @@ static int winShmLock(
51653 ){
51654 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
51655 winShm *p = pDbFd->pShm; /* The shared memory being locked */
50962 - winShm *pX; /* For looping over all siblings */
51656 winShmNode *pShmNode;
51657 int rc = SQLITE_OK; /* Result code */
50965 - u16 mask; /* Mask of locks to take or release */
51658 + u16 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst)); /* Mask of locks to [un]take */
51659
51660 if( p==0 ) return SQLITE_IOERR_SHMLOCK;
51661 pShmNode = p->pShmNode;
@@ -50976,85 +51669,81 @@ static int winShmLock(
51669 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
51670 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
51671
50979 - mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
50980 - assert( n>1 || mask==(1<<ofst) );
50981 - sqlite3_mutex_enter(pShmNode->mutex);
50982 - if( flags & SQLITE_SHM_UNLOCK ){
50983 - u16 allMask = 0; /* Mask of locks held by siblings */
51672 + /* Check that, if this to be a blocking lock, no locks that occur later
51673 + ** in the following list than the lock being obtained are already held:
51674 + **
51675 + ** 1. Recovery lock (ofst==2).
51676 + ** 2. Checkpointer lock (ofst==1).
51677 + ** 3. Write lock (ofst==0).
51678 + ** 4. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
51679 + **
51680 + ** In other words, if this is a blocking lock, none of the locks that
51681 + ** occur later in the above list than the lock being obtained may be
51682 + ** held.
51683 + */
51684 +#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && defined(SQLITE_DEBUG)
51685 + {
51686 + u16 lockMask = (p->exclMask|p->sharedMask);
51687 + assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
51688 + (ofst!=2 || lockMask==0)
51689 + && (ofst!=1 || lockMask==0 || lockMask==2)
51690 + && (ofst!=0 || lockMask<3)
51691 + && (ofst<3 || lockMask<(1<<ofst))
51692 + ));
51693 + }
51694 +#endif
51695
50985 - /* See if any siblings hold this same lock */
50986 - for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50987 - if( pX==p ) continue;
50988 - assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
50989 - allMask |= pX->sharedMask;
50990 - }
51696 + /* Check if there is any work to do. There are three cases:
51697 + **
51698 + ** a) An unlock operation where there are locks to unlock,
51699 + ** b) An shared lock where the requested lock is not already held
51700 + ** c) An exclusive lock where the requested lock is not already held
51701 + **
51702 + ** The SQLite core never requests an exclusive lock that it already holds.
51703 + ** This is assert()ed immediately below. */
51704 + assert( flags!=(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK)
51705 + || 0==(p->exclMask & mask)
51706 + );
51707 + if( ((flags & SQLITE_SHM_UNLOCK) && ((p->exclMask|p->sharedMask) & mask))
51708 + || (flags==(SQLITE_SHM_SHARED|SQLITE_SHM_LOCK) && 0==(p->sharedMask & mask))
51709 + || (flags==(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK))
51710 + ){
51711
50992 - /* Unlock the system-level locks */
50993 - if( (mask & allMask)==0 ){
50994 - rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
50995 - }else{
50996 - rc = SQLITE_OK;
50997 - }
51712 + if( flags & SQLITE_SHM_UNLOCK ){
51713 + /* Case (a) - unlock. */
51714
50999 - /* Undo the local locks */
51000 - if( rc==SQLITE_OK ){
51001 - p->exclMask &= ~mask;
51002 - p->sharedMask &= ~mask;
51003 - }
51004 - }else if( flags & SQLITE_SHM_SHARED ){
51005 - u16 allShared = 0; /* Union of locks held by connections other than "p" */
51715 + assert( (p->exclMask & p->sharedMask)==0 );
51716 + assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask );
51717 + assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask );
51718
51007 - /* Find out which shared locks are already held by sibling connections.
51008 - ** If any sibling already holds an exclusive lock, go ahead and return
51009 - ** SQLITE_BUSY.
51010 - */
51011 - for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
51012 - if( (pX->exclMask & mask)!=0 ){
51013 - rc = SQLITE_BUSY;
51014 - break;
51015 - }
51016 - allShared |= pX->sharedMask;
51017 - }
51719 + rc = winHandleUnlock(p->hShm, ofst+WIN_SHM_BASE, n);
51720
51019 - /* Get shared locks at the system level, if necessary */
51020 - if( rc==SQLITE_OK ){
51021 - if( (allShared & mask)==0 ){
51022 - rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
51023 - }else{
51024 - rc = SQLITE_OK;
51025 - }
51026 - }
51027 -
51028 - /* Get the local shared locks */
51029 - if( rc==SQLITE_OK ){
51030 - p->sharedMask |= mask;
51031 - }
51032 - }else{
51033 - /* Make sure no sibling connections hold locks that will block this
51034 - ** lock. If any do, return SQLITE_BUSY right away.
51035 - */
51036 - for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
51037 - if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
51038 - rc = SQLITE_BUSY;
51039 - break;
51721 + /* If successful, also clear the bits in sharedMask/exclMask */
51722 + if( rc==SQLITE_OK ){
51723 + p->exclMask = (p->exclMask & ~mask);
51724 + p->sharedMask = (p->sharedMask & ~mask);
51725 }
51041 - }
51042 -
51043 - /* Get the exclusive locks at the system level. Then if successful
51044 - ** also mark the local connection as being locked.
51045 - */
51046 - if( rc==SQLITE_OK ){
51047 - rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
51726 + }else{
51727 + int bExcl = ((flags & SQLITE_SHM_EXCLUSIVE) ? 1 : 0);
51728 + DWORD nMs = winFileBusyTimeout(pDbFd);
51729 + rc = winHandleLockTimeout(p->hShm, ofst+WIN_SHM_BASE, n, bExcl, nMs);
51730 if( rc==SQLITE_OK ){
51049 - assert( (p->sharedMask & mask)==0 );
51050 - p->exclMask |= mask;
51731 + if( bExcl ){
51732 + p->exclMask = (p->exclMask | mask);
51733 + }else{
51734 + p->sharedMask = (p->sharedMask | mask);
51735 + }
51736 }
51737 }
51738 }
51054 - sqlite3_mutex_leave(pShmNode->mutex);
51055 - OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
51056 - osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
51057 - sqlite3ErrName(rc)));
51739 +
51740 + OSTRACE((
51741 + "SHM-LOCK(%d,%d,%d) pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x,"
51742 + " rc=%s\n",
51743 + ofst, n, flags,
51744 + osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
51745 + sqlite3ErrName(rc))
51746 + );
51747 return rc;
51748 }
51749
@@ -51116,13 +51805,15 @@ static int winShmMap(
51805
51806 sqlite3_mutex_enter(pShmNode->mutex);
51807 if( pShmNode->isUnlocked ){
51119 - rc = winLockSharedMemory(pShmNode);
51808 + /* Take the DMS lock. */
51809 + assert( pShmNode->nRegion==0 );
51810 + rc = winLockSharedMemory(pShmNode, winFileBusyTimeout(pDbFd));
51811 if( rc!=SQLITE_OK ) goto shmpage_out;
51121 - pShmNode->isUnlocked = 0;
51812 }
51123 - assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
51813
51814 + assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
51815 if( pShmNode->nRegion<=iRegion ){
51816 + HANDLE hShared = pShmNode->hSharedShm;
51817 struct ShmRegion *apNew; /* New aRegion[] array */
51818 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
51819 sqlite3_int64 sz; /* Current size of wal-index file */
@@ -51133,10 +51824,9 @@ static int winShmMap(
51824 ** Check to see if it has been allocated (i.e. if the wal-index file is
51825 ** large enough to contain the requested region).
51826 */
51136 - rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
51827 + rc = winHandleSize(hShared, &sz);
51828 if( rc!=SQLITE_OK ){
51138 - rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
51139 - "winShmMap1", pDbFd->zPath);
51829 + rc = winLogError(rc, osGetLastError(), "winShmMap1", pDbFd->zPath);
51830 goto shmpage_out;
51831 }
51832
@@ -51145,19 +51835,17 @@ static int winShmMap(
51835 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
51836 **
51837 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
51148 - ** the requested memory region.
51149 - */
51838 + ** the requested memory region. */
51839 if( !isWrite ) goto shmpage_out;
51151 - rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
51840 + rc = winHandleTruncate(hShared, nByte);
51841 if( rc!=SQLITE_OK ){
51153 - rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
51154 - "winShmMap2", pDbFd->zPath);
51842 + rc = winLogError(rc, osGetLastError(), "winShmMap2", pDbFd->zPath);
51843 goto shmpage_out;
51844 }
51845 }
51846
51847 /* Map the requested memory region into this processes address space. */
51160 - apNew = (struct ShmRegion *)sqlite3_realloc64(
51848 + apNew = (struct ShmRegion*)sqlite3_realloc64(
51849 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
51850 );
51851 if( !apNew ){
@@ -51176,18 +51864,13 @@ static int winShmMap(
51864 void *pMap = 0; /* Mapped memory region */
51865
51866 #if SQLITE_OS_WINRT
51179 - hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
51180 - NULL, protect, nByte, NULL
51181 - );
51867 + hMap = osCreateFileMappingFromApp(hShared, NULL, protect, nByte, NULL);
51868 #elif defined(SQLITE_WIN32_HAS_WIDE)
51183 - hMap = osCreateFileMappingW(pShmNode->hFile.h,
51184 - NULL, protect, 0, nByte, NULL
51185 - );
51869 + hMap = osCreateFileMappingW(hShared, NULL, protect, 0, nByte, NULL);
51870 #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
51187 - hMap = osCreateFileMappingA(pShmNode->hFile.h,
51188 - NULL, protect, 0, nByte, NULL
51189 - );
51871 + hMap = osCreateFileMappingA(hShared, NULL, protect, 0, nByte, NULL);
51872 #endif
51873 +
51874 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
51875 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
51876 hMap ? "ok" : "failed"));
@@ -51230,7 +51913,9 @@ shmpage_out:
51913 }else{
51914 *pp = 0;
51915 }
51233 - if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
51916 + if( pShmNode->isReadonly && rc==SQLITE_OK ){
51917 + rc = SQLITE_READONLY;
51918 + }
51919 sqlite3_mutex_leave(pShmNode->mutex);
51920 return rc;
51921 }
@@ -51550,47 +52235,6 @@ static winVfsAppData winNolockAppData = {
52235 ** sqlite3_vfs object.
52236 */
52237
51553 -#if defined(__CYGWIN__)
51554 -/*
51555 -** Convert a filename from whatever the underlying operating system
51556 -** supports for filenames into UTF-8. Space to hold the result is
51557 -** obtained from malloc and must be freed by the calling function.
51558 -*/
51559 -static char *winConvertToUtf8Filename(const void *zFilename){
51560 - char *zConverted = 0;
51561 - if( osIsNT() ){
51562 - zConverted = winUnicodeToUtf8(zFilename);
51563 - }
51564 -#ifdef SQLITE_WIN32_HAS_ANSI
51565 - else{
51566 - zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
51567 - }
51568 -#endif
51569 - /* caller will handle out of memory */
51570 - return zConverted;
51571 -}
51572 -#endif
51573 -
51574 -/*
51575 -** Convert a UTF-8 filename into whatever form the underlying
51576 -** operating system wants filenames in. Space to hold the result
51577 -** is obtained from malloc and must be freed by the calling
51578 -** function.
51579 -*/
51580 -static void *winConvertFromUtf8Filename(const char *zFilename){
51581 - void *zConverted = 0;
51582 - if( osIsNT() ){
51583 - zConverted = winUtf8ToUnicode(zFilename);
51584 - }
51585 -#ifdef SQLITE_WIN32_HAS_ANSI
51586 - else{
51587 - zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
51588 - }
51589 -#endif
51590 - /* caller will handle out of memory */
51591 - return zConverted;
51592 -}
51593 -
52238 /*
52239 ** This function returns non-zero if the specified UTF-8 string buffer
52240 ** ends with a directory separator character or one was successfully
@@ -51603,7 +52247,14 @@ static int winMakeEndInDirSep(int nBuf, char *zBuf){
52247 if( winIsDirSep(zBuf[nLen-1]) ){
52248 return 1;
52249 }else if( nLen+1<nBuf ){
51606 - zBuf[nLen] = winGetDirSep();
52250 + if( !osGetenv ){
52251 + zBuf[nLen] = winGetDirSep();
52252 + }else if( winIsDriveLetterAndColon(zBuf) && winIsDirSep(zBuf[2]) ){
52253 + zBuf[nLen] = '\\';
52254 + zBuf[2]='\\';
52255 + }else{
52256 + zBuf[nLen] = '/';
52257 + }
52258 zBuf[nLen+1] = '\0';
52259 return 1;
52260 }
@@ -51630,14 +52281,14 @@ static int winTempDirDefined(void){
52281 ** The pointer returned in pzBuf must be freed via sqlite3_free().
52282 */
52283 static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
51633 - static char zChars[] =
52284 + static const char zChars[] =
52285 "abcdefghijklmnopqrstuvwxyz"
52286 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
52287 "0123456789";
52288 size_t i, j;
52289 DWORD pid;
52290 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
51640 - int nMax, nBuf, nDir, nLen;
52291 + i64 nMax, nBuf, nDir, nLen;
52292 char *zBuf;
52293
52294 /* It's odd to simulate an io-error here, but really this is just
@@ -51649,7 +52300,8 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
52300 /* Allocate a temporary buffer to store the fully qualified file
52301 ** name for the temporary file. If this fails, we cannot continue.
52302 */
51652 - nMax = pVfs->mxPathname; nBuf = nMax + 2;
52303 + nMax = pVfs->mxPathname;
52304 + nBuf = 2 + (i64)nMax;
52305 zBuf = sqlite3MallocZero( nBuf );
52306 if( !zBuf ){
52307 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
@@ -51680,7 +52332,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
52332 }
52333
52334 #if defined(__CYGWIN__)
51683 - else{
52335 + else if( osGetenv!=NULL ){
52336 static const char *azDirs[] = {
52337 0, /* getenv("SQLITE_TMPDIR") */
52338 0, /* getenv("TMPDIR") */
@@ -51696,11 +52348,11 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
52348 unsigned int i;
52349 const char *zDir = 0;
52350
51699 - if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
51700 - if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
51701 - if( !azDirs[2] ) azDirs[2] = getenv("TMP");
51702 - if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
51703 - if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
52351 + if( !azDirs[0] ) azDirs[0] = osGetenv("SQLITE_TMPDIR");
52352 + if( !azDirs[1] ) azDirs[1] = osGetenv("TMPDIR");
52353 + if( !azDirs[2] ) azDirs[2] = osGetenv("TMP");
52354 + if( !azDirs[3] ) azDirs[3] = osGetenv("TEMP");
52355 + if( !azDirs[4] ) azDirs[4] = osGetenv("USERPROFILE");
52356 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
52357 void *zConverted;
52358 if( zDir==0 ) continue;
@@ -51709,7 +52361,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
52361 ** it must be converted to a native Win32 path via the Cygwin API
52362 ** prior to using it.
52363 */
51712 - if( winIsDriveLetterAndColon(zDir) ){
52364 + {
52365 zConverted = winConvertFromUtf8Filename(zDir);
52366 if( !zConverted ){
52367 sqlite3_free(zBuf);
@@ -51722,44 +52374,12 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
52374 break;
52375 }
52376 sqlite3_free(zConverted);
51725 - }else{
51726 - zConverted = sqlite3MallocZero( nMax+1 );
51727 - if( !zConverted ){
51728 - sqlite3_free(zBuf);
51729 - OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51730 - return SQLITE_IOERR_NOMEM_BKPT;
51731 - }
51732 - if( cygwin_conv_path(
51733 - osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
51734 - zConverted, nMax+1)<0 ){
51735 - sqlite3_free(zConverted);
51736 - sqlite3_free(zBuf);
51737 - OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
51738 - return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
51739 - "winGetTempname2", zDir);
51740 - }
51741 - if( winIsDir(zConverted) ){
51742 - /* At this point, we know the candidate directory exists and should
51743 - ** be used. However, we may need to convert the string containing
51744 - ** its name into UTF-8 (i.e. if it is UTF-16 right now).
51745 - */
51746 - char *zUtf8 = winConvertToUtf8Filename(zConverted);
51747 - if( !zUtf8 ){
51748 - sqlite3_free(zConverted);
51749 - sqlite3_free(zBuf);
51750 - OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51751 - return SQLITE_IOERR_NOMEM_BKPT;
51752 - }
51753 - sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
51754 - sqlite3_free(zUtf8);
51755 - sqlite3_free(zConverted);
51756 - break;
51757 - }
51758 - sqlite3_free(zConverted);
52377 }
52378 }
52379 }
51762 -#elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
52380 +#endif
52381 +
52382 +#if !SQLITE_OS_WINRT && defined(_WIN32)
52383 else if( osIsNT() ){
52384 char *zMulti;
52385 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
@@ -51883,7 +52503,7 @@ static int winIsDir(const void *zConverted){
52503 return 0; /* Invalid name? */
52504 }
52505 attr = sAttrData.dwFileAttributes;
51886 -#if SQLITE_OS_WINCE==0
52506 +#if SQLITE_OS_WINCE==0 && defined(SQLITE_WIN32_HAS_ANSI)
52507 }else{
52508 attr = osGetFileAttributesA((char*)zConverted);
52509 #endif
@@ -51899,6 +52519,12 @@ static int winAccess(
52519 int *pResOut /* OUT: Result */
52520 );
52521
52522 +/*
52523 +** The Windows version of xAccess() accepts an extra bit in the flags
52524 +** parameter that prevents an anti-virus retry loop.
52525 +*/
52526 +#define NORETRY 0x4000
52527 +
52528 /*
52529 ** Open a file.
52530 */
@@ -51923,6 +52549,7 @@ static int winOpen(
52549 void *zConverted; /* Filename in OS encoding */
52550 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
52551 int cnt = 0;
52552 + int isRO = 0; /* file is known to be accessible readonly */
52553
52554 /* If argument zPath is a NULL pointer, this function is required to open
52555 ** a temporary file. Use this buffer to store the file name in.
@@ -52087,9 +52714,9 @@ static int winOpen(
52714 &extendedParameters);
52715 if( h!=INVALID_HANDLE_VALUE ) break;
52716 if( isReadWrite ){
52090 - int rc2, isRO = 0;
52717 + int rc2;
52718 sqlite3BeginBenignMalloc();
52092 - rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ, &isRO);
52719 + rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ|NORETRY, &isRO);
52720 sqlite3EndBenignMalloc();
52721 if( rc2==SQLITE_OK && isRO ) break;
52722 }
@@ -52104,9 +52731,9 @@ static int winOpen(
52731 NULL);
52732 if( h!=INVALID_HANDLE_VALUE ) break;
52733 if( isReadWrite ){
52107 - int rc2, isRO = 0;
52734 + int rc2;
52735 sqlite3BeginBenignMalloc();
52109 - rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ, &isRO);
52736 + rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ|NORETRY, &isRO);
52737 sqlite3EndBenignMalloc();
52738 if( rc2==SQLITE_OK && isRO ) break;
52739 }
@@ -52124,9 +52751,9 @@ static int winOpen(
52751 NULL);
52752 if( h!=INVALID_HANDLE_VALUE ) break;
52753 if( isReadWrite ){
52127 - int rc2, isRO = 0;
52754 + int rc2;
52755 sqlite3BeginBenignMalloc();
52129 - rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ, &isRO);
52756 + rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ|NORETRY, &isRO);
52757 sqlite3EndBenignMalloc();
52758 if( rc2==SQLITE_OK && isRO ) break;
52759 }
@@ -52141,7 +52768,7 @@ static int winOpen(
52768 if( h==INVALID_HANDLE_VALUE ){
52769 sqlite3_free(zConverted);
52770 sqlite3_free(zTmpname);
52144 - if( isReadWrite && !isExclusive ){
52771 + if( isReadWrite && isRO && !isExclusive ){
52772 return winOpen(pVfs, zName, id,
52773 ((flags|SQLITE_OPEN_READONLY) &
52774 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
@@ -52343,8 +52970,14 @@ static int winAccess(
52970 int rc = 0;
52971 DWORD lastErrno = 0;
52972 void *zConverted;
52973 + int noRetry = 0; /* Do not use winRetryIoerr() */
52974 UNUSED_PARAMETER(pVfs);
52975
52976 + if( (flags & NORETRY)!=0 ){
52977 + noRetry = 1;
52978 + flags &= ~NORETRY;
52979 + }
52980 +
52981 SimulateIOError( return SQLITE_IOERR_ACCESS; );
52982 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
52983 zFilename, flags, pResOut));
@@ -52367,7 +53000,10 @@ static int winAccess(
53000 memset(&sAttrData, 0, sizeof(sAttrData));
53001 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
53002 GetFileExInfoStandard,
52370 - &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
53003 + &sAttrData))
53004 + && !noRetry
53005 + && winRetryIoerr(&cnt, &lastErrno)
53006 + ){ /* Loop until true */}
53007 if( rc ){
53008 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
53009 ** as if it does not exist.
@@ -52435,6 +53071,7 @@ static BOOL winIsDriveLetterAndColon(
53071 return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
53072 }
53073
53074 +#ifdef _WIN32
53075 /*
53076 ** Returns non-zero if the specified path name should be used verbatim. If
53077 ** non-zero is returned from this function, the calling function must simply
@@ -52471,6 +53108,70 @@ static BOOL winIsVerbatimPathname(
53108 */
53109 return FALSE;
53110 }
53111 +#endif /* _WIN32 */
53112 +
53113 +#ifdef __CYGWIN__
53114 +/*
53115 +** Simplify a filename into its canonical form
53116 +** by making the following changes:
53117 +**
53118 +** * convert any '/' to '\' (win32) or reverse (Cygwin)
53119 +** * removing any trailing and duplicate / (except for UNC paths)
53120 +** * convert /./ into just /
53121 +**
53122 +** Changes are made in-place. Return the new name length.
53123 +**
53124 +** The original filename is in z[0..]. If the path is shortened,
53125 +** no-longer used bytes will be written by '\0'.
53126 +*/
53127 +static void winSimplifyName(char *z){
53128 + int i, j;
53129 + for(i=j=0; z[i]; ++i){
53130 + if( winIsDirSep(z[i]) ){
53131 +#if !defined(SQLITE_TEST)
53132 + /* Some test-cases assume that "./foo" and "foo" are different */
53133 + if( z[i+1]=='.' && winIsDirSep(z[i+2]) ){
53134 + ++i;
53135 + continue;
53136 + }
53137 +#endif
53138 + if( !z[i+1] || (winIsDirSep(z[i+1]) && (i!=0)) ){
53139 + continue;
53140 + }
53141 + z[j++] = osGetenv?'/':'\\';
53142 + }else{
53143 + z[j++] = z[i];
53144 + }
53145 + }
53146 + while(j<i) z[j++] = '\0';
53147 +}
53148 +
53149 +#define SQLITE_MAX_SYMLINKS 100
53150 +
53151 +static int mkFullPathname(
53152 + const char *zPath, /* Input path */
53153 + char *zOut, /* Output buffer */
53154 + int nOut /* Allocated size of buffer zOut */
53155 +){
53156 + int nPath = sqlite3Strlen30(zPath);
53157 + int iOff = 0;
53158 + if( zPath[0]!='/' ){
53159 + if( osGetcwd(zOut, nOut-2)==0 ){
53160 + return winLogError(SQLITE_CANTOPEN_BKPT, (DWORD)osErrno, "getcwd", zPath);
53161 + }
53162 + iOff = sqlite3Strlen30(zOut);
53163 + zOut[iOff++] = '/';
53164 + }
53165 + if( (iOff+nPath+1)>nOut ){
53166 + /* SQLite assumes that xFullPathname() nul-terminates the output buffer
53167 + ** even if it returns an error. */
53168 + zOut[iOff] = '\0';
53169 + return SQLITE_CANTOPEN_BKPT;
53170 + }
53171 + sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
53172 + return SQLITE_OK;
53173 +}
53174 +#endif /* __CYGWIN__ */
53175
53176 /*
53177 ** Turn a relative pathname into a full pathname. Write the full
@@ -52483,8 +53184,8 @@ static int winFullPathnameNoMutex(
53184 int nFull, /* Size of output buffer in bytes */
53185 char *zFull /* Output buffer */
53186 ){
52486 -#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
52487 - DWORD nByte;
53187 +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
53188 + int nByte;
53189 void *zConverted;
53190 char *zOut;
53191 #endif
@@ -52497,64 +53198,82 @@ static int winFullPathnameNoMutex(
53198 zRelative++;
53199 }
53200
52500 -#if defined(__CYGWIN__)
53201 SimulateIOError( return SQLITE_ERROR );
52502 - UNUSED_PARAMETER(nFull);
52503 - assert( nFull>=pVfs->mxPathname );
52504 - if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
52505 - /*
52506 - ** NOTE: We are dealing with a relative path name and the data
52507 - ** directory has been set. Therefore, use it as the basis
52508 - ** for converting the relative path name to an absolute
52509 - ** one by prepending the data directory and a slash.
52510 - */
52511 - char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
52512 - if( !zOut ){
52513 - return SQLITE_IOERR_NOMEM_BKPT;
52514 - }
52515 - if( cygwin_conv_path(
52516 - (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
52517 - CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
52518 - sqlite3_free(zOut);
52519 - return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
52520 - "winFullPathname1", zRelative);
52521 - }else{
52522 - char *zUtf8 = winConvertToUtf8Filename(zOut);
52523 - if( !zUtf8 ){
52524 - sqlite3_free(zOut);
52525 - return SQLITE_IOERR_NOMEM_BKPT;
52526 - }
52527 - sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
52528 - sqlite3_data_directory, winGetDirSep(), zUtf8);
52529 - sqlite3_free(zUtf8);
52530 - sqlite3_free(zOut);
52531 - }
52532 - }else{
52533 - char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
52534 - if( !zOut ){
52535 - return SQLITE_IOERR_NOMEM_BKPT;
52536 - }
52537 - if( cygwin_conv_path(
52538 - (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
52539 - zRelative, zOut, pVfs->mxPathname+1)<0 ){
52540 - sqlite3_free(zOut);
52541 - return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
52542 - "winFullPathname2", zRelative);
52543 - }else{
52544 - char *zUtf8 = winConvertToUtf8Filename(zOut);
52545 - if( !zUtf8 ){
52546 - sqlite3_free(zOut);
52547 - return SQLITE_IOERR_NOMEM_BKPT;
52548 - }
52549 - sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
52550 - sqlite3_free(zUtf8);
52551 - sqlite3_free(zOut);
53202 +
53203 +#ifdef __CYGWIN__
53204 + if( osGetcwd ){
53205 + zFull[nFull-1] = '\0';
53206 + if( !winIsDriveLetterAndColon(zRelative) || !winIsDirSep(zRelative[2]) ){
53207 + int rc = SQLITE_OK;
53208 + int nLink = 1; /* Number of symbolic links followed so far */
53209 + const char *zIn = zRelative; /* Input path for each iteration of loop */
53210 + char *zDel = 0;
53211 + struct stat buf;
53212 +
53213 + UNUSED_PARAMETER(pVfs);
53214 +
53215 + do {
53216 + /* Call lstat() on path zIn. Set bLink to true if the path is a symbolic
53217 + ** link, or false otherwise. */
53218 + int bLink = 0;
53219 + if( osLstat && osReadlink ) {
53220 + if( osLstat(zIn, &buf)!=0 ){
53221 + int myErrno = osErrno;
53222 + if( myErrno!=ENOENT ){
53223 + rc = winLogError(SQLITE_CANTOPEN_BKPT, (DWORD)myErrno, "lstat", zIn);
53224 + }
53225 + }else{
53226 + bLink = ((buf.st_mode & 0170000) == 0120000);
53227 + }
53228 +
53229 + if( bLink ){
53230 + if( zDel==0 ){
53231 + zDel = sqlite3MallocZero(nFull);
53232 + if( zDel==0 ) rc = SQLITE_NOMEM;
53233 + }else if( ++nLink>SQLITE_MAX_SYMLINKS ){
53234 + rc = SQLITE_CANTOPEN_BKPT;
53235 + }
53236 +
53237 + if( rc==SQLITE_OK ){
53238 + nByte = osReadlink(zIn, zDel, nFull-1);
53239 + if( nByte ==(DWORD)-1 ){
53240 + rc = winLogError(SQLITE_CANTOPEN_BKPT, (DWORD)osErrno, "readlink", zIn);
53241 + }else{
53242 + if( zDel[0]!='/' ){
53243 + int n;
53244 + for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
53245 + if( nByte+n+1>nFull ){
53246 + rc = SQLITE_CANTOPEN_BKPT;
53247 + }else{
53248 + memmove(&zDel[n], zDel, nByte+1);
53249 + memcpy(zDel, zIn, n);
53250 + nByte += n;
53251 + }
53252 + }
53253 + zDel[nByte] = '\0';
53254 + }
53255 + }
53256 +
53257 + zIn = zDel;
53258 + }
53259 + }
53260 +
53261 + assert( rc!=SQLITE_OK || zIn!=zFull || zIn[0]=='/' );
53262 + if( rc==SQLITE_OK && zIn!=zFull ){
53263 + rc = mkFullPathname(zIn, zFull, nFull);
53264 + }
53265 + if( bLink==0 ) break;
53266 + zIn = zFull;
53267 + }while( rc==SQLITE_OK );
53268 +
53269 + sqlite3_free(zDel);
53270 + winSimplifyName(zFull);
53271 + return rc;
53272 }
53273 }
52554 - return SQLITE_OK;
52555 -#endif
53274 +#endif /* __CYGWIN__ */
53275
52557 -#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
53276 +#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && defined(_WIN32)
53277 SimulateIOError( return SQLITE_ERROR );
53278 /* WinCE has no concept of a relative pathname, or so I am told. */
53279 /* WinRT has no way to convert a relative path to an absolute one. */
@@ -52573,7 +53292,8 @@ static int winFullPathnameNoMutex(
53292 return SQLITE_OK;
53293 #endif
53294
52576 -#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
53295 +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
53296 +#if defined(_WIN32)
53297 /* It's odd to simulate an io-error here, but really this is just
53298 ** using the io-error infrastructure to test that SQLite handles this
53299 ** function failing. This function could fail if, for example, the
@@ -52591,6 +53311,7 @@ static int winFullPathnameNoMutex(
53311 sqlite3_data_directory, winGetDirSep(), zRelative);
53312 return SQLITE_OK;
53313 }
53314 +#endif
53315 zConverted = winConvertFromUtf8Filename(zRelative);
53316 if( zConverted==0 ){
53317 return SQLITE_IOERR_NOMEM_BKPT;
@@ -52629,13 +53350,12 @@ static int winFullPathnameNoMutex(
53350 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
53351 "winFullPathname3", zRelative);
53352 }
52632 - nByte += 3;
52633 - zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
53353 + zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) + 3*sizeof(zTemp[0]) );
53354 if( zTemp==0 ){
53355 sqlite3_free(zConverted);
53356 return SQLITE_IOERR_NOMEM_BKPT;
53357 }
52638 - nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
53358 + nByte = osGetFullPathNameA((char*)zConverted, nByte+3, zTemp, 0);
53359 if( nByte==0 ){
53360 sqlite3_free(zConverted);
53361 sqlite3_free(zTemp);
@@ -52648,7 +53368,26 @@ static int winFullPathnameNoMutex(
53368 }
53369 #endif
53370 if( zOut ){
53371 +#ifdef __CYGWIN__
53372 + if( memcmp(zOut, "\\\\?\\", 4) ){
53373 + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
53374 + }else if( memcmp(zOut+4, "UNC\\", 4) ){
53375 + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut+4);
53376 + }else{
53377 + char *p = zOut+6;
53378 + *p = '\\';
53379 + if( osGetcwd ){
53380 + /* On Cygwin, UNC paths use forward slashes */
53381 + while( *p ){
53382 + if( *p=='\\' ) *p = '/';
53383 + ++p;
53384 + }
53385 + }
53386 + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut+6);
53387 + }
53388 +#else
53389 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
53390 +#endif /* __CYGWIN__ */
53391 sqlite3_free(zOut);
53392 return SQLITE_OK;
53393 }else{
@@ -52678,25 +53417,8 @@ static int winFullPathname(
53417 */
53418 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
53419 HANDLE h;
52681 -#if defined(__CYGWIN__)
52682 - int nFull = pVfs->mxPathname+1;
52683 - char *zFull = sqlite3MallocZero( nFull );
52684 - void *zConverted = 0;
52685 - if( zFull==0 ){
52686 - OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
52687 - return 0;
52688 - }
52689 - if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
52690 - sqlite3_free(zFull);
52691 - OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
52692 - return 0;
52693 - }
52694 - zConverted = winConvertFromUtf8Filename(zFull);
52695 - sqlite3_free(zFull);
52696 -#else
53420 void *zConverted = winConvertFromUtf8Filename(zFilename);
53421 UNUSED_PARAMETER(pVfs);
52699 -#endif
53422 if( zConverted==0 ){
53423 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
53424 return 0;
@@ -53045,7 +53767,7 @@ SQLITE_API int sqlite3_os_init(void){
53767
53768 /* Double-check that the aSyscall[] array has been constructed
53769 ** correctly. See ticket [bb3a86e890c8e96ab] */
53048 - assert( ArraySize(aSyscall)==80 );
53770 + assert( ArraySize(aSyscall)==89 );
53771
53772 /* get memory map allocation granularity */
53773 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
@@ -53664,13 +54386,13 @@ static int memdbOpen(
54386 }
54387 if( p==0 ){
54388 MemStore **apNew;
53667 - p = sqlite3Malloc( sizeof(*p) + szName + 3 );
54389 + p = sqlite3Malloc( sizeof(*p) + (i64)szName + 3 );
54390 if( p==0 ){
54391 sqlite3_mutex_leave(pVfsMutex);
54392 return SQLITE_NOMEM;
54393 }
54394 apNew = sqlite3Realloc(memdb_g.apMemStore,
53673 - sizeof(apNew[0])*(memdb_g.nMemStore+1) );
54395 + sizeof(apNew[0])*(1+(i64)memdb_g.nMemStore) );
54396 if( apNew==0 ){
54397 sqlite3_free(p);
54398 sqlite3_mutex_leave(pVfsMutex);
@@ -54103,7 +54825,7 @@ SQLITE_PRIVATE int sqlite3MemdbInit(void){
54825 ** no fewer collisions than the no-op *1. */
54826 #define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
54827
54106 -#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
54828 +#define BITVEC_NPTR ((u32)(BITVEC_USIZE/sizeof(Bitvec *)))
54829
54830
54831 /*
@@ -54286,7 +55008,7 @@ SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
55008 }
55009 }
55010 if( p->iSize<=BITVEC_NBIT ){
54289 - p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
55011 + p->u.aBitmap[i/BITVEC_SZELEM] &= ~(BITVEC_TELEM)(1<<(i&(BITVEC_SZELEM-1)));
55012 }else{
55013 unsigned int j;
55014 u32 *aiValues = pBuf;
@@ -54337,7 +55059,7 @@ SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
55059 ** individual bits within V.
55060 */
55061 #define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
54340 -#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
55062 +#define CLEARBIT(V,I) V[I>>3] &= ~(BITVEC_TELEM)(1<<(I&7))
55063 #define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
55064
55065 /*
@@ -54380,7 +55102,7 @@ SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
55102 /* Allocate the Bitvec to be tested and a linear array of
55103 ** bits to act as the reference */
55104 pBitvec = sqlite3BitvecCreate( sz );
54383 - pV = sqlite3MallocZero( (sz+7)/8 + 1 );
55105 + pV = sqlite3MallocZero( (7+(i64)sz)/8 + 1 );
55106 pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
55107 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
55108
@@ -55621,10 +56343,6 @@ static SQLITE_WSD struct PCacheGlobal {
56343 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
56344 PgFreeslot *pFree; /* Free page blocks */
56345 int nFreeSlot; /* Number of unused pcache slots */
55624 - /* The following value requires a mutex to change. We skip the mutex on
55625 - ** reading because (1) most platforms read a 32-bit integer atomically and
55626 - ** (2) even if an incorrect value is read, no great harm is done since this
55627 - ** is really just an optimization. */
56346 int bUnderPressure; /* True if low on PAGECACHE memory */
56347 } pcache1_g;
56348
@@ -55672,7 +56390,7 @@ SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
56390 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
56391 pcache1.pStart = pBuf;
56392 pcache1.pFree = 0;
55675 - pcache1.bUnderPressure = 0;
56393 + AtomicStore(&pcache1.bUnderPressure,0);
56394 while( n-- ){
56395 p = (PgFreeslot*)pBuf;
56396 p->pNext = pcache1.pFree;
@@ -55740,7 +56458,7 @@ static void *pcache1Alloc(int nByte){
56458 if( p ){
56459 pcache1.pFree = pcache1.pFree->pNext;
56460 pcache1.nFreeSlot--;
55743 - pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
56461 + AtomicStore(&pcache1.bUnderPressure,pcache1.nFreeSlot<pcache1.nReserve);
56462 assert( pcache1.nFreeSlot>=0 );
56463 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
56464 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
@@ -55779,7 +56497,7 @@ static void pcache1Free(void *p){
56497 pSlot->pNext = pcache1.pFree;
56498 pcache1.pFree = pSlot;
56499 pcache1.nFreeSlot++;
55782 - pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
56500 + AtomicStore(&pcache1.bUnderPressure,pcache1.nFreeSlot<pcache1.nReserve);
56501 assert( pcache1.nFreeSlot<=pcache1.nSlot );
56502 sqlite3_mutex_leave(pcache1.mutex);
56503 }else{
@@ -55910,7 +56628,7 @@ SQLITE_PRIVATE void sqlite3PageFree(void *p){
56628 */
56629 static int pcache1UnderMemoryPressure(PCache1 *pCache){
56630 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
55913 - return pcache1.bUnderPressure;
56631 + return AtomicLoad(&pcache1.bUnderPressure);
56632 }else{
56633 return sqlite3HeapNearlyFull();
56634 }
@@ -55927,12 +56645,12 @@ static int pcache1UnderMemoryPressure(PCache1 *pCache){
56645 */
56646 static void pcache1ResizeHash(PCache1 *p){
56647 PgHdr1 **apNew;
55930 - unsigned int nNew;
55931 - unsigned int i;
56648 + u64 nNew;
56649 + u32 i;
56650
56651 assert( sqlite3_mutex_held(p->pGroup->mutex) );
56652
55935 - nNew = p->nHash*2;
56653 + nNew = 2*(u64)p->nHash;
56654 if( nNew<256 ){
56655 nNew = 256;
56656 }
@@ -56155,7 +56873,7 @@ static void pcache1Destroy(sqlite3_pcache *p);
56873 static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
56874 PCache1 *pCache; /* The newly created page cache */
56875 PGroup *pGroup; /* The group the new page cache will belong to */
56158 - int sz; /* Bytes of memory required to allocate the new cache */
56876 + i64 sz; /* Bytes of memory required to allocate the new cache */
56877
56878 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
56879 assert( szExtra < 300 );
@@ -58043,6 +58761,9 @@ struct Pager {
58761 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
58762 char *zWal; /* File name for write-ahead log */
58763 #endif
58764 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
58765 + sqlite3 *dbWal;
58766 +#endif
58767 };
58768
58769 /*
@@ -58634,7 +59355,7 @@ static void checkPage(PgHdr *pPg){
59355 ** If an error occurs while reading from the journal file, an SQLite
59356 ** error code is returned.
59357 */
58637 -static int readSuperJournal(sqlite3_file *pJrnl, char *zSuper, u32 nSuper){
59358 +static int readSuperJournal(sqlite3_file *pJrnl, char *zSuper, u64 nSuper){
59359 int rc; /* Return code */
59360 u32 len; /* Length in bytes of super-journal name */
59361 i64 szJ; /* Total size in bytes of journal file pJrnl */
@@ -59189,6 +59910,15 @@ static void pager_unlock(Pager *pPager){
59910
59911 if( pagerUseWal(pPager) ){
59912 assert( !isOpen(pPager->jfd) );
59913 + if( pPager->eState==PAGER_ERROR ){
59914 + /* If an IO error occurs in wal.c while attempting to wrap the wal file,
59915 + ** then the Wal object may be holding a write-lock but no read-lock.
59916 + ** This call ensures that the write-lock is dropped as well. We cannot
59917 + ** have sqlite3WalEndReadTransaction() drop the write-lock, as it once
59918 + ** did, because this would break "BEGIN EXCLUSIVE" handling for
59919 + ** SQLITE_ENABLE_SETLK_TIMEOUT builds. */
59920 + sqlite3WalEndWriteTransaction(pPager->pWal);
59921 + }
59922 sqlite3WalEndReadTransaction(pPager->pWal);
59923 pPager->eState = PAGER_OPEN;
59924 }else if( !pPager->exclusiveMode ){
@@ -59870,12 +60600,12 @@ static int pager_delsuper(Pager *pPager, const char *zSuper){
60600 char *zJournal; /* Pointer to one journal within MJ file */
60601 char *zSuperPtr; /* Space to hold super-journal filename */
60602 char *zFree = 0; /* Free this buffer */
59873 - int nSuperPtr; /* Amount of space allocated to zSuperPtr[] */
60603 + i64 nSuperPtr; /* Amount of space allocated to zSuperPtr[] */
60604
60605 /* Allocate space for both the pJournal and pSuper file descriptors.
60606 ** If successful, open the super-journal file for reading.
60607 */
59878 - pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
60608 + pSuper = (sqlite3_file *)sqlite3MallocZero(2 * (i64)pVfs->szOsFile);
60609 if( !pSuper ){
60610 rc = SQLITE_NOMEM_BKPT;
60611 pJournal = 0;
@@ -59893,11 +60623,14 @@ static int pager_delsuper(Pager *pPager, const char *zSuper){
60623 */
60624 rc = sqlite3OsFileSize(pSuper, &nSuperJournal);
60625 if( rc!=SQLITE_OK ) goto delsuper_out;
59896 - nSuperPtr = pVfs->mxPathname+1;
60626 + nSuperPtr = 1 + (i64)pVfs->mxPathname;
60627 + assert( nSuperJournal>=0 && nSuperPtr>0 );
60628 zFree = sqlite3Malloc(4 + nSuperJournal + nSuperPtr + 2);
60629 if( !zFree ){
60630 rc = SQLITE_NOMEM_BKPT;
60631 goto delsuper_out;
60632 + }else{
60633 + assert( nSuperJournal<=0x7fffffff );
60634 }
60635 zFree[0] = zFree[1] = zFree[2] = zFree[3] = 0;
60636 zSuperJournal = &zFree[4];
@@ -60158,7 +60891,7 @@ static int pager_playback(Pager *pPager, int isHot){
60891 ** for pageSize.
60892 */
60893 zSuper = pPager->pTmpSpace;
60161 - rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
60894 + rc = readSuperJournal(pPager->jfd, zSuper, 1+(i64)pPager->pVfs->mxPathname);
60895 if( rc==SQLITE_OK && zSuper[0] ){
60896 rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS, &res);
60897 }
@@ -60297,7 +61030,7 @@ end_playback:
61030 ** which case it requires 4 0x00 bytes in memory immediately before
61031 ** the filename. */
61032 zSuper = &pPager->pTmpSpace[4];
60300 - rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
61033 + rc = readSuperJournal(pPager->jfd, zSuper, 1+(i64)pPager->pVfs->mxPathname);
61034 testcase( rc!=SQLITE_OK );
61035 }
61036 if( rc==SQLITE_OK
@@ -62068,6 +62801,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
62801 const char *zUri = 0; /* URI args to copy */
62802 int nUriByte = 1; /* Number of bytes of URI args at *zUri */
62803
62804 +
62805 /* Figure out how much space is required for each journal file-handle
62806 ** (there are two of them, the main journal and the sub-journal). */
62807 journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
@@ -62093,8 +62827,8 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
62827 */
62828 if( zFilename && zFilename[0] ){
62829 const char *z;
62096 - nPathname = pVfs->mxPathname+1;
62097 - zPathname = sqlite3DbMallocRaw(0, nPathname*2);
62830 + nPathname = pVfs->mxPathname + 1;
62831 + zPathname = sqlite3DbMallocRaw(0, 2*(i64)nPathname);
62832 if( zPathname==0 ){
62833 return SQLITE_NOMEM_BKPT;
62834 }
@@ -62181,14 +62915,14 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
62915 ROUND8(sizeof(*pPager)) + /* Pager structure */
62916 ROUND8(pcacheSize) + /* PCache object */
62917 ROUND8(pVfs->szOsFile) + /* The main db file */
62184 - journalFileSize * 2 + /* The two journal files */
62918 + (u64)journalFileSize * 2 + /* The two journal files */
62919 SQLITE_PTRSIZE + /* Space to hold a pointer */
62920 4 + /* Database prefix */
62187 - nPathname + 1 + /* database filename */
62188 - nUriByte + /* query parameters */
62189 - nPathname + 8 + 1 + /* Journal filename */
62921 + (u64)nPathname + 1 + /* database filename */
62922 + (u64)nUriByte + /* query parameters */
62923 + (u64)nPathname + 8 + 1 + /* Journal filename */
62924 #ifndef SQLITE_OMIT_WAL
62191 - nPathname + 4 + 1 + /* WAL filename */
62925 + (u64)nPathname + 4 + 1 + /* WAL filename */
62926 #endif
62927 3 /* Terminator */
62928 );
@@ -64911,6 +65645,11 @@ static int pagerOpenWal(Pager *pPager){
65645 pPager->fd, pPager->zWal, pPager->exclusiveMode,
65646 pPager->journalSizeLimit, &pPager->pWal
65647 );
65648 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
65649 + if( rc==SQLITE_OK ){
65650 + sqlite3WalDb(pPager->pWal, pPager->dbWal);
65651 + }
65652 +#endif
65653 }
65654 pagerFixMaplimit(pPager);
65655
@@ -65030,6 +65769,7 @@ SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager *pPager, int bLock){
65769 ** blocking locks are required.
65770 */
65771 SQLITE_PRIVATE void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){
65772 + pPager->dbWal = db;
65773 if( pagerUseWal(pPager) ){
65774 sqlite3WalDb(pPager->pWal, db);
65775 }
@@ -65643,6 +66383,11 @@ struct WalCkptInfo {
66383 /*
66384 ** An open write-ahead log file is represented by an instance of the
66385 ** following object.
66386 +**
66387 +** writeLock:
66388 +** This is usually set to 1 whenever the WRITER lock is held. However,
66389 +** if it is set to 2, then the WRITER lock is held but must be released
66390 +** by walHandleException() if a SEH exception is thrown.
66391 */
66392 struct Wal {
66393 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
@@ -65733,9 +66478,13 @@ struct WalIterator {
66478 u32 *aPgno; /* Array of page numbers. */
66479 int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
66480 int iZero; /* Frame number associated with aPgno[0] */
65736 - } aSegment[1]; /* One for every 32KB page in the wal-index */
66481 + } aSegment[FLEXARRAY]; /* One for every 32KB page in the wal-index */
66482 };
66483
66484 +/* Size (in bytes) of a WalIterator object suitable for N or fewer segments */
66485 +#define SZ_WALITERATOR(N) \
66486 + (offsetof(WalIterator,aSegment)*(N)*sizeof(struct WalSegment))
66487 +
66488 /*
66489 ** Define the parameters of the hash tables in the wal-index file. There
66490 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
@@ -65894,7 +66643,7 @@ static SQLITE_NOINLINE int walIndexPageRealloc(
66643
66644 /* Enlarge the pWal->apWiData[] array if required */
66645 if( pWal->nWiData<=iPage ){
65897 - sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
66646 + sqlite3_int64 nByte = sizeof(u32*)*(1+(i64)iPage);
66647 volatile u32 **apNew;
66648 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
66649 if( !apNew ){
@@ -66003,10 +66752,8 @@ static void walChecksumBytes(
66752 s1 = s2 = 0;
66753 }
66754
66006 - assert( nByte>=8 );
66007 - assert( (nByte&0x00000007)==0 );
66008 - assert( nByte<=65536 );
66009 - assert( nByte%4==0 );
66755 + /* nByte is a multiple of 8 between 8 and 65536 */
66756 + assert( nByte>=8 && (nByte&7)==0 && nByte<=65536 );
66757
66758 if( !nativeCksum ){
66759 do {
@@ -67096,8 +67843,7 @@ static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
67843
67844 /* Allocate space for the WalIterator object. */
67845 nSegment = walFramePage(iLast) + 1;
67099 - nByte = sizeof(WalIterator)
67100 - + (nSegment-1)*sizeof(struct WalSegment)
67846 + nByte = SZ_WALITERATOR(nSegment)
67847 + iLast*sizeof(ht_slot);
67848 p = (WalIterator *)sqlite3_malloc64(nByte
67849 + sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
@@ -67168,7 +67914,7 @@ static int walEnableBlockingMs(Wal *pWal, int nMs){
67914 static int walEnableBlocking(Wal *pWal){
67915 int res = 0;
67916 if( pWal->db ){
67171 - int tmout = pWal->db->busyTimeout;
67917 + int tmout = pWal->db->setlkTimeout;
67918 if( tmout ){
67919 res = walEnableBlockingMs(pWal, tmout);
67920 }
@@ -67554,7 +68300,9 @@ static int walHandleException(Wal *pWal){
68300 static const int S = 1;
68301 static const int E = (1<<SQLITE_SHM_NLOCK);
68302 int ii;
67557 - u32 mUnlock = pWal->lockMask & ~(
68303 + u32 mUnlock;
68304 + if( pWal->writeLock==2 ) pWal->writeLock = 0;
68305 + mUnlock = pWal->lockMask & ~(
68306 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
68307 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
68308 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
@@ -67826,7 +68574,12 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){
68574 if( bWriteLock
68575 || SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1))
68576 ){
67829 - pWal->writeLock = 1;
68577 + /* If the write-lock was just obtained, set writeLock to 2 instead of
68578 + ** the usual 1. This causes walIndexPage() to behave as if the
68579 + ** write-lock were held (so that it allocates new pages as required),
68580 + ** and walHandleException() to unlock the write-lock if a SEH exception
68581 + ** is thrown. */
68582 + if( !bWriteLock ) pWal->writeLock = 2;
68583 if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
68584 badHdr = walIndexTryHdr(pWal, pChanged);
68585 if( badHdr ){
@@ -68190,7 +68943,6 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){
68943 rc = walIndexReadHdr(pWal, pChanged);
68944 }
68945 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68193 - walDisableBlocking(pWal);
68946 if( rc==SQLITE_BUSY_TIMEOUT ){
68947 rc = SQLITE_BUSY;
68948 *pCnt |= WAL_RETRY_BLOCKED_MASK;
@@ -68205,6 +68957,7 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){
68957 ** WAL_RETRY this routine will be called again and will probably be
68958 ** right on the second iteration.
68959 */
68960 + (void)walEnableBlocking(pWal);
68961 if( pWal->apWiData[0]==0 ){
68962 /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
68963 ** We assume this is a transient condition, so return WAL_RETRY. The
@@ -68221,6 +68974,7 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){
68974 rc = SQLITE_BUSY_RECOVERY;
68975 }
68976 }
68977 + walDisableBlocking(pWal);
68978 if( rc!=SQLITE_OK ){
68979 return rc;
68980 }
@@ -68611,8 +69365,11 @@ SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
69365 ** read-lock.
69366 */
69367 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
68614 - sqlite3WalEndWriteTransaction(pWal);
69368 +#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
69369 + assert( pWal->writeLock==0 || pWal->readLock<0 );
69370 +#endif
69371 if( pWal->readLock>=0 ){
69372 + sqlite3WalEndWriteTransaction(pWal);
69373 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
69374 pWal->readLock = -1;
69375 }
@@ -68805,7 +69562,7 @@ SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
69562 ** read-transaction was even opened, making this call a no-op.
69563 ** Return early. */
69564 if( pWal->writeLock ){
68808 - assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
69565 + assert( !memcmp(&pWal->hdr,(void*)pWal->apWiData[0],sizeof(WalIndexHdr)) );
69566 return SQLITE_OK;
69567 }
69568 #endif
@@ -70254,6 +71011,12 @@ struct CellInfo {
71011 */
71012 #define BTCURSOR_MAX_DEPTH 20
71013
71014 +/*
71015 +** Maximum amount of storage local to a database page, regardless of
71016 +** page size.
71017 +*/
71018 +#define BT_MAX_LOCAL 65501 /* 65536 - 35 */
71019 +
71020 /*
71021 ** A cursor is a pointer to a particular entry within a particular
71022 ** b-tree within a database file.
@@ -70662,7 +71425,7 @@ SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
71425 */
71426 static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
71427 int i;
70665 - int skipOk = 1;
71428 + u8 skipOk = 1;
71429 Btree *p;
71430 assert( sqlite3_mutex_held(db->mutex) );
71431 for(i=0; i<db->nDb; i++){
@@ -71518,7 +72281,7 @@ static int saveCursorKey(BtCursor *pCur){
72281 ** below. */
72282 void *pKey;
72283 pCur->nKey = sqlite3BtreePayloadSize(pCur);
71521 - pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
72284 + pKey = sqlite3Malloc( ((i64)pCur->nKey) + 9 + 8 );
72285 if( pKey ){
72286 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
72287 if( rc==SQLITE_OK ){
@@ -71808,7 +72571,7 @@ SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
72571 */
72572 SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
72573 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
71811 - pCur->hints = x;
72574 + pCur->hints = (u8)x;
72575 }
72576
72577
@@ -72002,14 +72765,15 @@ static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
72765 static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
72766 int maxLocal; /* Maximum amount of payload held locally */
72767 maxLocal = pPage->maxLocal;
72768 + assert( nPayload>=0 );
72769 if( nPayload<=maxLocal ){
72006 - return nPayload;
72770 + return (int)nPayload;
72771 }else{
72772 int minLocal; /* Minimum amount of payload held locally */
72773 int surplus; /* Overflow payload available for local storage */
72774 minLocal = pPage->minLocal;
72011 - surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
72012 - return ( surplus <= maxLocal ) ? surplus : minLocal;
72775 + surplus = (int)(minLocal +(nPayload - minLocal)%(pPage->pBt->usableSize-4));
72776 + return (surplus <= maxLocal) ? surplus : minLocal;
72777 }
72778 }
72779
@@ -72119,11 +72883,13 @@ static void btreeParseCellPtr(
72883 pInfo->pPayload = pIter;
72884 testcase( nPayload==pPage->maxLocal );
72885 testcase( nPayload==(u32)pPage->maxLocal+1 );
72886 + assert( nPayload>=0 );
72887 + assert( pPage->maxLocal <= BT_MAX_LOCAL );
72888 if( nPayload<=pPage->maxLocal ){
72889 /* This is the (easy) common case where the entire payload fits
72890 ** on the local page. No overflow is required.
72891 */
72126 - pInfo->nSize = nPayload + (u16)(pIter - pCell);
72892 + pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell);
72893 if( pInfo->nSize<4 ) pInfo->nSize = 4;
72894 pInfo->nLocal = (u16)nPayload;
72895 }else{
@@ -72156,11 +72922,13 @@ static void btreeParseCellPtrIndex(
72922 pInfo->pPayload = pIter;
72923 testcase( nPayload==pPage->maxLocal );
72924 testcase( nPayload==(u32)pPage->maxLocal+1 );
72925 + assert( nPayload>=0 );
72926 + assert( pPage->maxLocal <= BT_MAX_LOCAL );
72927 if( nPayload<=pPage->maxLocal ){
72928 /* This is the (easy) common case where the entire payload fits
72929 ** on the local page. No overflow is required.
72930 */
72163 - pInfo->nSize = nPayload + (u16)(pIter - pCell);
72931 + pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell);
72932 if( pInfo->nSize<4 ) pInfo->nSize = 4;
72933 pInfo->nLocal = (u16)nPayload;
72934 }else{
@@ -72699,14 +73467,14 @@ static SQLITE_INLINE int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
73467 ** at the end of the page. So do additional corruption checks inside this
73468 ** routine and return SQLITE_CORRUPT if any problems are found.
73469 */
72702 -static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
72703 - u16 iPtr; /* Address of ptr to next freeblock */
72704 - u16 iFreeBlk; /* Address of the next freeblock */
73470 +static int freeSpace(MemPage *pPage, int iStart, int iSize){
73471 + int iPtr; /* Address of ptr to next freeblock */
73472 + int iFreeBlk; /* Address of the next freeblock */
73473 u8 hdr; /* Page header size. 0 or 100 */
72706 - u8 nFrag = 0; /* Reduction in fragmentation */
72707 - u16 iOrigSize = iSize; /* Original value of iSize */
72708 - u16 x; /* Offset to cell content area */
72709 - u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
73474 + int nFrag = 0; /* Reduction in fragmentation */
73475 + int iOrigSize = iSize; /* Original value of iSize */
73476 + int x; /* Offset to cell content area */
73477 + int iEnd = iStart + iSize; /* First byte past the iStart buffer */
73478 unsigned char *data = pPage->aData; /* Page content */
73479 u8 *pTmp; /* Temporary ptr into data[] */
73480
@@ -72733,7 +73501,7 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
73501 }
73502 iPtr = iFreeBlk;
73503 }
72736 - if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
73504 + if( iFreeBlk>(int)pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
73505 return SQLITE_CORRUPT_PAGE(pPage);
73506 }
73507 assert( iFreeBlk>iPtr || iFreeBlk==0 || CORRUPT_DB );
@@ -72748,7 +73516,7 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
73516 nFrag = iFreeBlk - iEnd;
73517 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
73518 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
72751 - if( iEnd > pPage->pBt->usableSize ){
73519 + if( iEnd > (int)pPage->pBt->usableSize ){
73520 return SQLITE_CORRUPT_PAGE(pPage);
73521 }
73522 iSize = iEnd - iStart;
@@ -72769,7 +73537,7 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
73537 }
73538 }
73539 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
72772 - data[hdr+7] -= nFrag;
73540 + data[hdr+7] -= (u8)nFrag;
73541 }
73542 pTmp = &data[hdr+5];
73543 x = get2byte(pTmp);
@@ -72790,7 +73558,8 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
73558 /* Insert the new freeblock into the freelist */
73559 put2byte(&data[iPtr], iStart);
73560 put2byte(&data[iStart], iFreeBlk);
72793 - put2byte(&data[iStart+2], iSize);
73561 + assert( iSize>=0 && iSize<=0xffff );
73562 + put2byte(&data[iStart+2], (u16)iSize);
73563 }
73564 pPage->nFree += iOrigSize;
73565 return SQLITE_OK;
@@ -73016,7 +73785,7 @@ static int btreeInitPage(MemPage *pPage){
73785 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
73786 pPage->maskPage = (u16)(pBt->pageSize - 1);
73787 pPage->nOverflow = 0;
73019 - pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
73788 + pPage->cellOffset = (u16)(pPage->hdrOffset + 8 + pPage->childPtrSize);
73789 pPage->aCellIdx = data + pPage->childPtrSize + 8;
73790 pPage->aDataEnd = pPage->aData + pBt->pageSize;
73791 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
@@ -73050,8 +73819,8 @@ static int btreeInitPage(MemPage *pPage){
73819 static void zeroPage(MemPage *pPage, int flags){
73820 unsigned char *data = pPage->aData;
73821 BtShared *pBt = pPage->pBt;
73053 - u8 hdr = pPage->hdrOffset;
73054 - u16 first;
73822 + int hdr = pPage->hdrOffset;
73823 + int first;
73824
73825 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
73826 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
@@ -73068,7 +73837,7 @@ static void zeroPage(MemPage *pPage, int flags){
73837 put2byte(&data[hdr+5], pBt->usableSize);
73838 pPage->nFree = (u16)(pBt->usableSize - first);
73839 decodeFlags(pPage, flags);
73071 - pPage->cellOffset = first;
73840 + pPage->cellOffset = (u16)first;
73841 pPage->aDataEnd = &data[pBt->pageSize];
73842 pPage->aCellIdx = &data[first];
73843 pPage->aDataOfst = &data[pPage->childPtrSize];
@@ -73854,7 +74623,7 @@ SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve,
74623 BtShared *pBt = p->pBt;
74624 assert( nReserve>=0 && nReserve<=255 );
74625 sqlite3BtreeEnter(p);
73857 - pBt->nReserveWanted = nReserve;
74626 + pBt->nReserveWanted = (u8)nReserve;
74627 x = pBt->pageSize - pBt->usableSize;
74628 if( nReserve<x ) nReserve = x;
74629 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
@@ -73960,7 +74729,7 @@ SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
74729 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
74730 if( newFlag>=0 ){
74731 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
73963 - p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
74732 + p->pBt->btsFlags |= (u16)(BTS_SECURE_DELETE*newFlag);
74733 }
74734 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
74735 sqlite3BtreeLeave(p);
@@ -74480,6 +75249,13 @@ static SQLITE_NOINLINE int btreeBeginTrans(
75249 (void)sqlite3PagerWalWriteLock(pPager, 0);
75250 unlockBtreeIfUnused(pBt);
75251 }
75252 +#if defined(SQLITE_ENABLE_SETLK_TIMEOUT)
75253 + if( rc==SQLITE_BUSY_TIMEOUT ){
75254 + /* If a blocking lock timed out, break out of the loop here so that
75255 + ** the busy-handler is not invoked. */
75256 + break;
75257 + }
75258 +#endif
75259 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
75260 btreeInvokeBusyHandler(pBt) );
75261 sqlite3PagerWalDb(pPager, 0);
@@ -76889,7 +77665,7 @@ bypass_moveto_root:
77665 rc = SQLITE_CORRUPT_PAGE(pPage);
77666 goto moveto_index_finish;
77667 }
76892 - pCellKey = sqlite3Malloc( nCell+nOverrun );
77668 + pCellKey = sqlite3Malloc( (u64)nCell+(u64)nOverrun );
77669 if( pCellKey==0 ){
77670 rc = SQLITE_NOMEM_BKPT;
77671 goto moveto_index_finish;
@@ -78408,7 +79184,8 @@ static int rebuildPage(
79184 }
79185
79186 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
78411 - pPg->nCell = nCell;
79187 + assert( nCell < 10922 );
79188 + pPg->nCell = (u16)nCell;
79189 pPg->nOverflow = 0;
79190
79191 put2byte(&aData[hdr+1], 0);
@@ -78655,9 +79432,13 @@ static int editPage(
79432 if( pageInsertArray(
79433 pPg, pBegin, &pData, pCellptr,
79434 iNew+nCell, nNew-nCell, pCArray
78658 - ) ) goto editpage_fail;
79435 + )
79436 + ){
79437 + goto editpage_fail;
79438 + }
79439
78660 - pPg->nCell = nNew;
79440 + assert( nNew < 10922 );
79441 + pPg->nCell = (u16)nNew;
79442 pPg->nOverflow = 0;
79443
79444 put2byte(&aData[hdr+3], pPg->nCell);
@@ -78966,7 +79747,7 @@ static int balance_nonroot(
79747 int pageFlags; /* Value of pPage->aData[0] */
79748 int iSpace1 = 0; /* First unused byte of aSpace1[] */
79749 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
78969 - int szScratch; /* Size of scratch memory requested */
79750 + u64 szScratch; /* Size of scratch memory requested */
79751 MemPage *apOld[NB]; /* pPage and up to two siblings */
79752 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
79753 u8 *pRight; /* Location in parent of right-sibling pointer */
@@ -80251,7 +81032,7 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
81032 if( pCur->info.nKey==pX->nKey ){
81033 BtreePayload x2;
81034 x2.pData = pX->pKey;
80254 - x2.nData = pX->nKey;
81035 + x2.nData = (int)pX->nKey; assert( pX->nKey<=0x7fffffff );
81036 x2.nZero = 0;
81037 return btreeOverwriteCell(pCur, &x2);
81038 }
@@ -80432,7 +81213,7 @@ SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64
81213
81214 getCellInfo(pSrc);
81215 if( pSrc->info.nPayload<0x80 ){
80435 - *(aOut++) = pSrc->info.nPayload;
81216 + *(aOut++) = (u8)pSrc->info.nPayload;
81217 }else{
81218 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
81219 }
@@ -80445,7 +81226,7 @@ SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64
81226 nRem = pSrc->info.nPayload;
81227 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
81228 memcpy(aOut, aIn, nIn);
80448 - pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
81229 + pBt->nPreformatSize = nIn + (int)(aOut - pBt->pTmpSpace);
81230 return SQLITE_OK;
81231 }else{
81232 int rc = SQLITE_OK;
@@ -80457,7 +81238,7 @@ SQLITE_PRIVATE int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64
81238 u32 nOut; /* Size of output buffer aOut[] */
81239
81240 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
80460 - pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
81241 + pBt->nPreformatSize = (int)nOut + (int)(aOut - pBt->pTmpSpace);
81242 if( nOut<pSrc->info.nPayload ){
81243 pPgnoOut = &aOut[nOut];
81244 pBt->nPreformatSize += 4;
@@ -82078,6 +82859,7 @@ SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
82859 */
82860 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
82861 BtShared *pBt = p->pBt;
82862 + assert( nBytes==0 || nBytes==sizeof(Schema) );
82863 sqlite3BtreeEnter(p);
82864 if( !pBt->pSchema && nBytes ){
82865 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
@@ -83194,7 +83976,7 @@ static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){
83976 ** corresponding string value, then it is important that the string be
83977 ** derived from the numeric value, not the other way around, to ensure
83978 ** that the index and table are consistent. See ticket
83197 -** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
83979 +** https://sqlite.org/src/info/343634942dd54ab (2018-01-31) for
83980 ** an example.
83981 **
83982 ** This routine looks at pMem to verify that if it has both a numeric
@@ -83380,7 +84162,7 @@ SQLITE_PRIVATE void sqlite3VdbeMemZeroTerminateIfAble(Mem *pMem){
84162 return;
84163 }
84164 if( pMem->enc!=SQLITE_UTF8 ) return;
83383 - if( NEVER(pMem->z==0) ) return;
84165 + assert( pMem->z!=0 );
84166 if( pMem->flags & MEM_Dyn ){
84167 if( pMem->xDel==sqlite3_free
84168 && sqlite3_msize(pMem->z) >= (u64)(pMem->n+1)
@@ -84493,7 +85275,7 @@ static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
85275
85276 if( pRec==0 ){
85277 Index *pIdx = p->pIdx; /* Index being probed */
84496 - int nByte; /* Bytes of space to allocate */
85278 + i64 nByte; /* Bytes of space to allocate */
85279 int i; /* Counter variable */
85280 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
85281
@@ -84559,7 +85341,7 @@ static int valueFromFunction(
85341 ){
85342 sqlite3_context ctx; /* Context object for function invocation */
85343 sqlite3_value **apVal = 0; /* Function arguments */
84562 - int nVal = 0; /* Size of apVal[] array */
85344 + int nVal = 0; /* Number of function arguments */
85345 FuncDef *pFunc = 0; /* Function definition */
85346 sqlite3_value *pVal = 0; /* New value */
85347 int rc = SQLITE_OK; /* Return code */
@@ -85557,12 +86339,10 @@ SQLITE_PRIVATE int sqlite3VdbeAddFunctionCall(
86339 int eCallCtx /* Calling context */
86340 ){
86341 Vdbe *v = pParse->pVdbe;
85560 - int nByte;
86342 int addr;
86343 sqlite3_context *pCtx;
86344 assert( v );
85564 - nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
85565 - pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
86345 + pCtx = sqlite3DbMallocRawNN(pParse->db, SZ_CONTEXT(nArg));
86346 if( pCtx==0 ){
86347 assert( pParse->db->mallocFailed );
86348 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
@@ -85838,7 +86618,7 @@ static Op *opIterNext(VdbeOpIter *p){
86618 }
86619
86620 if( pRet->p4type==P4_SUBPROGRAM ){
85841 - int nByte = (p->nSub+1)*sizeof(SubProgram*);
86621 + i64 nByte = (1+(u64)p->nSub)*sizeof(SubProgram*);
86622 int j;
86623 for(j=0; j<p->nSub; j++){
86624 if( p->apSub[j]==pRet->p4.pProgram ) break;
@@ -85968,8 +86748,8 @@ SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
86748 ** (1) For each jump instruction with a negative P2 value (a label)
86749 ** resolve the P2 value to an actual address.
86750 **
85971 -** (2) Compute the maximum number of arguments used by any SQL function
85972 -** and store that value in *pMaxFuncArgs.
86751 +** (2) Compute the maximum number of arguments used by the xUpdate/xFilter
86752 +** methods of any virtual table and store that value in *pMaxVtabArgs.
86753 **
86754 ** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
86755 ** indicate what the prepared statement actually does.
@@ -85982,8 +86762,8 @@ SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
86762 ** script numbers the opcodes correctly. Changes to this routine must be
86763 ** coordinated with changes to mkopcodeh.tcl.
86764 */
85985 -static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
85986 - int nMaxArgs = *pMaxFuncArgs;
86765 +static void resolveP2Values(Vdbe *p, int *pMaxVtabArgs){
86766 + int nMaxVtabArgs = *pMaxVtabArgs;
86767 Op *pOp;
86768 Parse *pParse = p->pParse;
86769 int *aLabel = pParse->aLabel;
@@ -86028,15 +86808,19 @@ static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
86808 }
86809 #ifndef SQLITE_OMIT_VIRTUALTABLE
86810 case OP_VUpdate: {
86031 - if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
86811 + if( pOp->p2>nMaxVtabArgs ) nMaxVtabArgs = pOp->p2;
86812 break;
86813 }
86814 case OP_VFilter: {
86815 int n;
86816 + /* The instruction immediately prior to VFilter will be an
86817 + ** OP_Integer that sets the "argc" value for the VFilter. See
86818 + ** the code where OP_VFilter is generated at tag-20250207a. */
86819 assert( (pOp - p->aOp) >= 3 );
86820 assert( pOp[-1].opcode==OP_Integer );
86821 + assert( pOp[-1].p2==pOp->p3+1 );
86822 n = pOp[-1].p1;
86039 - if( n>nMaxArgs ) nMaxArgs = n;
86823 + if( n>nMaxVtabArgs ) nMaxVtabArgs = n;
86824 /* Fall through into the default case */
86825 /* no break */ deliberate_fall_through
86826 }
@@ -86077,7 +86861,7 @@ resolve_p2_values_loop_exit:
86861 pParse->aLabel = 0;
86862 }
86863 pParse->nLabel = 0;
86080 - *pMaxFuncArgs = nMaxArgs;
86864 + *pMaxVtabArgs = nMaxVtabArgs;
86865 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
86866 }
86867
@@ -86306,7 +87090,7 @@ SQLITE_PRIVATE void sqlite3VdbeScanStatus(
87090 const char *zName /* Name of table or index being scanned */
87091 ){
87092 if( IS_STMT_SCANSTATUS(p->db) ){
86309 - sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
87093 + i64 nByte = (1+(i64)p->nScan) * sizeof(ScanStatus);
87094 ScanStatus *aNew;
87095 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
87096 if( aNew ){
@@ -86416,6 +87200,9 @@ SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
87200 */
87201 SQLITE_PRIVATE void sqlite3VdbeTypeofColumn(Vdbe *p, int iDest){
87202 VdbeOp *pOp = sqlite3VdbeGetLastOp(p);
87203 +#ifdef SQLITE_DEBUG
87204 + while( pOp->opcode==OP_ReleaseReg ) pOp--;
87205 +#endif
87206 if( pOp->p3==iDest && pOp->opcode==OP_Column ){
87207 pOp->p5 |= OPFLAG_TYPEOFARG;
87208 }
@@ -87755,7 +88542,7 @@ SQLITE_PRIVATE void sqlite3VdbeMakeReady(
88542 int nVar; /* Number of parameters */
88543 int nMem; /* Number of VM memory registers */
88544 int nCursor; /* Number of cursors required */
87758 - int nArg; /* Number of arguments in subprograms */
88545 + int nArg; /* Max number args to xFilter or xUpdate */
88546 int n; /* Loop counter */
88547 struct ReusableSpace x; /* Reusable bulk memory */
88548
@@ -87827,6 +88614,9 @@ SQLITE_PRIVATE void sqlite3VdbeMakeReady(
88614 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
88615 }
88616 }
88617 +#ifdef SQLITE_DEBUG
88618 + p->napArg = nArg;
88619 +#endif
88620
88621 if( db->mallocFailed ){
88622 p->nVar = 0;
@@ -89324,6 +90114,7 @@ SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
90114 ){
90115 UnpackedRecord *p; /* Unpacked record to return */
90116 int nByte; /* Number of bytes required for *p */
90117 + assert( sizeof(UnpackedRecord) + sizeof(Mem)*65536 < 0x7fffffff );
90118 nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
90119 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
90120 if( !p ) return 0;
@@ -90630,10 +91421,11 @@ SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(
91421 preupdate.pCsr = pCsr;
91422 preupdate.op = op;
91423 preupdate.iNewReg = iReg;
90633 - preupdate.keyinfo.db = db;
90634 - preupdate.keyinfo.enc = ENC(db);
90635 - preupdate.keyinfo.nKeyField = pTab->nCol;
90636 - preupdate.keyinfo.aSortFlags = (u8*)&fakeSortOrder;
91424 + preupdate.pKeyinfo = (KeyInfo*)&preupdate.keyinfoSpace;
91425 + preupdate.pKeyinfo->db = db;
91426 + preupdate.pKeyinfo->enc = ENC(db);
91427 + preupdate.pKeyinfo->nKeyField = pTab->nCol;
91428 + preupdate.pKeyinfo->aSortFlags = (u8*)&fakeSortOrder;
91429 preupdate.iKey1 = iKey1;
91430 preupdate.iKey2 = iKey2;
91431 preupdate.pTab = pTab;
@@ -90643,8 +91435,8 @@ SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(
91435 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
91436 db->pPreUpdate = 0;
91437 sqlite3DbFree(db, preupdate.aRecord);
90646 - vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
90647 - vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
91438 + vdbeFreeUnpacked(db, preupdate.pKeyinfo->nKeyField+1,preupdate.pUnpacked);
91439 + vdbeFreeUnpacked(db, preupdate.pKeyinfo->nKeyField+1,preupdate.pNewUnpacked);
91440 sqlite3VdbeMemRelease(&preupdate.oldipk);
91441 if( preupdate.aNew ){
91442 int i;
@@ -92475,7 +93267,7 @@ SQLITE_API int sqlite3_bind_text64(
93267 assert( xDel!=SQLITE_DYNAMIC );
93268 if( enc!=SQLITE_UTF8 ){
93269 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
92478 - nData &= ~(u16)1;
93270 + nData &= ~(u64)1;
93271 }
93272 return bindText(pStmt, i, zData, nData, xDel, enc);
93273 }
@@ -92883,7 +93675,7 @@ SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppVa
93675 if( !aRec ) goto preupdate_old_out;
93676 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
93677 if( rc==SQLITE_OK ){
92886 - p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
93678 + p->pUnpacked = vdbeUnpackRecord(p->pKeyinfo, nRec, aRec);
93679 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
93680 }
93681 if( rc!=SQLITE_OK ){
@@ -92900,7 +93692,9 @@ SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppVa
93692 Column *pCol = &p->pTab->aCol[iIdx];
93693 if( pCol->iDflt>0 ){
93694 if( p->apDflt==0 ){
92903 - int nByte = sizeof(sqlite3_value*)*p->pTab->nCol;
93695 + int nByte;
93696 + assert( sizeof(sqlite3_value*)*UMXV(p->pTab->nCol) < 0x7fffffff );
93697 + nByte = sizeof(sqlite3_value*)*p->pTab->nCol;
93698 p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte);
93699 if( p->apDflt==0 ) goto preupdate_old_out;
93700 }
@@ -92946,7 +93740,7 @@ SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
93740 #else
93741 p = db->pPreUpdate;
93742 #endif
92949 - return (p ? p->keyinfo.nKeyField : 0);
93743 + return (p ? p->pKeyinfo->nKeyField : 0);
93744 }
93745 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
93746
@@ -93029,7 +93823,7 @@ SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppVa
93823 Mem *pData = &p->v->aMem[p->iNewReg];
93824 rc = ExpandBlob(pData);
93825 if( rc!=SQLITE_OK ) goto preupdate_new_out;
93032 - pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
93826 + pUnpack = vdbeUnpackRecord(p->pKeyinfo, pData->n, pData->z);
93827 if( !pUnpack ){
93828 rc = SQLITE_NOMEM;
93829 goto preupdate_new_out;
@@ -93050,7 +93844,8 @@ SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppVa
93844 */
93845 assert( p->op==SQLITE_UPDATE );
93846 if( !p->aNew ){
93053 - p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
93847 + assert( sizeof(Mem)*UMXV(p->pCsr->nField) < 0x7fffffff );
93848 + p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem)*p->pCsr->nField);
93849 if( !p->aNew ){
93850 rc = SQLITE_NOMEM;
93851 goto preupdate_new_out;
@@ -93820,11 +94615,11 @@ static VdbeCursor *allocateCursor(
94615 */
94616 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
94617
93823 - int nByte;
94618 + i64 nByte;
94619 VdbeCursor *pCx = 0;
93825 - nByte =
93826 - ROUND8P(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
93827 - (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
94620 + nByte = SZ_VDBECURSOR(nField);
94621 + assert( ROUND8(nByte)==nByte );
94622 + if( eCurType==CURTYPE_BTREE ) nByte += sqlite3BtreeCursorSize();
94623
94624 assert( iCur>=0 && iCur<p->nCursor );
94625 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
@@ -93848,7 +94643,7 @@ static VdbeCursor *allocateCursor(
94643 pMem->szMalloc = 0;
94644 return 0;
94645 }
93851 - pMem->szMalloc = nByte;
94646 + pMem->szMalloc = (int)nByte;
94647 }
94648
94649 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
@@ -93857,8 +94652,8 @@ static VdbeCursor *allocateCursor(
94652 pCx->nField = nField;
94653 pCx->aOffset = &pCx->aType[nField];
94654 if( eCurType==CURTYPE_BTREE ){
93860 - pCx->uc.pCursor = (BtCursor*)
93861 - &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
94655 + assert( ROUND8(SZ_VDBECURSOR(nField))==SZ_VDBECURSOR(nField) );
94656 + pCx->uc.pCursor = (BtCursor*)&pMem->z[SZ_VDBECURSOR(nField)];
94657 sqlite3BtreeCursorZero(pCx->uc.pCursor);
94658 }
94659 return pCx;
@@ -94862,7 +95657,7 @@ case OP_Halt: {
95657 sqlite3VdbeError(p, "%s", pOp->p4.z);
95658 }
95659 pcx = (int)(pOp - aOp);
94865 - sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
95660 + sqlite3_log(pOp->p1, "abort at %d: %s; [%s]", pcx, p->zErrMsg, p->zSql);
95661 }
95662 rc = sqlite3VdbeHalt(p);
95663 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
@@ -96188,7 +96983,7 @@ case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
96983 break;
96984 }
96985
96191 -/* Opcode: Once P1 P2 * * *
96986 +/* Opcode: Once P1 P2 P3 * *
96987 **
96988 ** Fall through to the next instruction the first time this opcode is
96989 ** encountered on each invocation of the byte-code program. Jump to P2
@@ -96204,6 +96999,12 @@ case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
96999 ** whether or not the jump should be taken. The bitmask is necessary
97000 ** because the self-altering code trick does not work for recursive
97001 ** triggers.
97002 +**
97003 +** The P3 operand is not used directly by this opcode. However P3 is
97004 +** used by the code generator as follows: If this opcode is the start
97005 +** of a subroutine and that subroutine uses a Bloom filter, then P3 will
97006 +** be the register that holds that Bloom filter. See tag-202407032019
97007 +** in the source code for implementation details.
97008 */
97009 case OP_Once: { /* jump */
97010 u32 iAddr; /* Address of this instruction */
@@ -97249,6 +98050,7 @@ case OP_MakeRecord: {
98050 zHdr += sqlite3PutVarint(zHdr, serial_type);
98051 if( pRec->n ){
98052 assert( pRec->z!=0 );
98053 + assert( pRec->z!=(const char*)sqlite3CtypeMap );
98054 memcpy(zPayload, pRec->z, pRec->n);
98055 zPayload += pRec->n;
98056 }
@@ -99600,7 +100402,7 @@ case OP_RowData: {
100402 /* The OP_RowData opcodes always follow OP_NotExists or
100403 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
100404 ** that might invalidate the cursor.
99603 - ** If this where not the case, on of the following assert()s
100405 + ** If this were not the case, one of the following assert()s
100406 ** would fail. Should this ever change (because of changes in the code
100407 ** generator) then the fix would be to insert a call to
100408 ** sqlite3VdbeCursorMoveto().
@@ -100869,7 +101671,7 @@ case OP_RowSetTest: { /* jump, in1, in3 */
101671 */
101672 case OP_Program: { /* jump0 */
101673 int nMem; /* Number of memory registers for sub-program */
100872 - int nByte; /* Bytes of runtime space required for sub-program */
101674 + i64 nByte; /* Bytes of runtime space required for sub-program */
101675 Mem *pRt; /* Register to allocate runtime space */
101676 Mem *pMem; /* Used to iterate through memory cells */
101677 Mem *pEnd; /* Last memory cell in new array */
@@ -100920,7 +101722,7 @@ case OP_Program: { /* jump0 */
101722 nByte = ROUND8(sizeof(VdbeFrame))
101723 + nMem * sizeof(Mem)
101724 + pProgram->nCsr * sizeof(VdbeCursor*)
100923 - + (pProgram->nOp + 7)/8;
101725 + + (7 + (i64)pProgram->nOp)/8;
101726 pFrame = sqlite3DbMallocZero(db, nByte);
101727 if( !pFrame ){
101728 goto no_mem;
@@ -100928,7 +101730,7 @@ case OP_Program: { /* jump0 */
101730 sqlite3VdbeMemRelease(pRt);
101731 pRt->flags = MEM_Blob|MEM_Dyn;
101732 pRt->z = (char*)pFrame;
100931 - pRt->n = nByte;
101733 + pRt->n = (int)nByte;
101734 pRt->xDel = sqlite3VdbeFrameMemDel;
101735
101736 pFrame->v = p;
@@ -101027,12 +101829,14 @@ case OP_Param: { /* out2 */
101829 ** statement counter is incremented (immediate foreign key constraints).
101830 */
101831 case OP_FkCounter: {
101030 - if( db->flags & SQLITE_DeferFKs ){
101031 - db->nDeferredImmCons += pOp->p2;
101032 - }else if( pOp->p1 ){
101832 + if( pOp->p1 ){
101833 db->nDeferredCons += pOp->p2;
101834 }else{
101035 - p->nFkConstraint += pOp->p2;
101835 + if( db->flags & SQLITE_DeferFKs ){
101836 + db->nDeferredImmCons += pOp->p2;
101837 + }else{
101838 + p->nFkConstraint += pOp->p2;
101839 + }
101840 }
101841 break;
101842 }
@@ -101247,7 +102051,7 @@ case OP_AggStep: {
102051 **
102052 ** Note: We could avoid this by using a regular memory cell from aMem[] for
102053 ** the accumulator, instead of allocating one here. */
101250 - nAlloc = ROUND8P( sizeof(pCtx[0]) + (n-1)*sizeof(sqlite3_value*) );
102054 + nAlloc = ROUND8P( SZ_CONTEXT(n) );
102055 pCtx = sqlite3DbMallocRawNN(db, nAlloc + sizeof(Mem));
102056 if( pCtx==0 ) goto no_mem;
102057 pCtx->pOut = (Mem*)((u8*)pCtx + nAlloc);
@@ -101907,6 +102711,7 @@ case OP_VFilter: { /* jump, ncycle */
102711
102712 /* Invoke the xFilter method */
102713 apArg = p->apArg;
102714 + assert( nArg<=p->napArg );
102715 for(i = 0; i<nArg; i++){
102716 apArg[i] = &pArgc[i+1];
102717 }
@@ -102117,6 +102922,7 @@ case OP_VUpdate: {
102922 u8 vtabOnConflict = db->vtabOnConflict;
102923 apArg = p->apArg;
102924 pX = &aMem[pOp->p3];
102925 + assert( nArg<=p->napArg );
102926 for(i=0; i<nArg; i++){
102927 assert( memIsValid(pX) );
102928 memAboutToChange(p, pX);
@@ -102693,8 +103499,8 @@ abort_due_to_error:
103499 p->rc = rc;
103500 sqlite3SystemError(db, rc);
103501 testcase( sqlite3GlobalConfig.xLog!=0 );
102696 - sqlite3_log(rc, "statement aborts at %d: [%s] %s",
102697 - (int)(pOp - aOp), p->zSql, p->zErrMsg);
103502 + sqlite3_log(rc, "statement aborts at %d: %s; [%s]",
103503 + (int)(pOp - aOp), p->zErrMsg, p->zSql);
103504 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
103505 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
103506 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
@@ -102903,6 +103709,7 @@ SQLITE_API int sqlite3_blob_open(
103709 char *zErr = 0;
103710 Table *pTab;
103711 Incrblob *pBlob = 0;
103712 + int iDb;
103713 Parse sParse;
103714
103715 #ifdef SQLITE_ENABLE_API_ARMOR
@@ -102948,7 +103755,10 @@ SQLITE_API int sqlite3_blob_open(
103755 sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
103756 }
103757 #endif
102951 - if( !pTab ){
103758 + if( pTab==0
103759 + || ((iDb = sqlite3SchemaToIndex(db, pTab->pSchema))==1 &&
103760 + sqlite3OpenTempDatabase(&sParse))
103761 + ){
103762 if( sParse.zErrMsg ){
103763 sqlite3DbFree(db, zErr);
103764 zErr = sParse.zErrMsg;
@@ -102959,15 +103769,11 @@ SQLITE_API int sqlite3_blob_open(
103769 goto blob_open_out;
103770 }
103771 pBlob->pTab = pTab;
102962 - pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
103772 + pBlob->zDb = db->aDb[iDb].zDbSName;
103773
103774 /* Now search pTab for the exact column. */
102965 - for(iCol=0; iCol<pTab->nCol; iCol++) {
102966 - if( sqlite3StrICmp(pTab->aCol[iCol].zCnName, zColumn)==0 ){
102967 - break;
102968 - }
102969 - }
102970 - if( iCol==pTab->nCol ){
103775 + iCol = sqlite3ColumnIndex(pTab, zColumn);
103776 + if( iCol<0 ){
103777 sqlite3DbFree(db, zErr);
103778 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
103779 rc = SQLITE_ERROR;
@@ -103047,7 +103853,6 @@ SQLITE_API int sqlite3_blob_open(
103853 {OP_Halt, 0, 0, 0}, /* 5 */
103854 };
103855 Vdbe *v = (Vdbe *)pBlob->pStmt;
103050 - int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
103856 VdbeOp *aOp;
103857
103858 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
@@ -103625,9 +104430,12 @@ struct VdbeSorter {
104430 u8 iPrev; /* Previous thread used to flush PMA */
104431 u8 nTask; /* Size of aTask[] array */
104432 u8 typeMask;
103628 - SortSubtask aTask[1]; /* One or more subtasks */
104433 + SortSubtask aTask[FLEXARRAY]; /* One or more subtasks */
104434 };
104435
104436 +/* Size (in bytes) of a VdbeSorter object that works with N or fewer subtasks */
104437 +#define SZ_VDBESORTER(N) (offsetof(VdbeSorter,aTask)+(N)*sizeof(SortSubtask))
104438 +
104439 #define SORTER_TYPE_INTEGER 0x01
104440 #define SORTER_TYPE_TEXT 0x02
104441
@@ -104229,7 +105037,7 @@ SQLITE_PRIVATE int sqlite3VdbeSorterInit(
105037 VdbeSorter *pSorter; /* The new sorter */
105038 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
105039 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
104232 - int sz; /* Size of pSorter in bytes */
105040 + i64 sz; /* Size of pSorter in bytes */
105041 int rc = SQLITE_OK;
105042 #if SQLITE_MAX_WORKER_THREADS==0
105043 # define nWorker 0
@@ -104257,8 +105065,10 @@ SQLITE_PRIVATE int sqlite3VdbeSorterInit(
105065 assert( pCsr->pKeyInfo );
105066 assert( !pCsr->isEphemeral );
105067 assert( pCsr->eCurType==CURTYPE_SORTER );
104260 - szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
104261 - sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
105068 + assert( sizeof(KeyInfo) + UMXV(pCsr->pKeyInfo->nKeyField)*sizeof(CollSeq*)
105069 + < 0x7fffffff );
105070 + szKeyInfo = SZ_KEYINFO(pCsr->pKeyInfo->nKeyField);
105071 + sz = SZ_VDBESORTER(nWorker+1);
105072
105073 pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
105074 pCsr->uc.pSorter = pSorter;
@@ -104470,7 +105280,7 @@ static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
105280 */
105281 static MergeEngine *vdbeMergeEngineNew(int nReader){
105282 int N = 2; /* Smallest power of two >= nReader */
104473 - int nByte; /* Total bytes of space to allocate */
105283 + i64 nByte; /* Total bytes of space to allocate */
105284 MergeEngine *pNew; /* Pointer to allocated object to return */
105285
105286 assert( nReader<=SORTER_MAX_MERGE_COUNT );
@@ -104722,6 +105532,10 @@ static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
105532 p->u.pNext = 0;
105533 for(i=0; aSlot[i]; i++){
105534 p = vdbeSorterMerge(pTask, p, aSlot[i]);
105535 + /* ,--Each aSlot[] holds twice as much as the previous. So we cannot use
105536 + ** | up all 64 aSlots[] with only a 64-bit address space.
105537 + ** v */
105538 + assert( i<ArraySize(aSlot) );
105539 aSlot[i] = 0;
105540 }
105541 aSlot[i] = p;
@@ -107513,7 +108327,6 @@ static int lookupName(
108327 Schema *pSchema = 0; /* Schema of the expression */
108328 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
108329 Table *pTab = 0; /* Table holding the row */
107516 - Column *pCol; /* A column of pTab */
108330 ExprList *pFJMatch = 0; /* Matches for FULL JOIN .. USING */
108331 const char *zCol = pRight->u.zToken;
108332
@@ -107564,7 +108377,6 @@ static int lookupName(
108377
108378 if( pSrcList ){
108379 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
107567 - u8 hCol;
108380 pTab = pItem->pSTab;
108381 assert( pTab!=0 && pTab->zName!=0 );
108382 assert( pTab->nCol>0 || pParse->nErr );
@@ -107652,43 +108464,38 @@ static int lookupName(
108464 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
108465 }
108466 }
107655 - hCol = sqlite3StrIHash(zCol);
107656 - for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
107657 - if( pCol->hName==hCol
107658 - && sqlite3StrICmp(pCol->zCnName, zCol)==0
107659 - ){
107660 - if( cnt>0 ){
107661 - if( pItem->fg.isUsing==0
107662 - || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
107663 - ){
107664 - /* Two or more tables have the same column name which is
107665 - ** not joined by USING. This is an error. Signal as much
107666 - ** by clearing pFJMatch and letting cnt go above 1. */
107667 - sqlite3ExprListDelete(db, pFJMatch);
107668 - pFJMatch = 0;
107669 - }else
107670 - if( (pItem->fg.jointype & JT_RIGHT)==0 ){
107671 - /* An INNER or LEFT JOIN. Use the left-most table */
107672 - continue;
107673 - }else
107674 - if( (pItem->fg.jointype & JT_LEFT)==0 ){
107675 - /* A RIGHT JOIN. Use the right-most table */
107676 - cnt = 0;
107677 - sqlite3ExprListDelete(db, pFJMatch);
107678 - pFJMatch = 0;
107679 - }else{
107680 - /* For a FULL JOIN, we must construct a coalesce() func */
107681 - extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
107682 - }
107683 - }
107684 - cnt++;
107685 - pMatch = pItem;
107686 - /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
107687 - pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
107688 - if( pItem->fg.isNestedFrom ){
107689 - sqlite3SrcItemColumnUsed(pItem, j);
108467 + j = sqlite3ColumnIndex(pTab, zCol);
108468 + if( j>=0 ){
108469 + if( cnt>0 ){
108470 + if( pItem->fg.isUsing==0
108471 + || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
108472 + ){
108473 + /* Two or more tables have the same column name which is
108474 + ** not joined by USING. This is an error. Signal as much
108475 + ** by clearing pFJMatch and letting cnt go above 1. */
108476 + sqlite3ExprListDelete(db, pFJMatch);
108477 + pFJMatch = 0;
108478 + }else
108479 + if( (pItem->fg.jointype & JT_RIGHT)==0 ){
108480 + /* An INNER or LEFT JOIN. Use the left-most table */
108481 + continue;
108482 + }else
108483 + if( (pItem->fg.jointype & JT_LEFT)==0 ){
108484 + /* A RIGHT JOIN. Use the right-most table */
108485 + cnt = 0;
108486 + sqlite3ExprListDelete(db, pFJMatch);
108487 + pFJMatch = 0;
108488 + }else{
108489 + /* For a FULL JOIN, we must construct a coalesce() func */
108490 + extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
108491 }
107691 - break;
108492 + }
108493 + cnt++;
108494 + pMatch = pItem;
108495 + /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
108496 + pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
108497 + if( pItem->fg.isNestedFrom ){
108498 + sqlite3SrcItemColumnUsed(pItem, j);
108499 }
108500 }
108501 if( 0==cnt && VisibleRowid(pTab) ){
@@ -107778,23 +108585,18 @@ static int lookupName(
108585
108586 if( pTab ){
108587 int iCol;
107781 - u8 hCol = sqlite3StrIHash(zCol);
108588 pSchema = pTab->pSchema;
108589 cntTab++;
107784 - for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
107785 - if( pCol->hName==hCol
107786 - && sqlite3StrICmp(pCol->zCnName, zCol)==0
107787 - ){
107788 - if( iCol==pTab->iPKey ){
107789 - iCol = -1;
107790 - }
107791 - break;
108590 + iCol = sqlite3ColumnIndex(pTab, zCol);
108591 + if( iCol>=0 ){
108592 + if( pTab->iPKey==iCol ) iCol = -1;
108593 + }else{
108594 + if( sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
108595 + iCol = -1;
108596 + }else{
108597 + iCol = pTab->nCol;
108598 }
108599 }
107794 - if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
107795 - /* IMP: R-51414-32910 */
107796 - iCol = -1;
107797 - }
108600 if( iCol<pTab->nCol ){
108601 cnt++;
108602 pMatch = 0;
@@ -108433,13 +109235,12 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
109235 ** sqlite_version() that might change over time cannot be used
109236 ** in an index or generated column. Curiously, they can be used
109237 ** in a CHECK constraint. SQLServer, MySQL, and PostgreSQL all
108436 - ** all this. */
109238 + ** allow this. */
109239 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
109240 NC_IdxExpr|NC_PartIdx|NC_GenCol, 0, pExpr);
109241 }else{
109242 assert( (NC_SelfRef & 0xff)==NC_SelfRef ); /* Must fit in 8 bits */
109243 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
108442 - if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
109244 }
109245 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
109246 && pParse->nested==0
@@ -108455,6 +109256,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
109256 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
109257 && !IN_RENAME_OBJECT
109258 ){
109259 + if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
109260 sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
109261 }
109262 }
@@ -109508,20 +110310,22 @@ SQLITE_PRIVATE int sqlite3ResolveSelfReference(
110310 Expr *pExpr, /* Expression to resolve. May be NULL. */
110311 ExprList *pList /* Expression list to resolve. May be NULL. */
110312 ){
109511 - SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
110313 + SrcList *pSrc; /* Fake SrcList for pParse->pNewTable */
110314 NameContext sNC; /* Name context for pParse->pNewTable */
110315 int rc;
110316 + u8 srcSpace[SZ_SRCLIST_1]; /* Memory space for the fake SrcList */
110317
110318 assert( type==0 || pTab!=0 );
110319 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr
110320 || type==NC_GenCol || pTab==0 );
110321 memset(&sNC, 0, sizeof(sNC));
109519 - memset(&sSrc, 0, sizeof(sSrc));
110322 + pSrc = (SrcList*)srcSpace;
110323 + memset(pSrc, 0, SZ_SRCLIST_1);
110324 if( pTab ){
109521 - sSrc.nSrc = 1;
109522 - sSrc.a[0].zName = pTab->zName;
109523 - sSrc.a[0].pSTab = pTab;
109524 - sSrc.a[0].iCursor = -1;
110325 + pSrc->nSrc = 1;
110326 + pSrc->a[0].zName = pTab->zName;
110327 + pSrc->a[0].pSTab = pTab;
110328 + pSrc->a[0].iCursor = -1;
110329 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
110330 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
110331 ** schema elements */
@@ -109529,7 +110333,7 @@ SQLITE_PRIVATE int sqlite3ResolveSelfReference(
110333 }
110334 }
110335 sNC.pParse = pParse;
109532 - sNC.pSrcList = &sSrc;
110336 + sNC.pSrcList = pSrc;
110337 sNC.ncFlags = type | NC_IsDDL;
110338 if( (rc = sqlite3ResolveExprNames(&sNC, pExpr))!=SQLITE_OK ) return rc;
110339 if( pList ) rc = sqlite3ResolveExprListNames(&sNC, pList);
@@ -109613,7 +110417,9 @@ SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr){
110417 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
110418 );
110419 }
109616 - if( op==TK_VECTOR ){
110420 + if( op==TK_VECTOR
110421 + || (op==TK_FUNCTION && pExpr->affExpr==SQLITE_AFF_DEFER)
110422 + ){
110423 assert( ExprUseXList(pExpr) );
110424 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
110425 }
@@ -109806,7 +110612,9 @@ SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr){
110612 p = p->pLeft;
110613 continue;
110614 }
109809 - if( op==TK_VECTOR ){
110615 + if( op==TK_VECTOR
110616 + || (op==TK_FUNCTION && p->affExpr==SQLITE_AFF_DEFER)
110617 + ){
110618 assert( ExprUseXList(p) );
110619 p = p->x.pList->a[0].pExpr;
110620 continue;
@@ -111278,7 +112086,7 @@ static Expr *exprDup(
112086 SQLITE_PRIVATE With *sqlite3WithDup(sqlite3 *db, With *p){
112087 With *pRet = 0;
112088 if( p ){
111281 - sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
112089 + sqlite3_int64 nByte = SZ_WITH(p->nCte);
112090 pRet = sqlite3DbMallocZero(db, nByte);
112091 if( pRet ){
112092 int i;
@@ -111389,7 +112197,6 @@ SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, const ExprList *p, int
112197 }
112198 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
112199 pItem->fg = pOldItem->fg;
111392 - pItem->fg.done = 0;
112200 pItem->u = pOldItem->u;
112201 }
112202 return pNew;
@@ -111406,11 +112213,9 @@ SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, const ExprList *p, int
112213 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, const SrcList *p, int flags){
112214 SrcList *pNew;
112215 int i;
111409 - int nByte;
112216 assert( db!=0 );
112217 if( p==0 ) return 0;
111412 - nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
111413 - pNew = sqlite3DbMallocRawNN(db, nByte );
112218 + pNew = sqlite3DbMallocRawNN(db, SZ_SRCLIST(p->nSrc) );
112219 if( pNew==0 ) return 0;
112220 pNew->nSrc = pNew->nAlloc = p->nSrc;
112221 for(i=0; i<p->nSrc; i++){
@@ -111472,7 +112277,7 @@ SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, const IdList *p){
112277 int i;
112278 assert( db!=0 );
112279 if( p==0 ) return 0;
111475 - pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) );
112280 + pNew = sqlite3DbMallocRawNN(db, SZ_IDLIST(p->nId));
112281 if( pNew==0 ) return 0;
112282 pNew->nId = p->nId;
112283 for(i=0; i<p->nId; i++){
@@ -111504,7 +112309,7 @@ SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, const Select *pDup, int fla
112309 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
112310 pNew->iLimit = 0;
112311 pNew->iOffset = 0;
111507 - pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
112312 + pNew->selFlags = p->selFlags & ~(u32)SF_UsesEphemeral;
112313 pNew->addrOpenEphm[0] = -1;
112314 pNew->addrOpenEphm[1] = -1;
112315 pNew->nSelectRow = p->nSelectRow;
@@ -111556,7 +112361,7 @@ SQLITE_PRIVATE SQLITE_NOINLINE ExprList *sqlite3ExprListAppendNew(
112361 struct ExprList_item *pItem;
112362 ExprList *pList;
112363
111559 - pList = sqlite3DbMallocRawNN(db, sizeof(ExprList)+sizeof(pList->a[0])*4 );
112364 + pList = sqlite3DbMallocRawNN(db, SZ_EXPRLIST(4));
112365 if( pList==0 ){
112366 sqlite3ExprDelete(db, pExpr);
112367 return 0;
@@ -111576,8 +112381,7 @@ SQLITE_PRIVATE SQLITE_NOINLINE ExprList *sqlite3ExprListAppendGrow(
112381 struct ExprList_item *pItem;
112382 ExprList *pNew;
112383 pList->nAlloc *= 2;
111579 - pNew = sqlite3DbRealloc(db, pList,
111580 - sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
112384 + pNew = sqlite3DbRealloc(db, pList, SZ_EXPRLIST(pList->nAlloc));
112385 if( pNew==0 ){
112386 sqlite3ExprListDelete(db, pList);
112387 sqlite3ExprDelete(db, pExpr);
@@ -112506,13 +113310,7 @@ SQLITE_PRIVATE const char *sqlite3RowidAlias(Table *pTab){
113310 int ii;
113311 assert( VisibleRowid(pTab) );
113312 for(ii=0; ii<ArraySize(azOpt); ii++){
112509 - int iCol;
112510 - for(iCol=0; iCol<pTab->nCol; iCol++){
112511 - if( sqlite3_stricmp(azOpt[ii], pTab->aCol[iCol].zCnName)==0 ) break;
112512 - }
112513 - if( iCol==pTab->nCol ){
112514 - return azOpt[ii];
112515 - }
113313 + if( sqlite3ColumnIndex(pTab, azOpt[ii])<0 ) return azOpt[ii];
113314 }
113315 return 0;
113316 }
@@ -112916,7 +113714,7 @@ static char *exprINAffinity(Parse *pParse, const Expr *pExpr){
113714 char *zRet;
113715
113716 assert( pExpr->op==TK_IN );
112919 - zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
113717 + zRet = sqlite3DbMallocRaw(pParse->db, 1+(i64)nVal);
113718 if( zRet ){
113719 int i;
113720 for(i=0; i<nVal; i++){
@@ -113176,11 +113974,12 @@ SQLITE_PRIVATE void sqlite3CodeRhsOfIN(
113974 sqlite3SelectDelete(pParse->db, pCopy);
113975 sqlite3DbFree(pParse->db, dest.zAffSdst);
113976 if( addrBloom ){
113977 + /* Remember that location of the Bloom filter in the P3 operand
113978 + ** of the OP_Once that began this subroutine. tag-202407032019 */
113979 sqlite3VdbeGetOp(v, addrOnce)->p3 = dest.iSDParm2;
113980 if( dest.iSDParm2==0 ){
113181 - sqlite3VdbeChangeToNoop(v, addrBloom);
113182 - }else{
113183 - sqlite3VdbeGetOp(v, addrOnce)->p3 = dest.iSDParm2;
113981 + /* If the Bloom filter won't actually be used, keep it small */
113982 + sqlite3VdbeGetOp(v, addrBloom)->p1 = 10;
113983 }
113984 }
113985 if( rc ){
@@ -113627,7 +114426,7 @@ static void sqlite3ExprCodeIN(
114426 if( ExprHasProperty(pExpr, EP_Subrtn) ){

This file is too large to show in full.

src/database/sqlite/vendored/sqlite3.h
+102 -44
@@ -133,7 +133,7 @@ extern "C" {
133 **
134 ** Since [version 3.6.18] ([dateof:3.6.18]),
135 ** SQLite source code has been stored in the
136 -** <a href="http://www.fossil-scm.org/">Fossil configuration management
136 +** <a href="http://fossil-scm.org/">Fossil configuration management
137 ** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
138 ** a string which identifies a particular check-in of SQLite
139 ** within its configuration management system. ^The SQLITE_SOURCE_ID
@@ -146,9 +146,9 @@ extern "C" {
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 -#define SQLITE_VERSION "3.49.1"
150 -#define SQLITE_VERSION_NUMBER 3049001
151 -#define SQLITE_SOURCE_ID "2025-02-18 13:38:58 873d4e274b4988d260ba8354a9718324a1c26187a4ab4c1cc0227c03d0f10e70"
149 +#define SQLITE_VERSION "3.50.1"
150 +#define SQLITE_VERSION_NUMBER 3050001
151 +#define SQLITE_SOURCE_ID "2025-06-06 14:52:32 b77dc5e0f596d2140d9ac682b2893ff65d3a4140aa86067a3efebe29dc914c95"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
@@ -1163,6 +1163,12 @@ struct sqlite3_io_methods {
1163 ** the value that M is to be set to. Before returning, the 32-bit signed
1164 ** integer is overwritten with the previous value of M.
1165 **
1166 +** <li>[[SQLITE_FCNTL_BLOCK_ON_CONNECT]]
1167 +** The [SQLITE_FCNTL_BLOCK_ON_CONNECT] opcode is used to configure the
1168 +** VFS to block when taking a SHARED lock to connect to a wal mode database.
1169 +** This is used to implement the functionality associated with
1170 +** SQLITE_SETLK_BLOCK_ON_CONNECT.
1171 +**
1172 ** <li>[[SQLITE_FCNTL_DATA_VERSION]]
1173 ** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
1174 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
@@ -1259,6 +1265,7 @@ struct sqlite3_io_methods {
1265 #define SQLITE_FCNTL_CKSM_FILE 41
1266 #define SQLITE_FCNTL_RESET_CACHE 42
1267 #define SQLITE_FCNTL_NULL_IO 43
1268 +#define SQLITE_FCNTL_BLOCK_ON_CONNECT 44
1269
1270 /* deprecated names */
1271 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
@@ -1989,13 +1996,16 @@ struct sqlite3_mem_methods {
1996 **
1997 ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1998 ** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
1992 -** the default size of lookaside memory on each [database connection].
1999 +** the default size of [lookaside memory] on each [database connection].
2000 ** The first argument is the
1994 -** size of each lookaside buffer slot and the second is the number of
1995 -** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
1996 -** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1997 -** option to [sqlite3_db_config()] can be used to change the lookaside
1998 -** configuration on individual connections.)^ </dd>
2001 +** size of each lookaside buffer slot ("sz") and the second is the number of
2002 +** slots allocated to each database connection ("cnt").)^
2003 +** ^(SQLITE_CONFIG_LOOKASIDE sets the <i>default</i> lookaside size.
2004 +** The [SQLITE_DBCONFIG_LOOKASIDE] option to [sqlite3_db_config()] can
2005 +** be used to change the lookaside configuration on individual connections.)^
2006 +** The [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to change the
2007 +** default lookaside configuration at compile-time.
2008 +** </dd>
2009 **
2010 ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2011 ** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
@@ -2232,31 +2242,50 @@ struct sqlite3_mem_methods {
2242 ** [[SQLITE_DBCONFIG_LOOKASIDE]]
2243 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
2244 ** <dd> The SQLITE_DBCONFIG_LOOKASIDE option is used to adjust the
2235 -** configuration of the lookaside memory allocator within a database
2245 +** configuration of the [lookaside memory allocator] within a database
2246 ** connection.
2247 ** The arguments to the SQLITE_DBCONFIG_LOOKASIDE option are <i>not</i>
2248 ** in the [DBCONFIG arguments|usual format].
2249 ** The SQLITE_DBCONFIG_LOOKASIDE option takes three arguments, not two,
2250 ** so that a call to [sqlite3_db_config()] that uses SQLITE_DBCONFIG_LOOKASIDE
2251 ** should have a total of five parameters.
2242 -** ^The first argument (the third parameter to [sqlite3_db_config()] is a
2252 +** <ol>
2253 +** <li><p>The first argument ("buf") is a
2254 ** pointer to a memory buffer to use for lookaside memory.
2244 -** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
2245 -** may be NULL in which case SQLite will allocate the
2246 -** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
2247 -** size of each lookaside buffer slot. ^The third argument is the number of
2248 -** slots. The size of the buffer in the first argument must be greater than
2249 -** or equal to the product of the second and third arguments. The buffer
2250 -** must be aligned to an 8-byte boundary. ^If the second argument to
2251 -** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
2252 -** rounded down to the next smaller multiple of 8. ^(The lookaside memory
2255 +** The first argument may be NULL in which case SQLite will allocate the
2256 +** lookaside buffer itself using [sqlite3_malloc()].
2257 +** <li><P>The second argument ("sz") is the
2258 +** size of each lookaside buffer slot. Lookaside is disabled if "sz"
2259 +** is less than 8. The "sz" argument should be a multiple of 8 less than
2260 +** 65536. If "sz" does not meet this constraint, it is reduced in size until
2261 +** it does.
2262 +** <li><p>The third argument ("cnt") is the number of slots. Lookaside is disabled
2263 +** if "cnt"is less than 1. The "cnt" value will be reduced, if necessary, so
2264 +** that the product of "sz" and "cnt" does not exceed 2,147,418,112. The "cnt"
2265 +** parameter is usually chosen so that the product of "sz" and "cnt" is less
2266 +** than 1,000,000.
2267 +** </ol>
2268 +** <p>If the "buf" argument is not NULL, then it must
2269 +** point to a memory buffer with a size that is greater than
2270 +** or equal to the product of "sz" and "cnt".
2271 +** The buffer must be aligned to an 8-byte boundary.
2272 +** The lookaside memory
2273 ** configuration for a database connection can only be changed when that
2274 ** connection is not currently using lookaside memory, or in other words
2255 -** when the "current value" returned by
2256 -** [sqlite3_db_status](D,[SQLITE_DBSTATUS_LOOKASIDE_USED],...) is zero.
2275 +** when the value returned by [SQLITE_DBSTATUS_LOOKASIDE_USED] is zero.
2276 ** Any attempt to change the lookaside memory configuration when lookaside
2277 ** memory is in use leaves the configuration unchanged and returns
2259 -** [SQLITE_BUSY].)^</dd>
2278 +** [SQLITE_BUSY].
2279 +** If the "buf" argument is NULL and an attempt
2280 +** to allocate memory based on "sz" and "cnt" fails, then
2281 +** lookaside is silently disabled.
2282 +** <p>
2283 +** The [SQLITE_CONFIG_LOOKASIDE] configuration option can be used to set the
2284 +** default lookaside configuration at initialization. The
2285 +** [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to set the default lookaside
2286 +** configuration at compile-time. Typical values for lookaside are 1200 for
2287 +** "sz" and 40 to 100 for "cnt".
2288 +** </dd>
2289 **
2290 ** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
2291 ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
@@ -2993,6 +3022,44 @@ SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3022 */
3023 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3024
3025 +/*
3026 +** CAPI3REF: Set the Setlk Timeout
3027 +** METHOD: sqlite3
3028 +**
3029 +** This routine is only useful in SQLITE_ENABLE_SETLK_TIMEOUT builds. If
3030 +** the VFS supports blocking locks, it sets the timeout in ms used by
3031 +** eligible locks taken on wal mode databases by the specified database
3032 +** handle. In non-SQLITE_ENABLE_SETLK_TIMEOUT builds, or if the VFS does
3033 +** not support blocking locks, this function is a no-op.
3034 +**
3035 +** Passing 0 to this function disables blocking locks altogether. Passing
3036 +** -1 to this function requests that the VFS blocks for a long time -
3037 +** indefinitely if possible. The results of passing any other negative value
3038 +** are undefined.
3039 +**
3040 +** Internally, each SQLite database handle store two timeout values - the
3041 +** busy-timeout (used for rollback mode databases, or if the VFS does not
3042 +** support blocking locks) and the setlk-timeout (used for blocking locks
3043 +** on wal-mode databases). The sqlite3_busy_timeout() method sets both
3044 +** values, this function sets only the setlk-timeout value. Therefore,
3045 +** to configure separate busy-timeout and setlk-timeout values for a single
3046 +** database handle, call sqlite3_busy_timeout() followed by this function.
3047 +**
3048 +** Whenever the number of connections to a wal mode database falls from
3049 +** 1 to 0, the last connection takes an exclusive lock on the database,
3050 +** then checkpoints and deletes the wal file. While it is doing this, any
3051 +** new connection that tries to read from the database fails with an
3052 +** SQLITE_BUSY error. Or, if the SQLITE_SETLK_BLOCK_ON_CONNECT flag is
3053 +** passed to this API, the new connection blocks until the exclusive lock
3054 +** has been released.
3055 +*/
3056 +SQLITE_API int sqlite3_setlk_timeout(sqlite3*, int ms, int flags);
3057 +
3058 +/*
3059 +** CAPI3REF: Flags for sqlite3_setlk_timeout()
3060 +*/
3061 +#define SQLITE_SETLK_BLOCK_ON_CONNECT 0x01
3062 +
3063 /*
3064 ** CAPI3REF: Convenience Routines For Running Queries
3065 ** METHOD: sqlite3
@@ -5108,7 +5175,7 @@ SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5175 ** other than [SQLITE_ROW] before any subsequent invocation of
5176 ** sqlite3_step(). Failure to reset the prepared statement using
5177 ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
5111 -** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1],
5178 +** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1]),
5179 ** sqlite3_step() began
5180 ** calling [sqlite3_reset()] automatically in this circumstance rather
5181 ** than returning [SQLITE_MISUSE]. This is not considered a compatibility
@@ -7004,6 +7071,8 @@ SQLITE_API int sqlite3_autovacuum_pages(
7071 **
7072 ** ^The second argument is a pointer to the function to invoke when a
7073 ** row is updated, inserted or deleted in a rowid table.
7074 +** ^The update hook is disabled by invoking sqlite3_update_hook()
7075 +** with a NULL pointer as the second parameter.
7076 ** ^The first argument to the callback is a copy of the third argument
7077 ** to sqlite3_update_hook().
7078 ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
@@ -11486,9 +11555,10 @@ SQLITE_API void sqlite3session_table_filter(
11555 ** is inserted while a session object is enabled, then later deleted while
11556 ** the same session object is disabled, no INSERT record will appear in the
11557 ** changeset, even though the delete took place while the session was disabled.
11489 -** Or, if one field of a row is updated while a session is disabled, and
11490 -** another field of the same row is updated while the session is enabled, the
11491 -** resulting changeset will contain an UPDATE change that updates both fields.
11558 +** Or, if one field of a row is updated while a session is enabled, and
11559 +** then another field of the same row is updated while the session is disabled,
11560 +** the resulting changeset will contain an UPDATE change that updates both
11561 +** fields.
11562 */
11563 SQLITE_API int sqlite3session_changeset(
11564 sqlite3_session *pSession, /* Session object */
@@ -11560,8 +11630,9 @@ SQLITE_API sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession
11630 ** database zFrom the contents of the two compatible tables would be
11631 ** identical.
11632 **
11563 -** It an error if database zFrom does not exist or does not contain the
11564 -** required compatible table.
11633 +** Unless the call to this function is a no-op as described above, it is an
11634 +** error if database zFrom does not exist or does not contain the required
11635 +** compatible table.
11636 **
11637 ** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
11638 ** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
@@ -11696,7 +11767,7 @@ SQLITE_API int sqlite3changeset_start_v2(
11767 ** The following flags may passed via the 4th parameter to
11768 ** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
11769 **
11699 -** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
11770 +** <dt>SQLITE_CHANGESETSTART_INVERT <dd>
11771 ** Invert the changeset while iterating through it. This is equivalent to
11772 ** inverting a changeset using sqlite3changeset_invert() before applying it.
11773 ** It is an error to specify this flag with a patchset.
@@ -12011,19 +12082,6 @@ SQLITE_API int sqlite3changeset_concat(
12082 void **ppOut /* OUT: Buffer containing output changeset */
12083 );
12084
12014 -
12015 -/*
12016 -** CAPI3REF: Upgrade the Schema of a Changeset/Patchset
12017 -*/
12018 -SQLITE_API int sqlite3changeset_upgrade(
12019 - sqlite3 *db,
12020 - const char *zDb,
12021 - int nIn, const void *pIn, /* Input changeset */
12022 - int *pnOut, void **ppOut /* OUT: Inverse of input */
12023 -);
12024 -
12025 -
12026 -
12085 /*
12086 ** CAPI3REF: Changegroup Handle
12087 **