From 3e6cf593845bc8a25450efbe73221873677ea0f7 Mon Sep 17 00:00:00 2001 From: Daniel Frey Date: Thu, 2 Jan 2025 22:32:51 +0100 Subject: [PATCH] Secure pipeline ctor --- include/tao/pq/pipeline.hpp | 12 +++++++++++- src/lib/pq/pipeline.cpp | 2 +- src/lib/pq/transaction.cpp | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/tao/pq/pipeline.hpp b/include/tao/pq/pipeline.hpp index 8bc5bf5..e938b70 100644 --- a/include/tao/pq/pipeline.hpp +++ b/include/tao/pq/pipeline.hpp @@ -13,6 +13,7 @@ namespace tao::pq { class connection; + class transaction; class pipeline : public transaction_base @@ -20,8 +21,17 @@ namespace tao::pq private: std::shared_ptr< transaction_base > m_previous; + friend class transaction; + + // pass-key idiom + class private_key final + { + private_key() = default; + friend class transaction; + }; + public: - explicit pipeline( const std::shared_ptr< pq::connection >& connection ); + pipeline( const private_key /*unused*/, const std::shared_ptr< pq::connection >& connection ); ~pipeline() override { diff --git a/src/lib/pq/pipeline.cpp b/src/lib/pq/pipeline.cpp index 3ce9669..2a1f170 100644 --- a/src/lib/pq/pipeline.cpp +++ b/src/lib/pq/pipeline.cpp @@ -10,7 +10,7 @@ namespace tao::pq { - pipeline::pipeline( const std::shared_ptr< pq::connection >& connection ) + pipeline::pipeline( const pipeline::private_key /*unused*/, const std::shared_ptr< pq::connection >& connection ) : transaction_base( connection ), m_previous( current_transaction()->shared_from_this() ) { diff --git a/src/lib/pq/transaction.cpp b/src/lib/pq/transaction.cpp index aad8d0c..e171dd9 100644 --- a/src/lib/pq/transaction.cpp +++ b/src/lib/pq/transaction.cpp @@ -107,7 +107,7 @@ namespace tao::pq auto transaction::pipeline() -> std::shared_ptr< pq::pipeline > { check_current_transaction(); - return std::make_shared< pq::pipeline >( m_connection ); + return std::make_shared< pq::pipeline >( pq::pipeline::private_key(), m_connection ); } void transaction::commit()