Subject: [Boost-bugs] [Boost C++ Libraries] #4368: boost::recursive_mutex constructor potential resource leak
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2010-06-23 18:35:18
#4368: boost::recursive_mutex constructor potential resource leak
------------------------------------------------------+---------------------
Reporter: Steve Hawkes <steve.hawkes@â¦> | Owner: anthonyw
Type: Bugs | Status: new
Milestone: Boost 1.44.0 | Component: thread
Version: Boost Development Trunk | Severity: Problem
Keywords: recursive_mutex |
------------------------------------------------------+---------------------
When running the Klocwork static code analysis tool on some code that uses
Boost.Thread, Klocwork reported the following potential issue (note that
this is for boost 1_36_0, but the latest trunk code appears to have the
same issue):
----
boost-1_36/boost/thread/pthread/recursive_mutex.hpp: 50[[BR]]
Resource acquired to 'attr' at line 42 may be lost here. Also there is one
similar error on line 56.[[BR]]
recursive_mutex.hpp#1:42: Resource is acquired: 'attr' is accessed via 1
argument of the call to function 'pthread_mutexattr_init'[[BR]]
recursive_mutex.hpp#1:43: init_attr_res is false[[BR]]
recursive_mutex.hpp#1:47: 'attr' might be changed[[BR]]
recursive_mutex.hpp#1:48: set_attr_res is true[[BR]]
recursive_mutex.hpp#1:50: Resource is lost: 'attr'
----
I'm not absolutely certain this is the issue Klocwork is attempting to
report, but in the boost/thread/pthread/recursive_mutex.hpp header file
the mutex attribute object is not destroyed if an exception is thrown due
to pthread_mutexattr_settype() failure. I see that
pthread_mutexattr_destroy() has been added since 1_36_0 for the case where
pthread_mutex_init() fails. A similar call is needed if
pthread_mutexattr_settype() fails.
{{{
recursive_mutex()
{
#ifdef BOOST_PTHREAD_HAS_MUTEXATTR_SETTYPE
pthread_mutexattr_t attr;
int const init_attr_res=pthread_mutexattr_init(&attr);
if(init_attr_res)
{
boost::throw_exception(thread_resource_error());
}
int const
set_attr_res=pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
if(set_attr_res)
{
// Shouldn't the following line be added here?
// BOOST_VERIFY(!pthread_mutexattr_destroy(&attr));
boost::throw_exception(thread_resource_error());
}
int const res=pthread_mutex_init(&m,&attr);
if(res)
{
BOOST_VERIFY(!pthread_mutexattr_destroy(&attr));
boost::throw_exception(thread_resource_error());
}
BOOST_VERIFY(!pthread_mutexattr_destroy(&attr));
}}}
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/4368> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:03 UTC