A function locks a mutex that nothing in the function unlocks
What it finds
pthread_mutex_lock(&q->lock); if (q->n == 0) return -1; ... with no pthread_mutex_unlock(&q->lock) anywhere in the function: the function returns with the mutex held, and the next thread that locks it waits forever. Functions that are meant to return with the lock held are rare and say so in their name.
The function must not unlock the same argument, pass it to another call or contain unlock, release or lock in its own name.
From the rule's source, mutex_never_unlocked.rs.
#include <pthread.h> struct queue { pthread_mutex_t lock; int n; }; int take(struct queue *q){ pthread_mutex_lock(&q->lock);tx0 reports: &q->lock is locked but never unlocked in take return --q->n;} int put(struct queue *q){ pthread_mutex_lock(&q->lock); int n = ++q->n; pthread_mutex_unlock(&q->lock); return n;} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/c:mutex-never-unlocked" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/c:mutex-never-unlocked] reason="..."tx0 explain tx0/c:mutex-never-unlocked shows the same in the terminal. Found a false positive? Report it.