Menu

Mutex Lock / Unlock

2018-08-09
2018-08-11
  • Larry Blackbird

    Larry Blackbird - 2018-08-09

    Hi folks,

    is there a way that Cppcheck checks the mutex locking / unlocking functionalitites?

    An example (with an error):

    void foo() {
        mutex_lock();
        if (condition) { // condition is true
            return;
        }
        mutex_unlock();
    }
    

    Is there a way Cppcheck can detect such a problem (the mutex stays locked)?

    Cheers

     
  • versat

    versat - 2018-08-10

    This is not yet possible but there is already at least one ticket suggestion such functionality:
    https://trac.cppcheck.net/ticket/7315

     
  • Daniel Marjamäki

    I am skeptic that we can write an effective checker for this.. What if there is a function call..

    void foo() {
        mutex_lock();
        if (condition) {
            bar(); // bar() might call mutex_unlock()
            return;
        }
        mutex_unlock();
    }
    

    If the checker is silent whenever there is a function call, the checker will not be very effective. If function calls are ignored it will produce false positives.

    It could use whole-program-analysis to make it a bit more effective but that does not help if libs/etc are called.

     

    Last edit: Daniel Marjamäki 2018-08-11
  • Paul Fultz

    Paul Fultz - 2018-08-11

    Dont we do similiar analysis already for resource leaks? This would just be a matter of adding the functions to the library config files, right?

     
  • Daniel Marjamäki

    The checker only checks local pointer variables. So we don't have so much problem with function calls.

    void foo() {
         FILE *f = fopen("ab.txt", "rt");
         if (condition) {
             bar();  // <- Can't deallocate f
             return;
         }
         fclose(f);
    }
    

    If the pointer is made global then the checker will not warn.

     
  • Daniel Marjamäki

    This would just be a matter of adding the functions to the library config files, right?

    It would be possible to check lock/unlock with cppcheck with some tweaks.

    I don't know if you like it .. but you can for instance try this:

    1. Create two new functions for locking/unlocking:
    // The functions below are needed by the compiler, but must be hidden in cppcheck analysis. You can use -DCPPCHECK to hide them.
    #ifndef CPPCHECK
    int  safe_mutex_lock() { mutex_lock(); return 0; }
    void safe_mutex_unlock(int) { mutex_unlock(); }
    #endif
    
    1. Then use the "safe" lock/unlock functions in the code:
    void foo() {
        int safe_lock = safe_mutex_lock();
        if (condition) { // condition is true
            return;
        }
        safe_mutex_unlock(safe_lock);
    }
    
    1. Write a configuration for the safe lock/unlock
        <resource>
            <alloc>safe_mutex_lock</alloc>
            <dealloc>safe_mutex_unlock</dealloc>
        </resource>
    
    1. You should now see the warning about "safe_lock" resource leak.
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.