Skip to content

Commit

Permalink
Add mutex for delayed work list
Browse files Browse the repository at this point in the history
The delayed_work list can be accessed by multiple threads which
can result in memory corruption.

Prevent this by protecting the access with a mutex.
  • Loading branch information
thomasjwinter authored and carlgsmith committed Apr 5, 2024
1 parent b0801a8 commit 2d07ec7
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions alfred.c
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ load_script_files (alfred_instance alfred, const char *path)
return res;
}

static pthread_mutex_t delayed_work_lock = PTHREAD_MUTEX_INITIALIZER;
GList *delayed_work = NULL;
struct delayed_work_s {
guint id;
Expand All @@ -833,7 +834,9 @@ delayed_work_process (gpointer arg1)
struct delayed_work_s *dw = (struct delayed_work_s *) arg1;

/* Remove the script to be run */
pthread_mutex_lock (&delayed_work_lock);
delayed_work = g_list_remove (delayed_work, dw);
pthread_mutex_unlock (&delayed_work_lock);

if (lua_apteryx_instance_lock (dw->instance))
{
Expand Down Expand Up @@ -983,7 +986,9 @@ rate_limit (lua_State *ls)
{
if (validate_script_or_function_args (ls, "Alfred.rate_limit()"))
{
pthread_mutex_lock (&delayed_work_lock);
delayed_work_add (ls, false);
pthread_mutex_unlock (&delayed_work_lock);
}
return 0;
}
Expand All @@ -993,7 +998,9 @@ after_quiet (lua_State *ls)
{
if (validate_script_or_function_args (ls, "Alfred.after_quiet()"))
{
pthread_mutex_lock (&delayed_work_lock);//aa
delayed_work_add (ls, true);
pthread_mutex_unlock (&delayed_work_lock);
}
return 0;
}
Expand Down

0 comments on commit 2d07ec7

Please sign in to comment.