shlomif / fc-solve

Freecell Solver - a C library for automatically solving Freecell and some other variants of card Solitaire
https://fc-solve.shlomifish.org/
MIT License
60 stars 13 forks source link

Suggestions about error handlings for locking #85

Closed ryancaicse closed 2 years ago

ryancaicse commented 2 years ago

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

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);
}

https://github.com/shlomif/fc-solve/blob/45efed24104b1c3ac80efb44904579d0fb238ae4/fc-solve/source/lock.h#L71-L74

shlomif commented 2 years ago

Hi!

See https://github.com/shlomif/fc-solve/commit/a713f7980a936da419ef8b40b012bd9c47563375 . I think it is a minor concern due to the usecase of the solvers, anyway.

ryancaicse commented 2 years ago

Thanks!