Skip to content

Commit d678fdc

Browse files
author
Bogdan Degtyariov
committed
SQLForeignKeys() crashing when SQL_MODE='ANSI_QUOTES' (Bug# 18641824), SQLForeignKeys() returns empty result with NO_I_S=0 (Bug# 26388694)
1 parent a433701 commit d678fdc

File tree

5 files changed

+80
-14
lines changed

5 files changed

+80
-14
lines changed

ChangeLog

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
(Bug# 18805392)
1111
* SQLExecute() after SQLFreeStmt(SQL_RESET_PARAMS) results
1212
in assert failure (Bug# 19148246)
13+
* SQLForeignKeys() crashing when SQL_MODE='ANSI_QUOTES'
14+
(Bug# 18641824)
15+
* SQLForeignKeys() returns empty result with NO_I_S=0
16+
(Bug# 26388694)
1317

14-
Built using MySQL 5.
18+
Built using MySQL 5.7.19
1519

1620
----
1721

driver/catalog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ SQLRETURN foreign_keys_i_s(SQLHSTMT hstmt,
907907
" ON (D.TABLE_SCHEMA=A.REFERENCED_TABLE_SCHEMA AND D.TABLE_NAME=A.REFERENCED_TABLE_NAME"
908908
" AND D.COLUMN_NAME=A.REFERENCED_COLUMN_NAME)",
909909
ref_constraints_join,
910-
" WHERE D.CONSTRAINT_NAME='PRIMARY' ",
910+
" WHERE D.CONSTRAINT_NAME IS NOT NULL ",
911911
NullS);
912912

913913
if (pk_table_name && pk_table_name[0])

driver/utility.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4041,8 +4041,12 @@ const char get_identifier_quote(STMT *stmt)
40414041
The full list of all SQL modes takes over 512 symbols, so we reserve
40424042
some for the future
40434043
*/
4044-
char sql_mode[2048]= {0};
4045-
uint length= get_session_variable(stmt, "SQL_MODE", (char*)sql_mode);
4044+
char sql_mode[2048]= " ";
4045+
/*
4046+
The token finder skips the leading space and starts
4047+
with the first non-space value. Thus (sql_mode+1).
4048+
*/
4049+
uint length= get_session_variable(stmt, "SQL_MODE", (char*)(sql_mode+1));
40464050

40474051
const char *end= sql_mode + length;
40484052
if (find_first_token(stmt->dbc->ansi_charset_info, sql_mode, end, "ANSI_QUOTES"))

test/my_crash.c

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,59 @@
2525
#include "odbctap.h"
2626
#include "../VersionInfo.h"
2727

28+
/*
29+
18641824 SQLFOREIGNKEYS() CRASHING WHEN SQL_MODE='ANSI_QUOTES'
30+
26388694 SQLForeignKeys() returns empty result with NO_I_S=0
31+
32+
This test case will run with NO_I_S=0 and NO_I_S=1 and test for
33+
two bugs for each NO_I_S value
34+
*/
35+
DECLARE_TEST(t_bug18641824)
36+
{
37+
int i = 0;
38+
char *pk_tab = "tab_pk";
39+
char *fk_tab = "tab_fk";
40+
char *pk_cols[] = { "i1d", "i2d" };
41+
char *fk_cols[] = { "fi1d", "fi2d" };
42+
43+
DECLARE_BASIC_HANDLES(henv1, hdbc1, hstmt1);
44+
is(OK == alloc_basic_handles(&henv1, &hdbc1, &hstmt1));
45+
46+
ok_sql(hstmt1, "set SQL_MODE='ANSI_QUOTES'");
47+
ok_sql(hstmt1, "DROP SCHEMA IF EXISTS fk_check");
48+
ok_sql(hstmt1, "CREATE SCHEMA fk_check");
49+
ok_sql(hstmt1, "USE fk_check");
50+
ok_sql(hstmt1, "CREATE TABLE tab_pk (i1d int, unique(i1d), i2d int, unique (i2d))");
51+
ok_sql(hstmt1, "CREATE TABLE tab_fk (" \
52+
"fi1d int,CONSTRAINT Constraint_1 FOREIGN KEY(fi1d) REFERENCES tab_pk(i1d)," \
53+
"fi2d int,CONSTRAINT Constraint_2 FOREIGN KEY(fi2d) REFERENCES tab_pk(i2d))");
54+
ok_stmt(hstmt1, SQLForeignKeys(hstmt1, NULL, 0, NULL, 0, (SQLCHAR*)"tab_pk", SQL_NTS,
55+
NULL, 0, NULL, 0, (SQLCHAR*)"tab_fk", SQL_NTS));
56+
57+
while (SQLFetch(hstmt1) == SQL_SUCCESS)
58+
{
59+
char buf[1024];
60+
printf("Row: %d\n", ++i);
61+
ok_stmt(hstmt1, SQLGetData(hstmt1, 3, SQL_CHAR, buf, sizeof(buf), NULL));
62+
printf("Primary key table: %s\n", buf);
63+
is_str(pk_tab, buf, strlen(buf));
64+
65+
ok_stmt(hstmt1, SQLGetData(hstmt1, 4, SQL_CHAR, buf, sizeof(buf), NULL));
66+
printf("Primary key column: %s\n", buf);
67+
is(strcmp(pk_cols[0], buf) == 0 || strcmp(pk_cols[1], buf) == 0);
68+
69+
ok_stmt(hstmt1, SQLGetData(hstmt1, 7, SQL_CHAR, buf, sizeof(buf), NULL));
70+
printf("Foreign key table: %s\n", buf);
71+
is_str(fk_tab, buf, strlen(buf));
72+
73+
ok_stmt(hstmt1, SQLGetData(hstmt1, 8, SQL_CHAR, buf, sizeof(buf), NULL));
74+
printf("Foreign key column: %s\n", buf);
75+
is(strcmp(fk_cols[0], buf) == 0 || strcmp(fk_cols[1], buf) == 0);
76+
}
77+
is(i==2);
78+
free_basic_handles(&henv1, &hdbc1, &hstmt1);
79+
return OK;
80+
}
2881

2982
/*
3083
18805392 SEGMENTATION FAULT IN SQLFETCH() WHEN USED WITH DYNAMIC CURSOR
@@ -40,9 +93,9 @@ DECLARE_TEST(t_bug18805392)
4093

4194
ok_stmt(hstmt1, SQLSetStmtAttr(hstmt1, SQL_ATTR_CURSOR_TYPE,
4295
(SQLPOINTER)SQL_CURSOR_DYNAMIC, 0));
43-
ok_sql(hstmt, "DROP TABLE IF EXISTS t_bug18805392");
44-
ok_sql(hstmt, "CREATE TABLE t_bug18805392 (record bigint)");
45-
ok_sql(hstmt, "INSERT INTO t_bug18805392 VALUES (1234567891234),"
96+
ok_sql(hstmt1, "DROP TABLE IF EXISTS t_bug18805392");
97+
ok_sql(hstmt1, "CREATE TABLE t_bug18805392 (record bigint)");
98+
ok_sql(hstmt1, "INSERT INTO t_bug18805392 VALUES (1234567891234),"
4699
"(51234567891234),(61234567891234),(56789453632)");
47100

48101
ok_stmt(hstmt1, SQLPrepare(hstmt1, (SQLCHAR *)"SELECT * FROM t_bug18805392 where record>?", SQL_NTS));
@@ -628,7 +681,7 @@ DECLARE_TEST(t_bug18286366)
628681
is_str(my_fetch_str(hstmt1, buff, 12), tmp_buff, len);
629682
}
630683

631-
ok_sql(hstmt, "DROP TABLE IF EXISTS t_bug16920750b, t_bug16920750a");
684+
ok_sql(hstmt, "DROP TABLE IF EXISTS t_bug18286366b, t_bug18286366a");
632685
free_basic_handles(&henv1, &hdbc1, &hstmt1);
633686
return OK;
634687
}
@@ -889,7 +942,8 @@ DECLARE_TEST(t_bug18796005)
889942

890943

891944
BEGIN_TESTS
892-
//ADD_TEST(t_bug18805392)
945+
ADD_TEST(t_bug18641824)
946+
ADD_TEST(t_bug18805392)
893947
ADD_TEST(t_bug19148246)
894948
ADD_TEST(t_bug69950)
895949
ADD_TEST(t_bug70642)

test/my_keys.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,16 +438,20 @@ DECLARE_TEST(t_bug16920750)
438438
SQLCHAR buff[255];
439439
SQLLEN len;
440440

441+
ok_sql(hstmt, "DROP SCHEMA IF EXISTS fk_test");
442+
ok_sql(hstmt, "CREATE SCHEMA fk_test");
443+
ok_sql(hstmt, "USE fk_test");
444+
441445
ok_sql(hstmt, "DROP TABLE IF EXISTS t_bug16920750c, t_bug16920750b, t_bug16920750a");
442446
ok_sql(hstmt, "CREATE TABLE t_bug16920750a (category INT NOT NULL, id INT NOT NULL, price DECIMAL, "
443447
"PRIMARY KEY(category, id) ) ENGINE=INNODB;");
444448
ok_sql(hstmt, "CREATE TABLE t_bug16920750b (id INT NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB");
445449
ok_sql(hstmt, "CREATE TABLE t_bug16920750c (no INT NOT NULL AUTO_INCREMENT, product_category "
446450
"INT NOT NULL, product_id INT NOT NULL, customer_id INT NOT NULL, "
447451
"PRIMARY KEY(no), INDEX (product_category, product_id), INDEX (customer_id), "
448-
"CONSTRAINT `constraint_1` FOREIGN KEY (product_category, product_id) "
452+
"CONSTRAINT `constraint__1` FOREIGN KEY (product_category, product_id) "
449453
"REFERENCES t_bug16920750a(category, id) ON UPDATE CASCADE ON DELETE RESTRICT, "
450-
"CONSTRAINT `constraint_2` FOREIGN KEY (customer_id) "
454+
"CONSTRAINT `constraint__2` FOREIGN KEY (customer_id) "
451455
"REFERENCES t_bug16920750b(id) ) ENGINE=InnoDB");
452456

453457
ok_stmt(hstmt, SQLForeignKeys(hstmt, NULL, 0, NULL, 0, NULL, 0, NULL, 0,
@@ -458,7 +462,7 @@ DECLARE_TEST(t_bug16920750)
458462
is_str(my_fetch_str(hstmt, buff, 4), "category", 8);
459463
is_str(my_fetch_str(hstmt, buff, 7), "t_bug16920750c", 14);
460464
is_str(my_fetch_str(hstmt, buff, 8), "product_category", 16);
461-
is_str(my_fetch_str(hstmt, buff, 12), "constraint_1", 12);
465+
is_str(my_fetch_str(hstmt, buff, 12), "constraint__1", 12);
462466
is_str(my_fetch_str(hstmt, buff, 10), "0", 1);
463467
is_str(my_fetch_str(hstmt, buff, 11), "3", 1);
464468

@@ -467,7 +471,7 @@ DECLARE_TEST(t_bug16920750)
467471
is_str(my_fetch_str(hstmt, buff, 4), "id", 2);
468472
is_str(my_fetch_str(hstmt, buff, 7), "t_bug16920750c", 14);
469473
is_str(my_fetch_str(hstmt, buff, 8), "customer_id", 11);
470-
is_str(my_fetch_str(hstmt, buff, 12), "constraint_2", 12);
474+
is_str(my_fetch_str(hstmt, buff, 12), "constraint__2", 12);
471475
is_str(my_fetch_str(hstmt, buff, 10), "3", 1);
472476
is_str(my_fetch_str(hstmt, buff, 11), "3", 1);
473477

@@ -476,7 +480,7 @@ DECLARE_TEST(t_bug16920750)
476480
is_str(my_fetch_str(hstmt, buff, 4), "id", 2);
477481
is_str(my_fetch_str(hstmt, buff, 7), "t_bug16920750c", 14);
478482
is_str(my_fetch_str(hstmt, buff, 8), "product_id", 10);
479-
is_str(my_fetch_str(hstmt, buff, 12), "constraint_1", 12);
483+
is_str(my_fetch_str(hstmt, buff, 12), "constraint__1", 12);
480484
is_str(my_fetch_str(hstmt, buff, 10), "0", 1);
481485
is_str(my_fetch_str(hstmt, buff, 11), "3", 1);
482486

0 commit comments

Comments
 (0)