Browse Source

fix memory leaks

Semyon Verchenko 6 years ago
parent
commit
5b50311241
2 changed files with 16 additions and 3 deletions
  1. 10 2
      irq.c
  2. 6 1
      pxm.c

+ 10 - 2
irq.c

@@ -135,9 +135,10 @@ static int parse_local_cpus(lub_list_t *irqs, const char *sysfs_path,
 	char *str = NULL;
 	size_t sz;
 	cpumask_t local_cpus;
-	cpus_init(local_cpus);
 	irq_t *irq = NULL;
 	cpumask_t cpumask;
+
+	cpus_init(local_cpus);
 	cpus_init(cpumask);
 
 	irq = irq_list_search(irqs, num);
@@ -147,16 +148,23 @@ static int parse_local_cpus(lub_list_t *irqs, const char *sysfs_path,
 	/* Find proximity in config file. */
 	if (!pxm_search(pxms, sysfs_path, &cpumask)) {
 		cpus_copy(irq->local_cpus, cpumask);
+		cpus_free(local_cpus);
+		cpus_free(cpumask);
 		return 0;
 	}
 
 	snprintf(path, sizeof(path),
 		"%s/%s/local_cpus", SYSFS_PCI_PATH, sysfs_path);
 	path[sizeof(path) - 1] = '\0';
-	if (!(fd = fopen(path, "r")))
+	if (!(fd = fopen(path, "r"))){
+		cpus_free(local_cpus);
+		cpus_free(cpumask);
 		return -1;
+	}
 	if (getline(&str, &sz, fd) < 0) {
 		fclose(fd);
+		cpus_free(local_cpus);
+		cpus_free(cpumask);
 		return -1;
 	}
 	fclose(fd);

+ 6 - 1
pxm.c

@@ -131,7 +131,6 @@ int parse_pxm_config(const char *fname, lub_list_t *pxms, lub_list_t *numas)
 		char *pxm_cmd = NULL;
 		char *pxm_pxm = NULL;
 		cpumask_t cpumask;
-		cpus_init(cpumask);
 
 		ln++; /* Next line */
 		if (getline(&line, &size, file) <= 0)
@@ -163,6 +162,9 @@ int parse_pxm_config(const char *fname, lub_list_t *pxms, lub_list_t *numas)
 			continue;
 		}
 
+
+		cpus_init(cpumask);
+
 		if (!strcasecmp(pxm_cmd, "cpumask")) {
 			cpumask_parse_user(pxm_pxm, strlen(pxm_pxm),
 				cpumask);
@@ -173,6 +175,7 @@ int parse_pxm_config(const char *fname, lub_list_t *pxms, lub_list_t *numas)
 			if (endptr == pxm_pxm) {
 				fprintf(stderr, "Warning: Wrong NUMA node in "
 					"line %u in %s\n", ln, fname);
+				cpus_free(cpumask);
 				continue;
 			}
 			if (noden == -1) /* Non-NUMA = all CPUs */
@@ -183,6 +186,7 @@ int parse_pxm_config(const char *fname, lub_list_t *pxms, lub_list_t *numas)
 				if (!numa) {
 					fprintf(stderr, "Warning: Wrong NUMA node. Line %u in %s\n",
 						ln, fname);
+					cpus_free(cpumask);
 					continue;
 				}
 				cpus_clear(cpumask);
@@ -191,6 +195,7 @@ int parse_pxm_config(const char *fname, lub_list_t *pxms, lub_list_t *numas)
 		} else {
 			fprintf(stderr, "Warning: Illegal command %u in %s\n",
 				ln, fname);
+			cpus_free(cpumask);
 			continue;
 		}