From 78e61520905168fcca76009e005094898278e4f2 Mon Sep 17 00:00:00 2001 From: Kasper Seweryn Date: Mon, 1 Mar 2021 06:36:34 +0000 Subject: [PATCH 01/19] Fix #324 (#395) * Attempt to patch #324 * Add check for MinGW and code attribution --- .../network/tcp/server/ConnectionProvider.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/oatpp/network/tcp/server/ConnectionProvider.cpp b/src/oatpp/network/tcp/server/ConnectionProvider.cpp index 173b5ca7..c45989fb 100644 --- a/src/oatpp/network/tcp/server/ConnectionProvider.cpp +++ b/src/oatpp/network/tcp/server/ConnectionProvider.cpp @@ -43,6 +43,31 @@ #endif #endif + +// Workaround for MinGW from: https://www.mail-archive.com/users@ipv6.org/msg02107.html +#if defined(__MINGW32__) && _WIN32_WINNT < 0x0600 + const char * inet_ntop (int af, const void *src, char *dst, socklen_t cnt) { + if (af == AF_INET) { + struct sockaddr_in in; + + memset (&in, 0, sizeof(in)); + in.sin_family = AF_INET; + memcpy (&in.sin_addr, src, sizeof(struct in_addr)); + getnameinfo ((struct sockaddr *)&in, sizeof (struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST); + return dst; + } else if (af == AF_INET6) { + struct sockaddr_in6 in; + memset (&in, 0, sizeof(in)); + in.sin6_family = AF_INET6; + memcpy (&in.sin6_addr, src, sizeof(struct in_addr6)); + getnameinfo ((struct sockaddr *)&in, sizeof (struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST); + return dst; + } + + return NULL; + } +#endif + namespace oatpp { namespace network { namespace tcp { namespace server { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// From 24c89a7ce8f57e0bdf46d50ee8afcf5b555099b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt-Alexander=20Mokro=C3=9F?= Date: Mon, 1 Mar 2021 08:50:43 +0100 Subject: [PATCH 02/19] Add hints and advice to the threading-examples in `Server.hpp` and added the link to the warning in `Server.cpp` --- src/oatpp/network/Server.cpp | 2 +- src/oatpp/network/Server.hpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/oatpp/network/Server.cpp b/src/oatpp/network/Server.cpp index 5fce1697..b1f228fe 100644 --- a/src/oatpp/network/Server.cpp +++ b/src/oatpp/network/Server.cpp @@ -115,7 +115,7 @@ void Server::run(std::function conditional) { void Server::run(bool startAsNewThread) { std::unique_lock ul(m_mutex); - OATPP_LOGW("[oatpp::network::server::run(bool)]", "Using oatpp::network::server::run(bool) is deprecated and will be removed in the next release. Please implement your own threading.") + OATPP_LOGW("[oatpp::network::server::run(bool)]", "Using oatpp::network::server::run(bool) is deprecated and will be removed in the next release. Please implement your own threading (See https://github.com/oatpp/oatpp-threaded-starter).") switch (getStatus()) { case STATUS_STARTING: throw std::runtime_error("[oatpp::network::server::run()] Error. Server already starting"); diff --git a/src/oatpp/network/Server.hpp b/src/oatpp/network/Server.hpp index ba93b35a..60d45c7c 100644 --- a/src/oatpp/network/Server.hpp +++ b/src/oatpp/network/Server.hpp @@ -127,6 +127,13 @@ public: * to &id:oatpp::network::ConnectionHandler;. * @param startAsNewThread - Start the server blocking (thread of callee) or non-blocking (own thread) * @deprecated Deprecated since 1.3.0, will be removed in the next release. + * The new repository https://github.com/oatpp/oatpp-threaded-starter shows many configurations how to run Oat++ in its own thread. + * From simple No-Stop to Stop-Simple and ending in Oat++ completely isolated in its own thread-scope. + * We recommend the Stop-Simple for most applications! You can find it here: https://github.com/oatpp/oatpp-threaded-starter/blob/master/src/App_StopSimple.cpp + * The other examples are non trivial and highly specialized on specific environments or requirements. + * Please read the comments carefully and think about the consequences twice. + * If someone wants to use them please get back to us in an issue in the new repository and we can assist you with them. + * Again: These examples introduce special conditions and requirements for your code! */ void run(bool startAsNewThread); From c8996c42d79cb5a58df7b6b53cf1698c5c79fb9a Mon Sep 17 00:00:00 2001 From: boldrij Date: Thu, 22 Apr 2021 20:03:54 +0200 Subject: [PATCH 03/19] Update port after binding Typicaly in case we use a port equal 0 to let the system find a free port, we need to update the port property of the provider --- .../network/tcp/server/ConnectionProvider.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/oatpp/network/tcp/server/ConnectionProvider.cpp b/src/oatpp/network/tcp/server/ConnectionProvider.cpp index c45989fb..bc986aa8 100644 --- a/src/oatpp/network/tcp/server/ConnectionProvider.cpp +++ b/src/oatpp/network/tcp/server/ConnectionProvider.cpp @@ -184,6 +184,13 @@ oatpp::v_io_handle ConnectionProvider::instantiateServer(){ throw std::runtime_error("[oatpp::network::tcp::server::ConnectionProvider::instantiateServer()]: Error. Call to ioctlsocket failed."); } + // Update port after binding (typicaly in case of port = 0) + struct ::sockaddr_in s_in; + ::memset(&s_in, 0, sizeof(s_in)); + ::socklen_t s_in_len = sizeof(s_in); + ::getsockname(serverHandle, (struct sockaddr *)&s_in, &s_in_len); + setProperty(PROPERTY_PORT, oatpp::utils::conversion::int32ToStr(ntohs(s_in.sin_port))) + return serverHandle; } @@ -257,6 +264,13 @@ oatpp::v_io_handle ConnectionProvider::instantiateServer(){ fcntl(serverHandle, F_SETFL, O_NONBLOCK); + // Update port after binding (typicaly in case of port = 0) + struct ::sockaddr_in s_in; + ::memset(&s_in, 0, sizeof(s_in)); + ::socklen_t s_in_len = sizeof(s_in); + ::getsockname(serverHandle, (struct sockaddr *)&s_in, &s_in_len); + setProperty(PROPERTY_PORT, oatpp::utils::conversion::int32ToStr(ntohs(s_in.sin_port))) + return serverHandle; } From 899f675ebfa277793c45192c295cc65065baf001 Mon Sep 17 00:00:00 2001 From: boldrij Date: Thu, 22 Apr 2021 20:10:32 +0200 Subject: [PATCH 04/19] Update ConnectionProvider.cpp forget ; --- src/oatpp/network/tcp/server/ConnectionProvider.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/oatpp/network/tcp/server/ConnectionProvider.cpp b/src/oatpp/network/tcp/server/ConnectionProvider.cpp index bc986aa8..cf251ce0 100644 --- a/src/oatpp/network/tcp/server/ConnectionProvider.cpp +++ b/src/oatpp/network/tcp/server/ConnectionProvider.cpp @@ -189,7 +189,7 @@ oatpp::v_io_handle ConnectionProvider::instantiateServer(){ ::memset(&s_in, 0, sizeof(s_in)); ::socklen_t s_in_len = sizeof(s_in); ::getsockname(serverHandle, (struct sockaddr *)&s_in, &s_in_len); - setProperty(PROPERTY_PORT, oatpp::utils::conversion::int32ToStr(ntohs(s_in.sin_port))) + setProperty(PROPERTY_PORT, oatpp::utils::conversion::int32ToStr(ntohs(s_in.sin_port))); return serverHandle; @@ -269,7 +269,7 @@ oatpp::v_io_handle ConnectionProvider::instantiateServer(){ ::memset(&s_in, 0, sizeof(s_in)); ::socklen_t s_in_len = sizeof(s_in); ::getsockname(serverHandle, (struct sockaddr *)&s_in, &s_in_len); - setProperty(PROPERTY_PORT, oatpp::utils::conversion::int32ToStr(ntohs(s_in.sin_port))) + setProperty(PROPERTY_PORT, oatpp::utils::conversion::int32ToStr(ntohs(s_in.sin_port))); return serverHandle; From f3d513992f25e4d39c871dacc38ca82f683fb071 Mon Sep 17 00:00:00 2001 From: MHaselmaier Date: Sun, 2 May 2021 09:30:59 +0200 Subject: [PATCH 05/19] added missing throw keyword --- src/oatpp/core/async/Executor.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/oatpp/core/async/Executor.cpp b/src/oatpp/core/async/Executor.cpp index 7a1ab2fe..679e1e4b 100644 --- a/src/oatpp/core/async/Executor.cpp +++ b/src/oatpp/core/async/Executor.cpp @@ -54,12 +54,12 @@ void Executor::SubmissionProcessor::run() { void Executor::SubmissionProcessor::pushTasks(oatpp::collection::FastQueue& tasks) { (void)tasks; - std::runtime_error("[oatpp::async::Executor::SubmissionProcessor::pushTasks]: Error. This method does nothing."); + throw std::runtime_error("[oatpp::async::Executor::SubmissionProcessor::pushTasks]: Error. This method does nothing."); } void Executor::SubmissionProcessor::pushOneTask(CoroutineHandle* task) { (void)task; - std::runtime_error("[oatpp::async::Executor::SubmissionProcessor::pushOneTask]: Error. This method does nothing."); + throw std::runtime_error("[oatpp::async::Executor::SubmissionProcessor::pushOneTask]: Error. This method does nothing."); } void Executor::SubmissionProcessor::stop() { From 250d399d64266fd534f6d49ec5c2b89303f008c5 Mon Sep 17 00:00:00 2001 From: MHaselmaier Date: Sun, 2 May 2021 11:56:09 +0200 Subject: [PATCH 06/19] Added missing override specifier --- src/oatpp/core/data/stream/BufferStream.cpp | 2 +- src/oatpp/core/data/stream/Stream.cpp | 2 +- src/oatpp/web/protocol/http/outgoing/Request.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/oatpp/core/data/stream/BufferStream.cpp b/src/oatpp/core/data/stream/BufferStream.cpp index f966e21e..f29322ab 100644 --- a/src/oatpp/core/data/stream/BufferStream.cpp +++ b/src/oatpp/core/data/stream/BufferStream.cpp @@ -147,7 +147,7 @@ oatpp::async::CoroutineStarter BufferOutputStream::flushToStreamAsync(const std: , m_stream(stream) {} - Action act() { + virtual Action act() override { if(m_inlineData.currBufferPtr == nullptr) { m_inlineData.currBufferPtr = m_this->m_data; m_inlineData.bytesLeft = m_this->m_position; diff --git a/src/oatpp/core/data/stream/Stream.cpp b/src/oatpp/core/data/stream/Stream.cpp index ac82dc02..031551f8 100644 --- a/src/oatpp/core/data/stream/Stream.cpp +++ b/src/oatpp/core/data/stream/Stream.cpp @@ -119,7 +119,7 @@ async::CoroutineStarter WriteCallback::writeExactSizeDataAsync(const void* data, , m_inlineData(data, size) {} - Action act() { + virtual Action act() override { return m_this->writeExactSizeDataAsyncInline(m_inlineData, finish()); } diff --git a/src/oatpp/web/protocol/http/outgoing/Request.cpp b/src/oatpp/web/protocol/http/outgoing/Request.cpp index e2eb4fc0..f2f9211a 100644 --- a/src/oatpp/web/protocol/http/outgoing/Request.cpp +++ b/src/oatpp/web/protocol/http/outgoing/Request.cpp @@ -165,7 +165,7 @@ oatpp::async::CoroutineStarter Request::sendAsync(std::shared_ptr _this , m_headersWriteBuffer(std::make_shared()) {} - Action act() { + virtual Action act() override { v_buff_size bodySize = -1; From 91c3818a64ab07b0e633a63b78505050d0075785 Mon Sep 17 00:00:00 2001 From: MHaselmaier Date: Sun, 2 May 2021 11:57:26 +0200 Subject: [PATCH 07/19] Replaced goto statement with break --- src/oatpp/core/async/Processor.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/oatpp/core/async/Processor.cpp b/src/oatpp/core/async/Processor.cpp index 591af5ef..848e325d 100644 --- a/src/oatpp/core/async/Processor.cpp +++ b/src/oatpp/core/async/Processor.cpp @@ -180,7 +180,7 @@ bool Processor::iterate(v_int32 numIterations) { auto CP = m_queue.first; if (CP == nullptr) { - goto end_loop; + break; } if (CP->finished()) { m_queue.popFrontNoData(); @@ -217,8 +217,6 @@ bool Processor::iterate(v_int32 numIterations) { } - end_loop: - popTasks(); std::lock_guard lock(m_taskLock); From 7ca0ec7875a795ed1ded6d407dc6e08400eb189d Mon Sep 17 00:00:00 2001 From: MHaselmaier Date: Sun, 2 May 2021 11:59:49 +0200 Subject: [PATCH 08/19] Declared variable when used, not a the start of function --- src/oatpp/network/tcp/server/ConnectionProvider.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/oatpp/network/tcp/server/ConnectionProvider.cpp b/src/oatpp/network/tcp/server/ConnectionProvider.cpp index cf251ce0..36bb2c04 100644 --- a/src/oatpp/network/tcp/server/ConnectionProvider.cpp +++ b/src/oatpp/network/tcp/server/ConnectionProvider.cpp @@ -122,8 +122,6 @@ void ConnectionProvider::stop() { oatpp::v_io_handle ConnectionProvider::instantiateServer(){ - int iResult; - SOCKET serverHandle = INVALID_SOCKET; struct addrinfo *result = nullptr; @@ -143,7 +141,7 @@ oatpp::v_io_handle ConnectionProvider::instantiateServer(){ auto portStr = oatpp::utils::conversion::int32ToStr(m_address.port); - iResult = getaddrinfo(m_address.host->c_str(), portStr->c_str(), &hints, &result); + const int iResult = getaddrinfo(m_address.host->c_str(), portStr->c_str(), &hints, &result); if (iResult != 0) { OATPP_LOGE("[oatpp::network::tcp::server::ConnectionProvider::instantiateServer()]", "Error. Call to getaddrinfo() failed with result=%d", iResult); throw std::runtime_error("[oatpp::network::tcp::server::ConnectionProvider::instantiateServer()]: Error. Call to getaddrinfo() failed."); From 9fb654b8fb0ab77bd389aa8367e740a7be8a574e Mon Sep 17 00:00:00 2001 From: MHaselmaier Date: Sun, 2 May 2021 14:23:31 +0200 Subject: [PATCH 09/19] Removed superfluous virutal --- src/oatpp/core/data/stream/BufferStream.cpp | 2 +- src/oatpp/core/data/stream/Stream.cpp | 2 +- src/oatpp/web/protocol/http/outgoing/Request.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/oatpp/core/data/stream/BufferStream.cpp b/src/oatpp/core/data/stream/BufferStream.cpp index f29322ab..b745f099 100644 --- a/src/oatpp/core/data/stream/BufferStream.cpp +++ b/src/oatpp/core/data/stream/BufferStream.cpp @@ -147,7 +147,7 @@ oatpp::async::CoroutineStarter BufferOutputStream::flushToStreamAsync(const std: , m_stream(stream) {} - virtual Action act() override { + Action act() override { if(m_inlineData.currBufferPtr == nullptr) { m_inlineData.currBufferPtr = m_this->m_data; m_inlineData.bytesLeft = m_this->m_position; diff --git a/src/oatpp/core/data/stream/Stream.cpp b/src/oatpp/core/data/stream/Stream.cpp index 031551f8..d23ee35b 100644 --- a/src/oatpp/core/data/stream/Stream.cpp +++ b/src/oatpp/core/data/stream/Stream.cpp @@ -119,7 +119,7 @@ async::CoroutineStarter WriteCallback::writeExactSizeDataAsync(const void* data, , m_inlineData(data, size) {} - virtual Action act() override { + Action act() override { return m_this->writeExactSizeDataAsyncInline(m_inlineData, finish()); } diff --git a/src/oatpp/web/protocol/http/outgoing/Request.cpp b/src/oatpp/web/protocol/http/outgoing/Request.cpp index f2f9211a..afa0803c 100644 --- a/src/oatpp/web/protocol/http/outgoing/Request.cpp +++ b/src/oatpp/web/protocol/http/outgoing/Request.cpp @@ -165,7 +165,7 @@ oatpp::async::CoroutineStarter Request::sendAsync(std::shared_ptr _this , m_headersWriteBuffer(std::make_shared()) {} - virtual Action act() override { + Action act() override { v_buff_size bodySize = -1; From 881a3b9b492ee5d9d656c1da0d3252d9e80fdc57 Mon Sep 17 00:00:00 2001 From: MHaselmaier Date: Sun, 2 May 2021 16:03:26 +0200 Subject: [PATCH 10/19] Added missing virtual destructor. Removed superflous virtual. --- src/oatpp/core/async/Coroutine.hpp | 2 +- src/oatpp/core/data/mapping/type/Enum.hpp | 2 ++ src/oatpp/core/data/mapping/type/List.hpp | 2 ++ src/oatpp/core/data/mapping/type/Object.hpp | 2 ++ src/oatpp/core/data/mapping/type/PairList.hpp | 2 ++ src/oatpp/core/data/mapping/type/Type.hpp | 3 +++ src/oatpp/core/data/mapping/type/UnorderedMap.hpp | 2 ++ src/oatpp/core/data/mapping/type/UnorderedSet.hpp | 2 ++ src/oatpp/core/data/mapping/type/Vector.hpp | 2 ++ src/oatpp/web/client/HttpRequestExecutor.hpp | 2 +- 10 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/oatpp/core/async/Coroutine.hpp b/src/oatpp/core/async/Coroutine.hpp index 0d9cdee7..73bd729b 100644 --- a/src/oatpp/core/async/Coroutine.hpp +++ b/src/oatpp/core/async/Coroutine.hpp @@ -759,7 +759,7 @@ public: * @param ptr - pointer of the function to call. * @return - Action. */ - virtual Action call(const AbstractCoroutine::FunctionPtr& ptr) override { + Action call(const AbstractCoroutine::FunctionPtr& ptr) override { Function f = static_cast(ptr); return (static_cast(this)->*f)(); } diff --git a/src/oatpp/core/data/mapping/type/Enum.hpp b/src/oatpp/core/data/mapping/type/Enum.hpp index 202c3542..b2066d64 100644 --- a/src/oatpp/core/data/mapping/type/Enum.hpp +++ b/src/oatpp/core/data/mapping/type/Enum.hpp @@ -87,6 +87,8 @@ namespace __class { : notNull(pNotNull) {} + virtual ~PolymorphicDispatcher() = default; + const bool notNull; virtual type::Void createObject() const = 0; diff --git a/src/oatpp/core/data/mapping/type/List.hpp b/src/oatpp/core/data/mapping/type/List.hpp index 9d657dda..fa95db3b 100644 --- a/src/oatpp/core/data/mapping/type/List.hpp +++ b/src/oatpp/core/data/mapping/type/List.hpp @@ -51,6 +51,8 @@ namespace __class { class PolymorphicDispatcher { public: + virtual ~PolymorphicDispatcher() = default; + virtual type::Void createObject() const = 0; /** diff --git a/src/oatpp/core/data/mapping/type/Object.hpp b/src/oatpp/core/data/mapping/type/Object.hpp index 776a50d4..75a36bca 100644 --- a/src/oatpp/core/data/mapping/type/Object.hpp +++ b/src/oatpp/core/data/mapping/type/Object.hpp @@ -176,6 +176,8 @@ namespace __class { class PolymorphicDispatcher { public: + + virtual ~PolymorphicDispatcher() = default; virtual type::Void createObject() const = 0; diff --git a/src/oatpp/core/data/mapping/type/PairList.hpp b/src/oatpp/core/data/mapping/type/PairList.hpp index d65ba9e2..765d65d8 100644 --- a/src/oatpp/core/data/mapping/type/PairList.hpp +++ b/src/oatpp/core/data/mapping/type/PairList.hpp @@ -52,6 +52,8 @@ namespace __class { class PolymorphicDispatcher { public: + virtual ~PolymorphicDispatcher() = default; + virtual type::Void createObject() const = 0; /** diff --git a/src/oatpp/core/data/mapping/type/Type.hpp b/src/oatpp/core/data/mapping/type/Type.hpp index 14591499..8a1d3f78 100644 --- a/src/oatpp/core/data/mapping/type/Type.hpp +++ b/src/oatpp/core/data/mapping/type/Type.hpp @@ -230,6 +230,9 @@ public: */ class AbstractInterpretation { public: + + virtual ~AbstractInterpretation() = default; + /** * Convert the object to its interpretation. * @param originalValue diff --git a/src/oatpp/core/data/mapping/type/UnorderedMap.hpp b/src/oatpp/core/data/mapping/type/UnorderedMap.hpp index 78e26f52..9828514e 100644 --- a/src/oatpp/core/data/mapping/type/UnorderedMap.hpp +++ b/src/oatpp/core/data/mapping/type/UnorderedMap.hpp @@ -51,6 +51,8 @@ namespace __class { */ class PolymorphicDispatcher { public: + + virtual ~PolymorphicDispatcher() = default; virtual type::Void createObject() const = 0; diff --git a/src/oatpp/core/data/mapping/type/UnorderedSet.hpp b/src/oatpp/core/data/mapping/type/UnorderedSet.hpp index a0344c9a..ea0909e0 100644 --- a/src/oatpp/core/data/mapping/type/UnorderedSet.hpp +++ b/src/oatpp/core/data/mapping/type/UnorderedSet.hpp @@ -51,6 +51,8 @@ namespace __class { class PolymorphicDispatcher { public: + virtual ~PolymorphicDispatcher() = default; + virtual type::Void createObject() const = 0; /** diff --git a/src/oatpp/core/data/mapping/type/Vector.hpp b/src/oatpp/core/data/mapping/type/Vector.hpp index 1b8ebd97..14c20e54 100644 --- a/src/oatpp/core/data/mapping/type/Vector.hpp +++ b/src/oatpp/core/data/mapping/type/Vector.hpp @@ -51,6 +51,8 @@ namespace __class { class PolymorphicDispatcher { public: + virtual ~PolymorphicDispatcher() = default; + virtual type::Void createObject() const = 0; /** diff --git a/src/oatpp/web/client/HttpRequestExecutor.hpp b/src/oatpp/web/client/HttpRequestExecutor.hpp index bc5c66be..aed2520c 100644 --- a/src/oatpp/web/client/HttpRequestExecutor.hpp +++ b/src/oatpp/web/client/HttpRequestExecutor.hpp @@ -107,7 +107,7 @@ public: * Invalidate connection. * @param connectionHandle */ - virtual void invalidateConnection(const std::shared_ptr& connectionHandle) override; + void invalidateConnection(const std::shared_ptr& connectionHandle) override; /** * Execute http request. From 19e9c59b6239ace1709b6539513c2b397d2f80aa Mon Sep 17 00:00:00 2001 From: MHaselmaier Date: Mon, 3 May 2021 12:07:03 +0200 Subject: [PATCH 11/19] Used perfect forwarding in Coroutine::start --- src/oatpp/core/async/Coroutine.hpp | 4 ++-- src/oatpp/web/protocol/http/outgoing/Request.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/oatpp/core/async/Coroutine.hpp b/src/oatpp/core/async/Coroutine.hpp index 73bd729b..93b8f3dc 100644 --- a/src/oatpp/core/async/Coroutine.hpp +++ b/src/oatpp/core/async/Coroutine.hpp @@ -597,8 +597,8 @@ public: * @return - &id:oatpp::async::CoroutineStarter;. */ template - static CoroutineStarter start(ConstructorArgs... args) { - return new T(args...); + static CoroutineStarter start(ConstructorArgs&&... args) { + return new T(std::forward(args)...); } /** diff --git a/src/oatpp/web/protocol/http/outgoing/Request.cpp b/src/oatpp/web/protocol/http/outgoing/Request.cpp index afa0803c..72dca83c 100644 --- a/src/oatpp/web/protocol/http/outgoing/Request.cpp +++ b/src/oatpp/web/protocol/http/outgoing/Request.cpp @@ -158,9 +158,9 @@ oatpp::async::CoroutineStarter Request::sendAsync(std::shared_ptr _this std::shared_ptr m_headersWriteBuffer; public: - SendAsyncCoroutine(const std::shared_ptr& request, + SendAsyncCoroutine(std::shared_ptr request, const std::shared_ptr& stream) - : m_this(request) + : m_this(std::move(request)) , m_stream(stream) , m_headersWriteBuffer(std::make_shared()) {} @@ -231,7 +231,7 @@ oatpp::async::CoroutineStarter Request::sendAsync(std::shared_ptr _this }; - return SendAsyncCoroutine::start(_this, stream); + return SendAsyncCoroutine::start(std::move(_this), stream); } From ffcc180dc19e0bb0141b2fd7520cc2b9efa553de Mon Sep 17 00:00:00 2001 From: MHaselmaier Date: Mon, 3 May 2021 12:10:21 +0200 Subject: [PATCH 12/19] Removed unesseccary copies --- src/oatpp/core/async/Executor.cpp | 7 ++++--- src/oatpp/web/mime/multipart/Part.cpp | 4 ++-- src/oatpp/web/mime/multipart/Part.hpp | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/oatpp/core/async/Executor.cpp b/src/oatpp/core/async/Executor.cpp index 679e1e4b..9237d408 100644 --- a/src/oatpp/core/async/Executor.cpp +++ b/src/oatpp/core/async/Executor.cpp @@ -94,7 +94,7 @@ Executor::Executor(v_int32 processorWorkersCount, v_int32 ioWorkersCount, v_int3 m_allWorkers.insert(m_allWorkers.end(), m_processorWorkers.begin(), m_processorWorkers.end()); std::vector> ioWorkers; - + ioWorkers.reserve(ioWorkersCount); switch(ioWorkerType) { case IO_WORKER_TYPE_NAIVE: { @@ -119,6 +119,7 @@ Executor::Executor(v_int32 processorWorkersCount, v_int32 ioWorkersCount, v_int3 linkWorkers(ioWorkers); std::vector> timerWorkers; + timerWorkers.reserve(timerWorkersCount); for(v_int32 i = 0; i < timerWorkersCount; i++) { timerWorkers.push_back(std::make_shared()); } @@ -233,10 +234,10 @@ void Executor::stop() { } v_int32 Executor::getTasksCount() { - + v_int32 result = 0; - for(auto procWorker : m_processorWorkers) { + for(const auto& procWorker : m_processorWorkers) { result += procWorker->getProcessor().getTasksCount(); } diff --git a/src/oatpp/web/mime/multipart/Part.cpp b/src/oatpp/web/mime/multipart/Part.cpp index 1ad9044c..21b7a911 100644 --- a/src/oatpp/web/mime/multipart/Part.cpp +++ b/src/oatpp/web/mime/multipart/Part.cpp @@ -33,7 +33,7 @@ namespace oatpp { namespace web { namespace mime { namespace multipart { Part::Part(const Headers &headers, const std::shared_ptr &inputStream, - const oatpp::String inMemoryData, + const oatpp::String& inMemoryData, v_int64 knownSize) : m_headers(headers) , m_inputStream(inputStream) @@ -60,7 +60,7 @@ Part::Part(const Headers& headers) : Part(headers, nullptr, nullptr, -1) {} Part::Part() : Part(Headers(), nullptr, nullptr, -1) {} void Part::setDataInfo(const std::shared_ptr& inputStream, - const oatpp::String inMemoryData, + const oatpp::String& inMemoryData, v_int64 knownSize) { m_inputStream = inputStream; diff --git a/src/oatpp/web/mime/multipart/Part.hpp b/src/oatpp/web/mime/multipart/Part.hpp index ba0bc9e4..7cbd597a 100644 --- a/src/oatpp/web/mime/multipart/Part.hpp +++ b/src/oatpp/web/mime/multipart/Part.hpp @@ -61,7 +61,7 @@ public: */ Part(const Headers& headers, const std::shared_ptr& inputStream, - const oatpp::String inMemoryData, + const oatpp::String& inMemoryData, v_int64 knownSize); /** @@ -82,7 +82,7 @@ public: * @param knownSize - known size of the data in the input stream. Pass `-1` value if size is unknown. */ void setDataInfo(const std::shared_ptr& inputStream, - const oatpp::String inMemoryData, + const oatpp::String& inMemoryData, v_int64 knownSize); /** From 25a7d4c8bfb4676c420e03c6272ad9c4fe5c5b3b Mon Sep 17 00:00:00 2001 From: MHaselmaier Date: Mon, 3 May 2021 12:12:43 +0200 Subject: [PATCH 13/19] Utilized std::vector constructor to default initialize n elements instead of calling push_back n times --- src/oatpp/core/data/buffer/Processor.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/oatpp/core/data/buffer/Processor.cpp b/src/oatpp/core/data/buffer/Processor.cpp index b5db589a..4b8860ce 100644 --- a/src/oatpp/core/data/buffer/Processor.cpp +++ b/src/oatpp/core/data/buffer/Processor.cpp @@ -86,11 +86,8 @@ void InlineWriteData::setEof() { // ProcessingPipeline ProcessingPipeline::ProcessingPipeline(const std::vector>& processors) - : m_processors(processors) + : m_processors(processors), m_intermediateData(processors.size() - 1) { - for(v_int32 i = 0; i < (v_int32) m_processors.size() - 1; i ++) { - m_intermediateData.push_back(data::buffer::InlineReadData()); - } } v_io_size ProcessingPipeline::suggestInputStreamReadSize() { From 4f1e0b81d94d39deea3ffc686530c25509f2bbcb Mon Sep 17 00:00:00 2001 From: MHaselmaier Date: Mon, 3 May 2021 21:59:43 +0200 Subject: [PATCH 14/19] Fixed memory leak in move assignement operators --- src/oatpp/core/async/Coroutine.cpp | 30 +++++++++++++++++++----------- src/oatpp/core/async/Coroutine.hpp | 8 ++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/oatpp/core/async/Coroutine.cpp b/src/oatpp/core/async/Coroutine.cpp index 5794d405..9fccfca0 100644 --- a/src/oatpp/core/async/Coroutine.cpp +++ b/src/oatpp/core/async/Coroutine.cpp @@ -168,23 +168,16 @@ CoroutineStarter::CoroutineStarter(CoroutineStarter&& other) } CoroutineStarter::~CoroutineStarter() { - if(m_first != nullptr) { - auto curr = m_first; - while(curr != nullptr) { - AbstractCoroutine* next = nullptr; - if(curr->m_parentReturnAction.m_type == Action::TYPE_COROUTINE) { - next = curr->m_parentReturnAction.m_data.coroutine; - } - delete curr; - curr = next; - } - } + freeCoroutines(); } /* * Move assignment operator. */ CoroutineStarter& CoroutineStarter::operator=(CoroutineStarter&& other) { + if (this == std::addressof(other)) return *this; + + freeCoroutines(); m_first = other.m_first; m_last = other.m_last; other.m_first = nullptr; @@ -216,6 +209,21 @@ CoroutineStarter& CoroutineStarter::next(CoroutineStarter&& starter) { return *this; } +void CoroutineStarter::freeCoroutines() +{ + if (m_first != nullptr) { + auto curr = m_first; + while (curr != nullptr) { + AbstractCoroutine* next = nullptr; + if (curr->m_parentReturnAction.m_type == Action::TYPE_COROUTINE) { + next = curr->m_parentReturnAction.m_data.coroutine; + } + delete curr; + curr = next; + } + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // CoroutineHandle diff --git a/src/oatpp/core/async/Coroutine.hpp b/src/oatpp/core/async/Coroutine.hpp index 93b8f3dc..77dd05d7 100644 --- a/src/oatpp/core/async/Coroutine.hpp +++ b/src/oatpp/core/async/Coroutine.hpp @@ -341,6 +341,11 @@ class CoroutineStarter { private: AbstractCoroutine* m_first; AbstractCoroutine* m_last; + +private: + + void freeCoroutines(); + public: /** @@ -686,6 +691,9 @@ public: * Move assignment operator. */ StarterForResult& operator=(StarterForResult&& other) { + if (this == std::addressof(other)) return *this; + + delete m_coroutine; m_coroutine = other.m_coroutine; other.m_coroutine = nullptr; return *this; From 185464b15502ebc1224fefdc7f9eaa33b0216d00 Mon Sep 17 00:00:00 2001 From: MHaselmaier Date: Thu, 6 May 2021 21:27:35 +0200 Subject: [PATCH 15/19] Fixed compiler warning for unreferenced local variables and truncations on MSVC --- src/oatpp/core/data/mapping/type/Enum.hpp | 4 ++-- test/oatpp/core/data/mapping/TypeResolverTest.cpp | 2 +- test/oatpp/core/data/mapping/type/AnyTest.cpp | 2 +- test/oatpp/core/data/mapping/type/PrimitiveTest.cpp | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/oatpp/core/data/mapping/type/Enum.hpp b/src/oatpp/core/data/mapping/type/Enum.hpp index b2066d64..14dd1726 100644 --- a/src/oatpp/core/data/mapping/type/Enum.hpp +++ b/src/oatpp/core/data/mapping/type/Enum.hpp @@ -497,7 +497,7 @@ Void EnumInterpreterAsString::fromInterpretation(const Void& interVa try { const auto &entry = EnumOW::getEntryByName(interValue.staticCast()); return EnumOW(entry.value); - } catch (const std::runtime_error& e) { // TODO - add a specific error for this. + } catch (const std::runtime_error&) { // TODO - add a specific error for this. error = EnumInterpreterError::ENTRY_NOT_FOUND; } return Void(nullptr, EnumOW::Class::getType()); @@ -558,7 +558,7 @@ Void EnumInterpreterAsNumber::fromInterpretation(const Void& interVa interValue.staticCast() ); return EnumOW(entry.value); - } catch (const std::runtime_error& e) { // TODO - add a specific error for this. + } catch (const std::runtime_error&) { // TODO - add a specific error for this. error = EnumInterpreterError::ENTRY_NOT_FOUND; } return Void(nullptr, EnumOW::Class::getType()); diff --git a/test/oatpp/core/data/mapping/TypeResolverTest.cpp b/test/oatpp/core/data/mapping/TypeResolverTest.cpp index d16f986e..3b89f582 100644 --- a/test/oatpp/core/data/mapping/TypeResolverTest.cpp +++ b/test/oatpp/core/data/mapping/TypeResolverTest.cpp @@ -95,7 +95,7 @@ void TypeResolverTest::onRun() { dto1->f_int64 = 64; dto1->f_uint64 = 6464; - dto1->f_float32 = 0.32; + dto1->f_float32 = 0.32f; dto1->f_float64 = 0.64; dto1->f_bool = true; diff --git a/test/oatpp/core/data/mapping/type/AnyTest.cpp b/test/oatpp/core/data/mapping/type/AnyTest.cpp index f497925a..14245e37 100644 --- a/test/oatpp/core/data/mapping/type/AnyTest.cpp +++ b/test/oatpp/core/data/mapping/type/AnyTest.cpp @@ -116,7 +116,7 @@ void AnyTest::onRun() { try { auto obj = any.retrieve>(); // wrong object - } catch (std::runtime_error& e) { + } catch (std::runtime_error&) { wasError = true; } diff --git a/test/oatpp/core/data/mapping/type/PrimitiveTest.cpp b/test/oatpp/core/data/mapping/type/PrimitiveTest.cpp index a120e65c..56c62587 100644 --- a/test/oatpp/core/data/mapping/type/PrimitiveTest.cpp +++ b/test/oatpp/core/data/mapping/type/PrimitiveTest.cpp @@ -42,15 +42,15 @@ void PrimitiveTest::onRun() { { checkHash(oatpp::Boolean(true)); - checkHash(oatpp::Int8(0xFF)); + checkHash(oatpp::Int8(0x7F)); checkHash(oatpp::UInt8(0xFF)); - checkHash(oatpp::Int16(0xFFFF)); + checkHash(oatpp::Int16(0x7FFF)); checkHash(oatpp::UInt16(0xFFFF)); - checkHash(oatpp::Int32(0xFFFFFFFF)); + checkHash(oatpp::Int32(0x7FFFFFFF)); checkHash(oatpp::UInt32(0xFFFFFFFF)); - checkHash(oatpp::Int64(0xFFFFFFFFFFFFFFFF)); + checkHash(oatpp::Int64(0x7FFFFFFFFFFFFFFF)); checkHash(oatpp::UInt64(0xFFFFFFFFFFFFFFFF)); - checkHash(oatpp::Float32(0.2)); + checkHash(oatpp::Float32(0.2f)); checkHash(oatpp::Float64(0.2)); } From da43b356928bb37c68e3202573aa8845c7e754b4 Mon Sep 17 00:00:00 2001 From: davkor Date: Fri, 7 May 2021 15:10:08 +0100 Subject: [PATCH 16/19] migrating fuzzers from oss-fuzz. --- test/fuzzers/json-fuzz.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 test/fuzzers/json-fuzz.cpp diff --git a/test/fuzzers/json-fuzz.cpp b/test/fuzzers/json-fuzz.cpp new file mode 100644 index 00000000..491bed64 --- /dev/null +++ b/test/fuzzers/json-fuzz.cpp @@ -0,0 +1,27 @@ +#include "oatpp/parser/json/mapping/ObjectMapper.hpp" +#include "oatpp/core/macro/codegen.hpp" + +typedef oatpp::parser::Caret ParsingCaret; +typedef oatpp::parser::json::mapping::Serializer Serializer; +typedef oatpp::parser::json::mapping::Deserializer Deserializer; + +#include OATPP_CODEGEN_BEGIN(DTO) + +class EmptyDto : public oatpp::DTO { + DTO_INIT(EmptyDto, DTO) +}; + +class Test1 : public oatpp::DTO { + DTO_INIT(Test1, DTO) + DTO_FIELD(String, strF); +}; + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + oatpp::String input(reinterpret_cast(data), size, true); + oatpp::parser::json::mapping::ObjectMapper mapper; + try { + mapper.readFromString>(input); + } catch(...) {} + + return 0; +} From d107e4b658123115c8d8e5b8997d88d86553467e Mon Sep 17 00:00:00 2001 From: davkor Date: Fri, 7 May 2021 17:52:17 +0100 Subject: [PATCH 17/19] Moved fuzzer to dedicated folder. Signed-off-by: davkor --- .../oatpp/parser/json/mapping/ObjectMapper.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/fuzzers/json-fuzz.cpp => fuzzers/oatpp/parser/json/mapping/ObjectMapper.cpp (100%) diff --git a/test/fuzzers/json-fuzz.cpp b/fuzzers/oatpp/parser/json/mapping/ObjectMapper.cpp similarity index 100% rename from test/fuzzers/json-fuzz.cpp rename to fuzzers/oatpp/parser/json/mapping/ObjectMapper.cpp From 6ad5cd2791544e1224bf9ae44ffeb39a3807f4e7 Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Mon, 10 May 2021 21:28:34 +0200 Subject: [PATCH 18/19] Always use localtime_s on windows. Previously a function was used that only handles 32 bit time_t values while time_t was 64 bit --- src/oatpp/core/base/Environment.cpp | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/oatpp/core/base/Environment.cpp b/src/oatpp/core/base/Environment.cpp index b870835a..20f3104f 100644 --- a/src/oatpp/core/base/Environment.cpp +++ b/src/oatpp/core/base/Environment.cpp @@ -34,18 +34,11 @@ #if defined(WIN32) || defined(_WIN32) #include -#endif -#if (defined(WIN32) || defined(_WIN32)) && defined(_WIN64) -struct tm* localtime_r(time_t *_clock, struct tm *_result) { - _localtime64_s(_result, _clock); - return _result; -} -#elif (defined(WIN32) || defined(_WIN32)) && not defined(_WIN64) -struct tm* localtime_r(time_t *_clock, struct tm *_result) { - _localtime32_s(_result, _clock); - return _result; -} + struct tm* localtime_r(time_t *_clock, struct tm *_result) { + localtime_s(_result, _clock); + return _result; + } #endif namespace oatpp { namespace base { From 773f699601fcbd5a1378a59774d60e21eafdae9c Mon Sep 17 00:00:00 2001 From: MHaselmaier Date: Sat, 15 May 2021 09:59:11 +0200 Subject: [PATCH 19/19] Added /permissive- flag on MSVC to enforce standard compliancy --- src/CMakeLists.txt | 6 ++++++ test/CMakeLists.txt | 3 +++ 2 files changed, 9 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 00cedd8f..f1055af3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -277,6 +277,9 @@ set_target_properties(oatpp PROPERTIES CXX_EXTENSIONS OFF CXX_STANDARD_REQUIRED ON ) +if (MSVC) + target_compile_options(oatpp PRIVATE /permissive-) +endif() set(CMAKE_THREAD_PREFER_PTHREAD ON) @@ -321,6 +324,9 @@ set_target_properties(oatpp-test PROPERTIES CXX_EXTENSIONS OFF CXX_STANDARD_REQUIRED ON ) +if (MSVC) + target_compile_options(oatpp-test PRIVATE /permissive-) +endif() target_link_libraries(oatpp-test PUBLIC oatpp) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 76aebcd7..8458e4d7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -118,6 +118,9 @@ set_target_properties(oatppAllTests PROPERTIES CXX_EXTENSIONS OFF CXX_STANDARD_REQUIRED ON ) +if (MSVC) + target_compile_options(oatppAllTests PRIVATE /permissive-) +endif() target_include_directories(oatppAllTests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})