Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handlings for pthread_mutex_lock #1273

Open
ycaibb opened this issue Dec 13, 2021 · 0 comments
Open

Error handlings for pthread_mutex_lock #1273

ycaibb opened this issue Dec 13, 2021 · 0 comments
Assignees
Labels

Comments

@ycaibb
Copy link
Contributor

ycaibb commented Dec 13, 2021

Hi, I have a suggestion about error handlings for locking. Would it be better to handle the possible errors that return from pthread_mutex_lock.

# define CHEROKEE_MUTEX_LOCK(m) pthread_mutex_lock(m)
# define CHEROKEE_MUTEX_UNLOCK(m) pthread_mutex_unlock(m)

webserver/cherokee/nonce.c

Lines 110 to 115 in 5b1dbdb

CHEROKEE_MUTEX_LOCK (&nonces->access);
ret = cherokee_avl_get (&nonces->table, nonce, (void **)&entry);
if (ret == ret_ok) {
entry_free (nonces, entry);
}
CHEROKEE_MUTEX_UNLOCK (&nonces->access);

Possible situations that return errors.

    EAGAIN The mutex could not be acquired because the maximum number
              of recursive locks for mutex has been exceeded.

       EINVAL The mutex was created with the protocol attribute having
              the value PTHREAD_PRIO_PROTECT and the calling thread's
              priority is higher than the mutex's current priority
              ceiling.

       ENOTRECOVERABLE
              The state protected by the mutex is not recoverable.

       EOWNERDEAD
              The mutex is a robust mutex and the process containing the
              previous owning thread terminated while holding the mutex
              lock. The mutex lock shall be acquired by the calling
              thread and it is up to the new owner to make the state
              consistent.

       EDEADLK
              The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current
              thread already owns the mutex.

       EOWNERDEAD
              The mutex is a robust mutex and the previous owning thread
              terminated while holding the mutex lock. The mutex lock
              shall be acquired by the calling thread and it is up to
              the new owner to make the state consistent.

       EDEADLK
              A deadlock condition was detected.

For example, this example does not check the value returned by pthread_mutex_lock() for errors. If pthread_mutex_lock() cannot acquire the mutex for any reason, the function may introduce a race condition into the program (CWE-413). The manners of error handlings could be flagging any warnings or returning before accessing the critical region.

void f(pthread_mutex_t *mutex) {
pthread_mutex_lock(mutex);

/* access shared resource */


pthread_mutex_unlock(mutex);
}
@skinkie skinkie self-assigned this Dec 13, 2021
@skinkie skinkie added the t:bug label Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants