Skip to content

Commit

Permalink
Stop leaking temporary paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Hummer12007 committed Jan 25, 2020
1 parent 5a53dc0 commit a6a1c9b
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions brightnessctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,11 @@ bool do_write_device(struct device *d) {
bool read_device(struct device *d, char *class, char *id) {
DIR *dirp;
FILE *f;
char *dev_path;
char *dev_path = NULL;
char *ent_path;
int error = 0;
struct dirent *ent;
bool cur;
d->class = strdup(class);
d->id = strdup(id);
dev_path = device_path(d);
Expand All @@ -433,39 +435,31 @@ bool read_device(struct device *d, char *class, char *id) {
while ((ent = readdir(dirp))) {
if (!strcmp(ent->d_name, ".") && !strcmp(ent->d_name, ".."))
continue;
if (!strcmp(ent->d_name, "brightness")) {
if ((f = fopen(dir_child(dev_path, ent->d_name), "r"))) {
clearerr(f);
if (fscanf(f, "%u", &d->curr_brightness) == EOF) {
fprintf(stderr, "End-of-file reading brightness of device '%s'.", d->id);
error++;
} else if (ferror(f)) {
fprintf(stderr, "Error reading brightness of device '%s': %s.", d->id, strerror(errno));
error++;
}
fclose(f);
} else
goto fail;
}
if (!strcmp(ent->d_name, "max_brightness")) {
if ((f = fopen(dir_child(dev_path, ent->d_name), "r"))) {
clearerr(f);
if (fscanf(f, "%u", &d->max_brightness) == EOF) {
fprintf(stderr, "End-of-file reading max brightness of device '%s'.", d->id);
error++;
} else if (ferror(f)) {
fprintf(stderr, "Error reading max brightness of device '%s': %s.", d->id, strerror(errno));
error++;
}
fclose(f);
} else
if ((cur = !strcmp(ent->d_name, "brightness")) ||
!strcmp(ent->d_name, "max_brightness")) {
if (!(f = fopen(ent_path = dir_child(dev_path, ent->d_name), "r")))
goto fail;
clearerr(f);
if (fscanf(f, "%u", cur ? &d->curr_brightness : &d->max_brightness) == EOF) {
fprintf(stderr, "End-of-file reading %s of device '%s'.",
cur ? "brightness" : "max brightness", d->id);
error++;
} else if (ferror(f)) {
fprintf(stderr, "Error reading %s of device '%s': %s.",
cur ? "brightness" : "max brightness", d->id, strerror(errno));
error++;
}
fclose(f);
free(ent_path);
ent_path = NULL;
}
}
errno = 0;
fail:
closedir(dirp);
dfail:
free(dev_path);
free(ent_path);
if (errno) {
perror("Error reading device");
error++;
Expand All @@ -477,8 +471,9 @@ int read_class(struct device **devs, char *class) {
DIR *dirp;
struct dirent *ent;
struct device *dev;
char *c_path;
int cnt = 0;
dirp = opendir(class_path(class));
dirp = opendir(c_path = class_path(class));
if (!dirp)
return 0;
while ((ent = readdir(dirp))) {
Expand All @@ -492,6 +487,7 @@ int read_class(struct device **devs, char *class) {
devs[cnt++] = dev;
}
closedir(dirp);
free(c_path);
return cnt;
}

Expand Down

0 comments on commit a6a1c9b

Please sign in to comment.