/ Check-in [29b48972]
Login

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

Overview
Comment:Report an error if a USING or ON clause is specified following a from-list element that is not to the right of a join operator. Fix for #3846. (CVS 6832)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 29b48972b65a17dab343b063a620cf8d456a923c
User & Date: danielk1977 2009-07-01 16:12:08
Context
2009-07-01
18:04
Fix a 1-byte buffer overwrite that can occur when a virtual table overloads an SQL function. (CVS 6833) check-in: bfe336a8 user: danielk1977 tags: trunk
16:12
Report an error if a USING or ON clause is specified following a from-list element that is not to the right of a join operator. Fix for #3846. (CVS 6832) check-in: 29b48972 user: danielk1977 tags: trunk
14:56
Do not ignore alias "a" in a query of the form "SELECT ... FROM (...) AS a" Fix for #3935. Also expand upon (6751) to fix some similar obscure memory leaks. (CVS 6831) check-in: 42f9d1e5 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/build.c.

    18     18   **     CREATE INDEX
    19     19   **     DROP INDEX
    20     20   **     creating ID lists
    21     21   **     BEGIN TRANSACTION
    22     22   **     COMMIT
    23     23   **     ROLLBACK
    24     24   **
    25         -** $Id: build.c,v 1.555 2009/07/01 14:56:40 danielk1977 Exp $
           25  +** $Id: build.c,v 1.556 2009/07/01 16:12:08 danielk1977 Exp $
    26     26   */
    27     27   #include "sqliteInt.h"
    28     28   
    29     29   /*
    30     30   ** This routine is called when a new SQL statement is beginning to
    31     31   ** be parsed.  Initialize the pParse structure as needed.
    32     32   */
