From 011590b20ef57a8bd17cfa6c77063fa4841a0590 Mon Sep 17 00:00:00 2001 From: Michael Schmoock Date: Thu, 24 Sep 2020 14:43:31 +0200 Subject: [PATCH] fix: broken SQL statement in wallet db_set_utxo I discovered this accidentally when using the `tests/plugins/dblog.py` plugin on another testcase: tests/test_connection.py::test_fail_unconfirmed There the plugin/hook crashes because it can't execute the statement: ```json { "jsonrpc": "2.0", "id": 34, "error": { "code": -32600, "message": "Error while processing db_write: unrecognized token: \"174WHERE\"", "traceback": "Traceback (most recent call last):\n File \"/home/will/projects/lightning.git/contrib/pyln-client/pyln/client/plugin.py\", line 535, in _dispatch_request\n result = self._exec_func(method.func, request)\n File \"/home/will/projects/lightning.git/contrib/pyln-client/pyln/client/plugin.py\", line 520, in _exec_func\n return func(*ba.args, **ba.kwargs)\n File \"/home/will/projects/lightning.git/tests/plugins/dblog.py\", line 45, in db_write\n plugin.conn.execute(c)\nsqlite3.OperationalError: unrecognized token: \"174WHERE\"\n" } } ``` Changelog-Fixed: plugin: Regression with SQL statement expansion that could result in invalid statements being passed to the `db_write` hook. --- wallet/db_postgres_sqlgen.c | 6 +++--- wallet/db_sqlite3_sqlgen.c | 6 +++--- wallet/statements_gettextgen.po | 4 ++-- wallet/wallet.c | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/wallet/db_postgres_sqlgen.c b/wallet/db_postgres_sqlgen.c index 83364a10e..9b3df434b 100644 --- a/wallet/db_postgres_sqlgen.c +++ b/wallet/db_postgres_sqlgen.c @@ -1053,8 +1053,8 @@ struct db_query db_postgres_queries[] = { .readonly = true, }, { - .name = "UPDATE outputs SET status=?, reserved_til=?WHERE prev_out_tx=? AND prev_out_index=?", - .query = "UPDATE outputs SET status=$1, reserved_til=$2WHERE prev_out_tx=$3 AND prev_out_index=$4", + .name = "UPDATE outputs SET status=?, reserved_til=? WHERE prev_out_tx=? AND prev_out_index=?", + .query = "UPDATE outputs SET status=$1, reserved_til=$2 WHERE prev_out_tx=$3 AND prev_out_index=$4", .placeholders = 4, .readonly = false, }, @@ -1654,4 +1654,4 @@ struct db_query db_postgres_queries[] = { #endif /* LIGHTNINGD_WALLET_GEN_DB_POSTGRES */ -// SHA256STAMP:fa191e6b54c56b2d4e85a24ca83bc0d41dddec5dbde948e2bc5d627426089ade +// SHA256STAMP:5fa02e1dbf8ea4e155e08671e9f36b66c8a5b8ad04226e6695eacf2ea423b8bd diff --git a/wallet/db_sqlite3_sqlgen.c b/wallet/db_sqlite3_sqlgen.c index fd4f4d1fb..e637e136e 100644 --- a/wallet/db_sqlite3_sqlgen.c +++ b/wallet/db_sqlite3_sqlgen.c @@ -1053,8 +1053,8 @@ struct db_query db_sqlite3_queries[] = { .readonly = true, }, { - .name = "UPDATE outputs SET status=?, reserved_til=?WHERE prev_out_tx=? AND prev_out_index=?", - .query = "UPDATE outputs SET status=?, reserved_til=?WHERE prev_out_tx=? AND prev_out_index=?", + .name = "UPDATE outputs SET status=?, reserved_til=? WHERE prev_out_tx=? AND prev_out_index=?", + .query = "UPDATE outputs SET status=?, reserved_til=? WHERE prev_out_tx=? AND prev_out_index=?", .placeholders = 4, .readonly = false, }, @@ -1654,4 +1654,4 @@ struct db_query db_sqlite3_queries[] = { #endif /* LIGHTNINGD_WALLET_GEN_DB_SQLITE3 */ -// SHA256STAMP:fa191e6b54c56b2d4e85a24ca83bc0d41dddec5dbde948e2bc5d627426089ade +// SHA256STAMP:5fa02e1dbf8ea4e155e08671e9f36b66c8a5b8ad04226e6695eacf2ea423b8bd diff --git a/wallet/statements_gettextgen.po b/wallet/statements_gettextgen.po index c85ef2034..71d1dc99b 100644 --- a/wallet/statements_gettextgen.po +++ b/wallet/statements_gettextgen.po @@ -695,7 +695,7 @@ msgid "SELECT prev_out_tx, prev_out_index, value, type, status, keyindex, chann msgstr "" #: wallet/wallet.c:417 -msgid "UPDATE outputs SET status=?, reserved_til=?WHERE prev_out_tx=? AND prev_out_index=?" +msgid "UPDATE outputs SET status=?, reserved_til=? WHERE prev_out_tx=? AND prev_out_index=?" msgstr "" #: wallet/wallet.c:502 @@ -1089,4 +1089,4 @@ msgstr "" #: wallet/test/run-wallet.c:1359 msgid "INSERT INTO channels (id) VALUES (1);" msgstr "" -# SHA256STAMP:fb0b381451867f44c3f35f2202e741a06ff9f531329facf6e9df302036eff4e4 +# SHA256STAMP:c44c0588e8f2139e6530e926acbd3186bbbd65bec871d5274c8480fd8dcbd519 diff --git a/wallet/wallet.c b/wallet/wallet.c index 6b4f959d8..c47f589d9 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -414,7 +414,7 @@ static void db_set_utxo(struct db *db, const struct utxo *utxo) assert(!utxo->reserved_til); stmt = db_prepare_v2( - db, SQL("UPDATE outputs SET status=?, reserved_til=?" + db, SQL("UPDATE outputs SET status=?, reserved_til=? " "WHERE prev_out_tx=? AND prev_out_index=?")); db_bind_int(stmt, 0, output_status_in_db(utxo->status)); db_bind_int(stmt, 1, utxo->reserved_til);