Opened 12 years ago

Closed 12 years ago

#4368 closed Bugs (fixed)

boost::recursive_mutex constructor potential resource leak

Reported by: Steve Hawkes <steve.hawkes@…> Owned by: Anthony Williams
Milestone: Boost 1.44.0 Component: thread
Version: Boost Development Trunk Severity: Problem
Keywords: recursive_mutex Cc:

Description

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
Resource acquired to 'attr' at line 42 may be lost here. Also there is one similar error on line 56.
recursive_mutex.hpp#1:42: Resource is acquired: 'attr' is accessed via 1 argument of the call to function 'pthread_mutexattr_init'
recursive_mutex.hpp#1:43: init_attr_res is false
recursive_mutex.hpp#1:47: 'attr' might be changed
recursive_mutex.hpp#1:48: set_attr_res is true
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));

Change History (1)

comment:1 by Anthony Williams, 12 years ago

Resolution: fixed
Status: newclosed

Fixed on trunk

Note: See TracTickets for help on using tickets.