From b23f61b73fcbb9e3e369b222c7c3559280a4f720 Mon Sep 17 00:00:00 2001 From: drh <> Date: Sat, 20 Jan 2024 16:29:19 +0000 Subject: [PATCH 1/5] Rig sqlite3_serialize() so that it will initialize a previously uninitialized database prior to serializing it, so that it does not have a zero-byte size and does not return NULL (except for OOM). [forum:/forumpost/498777780e16880a|Forum thread 498777780e16880a]. FossilOrigin-Name: e638d5e408ea2e189b6771d16bbc2e42c606e88e05fbea78079b6e39e41f344c --- manifest | 14 +++++++------- manifest.uuid | 2 +- src/memdb.c | 8 ++++++++ test/memdb1.test | 12 ++++++++++++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/manifest b/manifest index 320c5bbcb4..a4427cb088 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C When\sdoing\sa\stext-affinity\scomparison\sbetween\stwo\svalues\swhere\sone\sor\sboth\nhave\sboth\sa\stext\sand\sa\snumeric\stype,\smake\ssure\sthe\snumeric\stype\sdoes\snot\nconfuse\sthe\sanswer.\s\sThis\sis\sa\sdeeper\sfix\sto\sthe\sproblem\sobserved\sby\n[forum:/forumpost/3776b48e71|forum\spose\s3776b48e71].\s\sThe\sproblem\sbisects\nto\s[25f2246be404f38b]\son\s2014-08-24,\sprior\sto\sversion\s3.8.7. -D 2024-01-20T15:13:13.896 +C Rig\ssqlite3_serialize()\sso\sthat\sit\swill\sinitialize\sa\spreviously\suninitialized\ndatabase\sprior\sto\sserializing\sit,\sso\sthat\sit\sdoes\snot\shave\sa\szero-byte\ssize\nand\sdoes\snot\sreturn\sNULL\s(except\sfor\sOOM).\n[forum:/forumpost/498777780e16880a|Forum\sthread\s498777780e16880a]. +D 2024-01-20T16:29:19.033 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -708,7 +708,7 @@ F src/mem1.c 3bb59158c38e05f6270e761a9f435bf19827a264c13d1631c58b84bdc96d73b2 F src/mem2.c c8bfc9446fd0798bddd495eb5d9dbafa7d4b7287d8c22d50a83ac9daa26d8a75 F src/mem3.c 30301196cace2a085cbedee1326a49f4b26deff0af68774ca82c1f7c06fda4f6 F src/mem5.c b7da5c10a726aacacc9ad7cdcb0667deec643e117591cc69cf9b4b9e7f3e96ff -F src/memdb.c 559c42e61eb70cd6d4bc692b042497133c6d96c09a3d514d92f3dac72268e223 +F src/memdb.c 16679def118b5fd75292a253166d3feba3ec9c6189205bf209643ecdb2174ecc F src/memjournal.c c283c6c95d940eb9dc70f1863eef3ee40382dbd35e5a1108026e7817c206e8a0 F src/msvc.h 80b35f95d93bf996ccb3e498535255f2ef1118c78764719a7cd15ab4106ccac9 F src/mutex.c 1b4c7e5e3621b510e0c18397210be27cd54c8084141144fbbafd003fde948e88 @@ -1396,7 +1396,7 @@ F test/malloctraceviewer.tcl 3e3ddf11e30d2b20f53aa16aa6615082fb24a100bea61cca721 F test/manydb.test 28385ae2087967aa05c38624cec7d96ec74feb3e F test/mem5.test c6460fba403c5703141348cd90de1c294188c68f F test/memdb.test c1f2a343ad14398d5d6debda6ea33e80d0dafcc7 -F test/memdb1.test 2c4e9cc10d21c6bf4e217d72b7f6b8ba9b2605971bb2c5e6df76018e189f98f5 +F test/memdb1.test bef8623da0c0c2d8d4e0426dee5a52008a5714860901f8bca7638830b2645dbe F test/memdb2.test 4ba1fc09e2f51df80d148a540e4a3fa66d0462e91167b27497084de4d1f6b5b4 F test/memjournal.test 70f3a00c7f84ee2978ad14e831231caa1e7f23915a2c54b4f775a021d5740c6c F test/memjournal2.test dbc2c5cb5f7b38950f4f6dc3e73fcecf0fcbed3fc32c7ce913bba164d288da1e @@ -2159,8 +2159,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 01868ebcd25fadb2034da234c0636e82d07c5abc902ef66493cadfc988e74d7b -R c017e3ff6a3cf5fef0c29004a344e6f7 +P 709841f88c77276f09701bf38e25503c64b3a0afbe2fbf878136db12f31cbe21 +R 6b35ed1d4541fe33e619d0defc51d4d4 U drh -Z f4a73b0af78f34dd85f781f1eb7187c3 +Z 0850c6fe7ffc6bdccabb75e3570a33ee # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index d23f19cb29..2ff1ab4571 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -709841f88c77276f09701bf38e25503c64b3a0afbe2fbf878136db12f31cbe21 \ No newline at end of file +e638d5e408ea2e189b6771d16bbc2e42c606e88e05fbea78079b6e39e41f344c \ No newline at end of file diff --git a/src/memdb.c b/src/memdb.c index 657cb9ca65..d83a51d54d 100644 --- a/src/memdb.c +++ b/src/memdb.c @@ -799,6 +799,14 @@ unsigned char *sqlite3_serialize( pOut = 0; }else{ sz = sqlite3_column_int64(pStmt, 0)*szPage; + if( sz==0 ){ + sqlite3_reset(pStmt); + sqlite3_exec(db, "BEGIN IMMEDIATE; COMMIT;", 0, 0, 0); + rc = sqlite3_step(pStmt); + if( rc==SQLITE_ROW ){ + sz = sqlite3_column_int64(pStmt, 0)*szPage; + } + } if( piSize ) *piSize = sz; if( mFlags & SQLITE_SERIALIZE_NOCOPY ){ pOut = 0; diff --git a/test/memdb1.test b/test/memdb1.test index 5e219a4c01..05d9fa04ed 100644 --- a/test/memdb1.test +++ b/test/memdb1.test @@ -267,4 +267,16 @@ if {[wal_is_capable]} { } {1 {database disk image is malformed}} } +# 2024-01-20 +# https://sqlite.org/forum/forumpost/498777780e16880a +# +# Make sure a database is initialized before serializing it. +# +reset_db +sqlite3 dbempty :memory: +do_test 900 { + set len [string length [dbempty serialize]] + expr {$len>0} +} 1 + finish_test From 2e12890732f8c23719a46e56175323cf7d1daccb Mon Sep 17 00:00:00 2001 From: drh <> Date: Sat, 20 Jan 2024 16:38:00 +0000 Subject: [PATCH 2/5] Make sure that %V and %G are testing for every since day in between 1970-01-01 and 2023-01-19. FossilOrigin-Name: 39c475f5fb86526622a715860385f0a3a4b2c18d8ef209779c423527e0639385 --- manifest | 12 ++++++------ manifest.uuid | 2 +- test/date4.test | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/manifest b/manifest index a4427cb088..a8079c2744 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Rig\ssqlite3_serialize()\sso\sthat\sit\swill\sinitialize\sa\spreviously\suninitialized\ndatabase\sprior\sto\sserializing\sit,\sso\sthat\sit\sdoes\snot\shave\sa\szero-byte\ssize\nand\sdoes\snot\sreturn\sNULL\s(except\sfor\sOOM).\n[forum:/forumpost/498777780e16880a|Forum\sthread\s498777780e16880a]. -D 2024-01-20T16:29:19.033 +C Make\ssure\sthat\s%V\sand\s%G\sare\stesting\sfor\severy\ssince\sday\sin\sbetween\s1970-01-01\nand\s2023-01-19. +D 2024-01-20T16:38:00.290 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -1033,7 +1033,7 @@ F test/dataversion1.test 6e5e86ac681f0782e766ebcb56c019ae001522d114e0e111e5ebf68 F test/date.test b1a5208e6a9c390cf1d1fa4e38a910386b427b0e27046901c6b0fb995d19800e F test/date2.test 7e12ec14aaf4d5e6294b4ba140445b0eca06ea50062a9c3a69c4ee13d0b6f8b1 F test/date3.test a1b77abf05c6772fe5ca2337cac1398892f2a41e62bce7e6be0f4a08a0e64ae5 -F test/date4.test 77b3781c28d3a5e181c6474315aa58e1f6cf79f5171a4e46e248837b506e7adf +F test/date4.test 75dc8401e8c0639a228cd26a6eaa4ff5ea8ccda912b9853d1c9462c476670e17 F test/dbdata.test 042f49acff3438f940eeba5868d3af080ae64ddf26ae78f80c92bec3ca7d8603 F test/dbfuzz.c 73047c920d6210e5912c87cdffd9a1c281d4252e F test/dbfuzz001.test 6c9a4622029d69dc38926f115864b055cb2f39badd25ec22cbfb130c8ba8e9c3 @@ -2159,8 +2159,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 709841f88c77276f09701bf38e25503c64b3a0afbe2fbf878136db12f31cbe21 -R 6b35ed1d4541fe33e619d0defc51d4d4 +P e638d5e408ea2e189b6771d16bbc2e42c606e88e05fbea78079b6e39e41f344c +R def255443f75553b73e601b4d3401144 U drh -Z 0850c6fe7ffc6bdccabb75e3570a33ee +Z f250ebbbb7ead57b6a863fb7b3b9221e # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 2ff1ab4571..d3659e6025 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -e638d5e408ea2e189b6771d16bbc2e42c606e88e05fbea78079b6e39e41f344c \ No newline at end of file +39c475f5fb86526622a715860385f0a3a4b2c18d8ef209779c423527e0639385 \ No newline at end of file diff --git a/test/date4.test b/test/date4.test index 0dd06debb7..56a9090b1b 100644 --- a/test/date4.test +++ b/test/date4.test @@ -28,8 +28,8 @@ if {$tcl_platform(os)=="Linux"} { } else { set FMT {%d,%e,%F,%H,%I,%j,%p,%R,%u,%w,%W,%%} } -for {set i 0} {$i<=24854} {incr i} { - set TS [expr {$i*86401}] +for {set i 0} {$i<=24858} {incr i} { + set TS [expr {$i*86390}] do_execsql_test date4-$i { SELECT strftime($::FMT,$::TS,'unixepoch'); } [list [strftime $FMT $TS]] From 6c64e24f9d0675095a2c67a025e3db7bf4636821 Mon Sep 17 00:00:00 2001 From: drh <> Date: Sat, 20 Jan 2024 18:21:50 +0000 Subject: [PATCH 3/5] Fix a memory leak in new memdb1.test test cases that were added by [e638d5e408ea2e18]. No changes to SQLite itself. FossilOrigin-Name: bb2b7a658e0186d8b09199170db17744536602d8282b83c71ed969494d2ca465 --- manifest | 12 ++++++------ manifest.uuid | 2 +- test/memdb1.test | 1 + 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/manifest b/manifest index a8079c2744..165799a685 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Make\ssure\sthat\s%V\sand\s%G\sare\stesting\sfor\severy\ssince\sday\sin\sbetween\s1970-01-01\nand\s2023-01-19. -D 2024-01-20T16:38:00.290 +C Fix\sa\smemory\sleak\sin\snew\smemdb1.test\stest\scases\sthat\swere\sadded\sby\n[e638d5e408ea2e18].\s\sNo\schanges\sto\sSQLite\sitself. +D 2024-01-20T18:21:50.590 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -1396,7 +1396,7 @@ F test/malloctraceviewer.tcl 3e3ddf11e30d2b20f53aa16aa6615082fb24a100bea61cca721 F test/manydb.test 28385ae2087967aa05c38624cec7d96ec74feb3e F test/mem5.test c6460fba403c5703141348cd90de1c294188c68f F test/memdb.test c1f2a343ad14398d5d6debda6ea33e80d0dafcc7 -F test/memdb1.test bef8623da0c0c2d8d4e0426dee5a52008a5714860901f8bca7638830b2645dbe +F test/memdb1.test cd6b6b1632cf2f76c8a6f35be013f4011f3ab2c8d74d3d1424576f29bfa4a9a1 F test/memdb2.test 4ba1fc09e2f51df80d148a540e4a3fa66d0462e91167b27497084de4d1f6b5b4 F test/memjournal.test 70f3a00c7f84ee2978ad14e831231caa1e7f23915a2c54b4f775a021d5740c6c F test/memjournal2.test dbc2c5cb5f7b38950f4f6dc3e73fcecf0fcbed3fc32c7ce913bba164d288da1e @@ -2159,8 +2159,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P e638d5e408ea2e189b6771d16bbc2e42c606e88e05fbea78079b6e39e41f344c -R def255443f75553b73e601b4d3401144 +P 39c475f5fb86526622a715860385f0a3a4b2c18d8ef209779c423527e0639385 +R 74384040736e0977d7c5dfb4dce5d968 U drh -Z f250ebbbb7ead57b6a863fb7b3b9221e +Z 312b96149522d237d108c8a3641b64b6 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index d3659e6025..0ffeb5f5fe 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -39c475f5fb86526622a715860385f0a3a4b2c18d8ef209779c423527e0639385 \ No newline at end of file +bb2b7a658e0186d8b09199170db17744536602d8282b83c71ed969494d2ca465 \ No newline at end of file diff --git a/test/memdb1.test b/test/memdb1.test index 05d9fa04ed..e6820da4a2 100644 --- a/test/memdb1.test +++ b/test/memdb1.test @@ -278,5 +278,6 @@ do_test 900 { set len [string length [dbempty serialize]] expr {$len>0} } 1 +dbempty close finish_test From 514bf99b6aa18f05f78d02b9b3afddc51d006eca Mon Sep 17 00:00:00 2001 From: drh <> Date: Sat, 20 Jan 2024 18:26:28 +0000 Subject: [PATCH 4/5] Simplifications to the strftime() logic. FossilOrigin-Name: aaa5a044d8a4942278eb4269fa8c7252f9f1dc161408e618e7c97c55a12283ff --- manifest | 12 +++++----- manifest.uuid | 2 +- src/date.c | 64 ++++++++++++++++++++++++++++++--------------------- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/manifest b/manifest index 165799a685..821d38be63 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sa\smemory\sleak\sin\snew\smemdb1.test\stest\scases\sthat\swere\sadded\sby\n[e638d5e408ea2e18].\s\sNo\schanges\sto\sSQLite\sitself. -D 2024-01-20T18:21:50.590 +C Simplifications\sto\sthe\sstrftime()\slogic. +D 2024-01-20T18:26:28.712 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -684,7 +684,7 @@ F src/build.c e7d9044592eeeea8e78d8ae53ca8d31fd6e92ca0d4f53e2f2e8ccf7352e0b04b F src/callback.c db3a45e376deff6a16c0058163fe0ae2b73a2945f3f408ca32cf74960b28d490 F src/complete.c a3634ab1e687055cd002e11b8f43eb75c17da23e F src/ctime.c 23331529e654be40ca97d171cbbffe9b3d4c71cc53b78fe5501230675952da8b -F src/date.c 35ea61eb7ab5b918db7a96725346234bf8fb5bc161c7b868c20a7f24f351a2da +F src/date.c 48a110a2a267398ad9b04fe3db69dfa194f91719f94fa55cd560e03510f963e6 F src/dbpage.c 80e46e1df623ec40486da7a5086cb723b0275a6e2a7b01d9f9b5da0f04ba2782 F src/dbstat.c 3b677254d512fcafd4d0b341bf267b38b235ccfddbef24f9154e19360fa22e43 F src/delete.c cb766727c78e715f9fb7ec8a7d03658ed2a3016343ca687acfcec9083cdca500 @@ -2159,8 +2159,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 39c475f5fb86526622a715860385f0a3a4b2c18d8ef209779c423527e0639385 -R 74384040736e0977d7c5dfb4dce5d968 +P bb2b7a658e0186d8b09199170db17744536602d8282b83c71ed969494d2ca465 +R 8518c96f5a8faebc2a9945073340f70a U drh -Z 312b96149522d237d108c8a3641b64b6 +Z f670e53a135b10311c59f478b3527211 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 0ffeb5f5fe..e3a0693990 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -bb2b7a658e0186d8b09199170db17744536602d8282b83c71ed969494d2ca465 \ No newline at end of file +aaa5a044d8a4942278eb4269fa8c7252f9f1dc161408e618e7c97c55a12283ff \ No newline at end of file diff --git a/src/date.c b/src/date.c index 76e1894f8d..e468925d5e 100644 --- a/src/date.c +++ b/src/date.c @@ -1237,10 +1237,14 @@ static void dateFunc( } /* -** Compute the one-based day of the year for the DateTime pDate. -** Jan01 = 1, Jan02 = 2, ... Dec31 = 356 or 366. +** Compute the number of days after the most recent January 1. +** +** In other words, compute the zero-based day number for the +** current year: +** +** Jan01 = 0, Jan02 = 2, ... Dec31 = 364 or 365. */ -static int dayOfYear(DateTime *pDate){ +static int daysAfterJan01(DateTime *pDate){ DateTime jan01 = *pDate; assert( jan01.validYMD ); assert( jan01.validHMS ); @@ -1249,18 +1253,33 @@ static int dayOfYear(DateTime *pDate){ jan01.M = 1; jan01.D = 1; computeJD(&jan01); - return (int)((pDate->iJD-jan01.iJD+43200000)/86400000) + 1; + return (int)((pDate->iJD-jan01.iJD+43200000)/86400000); } /* -** Return the day of the week. 1==Monday, 2=Tues, ..., 7=Sunday. +** Return the number of days after the most recent Monday. +** +** In other words, return the day of the week according +** to this code: +** +** 0=Monday, 1=Tuesday, 2=Wednesday, ..., 6=Sunday. */ -static int dayOfWeek(DateTime *pDate){ - int w; +static int daysAfterMonday(DateTime *pDate){ assert( pDate->validJD ); - w = ((pDate->iJD+129600000)/86400000) % 7; - if( w==0 ) w = 7; - return w; + return (int)((pDate->iJD+43200000)/86400000) % 7; +} + +/* +** Return the number of days after the most recent Sunday. +** +** In other words, return the day of the week according +** to this code: +** +** 0=Sunday, 1=Monday, 2=Tues, ..., 6=Saturday +*/ +static int daysAfterSunday(DateTime *pDate){ + assert( pDate->validJD ); + return (int)((pDate->iJD+129600000)/86400000) % 7; } /* @@ -1344,7 +1363,7 @@ static void strftimeFunc( DateTime y = x; assert( y.validJD ); /* Move y so that it is the Thursday in the same week as x */ - y.iJD += (4 - dayOfWeek(&x))*86400000; + y.iJD += (3 - daysAfterMonday(&x))*86400000; y.validYMD = 0; computeYMD(&y); if( cf=='g' ){ @@ -1368,8 +1387,7 @@ static void strftimeFunc( break; } case 'j': { /* Day of year. Jan01==1, Jan02==2, and so forth */ - int nDay = dayOfYear(&x); - sqlite3_str_appendf(&sRes,"%03d",nDay); + sqlite3_str_appendf(&sRes,"%03d",daysAfterJan01(&x)+1); break; } case 'J': { /* Julian day number. (Non-standard) */ @@ -1417,35 +1435,29 @@ static void strftimeFunc( } case 'u': /* Day of week. 1 to 7. Monday==1, Sunday==7 */ case 'w': { /* Day of week. 0 to 6. Sunday==0, Monday==1 */ - char c = (char)(((x.iJD+129600000)/86400000) % 7) + '0'; + char c = (char)daysAfterSunday(&x) + '0'; if( c=='0' && cf=='u' ) c = '7'; sqlite3_str_appendchar(&sRes, 1, c); break; } case 'U': { /* Week num. 00-53. First Sun of the year is week 01 */ - int wd; /* 0=Sunday, 1=Monday, 2=Tuesday, ... 7=Saturday */ - int nDay; /* Day of the year. 0..364 or 0..365 for leapyears */ - nDay = dayOfYear(&x); - wd = (int)(((x.iJD+43200000)/86400000 + 1)%7); - sqlite3_str_appendf(&sRes,"%02d",(nDay+6-wd)/7); + sqlite3_str_appendf(&sRes,"%02d", + (daysAfterJan01(&x)-daysAfterSunday(&x)+7)/7); break; } case 'V': { /* Week num. 01-53. First week with a Thur is week 01 */ DateTime y = x; /* Adjust y so that is the Thursday in the same week as x */ assert( y.validJD ); - y.iJD += (4 - dayOfWeek(&x))*86400000; + y.iJD += (3 - daysAfterMonday(&x))*86400000; y.validYMD = 0; computeYMD(&y); - sqlite3_str_appendf(&sRes,"%02d", (dayOfYear(&y)-1)/7+1); + sqlite3_str_appendf(&sRes,"%02d", daysAfterJan01(&y)/7+1); break; } case 'W': { /* Week num. 00-53. First Mon of the year is week 01 */ - int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */ - int nDay; /* Day of the year. 0..364 or 0..365 for leapyears */ - nDay = dayOfYear(&x); - wd = (int)(((x.iJD+43200000)/86400000)%7); - sqlite3_str_appendf(&sRes,"%02d",(nDay+6-wd)/7); + sqlite3_str_appendf(&sRes,"%02d", + (daysAfterJan01(&x)-daysAfterMonday(&x)+7)/7); break; } case 'Y': { From f28bff745c778598ef1740e4cbb1a44b176aac83 Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 20 Jan 2024 18:41:13 +0000 Subject: [PATCH 5/5] Ensure that values generated by DEFAULT clauses that specify real numbers that can be expressed as 64-bit integers (e.g. -1234.0) are not silently converted to integers. FossilOrigin-Name: 298d6977285c71be917896bc875a8a26d985dcf0a74069b7c4d290e8ff0ac618 --- manifest | 20 ++++++++++---------- manifest.uuid | 2 +- src/vdbemem.c | 13 +++++++++++-- test/alter2.test | 8 ++++---- test/literal.test | 6 ++++++ test/tkt-8454a207b9.test | 2 +- 6 files changed, 33 insertions(+), 18 deletions(-) diff --git a/manifest b/manifest index 821d38be63..eec8b0a2e9 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Simplifications\sto\sthe\sstrftime()\slogic. -D 2024-01-20T18:26:28.712 +C Ensure\sthat\svalues\sgenerated\sby\sDEFAULT\sclauses\sthat\sspecify\sreal\snumbers\sthat\scan\sbe\sexpressed\sas\s64-bit\sintegers\s(e.g.\s-1234.0)\sare\snot\ssilently\sconverted\sto\sintegers. +D 2024-01-20T18:41:13.528 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -814,7 +814,7 @@ F src/vdbeInt.h 949669dfd8a41550d27dcb905b494f2ccde9a2e6c1b0b04daa1227e2e74c2b2c F src/vdbeapi.c 8f57d60c89da0b60e6d4e272358c511f6bae4e24330bdb11f8b42f986d1bf21b F src/vdbeaux.c c5a471b34e9c4cfc0295a3e10734fd197670ffaebcb742f284c8e17e8026ceea F src/vdbeblob.c 13f9287b55b6356b4b1845410382d6bede203ceb29ef69388a4a3d007ffacbe5 -F src/vdbemem.c bbe87b3e851bcc5cad2a9329ad0387432e41ddf2f98f2682c524ad676b739987 +F src/vdbemem.c 3e37dab421b74e9ce55c1e88fbc7bc6fead590b5ab258bc684f8b70abb1d6e71 F src/vdbesort.c 237840ca1947511fa59bd4e18b9eeae93f2af2468c34d2427b059f896230a547 F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf823 F src/vdbevtab.c 2143db7db0ceed69b21422581f434baffc507a08d831565193a7a02882a1b6a7 @@ -838,7 +838,7 @@ F test/aggorderby.test cc3abf5de64d46ff66395ca8c2346b66c2576d5aedb7bffc5b0742508 F test/alias.test 4529fbc152f190268a15f9384a5651bbbabc9d87 F test/all.test 2ecb8bbd52416642e41c9081182a8df05d42c75637afd4488aace78cc4b69e13 F test/alter.test 3c00eff1e2036b9f93e9cd0f3d3e63750ac87ecb5bc71b9d7bd07cbf2ac4c494 -F test/alter2.test a966ccfcddf9ce0a4e0e6ff1aca9e6e7948e0e242cd7e43fc091948521807687 +F test/alter2.test 7e3d26ab409df52df887b366a63902c3429b935c41cb962fd58ffc25784f2f19 F test/alter3.test ffc4ab29ce78a3517a66afd69b2730667e3471622509c283b2bd4c46f680fba3 F test/alter4.test 716caa071dd8a3c6d57225778d15d3c3cbf5e34b2e84ae44199aeb2bbf50a707 F test/alterauth.test 63442ba61ceb0c1eeb63aac1f4f5cebfa509d352276059d27106ae256bafc959 @@ -1355,7 +1355,7 @@ F test/like2.test d3be15fefee3e02fc88942a9b98f26c5339bbdef7783c90023c092c4955fe3 F test/like3.test a76e5938fadbe6d32807284c796bafd869974a961057bc5fc5a28e06de98745c F test/limit.test 350f5d03c29e7dff9a2cde016f84f8d368d40bcd02fa2b2a52fa10c4bf3cbfaf F test/limit2.test 9409b033284642a859fafc95f29a5a6a557bd57c1f0d7c3f554bd64ed69df77e -F test/literal.test 31ef70d425c39b993d0dd61b3f4c65d30b7491e8b3dbbd2d8a23b78b1e8931ca +F test/literal.test 12e3c865a9e20f984240dbfb362be2a53ad5ca4ee25a6d429cf059115a9ac48f F test/loadext.test faa4f6eed07a5aac35d57fdd7bc07f8fc82464cfd327567c10cf0ba3c86cde04 F test/loadext2.test 0408380b57adca04004247179837a18e866a74f7 F test/lock.test be4fe08118fb988fed741f429b7dd5d65e1c90db @@ -1715,7 +1715,7 @@ F test/tkt-7a31705a7e6.test 9e9c057b6a9497c8f7ba7b16871029414ccf6550e7345d9085d6 F test/tkt-7bbfb7d442.test e87b59e620700b5a52ecd92f05d56686c1cad9e1aa17456eada55e0bb821b698 F test/tkt-80ba201079.test 105a721e6aad0ae3c5946d7615d1e4d03f6145b8 F test/tkt-80e031a00f.test 7c93af53f43527f50020983a4bcc39b077e77c7362d7af8e04a5fd155fe5e829 -F test/tkt-8454a207b9.test aff2e76143cfa443ddce6f7d85968a2e9b57a3deb0b881b730120740555f9e2f +F test/tkt-8454a207b9.test ead80b7a01438ca1436cee029694a96c821346cf1e24f06de12f8172e139ddbb F test/tkt-868145d012.test a5f941107ece6a64410ca4755c6329b7eb57a356 F test/tkt-8c63ff0ec.test 258b7fc8d7e4e1cb5362c7d65c143528b9c4cbed F test/tkt-91e2e8ba6f.test 08c4f94ae07696b05c9b822da0b4e5337a2f54c5 @@ -2159,8 +2159,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P bb2b7a658e0186d8b09199170db17744536602d8282b83c71ed969494d2ca465 -R 8518c96f5a8faebc2a9945073340f70a -U drh -Z f670e53a135b10311c59f478b3527211 +P aaa5a044d8a4942278eb4269fa8c7252f9f1dc161408e618e7c97c55a12283ff +R 363a8d4d24bb7774a5f7eceb24c4f650 +U dan +Z ee966b48b47570fa6886cd7583990b8c # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index e3a0693990..3883df6b7b 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -aaa5a044d8a4942278eb4269fa8c7252f9f1dc161408e618e7c97c55a12283ff \ No newline at end of file +298d6977285c71be917896bc875a8a26d985dcf0a74069b7c4d290e8ff0ac618 \ No newline at end of file diff --git a/src/vdbemem.c b/src/vdbemem.c index 2d10cda8d5..cbc6712bfd 100644 --- a/src/vdbemem.c +++ b/src/vdbemem.c @@ -1662,8 +1662,17 @@ static int valueFromExpr( sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); } } - if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){ - sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); + if( affinity==SQLITE_AFF_BLOB ){ + if( op==TK_FLOAT ){ + assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) ); + sqlite3AtoF(pVal->z, &pVal->u.r, pVal->n, SQLITE_UTF8); + pVal->flags = MEM_Real; + }else if( op==TK_INTEGER ){ + /* This case is required by -9223372036854775808 and other strings + ** that look like integers but cannot be handled by the + ** sqlite3DecOrHexToI64() call above. */ + sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); + } }else{ sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8); } diff --git a/test/alter2.test b/test/alter2.test index aae0061ad4..20b75b59ee 100644 --- a/test/alter2.test +++ b/test/alter2.test @@ -371,7 +371,7 @@ do_test alter2-7.5 { execsql { SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1; } -} {1 integer -123 integer 5 text} +} {1 integer -123.0 real 5 text} #----------------------------------------------------------------------- # Test that UPDATE trigger tables work with default values, and that when @@ -397,11 +397,11 @@ do_test alter2-8.2 { UPDATE t1 SET c = 10 WHERE a = 1; SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1; } -} {1 integer -123 integer 10 text} +} {1 integer -123.0 real 10 text} ifcapable trigger { do_test alter2-8.3 { set ::val - } {-123 integer 5 text -123 integer 10 text} + } {-123.0 real 5 text -123.0 real 10 text} } #----------------------------------------------------------------------- @@ -425,7 +425,7 @@ ifcapable trigger { DELETE FROM t1 WHERE a = 2; } set ::val - } {-123 integer 5 text} + } {-123.0 real 5 text} } #----------------------------------------------------------------------- diff --git a/test/literal.test b/test/literal.test index 7bbb8f0f7a..4e957c05db 100644 --- a/test/literal.test +++ b/test/literal.test @@ -43,5 +43,11 @@ test_literal 1.11 '0xFF' text 0xFF test_literal 1.12 '-0xFF' text -0xFF test_literal 1.13 -'0xFF' integer 0 +test_literal 1.14 -9223372036854775808 integer -9223372036854775808 + +test_literal 2.1 1e12 real 1000000000000.0 +test_literal 2.2 1.0 real 1.0 +test_literal 2.3 1e1000 real Inf +test_literal 2.4 -1e1000 real -Inf finish_test diff --git a/test/tkt-8454a207b9.test b/test/tkt-8454a207b9.test index 20e142057d..42b95c8f53 100644 --- a/test/tkt-8454a207b9.test +++ b/test/tkt-8454a207b9.test @@ -49,7 +49,7 @@ do_test tkt-8454a207b9.4 { ALTER TABLE t1 ADD COLUMN e DEFAULT -123.0; SELECT e, typeof(e) FROM t1; } -} {-123 integer} +} {-123.0 real} do_test tkt-8454a207b9.5 { db eval { ALTER TABLE t1 ADD COLUMN f DEFAULT -123.5;