Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 983d9e06 authored by Colin Ian King's avatar Colin Ian King Committed by Rafael J. Wysocki
Browse files

cpupower: bench: parse.c: fix several resource leaks



The error handling in prepare_output has several issues with
resource leaks.  Ensure that filename is free'd and the directory
stream DIR is closed before returning.

Signed-off-by: default avatarColin Ian King <colin.king@canonical.com>
Signed-off-by: default avatarThomas Renninger <trenn@suse.com>
Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
parent 04b03594
Loading
Loading
Loading
Loading
+11 −3
Original line number Original line Diff line number Diff line
@@ -81,14 +81,18 @@ FILE *prepare_output(const char *dirname)


	len = strlen(dirname) + 30;
	len = strlen(dirname) + 30;
	filename = malloc(sizeof(char) * len);
	filename = malloc(sizeof(char) * len);
	if (!filename) {
		perror("malloc");
		goto out_dir;
	}


	if (uname(&sysdata) == 0) {
	if (uname(&sysdata) == 0) {
		len += strlen(sysdata.nodename) + strlen(sysdata.release);
		len += strlen(sysdata.nodename) + strlen(sysdata.release);
		filename = realloc(filename, sizeof(char) * len);
		filename = realloc(filename, sizeof(char) * len);


		if (filename == NULL) {
		if (!filename) {
			perror("realloc");
			perror("realloc");
			return NULL;
			goto out_dir;
		}
		}


		snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log",
		snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log",
@@ -104,12 +108,16 @@ FILE *prepare_output(const char *dirname)
	if (output == NULL) {
	if (output == NULL) {
		perror("fopen");
		perror("fopen");
		fprintf(stderr, "error: unable to open logfile\n");
		fprintf(stderr, "error: unable to open logfile\n");
		goto out;
	}
	}


	fprintf(stdout, "Logfile: %s\n", filename);
	fprintf(stdout, "Logfile: %s\n", filename);


	free(filename);
	fprintf(output, "#round load sleep performance powersave percentage\n");
	fprintf(output, "#round load sleep performance powersave percentage\n");
out:
	free(filename);
out_dir:
	closedir(dir);
	return output;
	return output;
}
}