Skip to content

Commit

Permalink
Prevent undefined behaviour when an appender is deleted without being…
Browse files Browse the repository at this point in the history
… closed (#400)
  • Loading branch information
swebb2066 authored Jul 25, 2024
1 parent 6b61e97 commit 1a8875a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
18 changes: 11 additions & 7 deletions src/main/cpp/aprserversocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ APRServerSocket::APRServerSocket(int port) :
}
}

void APRServerSocket::close(){
void APRServerSocket::close()
{
std::lock_guard<std::mutex> lock(_priv->mutex);

if (_priv->socket != 0)
Expand All @@ -103,19 +104,22 @@ accepts it
*/
SocketPtr APRServerSocket::accept()
{
std::lock_guard<std::mutex> lock(_priv->mutex);

if (_priv->socket == 0)
apr_socket_t* s;
{
std::lock_guard<std::mutex> lock(_priv->mutex);
s = _priv->socket;
}
if (s == 0)
{
throw IOException();
throw IOException(LOG4CXX_STR("socket is 0"));
}

apr_pollfd_t poll;
poll.p = _priv->pool.getAPRPool();
poll.desc_type = APR_POLL_SOCKET;
poll.reqevents = APR_POLLIN;
poll.rtnevents = 0;
poll.desc.s = _priv->socket;
poll.desc.s = s;
poll.client_data = NULL;

apr_int32_t signaled;
Expand All @@ -140,7 +144,7 @@ SocketPtr APRServerSocket::accept()
}

apr_socket_t* newSocket;
status = apr_socket_accept(&newSocket, _priv->socket, newPool);
status = apr_socket_accept(&newSocket, s, newPool);

if (status != APR_SUCCESS)
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/filewatchdog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct FileWatchdog::FileWatchdogPrivate{
#if LOG4CXX_EVENTS_AT_EXIT
, atExitRegistryRaii([this]{stopWatcher();})
#endif
{}
{ stopWatcher(); }
/**
The name of the file to observe for changes.
*/
Expand Down
18 changes: 9 additions & 9 deletions src/main/cpp/telnetappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct TelnetAppender::TelnetAppenderPriv : public AppenderSkeletonPrivate
#if LOG4CXX_EVENTS_AT_EXIT
, atExitRegistryRaii([this]{stopAcceptingConnections();})
#endif
{}
{ stopAcceptingConnections(); }

int port;
ConnectionList connections;
Expand All @@ -73,14 +73,14 @@ struct TelnetAppender::TelnetAppenderPriv : public AppenderSkeletonPrivate
if (!this->serverSocket || this->closed)
return;
this->closed = true;
// Interrupt accept()
try
{
this->serverSocket->close();
}
catch (Exception&)
{
}
}
// Interrupt accept()
try
{
this->serverSocket->close();
}
catch (Exception&)
{
}
if (this->sh.joinable())
this->sh.join();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct SocketAppenderSkeleton::SocketAppenderSkeletonPriv : public AppenderSkele
#if LOG4CXX_EVENTS_AT_EXIT
, atExitRegistryRaii([this]{stopMonitor();})
#endif
{}
{ stopMonitor(); }

SocketAppenderSkeletonPriv(helpers::InetAddressPtr address, int defaultPort, int reconnectionDelay) :
AppenderSkeletonPrivate(),
Expand All @@ -54,7 +54,7 @@ struct SocketAppenderSkeleton::SocketAppenderSkeletonPriv : public AppenderSkele
#if LOG4CXX_EVENTS_AT_EXIT
, atExitRegistryRaii([this]{stopMonitor();})
#endif
{}
{ stopMonitor(); }

SocketAppenderSkeletonPriv(const LogString& host, int port, int delay) :
AppenderSkeletonPrivate(),
Expand All @@ -66,7 +66,7 @@ struct SocketAppenderSkeleton::SocketAppenderSkeletonPriv : public AppenderSkele
#if LOG4CXX_EVENTS_AT_EXIT
, atExitRegistryRaii([this]{stopMonitor();})
#endif
{}
{ stopMonitor(); }

/**
host name
Expand Down
1 change: 0 additions & 1 deletion src/test/cpp/net/telnetappendertestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class TelnetAppenderTestCase : public AppenderSkeletonTestCase
#endif
LOG4CXX_INFO(root, "Hello, World " << i);
}
appender->close();
}

};
Expand Down

0 comments on commit 1a8875a

Please sign in to comment.