From c517ef2cfcce5f592ba4f52224d7fcfb4fca6a59 Mon Sep 17 00:00:00 2001 From: montanalow <montanalow@gmail.com> Date: Tue, 4 Dec 2018 19:41:10 -0800 Subject: [PATCH 1/5] improve ddl statement detection against leading commentss --- Modules/_sqlite/statement.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c index 78033d8efcaed1..d5b0bafe89491d 100644 --- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -55,6 +55,8 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con const char* sql_cstr; Py_ssize_t sql_cstr_len; const char* p; + unsigned char multi_line_comment; + unsigned char single_line_comment; self->st = NULL; self->in_use = 0; @@ -76,7 +78,31 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con /* Determine if the statement is a DML statement. SELECT is the only exception. See #9924. */ self->is_dml = 0; + single_line_comment = 0; + multi_line_comment = 0; for (p = sql_cstr; *p != 0; p++) { + // skip leading comments + if (single_line_comment && (*p == '\n')) { + single_line_comment = 0; + continue; + } else if (multi_line_comment && PyOS_strnicmp(p, "*/", 2) == 0) { + multi_line_comment = 0; + p++; + continue; + } + if (single_line_comment || multi_line_comment) { + continue; + } + // detect leading comments + if (PyOS_strnicmp(p, "--", 2) == 0) { + single_line_comment = 1; + continue; + } else if (PyOS_strnicmp(p, "/*", 2) == 0) { + multi_line_comment = 1; + p++; + continue; + } + // skip leading whitespace switch (*p) { case ' ': case '\r': From 39c742f10624343be25e80e5461b8b1fccaf16c6 Mon Sep 17 00:00:00 2001 From: montanalow <montanalow@gmail.com> Date: Tue, 4 Dec 2018 20:03:06 -0800 Subject: [PATCH 2/5] convert tabs to spaces --- Modules/_sqlite/statement.c | 50 ++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c index d5b0bafe89491d..60e1173a34cf58 100644 --- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -55,8 +55,8 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con const char* sql_cstr; Py_ssize_t sql_cstr_len; const char* p; - unsigned char multi_line_comment; - unsigned char single_line_comment; + unsigned char multi_line_comment; + unsigned char single_line_comment; self->st = NULL; self->in_use = 0; @@ -78,31 +78,31 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con /* Determine if the statement is a DML statement. SELECT is the only exception. See #9924. */ self->is_dml = 0; - single_line_comment = 0; - multi_line_comment = 0; + single_line_comment = 0; + multi_line_comment = 0; for (p = sql_cstr; *p != 0; p++) { // skip leading comments - if (single_line_comment && (*p == '\n')) { - single_line_comment = 0; - continue; - } else if (multi_line_comment && PyOS_strnicmp(p, "*/", 2) == 0) { - multi_line_comment = 0; - p++; - continue; - } - if (single_line_comment || multi_line_comment) { - continue; - } - // detect leading comments - if (PyOS_strnicmp(p, "--", 2) == 0) { - single_line_comment = 1; - continue; - } else if (PyOS_strnicmp(p, "/*", 2) == 0) { - multi_line_comment = 1; - p++; - continue; - } - // skip leading whitespace + if (single_line_comment && (*p == '\n')) { + single_line_comment = 0; + continue; + } else if (multi_line_comment && PyOS_strnicmp(p, "*/", 2) == 0) { + multi_line_comment = 0; + p++; + continue; + } + if (single_line_comment || multi_line_comment) { + continue; + } + // detect leading comments + if (PyOS_strnicmp(p, "--", 2) == 0) { + single_line_comment = 1; + continue; + } else if (PyOS_strnicmp(p, "/*", 2) == 0) { + multi_line_comment = 1; + p++; + continue; + } + // skip leading whitespace switch (*p) { case ' ': case '\r': From 57d466f27aaa19c5dd86fd49e961ae07f58e9332 Mon Sep 17 00:00:00 2001 From: montanalow <montanalow@gmail.com> Date: Tue, 4 Dec 2018 20:03:49 -0800 Subject: [PATCH 3/5] one more tab --- Modules/_sqlite/statement.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c index 60e1173a34cf58..0e28d06f270e2a 100644 --- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -81,7 +81,7 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con single_line_comment = 0; multi_line_comment = 0; for (p = sql_cstr; *p != 0; p++) { - // skip leading comments + // skip leading comments if (single_line_comment && (*p == '\n')) { single_line_comment = 0; continue; From b112167fc9c355533edf3aac758b8dba07a3181f Mon Sep 17 00:00:00 2001 From: Montana Low <montanalow@users.noreply.github.com> Date: Wed, 5 Dec 2018 09:35:13 -0800 Subject: [PATCH 4/5] Use strcmp instead of unnecessary PyOS_strnicmp --- Modules/_sqlite/statement.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c index 0e28d06f270e2a..675103467f930e 100644 --- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -85,7 +85,7 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con if (single_line_comment && (*p == '\n')) { single_line_comment = 0; continue; - } else if (multi_line_comment && PyOS_strnicmp(p, "*/", 2) == 0) { + } else if (multi_line_comment && strcmp(p, "*/") == 0) { multi_line_comment = 0; p++; continue; @@ -94,10 +94,10 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con continue; } // detect leading comments - if (PyOS_strnicmp(p, "--", 2) == 0) { + if (strcmp(p, "--") == 0) { single_line_comment = 1; continue; - } else if (PyOS_strnicmp(p, "/*", 2) == 0) { + } else if (strcmp(p, "/*") == 0) { multi_line_comment = 1; p++; continue; From 1e8fd4cb45b7f23cc8c00b7cf09208896d1e1ed9 Mon Sep 17 00:00:00 2001 From: Montana Low <montanalow@users.noreply.github.com> Date: Wed, 5 Dec 2018 22:04:33 -0800 Subject: [PATCH 5/5] use strncmp over strcmp --- Modules/_sqlite/statement.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c index 675103467f930e..6403adca3f59c7 100644 --- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -85,7 +85,7 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con if (single_line_comment && (*p == '\n')) { single_line_comment = 0; continue; - } else if (multi_line_comment && strcmp(p, "*/") == 0) { + } else if (multi_line_comment && strncmp(p, "*/", 2) == 0) { multi_line_comment = 0; p++; continue; @@ -94,10 +94,10 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con continue; } // detect leading comments - if (strcmp(p, "--") == 0) { + if (strncmp(p, "--", 2) == 0) { single_line_comment = 1; continue; - } else if (strcmp(p, "/*") == 0) { + } else if (strncmp(p, "/*", 2) == 0) { multi_line_comment = 1; p++; continue;