Skip to content

Commit

Permalink
Merge pull request #475 from cole-miller/prepare-fix
Browse files Browse the repository at this point in the history
gateway: Fix handling of statements with trailing whitepace, comments
  • Loading branch information
Mathieu Borderé committed Feb 10, 2023
2 parents d06dc5f + 2defbd0 commit 690818b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ static void prepareBarrierCb(struct barrier *barrier, int status)
const char *sql = g->sql;
struct stmt *stmt;
const char *tail;
sqlite3_stmt *tail_stmt;
int rc;

assert(req != NULL);
Expand Down Expand Up @@ -319,10 +320,14 @@ static void prepareBarrierCb(struct barrier *barrier, int status)
return;
}

if (req->schema == DQLITE_PREPARE_STMT_SCHEMA_V0 && *tail != '\0') {
stmt__registry_del(&g->stmts, stmt);
failure(req, SQLITE_ERROR, "nonempty statement tail");
return;
if (req->schema == DQLITE_PREPARE_STMT_SCHEMA_V0) {
rc = sqlite3_prepare_v2(g->leader->conn, tail, -1, &tail_stmt, NULL);
if (rc != 0 || tail_stmt != NULL) {
stmt__registry_del(&g->stmts, stmt);
sqlite3_finalize(tail_stmt);
failure(req, SQLITE_ERROR, "nonempty statement tail");
return;
}
}

switch (req->schema) {
Expand Down Expand Up @@ -789,6 +794,7 @@ static void querySqlBarrierCb(struct barrier *barrier, int status)
const char *sql = g->sql;
sqlite3_stmt *stmt;
const char *tail;
sqlite3_stmt *tail_stmt;
int tuple_format;
int rv;

Expand All @@ -815,8 +821,10 @@ static void querySqlBarrierCb(struct barrier *barrier, int status)
return;
}

if (*tail != '\0') {
rv = sqlite3_prepare_v2(g->leader->conn, tail, -1, &tail_stmt, NULL);
if (rv != 0 || tail_stmt != NULL) {
sqlite3_finalize(stmt);
sqlite3_finalize(tail_stmt);
failure(req, SQLITE_ERROR, "nonempty statement tail");
return;
}
Expand Down
15 changes: 15 additions & 0 deletions test/unit/test_gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,21 @@ TEST_CASE(prepare, nonempty_tail, NULL)
return MUNIT_OK;
}

/* Try to prepare a string containing a comment after the statement. */
TEST_CASE(prepare, comment_in_tail, NULL)
{
struct prepare_fixture *f = data;
(void)params;
f->request.db_id = 0;
f->request.sql = "CREATE TABLE test (n INT); /* comment */";
CLUSTER_ELECT(0);
ENCODE(&f->request, prepare);
HANDLE(PREPARE);
WAIT;
ASSERT_CALLBACK(0, STMT);
return MUNIT_OK;
}

/* Try to prepare a string containing more than one statement, successfully. */
TEST_CASE(prepare, nonempty_tail_v1, NULL)
{
Expand Down

0 comments on commit 690818b

Please sign in to comment.