Skip to content

Commit

Permalink
udev_enumerate.c: fix possible memory leak
Browse files Browse the repository at this point in the history
Early return due to pthread_create() failure causes memory leak.
  • Loading branch information
illiliti committed Jun 11, 2021
1 parent eb30a8b commit bd7d180
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions udev_enumerate.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,23 +276,20 @@ static int scan_devices(struct udev_enumerate *udev_enumerate, const char *path)
struct udev_enumerate_thread *thread;
pthread_mutex_t mutex;
struct dirent **de;
int cnt, i;
int ret, cnt, i;

cnt = scandir(path, &de, filter_dot, NULL);

if (cnt == -1) {
return 0;
}

ret = 1;
thread = calloc(cnt, sizeof(struct udev_enumerate_thread));

if (!thread) {
for (i = 0; i < cnt; i++) {
free(de[i]);
}

free(de);
return 0;
ret = 0;
goto free_de;
}

pthread_mutex_init(&mutex, NULL);
Expand All @@ -304,22 +301,25 @@ static int scan_devices(struct udev_enumerate *udev_enumerate, const char *path)
snprintf(thread[i].path, sizeof(thread[i].path), "%s/%s", path, de[i]->d_name);

if (pthread_create(&thread[i].thread, NULL, add_device, &thread[i]) != 0) {
return 0;
ret = 0;
break;
}
}

for (i = 0; i < cnt; i++) {
pthread_join(thread[i].thread, NULL);
}

free(thread);
pthread_mutex_destroy(&mutex);

free_de:
for (i = 0; i < cnt; i++) {
free(de[i]);
}

free(de);
free(thread);
pthread_mutex_destroy(&mutex);
return 1;
return ret;
}

int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
Expand Down

0 comments on commit bd7d180

Please sign in to comment.