1 From f4b79cfaefb87fa2c37a860c5a64f320a5265f99 Mon Sep 17 00:00:00 2001
2 From: Scott Hess <shess@chromium.org>
3 Date: Mon, 23 Mar 2015 11:24:11 -0700
4 Subject: [PATCH] [backport] Fix collation dequoting.
6 Backport https://www.sqlite.org/src/info/eddc05e7bb31fae7
7 "Fix a problem causing collation sequence names to be dequoted
8 multiple times under some circumstances."
12 third_party/sqlite/src/src/expr.c | 7 ++--
13 third_party/sqlite/src/src/parse.y | 6 ++--
14 third_party/sqlite/src/src/sqliteInt.h | 2 +-
15 third_party/sqlite/src/src/where.c | 9 +++--
16 third_party/sqlite/src/test/collate1.test | 58 +++++++++++++++++++++++++++++--
17 5 files changed, 68 insertions(+), 14 deletions(-)
19 diff --git a/third_party/sqlite/src/src/expr.c b/third_party/sqlite/src/src/expr.c
20 index 65f211e..2d96c8d 100644
21 --- a/third_party/sqlite/src/src/expr.c
22 +++ b/third_party/sqlite/src/src/expr.c
23 @@ -69,10 +69,11 @@ char sqlite3ExprAffinity(Expr *pExpr){
24 Expr *sqlite3ExprAddCollateToken(
25 Parse *pParse, /* Parsing context */
26 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
27 - const Token *pCollName /* Name of collating sequence */
28 + const Token *pCollName, /* Name of collating sequence */
29 + int dequote /* True to dequote pCollName */
32 - Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
33 + Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
36 pNew->flags |= EP_Collate|EP_Skip;
37 @@ -86,7 +87,7 @@ Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
40 s.n = sqlite3Strlen30(s.z);
41 - return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
42 + return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
46 diff --git a/third_party/sqlite/src/src/parse.y b/third_party/sqlite/src/src/parse.y
47 index 877827e..d888cff 100644
48 --- a/third_party/sqlite/src/src/parse.y
49 +++ b/third_party/sqlite/src/src/parse.y
50 @@ -854,7 +854,7 @@ expr(A) ::= VARIABLE(X). {
53 expr(A) ::= expr(E) COLLATE ids(C). {
54 - A.pExpr = sqlite3ExprAddCollateToken(pParse, E.pExpr, &C);
55 + A.pExpr = sqlite3ExprAddCollateToken(pParse, E.pExpr, &C, 1);
59 @@ -1200,14 +1200,14 @@ uniqueflag(A) ::= . {A = OE_None;}
60 idxlist_opt(A) ::= . {A = 0;}
61 idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;}
62 idxlist(A) ::= idxlist(X) COMMA nm(Y) collate(C) sortorder(Z). {
63 - Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &C);
64 + Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &C, 1);
65 A = sqlite3ExprListAppend(pParse,X, p);
66 sqlite3ExprListSetName(pParse,A,&Y,1);
67 sqlite3ExprListCheckLength(pParse, A, "index");
68 if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
70 idxlist(A) ::= nm(Y) collate(C) sortorder(Z). {
71 - Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &C);
72 + Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &C, 1);
73 A = sqlite3ExprListAppend(pParse,0, p);
74 sqlite3ExprListSetName(pParse, A, &Y, 1);
75 sqlite3ExprListCheckLength(pParse, A, "index");
76 diff --git a/third_party/sqlite/src/src/sqliteInt.h b/third_party/sqlite/src/src/sqliteInt.h
77 index 9d6a7d8..264f4fe 100644
78 --- a/third_party/sqlite/src/src/sqliteInt.h
79 +++ b/third_party/sqlite/src/src/sqliteInt.h
80 @@ -3462,7 +3462,7 @@ int sqlite3ReadSchema(Parse *pParse);
81 CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
82 CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
83 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
84 -Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*);
85 +Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
86 Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
87 Expr *sqlite3ExprSkipCollate(Expr*);
88 int sqlite3CheckCollSeq(Parse *, CollSeq *);
89 diff --git a/third_party/sqlite/src/src/where.c b/third_party/sqlite/src/src/where.c
90 index bc01107..793b01d 100644
91 --- a/third_party/sqlite/src/src/where.c
92 +++ b/third_party/sqlite/src/src/where.c
93 @@ -1252,7 +1252,7 @@ static void exprAnalyze(
97 - Token sCollSeqName; /* Name of collating sequence */
98 + const char *zCollSeqName; /* Name of collating sequence */
100 pLeft = pExpr->x.pList->a[1].pExpr;
101 pStr2 = sqlite3ExprDup(db, pStr1, 0);
102 @@ -1272,11 +1272,10 @@ static void exprAnalyze(
106 - sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
107 - sCollSeqName.n = 6;
108 + zCollSeqName = noCase ? "NOCASE" : "BINARY";
109 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
110 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
111 - sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
112 + sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
114 transferJoinMarkings(pNewExpr1, pExpr);
115 idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
116 @@ -1284,7 +1283,7 @@ static void exprAnalyze(
117 exprAnalyze(pSrc, pWC, idxNew1);
118 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
119 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
120 - sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
121 + sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
123 transferJoinMarkings(pNewExpr2, pExpr);
124 idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
125 diff --git a/third_party/sqlite/src/test/collate1.test b/third_party/sqlite/src/test/collate1.test
126 index 2085415..0716ac7 100644
127 --- a/third_party/sqlite/src/test/collate1.test
128 +++ b/third_party/sqlite/src/test/collate1.test
131 #***********************************************************************
132 # This file implements regression tests for SQLite library. The
133 -# focus of this script is page cache subsystem.
134 +# focus of this script is testing collation sequences.
136 -# $Id: collate1.test,v 1.5 2007/02/01 23:02:46 drh Exp $
138 set testdir [file dirname $argv0]
139 source $testdir/tester.tcl
140 +set testprefix collate1
143 # Tests are roughly organised as follows:
144 @@ -333,4 +333,58 @@ do_test collate1-5.3 {
150 +#-------------------------------------------------------------------------
151 +# Fix problems with handling collation sequences named '"""'.
153 +do_execsql_test 6.1 {
157 +do_catchsql_test 6.2 {
158 + CREATE TABLE x1(a);
159 + SELECT a FROM x1 ORDER BY a COLLATE """""""";
160 +} {1 {no such collation sequence: """}}
162 +do_catchsql_test 6.3 {
163 + SELECT a FROM x1 ORDER BY 1 COLLATE """""""";
164 +} {1 {no such collation sequence: """}}
166 +do_catchsql_test 6.4 {
167 + SELECT 0 UNION SELECT 0 ORDER BY 1 COLLATE """""""";
168 +} {1 {no such collation sequence: """}}
170 +db collate {"""} [list string compare -nocase]
172 +do_execsql_test 6.5 {
173 + PRAGMA foreign_keys = ON;
174 + CREATE TABLE p1(a PRIMARY KEY COLLATE '"""');
175 + CREATE TABLE c1(x, y REFERENCES p1);
178 +do_execsql_test 6.6 {
179 + INSERT INTO p1 VALUES('abc');
180 + INSERT INTO c1 VALUES(1, 'ABC');
183 +ifcapable foreignkey {
184 + do_catchsql_test 6.7 {
185 + DELETE FROM p1 WHERE rowid = 1
186 + } {1 {FOREIGN KEY constraint failed}}
189 +do_execsql_test 6.8 {
190 + INSERT INTO p1 VALUES('abb');
191 + INSERT INTO p1 VALUES('wxz');
192 + INSERT INTO p1 VALUES('wxy');
194 + INSERT INTO c1 VALUES(2, 'abb');
195 + INSERT INTO c1 VALUES(3, 'wxz');
196 + INSERT INTO c1 VALUES(4, 'WXY');
197 + SELECT x, y FROM c1 ORDER BY y COLLATE """""""";
198 +} {2 abb 1 ABC 4 WXY 3 wxz}