wolfcw / libfaketime

libfaketime modifies the system time for a single application
https://github.com/wolfcw/libfaketime
GNU General Public License v2.0
2.71k stars 325 forks source link

Two potential errors due to the unreleased lock&fakeMutex before program exit #340

Closed jenny-cheung closed 3 years ago

jenny-cheung commented 3 years ago

Dear developers:

First, thank you for your checking! The lockpthread_mutex_lock(&fakeMutex); is not released after the program exit if the relevant branch conditions satisfy. Although it does not affect much, it is worth fixing for good software quality. The fix is to insert pthread_mutex_unlock(&fakeMutex); beforeexit(1).

https://github.com/wolfcw/libfaketime/blob/8ae4c9bc0eaa80cd99cc3acdc001ee8bd8ac5800/test/timetest.c#L81

https://github.com/wolfcw/libfaketime/blob/8ae4c9bc0eaa80cd99cc3acdc001ee8bd8ac5800/test/timetest.c#L103

https://github.com/wolfcw/libfaketime/blob/8ae4c9bc0eaa80cd99cc3acdc001ee8bd8ac5800/test/timetest.c#L83

https://github.com/wolfcw/libfaketime/blob/8ae4c9bc0eaa80cd99cc3acdc001ee8bd8ac5800/test/timetest.c#L105

The relevant code is shown here.

void* pthread_test(void* args)
{
  ...;

  pthread_mutex_lock(&fakeMutex);
  rt = pthread_cond_timedwait(&fakeCond, &fakeMutex, &timeToWait);
  if (rt != ETIMEDOUT)
  {
    printf("pthread_cond_timedwait failed\n");
    exit(EXIT_FAILURE);  //exit without releasing
  }
 ....;

  pthread_mutex_lock(&fakeMutex);
  rt = pthread_cond_timedwait(&monotonic_cond, &fakeMutex, &timeToWait);
  if (rt != ETIMEDOUT)
  {
    printf("pthread_cond_timedwait failed\n");
    exit(EXIT_FAILURE);   //exit without releasing
  }
  ...;
}

Best,

wolfcw commented 3 years ago

Thanks for reporting this. Should be fixed now.

jenny-cheung commented 3 years ago

Thank you for your confirmation!