SQLite

Check-in [d45bee36f2]
Login

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

Overview
Comment:Avoid a dangling pointer comparison when renaming a table that has a trigger that itself contains a window function with an (illegal) column reference in a FOLLOWING expression.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d45bee36f2c1091a2d32c16ca8921bf4e7c9e40c46d0a36fbcb179ecfafcfbf0
User & Date: dan 2019-01-16 12:05:22.604
Context
2019-01-16
14:58
Fix a problem with renaming a column that is used as part of an ORDER BY on a compound SELECT within a database view or trigger. (check-in: b4b5741366 user: dan tags: trunk)
12:05
Avoid a dangling pointer comparison when renaming a table that has a trigger that itself contains a window function with an (illegal) column reference in a FOLLOWING expression. (check-in: d45bee36f2 user: dan tags: trunk)
11:38
Fix a memory leak that could occur in fts3 when handling a corrupt database. (check-in: 65cebb06a0 user: dan tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/window.c.
877
878
879
880
881
882
883

884
885
886
887
888
889
890
** value should be a non-negative integer.  If the value is not a
** constant, change it to NULL.  The fact that it is then a non-negative
** integer will be caught later.  But it is important not to leave
** variable values in the expression tree.
*/
static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
  if( 0==sqlite3ExprIsConstant(pExpr) ){

    sqlite3ExprDelete(pParse->db, pExpr);
    pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
  }
  return pExpr;
}

/*







>







877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
** value should be a non-negative integer.  If the value is not a
** constant, change it to NULL.  The fact that it is then a non-negative
** integer will be caught later.  But it is important not to leave
** variable values in the expression tree.
*/
static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
  if( 0==sqlite3ExprIsConstant(pExpr) ){
    if( IN_RENAME_OBJECT ) sqlite3RenameExprUnmap(pParse, pExpr);
    sqlite3ExprDelete(pParse->db, pExpr);
    pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
  }
  return pExpr;
}

/*
Changes to test/altertab2.test.
173
174
175
176
177
178
179























































180
181
  ALTER TABLE t1x RENAME d TO ddd;
  SELECT sql FROM sqlite_master WHERE type = 'trigger';
} {
{CREATE TRIGGER r1 AFTER INSERT ON "t1x" WHEN new.aaa NOT NULL BEGIN
    UPDATE "t1x" SET (c,ddd)=(aaa,b);
  END}
}
























































finish_test







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


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
  ALTER TABLE t1x RENAME d TO ddd;
  SELECT sql FROM sqlite_master WHERE type = 'trigger';
} {
{CREATE TRIGGER r1 AFTER INSERT ON "t1x" WHEN new.aaa NOT NULL BEGIN
    UPDATE "t1x" SET (c,ddd)=(aaa,b);
  END}
}

#-------------------------------------------------------------------------
do_execsql_test 5.0 {
  CREATE TABLE t2(a);
  CREATE TRIGGER r2 AFTER INSERT ON t2 WHEN new.a NOT NULL BEGIN
    SELECT a, rank() OVER w1 FROM t2
      WINDOW w1 AS (
        PARTITION BY b ORDER BY d ROWS BETWEEN 2 PRECEDING AND a FOLLOWING
      ),
      w2 AS (
        PARTITION BY b 
        ORDER BY d ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
      );
  END;
} {}

do_catchsql_test 5.0.1 {
  INSERT INTO t2 VALUES(1);
} {1 {no such column: b}}

do_execsql_test 5.1 {
  ALTER TABLE t2 RENAME TO t2x;
  SELECT sql FROM sqlite_master WHERE name = 'r2';
} {
  {CREATE TRIGGER r2 AFTER INSERT ON "t2x" WHEN new.a NOT NULL BEGIN
    SELECT a, rank() OVER w1 FROM "t2x"
      WINDOW w1 AS (
        PARTITION BY b ORDER BY d ROWS BETWEEN 2 PRECEDING AND a FOLLOWING
      ),
      w2 AS (
        PARTITION BY b 
        ORDER BY d ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
      );
  END}
}

do_execsql_test 5.2 {
  ALTER TABLE t2x RENAME a TO aaaa;
  SELECT sql FROM sqlite_master WHERE name = 'r2';
} {
  {CREATE TRIGGER r2 AFTER INSERT ON "t2x" WHEN new.aaaa NOT NULL BEGIN
    SELECT aaaa, rank() OVER w1 FROM "t2x"
      WINDOW w1 AS (
        PARTITION BY b ORDER BY d ROWS BETWEEN 2 PRECEDING AND a FOLLOWING
      ),
      w2 AS (
        PARTITION BY b 
        ORDER BY d ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
      );
  END}
}

do_catchsql_test 5.3 {
  INSERT INTO t2x VALUES(1);
} {1 {no such column: b}}

finish_test