Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Prevent sub-queries with "LIMIT 0" from leaving an extra value on the vdbe stack. Also updates to fuzz.test. (CVS 3993) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
b1d1b16e9857a1c05f60cf2ae15f5a53 |
User & Date: | danielk1977 2007-05-14 14:05:00.000 |
Context
2007-05-14
| ||
15:49 | Fix a bug in "flattening" optimization. Occured if the parent of the flattened sub-query is also the parent of a sub-query that uses a compound op (i.e. UNION, INTERSECT etc.). (CVS 3994) (check-in: 1c33829c9e user: danielk1977 tags: trunk) | |
14:05 | Prevent sub-queries with "LIMIT 0" from leaving an extra value on the vdbe stack. Also updates to fuzz.test. (CVS 3993) (check-in: b1d1b16e98 user: danielk1977 tags: trunk) | |
12:12 | In the windows driver, reacquire the shared lock if an exclusive lock fails. Ticket #2354. (CVS 3992) (check-in: fc489b5382 user: drh tags: trunk) | |
Changes
Changes to src/select.c.
︙ | ︙ | |||
8 9 10 11 12 13 14 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains C code routines that are called by the parser ** to handle SELECT statements in SQLite. ** | | | 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains C code routines that are called by the parser ** to handle SELECT statements in SQLite. ** ** $Id: select.c,v 1.346 2007/05/14 14:05:00 danielk1977 Exp $ */ #include "sqliteInt.h" /* ** Delete all the content of a Select structure but do not deallocate ** the select structure itself. |
︙ | ︙ | |||
1516 1517 1518 1519 1520 1521 1522 | if( p->pLimit ){ p->iLimit = iLimit = pParse->nMem; pParse->nMem += 2; v = sqlite3GetVdbe(pParse); if( v==0 ) return; sqlite3ExprCode(pParse, p->pLimit); sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); | | > | 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 | if( p->pLimit ){ p->iLimit = iLimit = pParse->nMem; pParse->nMem += 2; v = sqlite3GetVdbe(pParse); if( v==0 ) return; sqlite3ExprCode(pParse, p->pLimit); sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); sqlite3VdbeAddOp(v, OP_MemStore, iLimit, 1); VdbeComment((v, "# LIMIT counter")); sqlite3VdbeAddOp(v, OP_IfMemZero, iLimit, iBreak); sqlite3VdbeAddOp(v, OP_MemLoad, iLimit, 0); } if( p->pOffset ){ p->iOffset = iOffset = pParse->nMem++; v = sqlite3GetVdbe(pParse); if( v==0 ) return; sqlite3ExprCode(pParse, p->pOffset); sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); |
︙ | ︙ |
Changes to test/fuzz.test.
1 2 3 4 5 6 7 8 9 10 11 12 | # 2007 May 10 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is generating semi-random strings of SQL | | | > > > > > > | | | > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # 2007 May 10 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is generating semi-random strings of SQL # (a.k.a. "fuzz") and sending it into the parser to try to # generate errors. # # The tests in this file are really about testing fuzzily generated # SQL parse-trees. The majority of the fuzzily generated SQL is # valid as far as the parser is concerned. # # The most complicated trees are for SELECT statements. # # $Id: fuzz.test,v 1.8 2007/05/14 14:05:00 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl set ::REPEATS 20 # set ::REPEATS 5000 proc fuzz {TemplateList} { set n [llength $TemplateList] set i [expr {int(rand()*$n)}] set r [uplevel 1 subst -novar [list [lindex $TemplateList $i]]] string map {"\n" " "} $r } # Fuzzy generation primitives: # # Literal # UnaryOp # BinaryOp |
︙ | ︙ | |||
72 73 74 75 76 77 78 | # Return the complete text of an SQL expression. # set ::ExprDepth 0 proc Expr { {c {}} } { incr ::ExprDepth | | | | | | | | | | | | | | | | | | | | | | > > > | 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | # Return the complete text of an SQL expression. # set ::ExprDepth 0 proc Expr { {c {}} } { incr ::ExprDepth set TemplateList [concat $c $c $c {[Literal]}] if {$::ExprDepth < 3} { lappend TemplateList \ {[Expr $c] [BinaryOp] [Expr $c]} \ {[UnaryOp] [Expr $c]} \ {[Expr $c] ISNULL} \ {[Expr $c] NOTNULL} \ {CAST([Expr $c] AS blob)} \ {CAST([Expr $c] AS text)} \ {CAST([Expr $c] AS integer)} \ {CAST([Expr $c] AS real)} \ {abs([Expr])} \ {coalesce([Expr], [Expr])} \ {hex([Expr])} \ {length([Expr])} \ {lower([Expr])} \ {upper([Expr])} \ {quote([Expr])} \ {random()} \ {randomblob(min(max([Expr],1), 500))} \ {typeof([Expr])} \ {substr([Expr],[Expr],[Expr])} \ {CASE WHEN [Expr $c] THEN [Expr $c] ELSE [Expr $c] END} \ {[Literal]} {[Literal]} {[Literal]} \ {[Literal]} {[Literal]} {[Literal]} \ {[Literal]} {[Literal]} {[Literal]} \ {[Literal]} {[Literal]} {[Literal]} } if {$::SelectDepth < 10} { lappend TemplateList \ {([Select 1])} \ {[Expr $c] IN ([Select 1])} \ {[Expr $c] NOT IN ([Select 1])} \ |
︙ | ︙ | |||
117 118 119 120 121 122 123 | # set ::TableList [list] proc Table {} { set TemplateList [concat sqlite_master $::TableList] fuzz $TemplateList } | | > > > > > > > > > > > > > > > > > > > > > > > > > > | | | < > > > > > > > > > | > > > > > > > > > > > > | > > > > > | > | > | > > > > > > > > > | > > > > | > > | > > > > | | < > > > | | > | 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | # set ::TableList [list] proc Table {} { set TemplateList [concat sqlite_master $::TableList] fuzz $TemplateList } # Return one of: # # "SELECT DISTINCT", "SELECT ALL" or "SELECT" # proc SelectKw {} { set TemplateList { "SELECT DISTINCT" "SELECT ALL" "SELECT" } fuzz $TemplateList } # Return a result set for a SELECT statement. # proc ResultSet {{nRes 0} {c ""}} { if {$nRes == 0} { set nRes [expr {rand()*2 + 1}] } set aRes [list] for {set ii 0} {$ii < $nRes} {incr ii} { lappend aRes [Expr $c] } join $aRes ", " } set ::SelectDepth 0 set ::ColumnList [list] proc SimpleSelect {{nRes 0}} { set TemplateList { {[SelectKw] [ResultSet $nRes]} } # The ::SelectDepth variable contains the number of ancestor SELECT # statements (i.e. for a top level SELECT it is set to 0, for a # sub-select 1, for a sub-select of a sub-select 2 etc.). # # If this is already greater than 3, do not generate a complicated # SELECT statement. This tends to cause parser stack overflow (too # boring to bother with). # if {$::SelectDepth < 4} { lappend TemplateList \ {[SelectKw] [ResultSet $nRes $::ColumnList] FROM ([Select])} \ {[SelectKw] [ResultSet $nRes] FROM ([Select])} \ {[SelectKw] [ResultSet $nRes $::ColumnList] FROM [Table]} \ { [SelectKw] [ResultSet $nRes $::ColumnList] FROM ([Select]) GROUP BY [Expr] HAVING [Expr] } \ if {0 == $nRes} { lappend TemplateList \ {[SelectKw] * FROM ([Select])} \ {[SelectKw] * FROM [Table]} \ {[SelectKw] * FROM [Table] WHERE [Expr $::ColumnList]} \ { [SelectKw] * FROM [Table],[Table] AS t2 WHERE [Expr $::ColumnList] } \ } } fuzz $TemplateList } # Return a SELECT statement. # # If boolean parameter $isExpr is set to true, make sure the # returned SELECT statement returns a single column of data. # proc Select {{nMulti 0}} { set TemplateList { {[SimpleSelect $nMulti]} {[SimpleSelect $nMulti]} {[SimpleSelect $nMulti]} {[SimpleSelect $nMulti]} {[SimpleSelect $nMulti]} {[SimpleSelect $nMulti]} {[SimpleSelect $nMulti]} {[SimpleSelect $nMulti]} {[SimpleSelect $nMulti]} {[SimpleSelect $nMulti]} {[SimpleSelect $nMulti]} {[SimpleSelect $nMulti]} {[SimpleSelect $nMulti] ORDER BY [Expr]} {[SimpleSelect $nMulti] ORDER BY [Expr] LIMIT [Expr] OFFSET [Expr]} } if {$::SelectDepth < 4} { if {$nMulti == 0} { set nMulti [expr {(rand()*2)+1}] } lappend TemplateList \ {[SimpleSelect $nMulti] UNION [Select $nMulti]} \ {[SimpleSelect $nMulti] UNION ALL [Select $nMulti]} \ {[SimpleSelect $nMulti] EXCEPT [Select $nMulti]} \ {[SimpleSelect $nMulti] INTERSECT [Select $nMulti]} } incr ::SelectDepth set res [fuzz $TemplateList] incr ::SelectDepth -1 set res } # Generate and return a fuzzy INSERT statement. # |
︙ | ︙ | |||
188 189 190 191 192 193 194 195 196 197 198 199 | {[Update]} {[Insert]} {[Select]} {[Delete]} } fuzz $TemplateList } ######################################################################## set ::log [open fuzzy.log w] | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > | > > > | | 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | {[Update]} {[Insert]} {[Select]} {[Delete]} } fuzz $TemplateList } # Return an identifier. This just chooses randomly from a fixed set # of strings. proc Identifier {} { set TemplateList { This just chooses randomly a fixed We would also thank the developers for their analysis Samba } fuzz $TemplateList } proc Check {} { set sd $::SelectDepth set ::SelectDepth 500 set TemplateList { {} {CHECK ([Expr])} } set res [fuzz $TemplateList] set ::SelectDepth $sd set res } proc Coltype {} { set TemplateList { {INTEGER PRIMARY KEY} {VARCHAR [Check]} {PRIMARY KEY} } fuzz $TemplateList } proc CreateTable {} { set TemplateList { {CREATE TABLE [Identifier]([Identifier] [Coltype], [Identifier] [Coltype])} {CREATE TEMP TABLE [Identifier]([Identifier] [Coltype])} } fuzz $TemplateList } ######################################################################## set ::log [open fuzzy.log w] # # Usage: do_fuzzy_test <testname> ?<options>? # # -template # -errorlist # -repeats # proc do_fuzzy_test {testname args} { set ::fuzzyopts(-errorlist) [list] set ::fuzzyopts(-repeats) $::REPEATS array set ::fuzzyopts $args lappend ::fuzzyopts(-errorlist) {parser stack overflow} lappend ::fuzzyopts(-errorlist) {ORDER BY} lappend ::fuzzyopts(-errorlist) {GROUP BY} lappend ::fuzzyopts(-errorlist) {datatype mismatch} for {set ii 0} {$ii < $::fuzzyopts(-repeats)} {incr ii} { do_test ${testname}.$ii { set ::sql [subst $::fuzzyopts(-template)] puts $::log $::sql flush $::log set rc [catch {execsql $::sql} msg] set e 1 if {$rc} { |
︙ | ︙ | |||
297 298 299 300 301 302 303 304 305 306 307 308 309 310 | do_test fuzz-1.10 { # Bug in calculation of Parse.ckOffset causing an assert() # to fail. Probably harmless. execsql { SELECT coalesce(1, substr( 1, 2, length('in' IN (SELECT 1)))) } } {1} #---------------------------------------------------------------- # Test some fuzzily generated expressions. # do_fuzzy_test fuzz-2 -template { SELECT [Expr] } do_test fuzz-3.1 { | > > > > > > > > > > > > > > > > > > | 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | do_test fuzz-1.10 { # Bug in calculation of Parse.ckOffset causing an assert() # to fail. Probably harmless. execsql { SELECT coalesce(1, substr( 1, 2, length('in' IN (SELECT 1)))) } } {1} do_test fuzz-1.11 { # The literals (A, B, C, D) are not important, they are just used # to make the EXPLAIN output easier to read. # # The problem here is that the EXISTS(...) expression leaves an # extra value on the VDBE stack. This is confusing the parent and # leads to an assert() failure when OP_Insert encounters an integer # when it expects a record blob. # # Update: Any query with (LIMIT 0) was leaking stack. # execsql { SELECT 'A' FROM (SELECT 'B') ORDER BY EXISTS ( SELECT 'C' FROM (SELECT 'D' LIMIT 0) ) } } {A} #---------------------------------------------------------------- # Test some fuzzily generated expressions. # do_fuzzy_test fuzz-2 -template { SELECT [Expr] } do_test fuzz-3.1 { |
︙ | ︙ | |||
349 350 351 352 353 354 355 | do_test fuzz-5.1 {execsql BEGIN} {} do_fuzzy_test fuzz-5.2 -template {[Insert]} -errorlist table integrity_check fuzz-5.2.integrity do_test fuzz-5.3 {execsql COMMIT} {} integrity_check fuzz-5.4.integrity #---------------------------------------------------------------- | | > > > > > > | 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | do_test fuzz-5.1 {execsql BEGIN} {} do_fuzzy_test fuzz-5.2 -template {[Insert]} -errorlist table integrity_check fuzz-5.2.integrity do_test fuzz-5.3 {execsql COMMIT} {} integrity_check fuzz-5.4.integrity #---------------------------------------------------------------- # Now that there is data in the database, run some more SELECT # statements # set ::ColumnList [list a b c] set E {{no such col} {ambiguous column name}} do_fuzzy_test fuzz-6.1 -template {[Select]} -errorlist $E #---------------------------------------------------------------- # Run some SELECTs, INSERTs, UPDATEs and DELETEs in a transaction. # set E {{no such col} {ambiguous column name} {table}} do_test fuzz-7.1 {execsql BEGIN} {} do_fuzzy_test fuzz-7.2 -template {[Statement]} -errorlist $E integrity_check fuzz-7.3.integrity do_test fuzz-7.4 {execsql COMMIT} {} integrity_check fuzz-7.5.integrity #---------------------------------------------------------------- # Many CREATE TABLE statements: # do_fuzzy_test fuzz-8.1 -template {[CreateTable]} \ -errorlist {table duplicate} -repeats 1000 close $::log finish_test |