From c5c7a9d1989438029149f0c7c289b5d9c75287e3 Mon Sep 17 00:00:00 2001 From: Jan de Visser Date: Sun, 22 Aug 2021 13:26:15 -0400 Subject: [PATCH] SQLServer: Do not capture stack variables by reference in lambdas If you capture a stack variable by reference in a lamdba definition, and this lambda outlives the scope of the stack variable, this reference may point to garbage when the lambda is executed. Therefore capture as little as possible (typically only ``this``), and what is captured is captured by value --- Userland/Services/SQLServer/DatabaseConnection.cpp | 4 ++-- Userland/Services/SQLServer/SQLStatement.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Userland/Services/SQLServer/DatabaseConnection.cpp b/Userland/Services/SQLServer/DatabaseConnection.cpp index 4f5d032422..e83fccc925 100644 --- a/Userland/Services/SQLServer/DatabaseConnection.cpp +++ b/Userland/Services/SQLServer/DatabaseConnection.cpp @@ -38,7 +38,7 @@ DatabaseConnection::DatabaseConnection(String database_name, int client_id) dbgln_if(SQLSERVER_DEBUG, "DatabaseConnection {} initiating connection with database '{}'", connection_id(), m_database_name); s_connections.set(m_connection_id, *this); - deferred_invoke([&] { + deferred_invoke([this]() { m_database = SQL::Database::construct(String::formatted("/home/anon/sql/{}.db", m_database_name)); m_accept_statements = true; auto client_connection = ClientConnection::client_connection_for(m_client_id); @@ -53,7 +53,7 @@ void DatabaseConnection::disconnect() { dbgln_if(SQLSERVER_DEBUG, "DatabaseConnection::disconnect(connection_id {}, database '{}'", connection_id(), m_database_name); m_accept_statements = false; - deferred_invoke([&] { + deferred_invoke([this]() { m_database = nullptr; s_connections.remove(m_connection_id); auto client_connection = ClientConnection::client_connection_for(client_id()); diff --git a/Userland/Services/SQLServer/SQLStatement.cpp b/Userland/Services/SQLServer/SQLStatement.cpp index 6b8ed2f525..87c726beee 100644 --- a/Userland/Services/SQLServer/SQLStatement.cpp +++ b/Userland/Services/SQLServer/SQLStatement.cpp @@ -60,7 +60,7 @@ void SQLStatement::execute() return; } - deferred_invoke([&] { + deferred_invoke([this]() { auto maybe_error = parse(); if (maybe_error.has_value()) { report_error(maybe_error.value()); @@ -107,7 +107,7 @@ void SQLStatement::next() if (m_index < m_result->results().size()) { auto& tuple = m_result->results()[m_index++]; client_connection->async_next_result(statement_id(), tuple.to_string_vector()); - deferred_invoke([&] { + deferred_invoke([this]() { next(); }); } else {