payden / libwebsock

C library for easy WebSockets server.
GNU Lesser General Public License v3.0
216 stars 89 forks source link

A error due to the unreleased lock &global_alloc_free_lock before program exit #42

Open jenny-cheung opened 2 years ago

jenny-cheung commented 2 years ago

First, thank you for your checking! The lock&global_alloc_free_lock is not released if the !alloc is satisfied. The fix is to insert pthread_mutex_unlock(&global_alloc_free_lock); before exit(1).

Note that it is not a very harmful bug( e.g., resource leak, deadlock) since the program exits and all of the program resources will be cleaned up by some common OS. However, other OS systems (e.g., embedded systems) do not automatically free some resources at the exit. It is a good manner for resource management. Adding the unlock statement adds symmetry, so the code looks better. Also, the debugger would not warn this case : )

https://github.com/payden/libwebsock/blob/3c1615eeadb0b582b63851073bfe3e5132f31ebc/src/util.c#L78

void *
lws_calloc(size_t size)
{
   pthread_mutex_lock(&global_alloc_free_lock);
        ...;
  if (!alloc) {
    fprintf(stderr, "Failed calloc!  Exiting.\n");
    exit(-1); //exit without releasing
  }
  pthread_mutex_unlock(&global_alloc_free_lock);
  ...;
  return alloc;
}

Best,

AdanJSuarez commented 2 years ago

It is not a joke, sorry.

https://github.com/payden/libwebsock/issues/26