................................................................................
  3204   3204     Token *pAlias,          /* The right-hand side of the AS subexpression */
  3205   3205     Select *pSubquery,      /* A subquery used in place of a table name */
  3206   3206     Expr *pOn,              /* The ON clause of a join */
  3207   3207     IdList *pUsing          /* The USING clause of a join */
  3208   3208   ){
  3209   3209     struct SrcList_item *pItem;
  3210   3210     sqlite3 *db = pParse->db;
         3211  +  if( !p && (pOn || pUsing) ){
         3212  +    sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 
         3213  +      (pOn ? "ON" : "USING")
         3214  +    );
         3215  +    goto append_from_error;
         3216  +  }
  3211   3217     p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
  3212   3218     if( p==0 || NEVER(p->nSrc==0) ){
  3213         -    sqlite3ExprDelete(db, pOn);
  3214         -    sqlite3IdListDelete(db, pUsing);
  3215         -    sqlite3SelectDelete(db, pSubquery);
  3216         -    return p;
         3219  +    goto append_from_error;
  3217   3220     }
  3218   3221     pItem = &p->a[p->nSrc-1];
  3219   3222     assert( pAlias!=0 );
  3220   3223     if( pAlias->n ){
  3221   3224       pItem->zAlias = sqlite3NameFromToken(db, pAlias);
  3222   3225     }
  3223   3226     pItem->pSelect = pSubquery;
  3224         -  if( p->nSrc>1 ){
  3225         -    pItem->pOn = pOn;
  3226         -    pItem->pUsing = pUsing;
  3227         -  }else{
  3228         -    sqlite3ExprDelete(db, pOn);
  3229         -    sqlite3IdListDelete(db, pUsing);
  3230         -  }
         3227  +  pItem->pOn = pOn;
         3228  +  pItem->pUsing = pUsing;
  3231   3229     return p;
         3230  +
         3231  + append_from_error:
         3232  +  assert( p==0 );
         3233  +  sqlite3ExprDelete(db, pOn);
         3234  +  sqlite3IdListDelete(db, pUsing);
         3235  +  sqlite3SelectDelete(db, pSubquery);
         3236  +  return 0;
  3232   3237   }
  3233   3238   
  3234   3239   /*
  3235   3240   ** Add an INDEXED BY or NOT INDEXED clause to the most recently added 
  3236   3241   ** element of the source-list passed as the second argument.
  3237   3242   */
  3238   3243   void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){

Changes to test/join.test.

     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.
    12     12   #
    13     13   # This file implements tests for joins, including outer joins.
    14     14   #
    15         -# $Id: join.test,v 1.26 2008/12/05 00:00:07 drh Exp $
           15  +# $Id: join.test,v 1.27 2009/07/01 16:12:08 danielk1977 Exp $
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20     20   do_test join-1.1 {
    21     21     execsql {
    22     22       CREATE TABLE t1(a,b,c);
................................................................................
   305    305   } {1 {cannot join using column a - column not present in both tables}}
   306    306   do_test join-3.4.2 {
   307    307     catchsql {
   308    308       SELECT * FROM t1 JOIN t2 USING(d);
   309    309     }
   310    310   } {1 {cannot join using column d - column not present in both tables}}
   311    311   do_test join-3.5 {
   312         -  catchsql {
   313         -    SELECT * FROM t1 USING(a);
   314         -  }
   315         -} {0 {1 2 3 2 3 4 3 4 5}}
          312  +  catchsql { SELECT * FROM t1 USING(a) }
          313  +} {1 {a JOIN clause is required before USING}}
   316    314   do_test join-3.6 {
   317    315     catchsql {
   318    316       SELECT * FROM t1 JOIN t2 ON t3.a=t2.b;
   319    317     }
   320    318   } {1 {no such column: t3.a}}
   321    319   do_test join-3.7 {
   322    320     catchsql {

Changes to test/tkt3935.test.

     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.
    12     12   #
    13     13   # This file implements tests to verify that ticket #3935 has been fixed.
    14     14   #
    15         -# $Id: tkt3935.test,v 1.1 2009/07/01 14:56:41 danielk1977 Exp $
           15  +# $Id: tkt3935.test,v 1.2 2009/07/01 16:12:08 danielk1977 Exp $
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20     20   do_test tkt3935.1 {
    21     21     execsql {
    22     22       CREATE TABLE t1(a, b);
................................................................................
    29     29   } {}
    30     30   do_test tkt3935.3 {
    31     31     execsql { SELECT j1.b FROM (t1 INNER JOIN t2 ON a=c) AS j1 }
    32     32   } {}
    33     33   
    34     34   
    35     35   do_test tkt3935.4 {
    36         -  execsql { SELECT a FROM (t1) AS t ON b USING(a) }
    37         -} {}
           36  +  catchsql { SELECT a FROM (t1) AS t ON b USING(a) }
           37  +} {1 {a JOIN clause is required before ON}}
    38     38   do_test tkt3935.5 {
    39         -  execsql { SELECT a FROM (t1) AS t ON b }
    40         -} {}
           39  +  catchsql { SELECT a FROM (t1) AS t ON b }
           40  +} {1 {a JOIN clause is required before ON}}
    41     41   do_test tkt3935.6 {
    42         -  execsql { SELECT a FROM (SELECT * FROM t1) AS t ON b USING(a) }
    43         -} {}
           42  +  catchsql { SELECT a FROM (SELECT * FROM t1) AS t ON b USING(a) }
           43  +} {1 {a JOIN clause is required before ON}}
    44     44   do_test tkt3935.7 {
    45         -  execsql { SELECT a FROM (SELECT * FROM t1) AS t ON b }
    46         -} {}
           45  +  catchsql { SELECT a FROM (SELECT * FROM t1) AS t ON b }
           46  +} {1 {a JOIN clause is required before ON}}
    47     47   do_test tkt3935.8 {
    48         -  execsql { SELECT a FROM t1 AS t ON b }
    49         -} {}
           48  +  catchsql { SELECT a FROM t1 AS t ON b }
           49  +} {1 {a JOIN clause is required before ON}}
    50     50   do_test tkt3935.9 {
    51         -  execsql { SELECT a FROM t1 AS t ON b USING(a) }
    52         -} {}
           51  +  catchsql { SELECT a FROM t1 AS t ON b USING(a) }
           52  +} {1 {a JOIN clause is required before ON}}
           53  +do_test tkt3935.10 {
           54  +  catchsql { SELECT a FROM t1 AS t USING(a) }
           55  +} {1 {a JOIN clause is required before USING}}
    53     56   
    54     57   finish_test

Changes to test/vtab6.test.

    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.
    12     12   #
    13     13   # This file implements tests for joins, including outer joins involving
    14     14   # virtual tables. The test cases in this file are copied from the file
    15     15   # join.test, and some of the comments still reflect that.
    16     16   #
    17         -# $Id: vtab6.test,v 1.4 2008/07/12 14:52:21 drh Exp $
           17  +# $Id: vtab6.test,v 1.5 2009/07/01 16:12:08 danielk1977 Exp $
    18     18   
    19     19   set testdir [file dirname $argv0]
    20     20   source $testdir/tester.tcl
    21     21   
    22     22   ifcapable !vtab {
    23     23     finish_test
    24     24     return
................................................................................
   261    261   } {1 {cannot have both ON and USING clauses in the same join}}
   262    262   do_test vtab6-3.4 {
   263    263     catchsql {
   264    264       SELECT * FROM t1 JOIN t2 USING(a);
   265    265     }
   266    266   } {1 {cannot join using column a - column not present in both tables}}
   267    267   do_test vtab6-3.5 {
   268         -  catchsql {
   269         -    SELECT * FROM t1 USING(a);
   270         -  }
   271         -} {0 {1 2 3 2 3 4 3 4 5}}
          268  +  catchsql { SELECT * FROM t1 USING(a) }
          269  +} {1 {a JOIN clause is required before USING}}
   272    270   do_test vtab6-3.6 {
   273    271     catchsql {
   274    272       SELECT * FROM t1 JOIN t2 ON t3.a=t2.b;
   275    273     }
   276    274   } {1 {no such column: t3.a}}
   277    275   do_test vtab6-3.7 {
   278    276     catchsql {