From cc1d7d17c6d2df0f603932becf238fdf264a9e30 Mon Sep 17 00:00:00 2001
From: Pingfan Liu <piliu@redhat.com>
Date: Thu, 10 Jun 2021 11:17:40 +0800
Subject: [PATCH 5/6] sysfs.c: prevent mem leak in sysfs_node_read()
Signed-off-by: Pingfan Liu <piliu@redhat.com>
---
sysfs.c | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/sysfs.c b/sysfs.c
index 9ddf50d..a35c4b5 100644
--- a/sysfs.c
+++ b/sysfs.c
@@ -33,7 +33,7 @@ hidden char *sysfs_read(char *name)
hidden int sysfs_node_read(struct bitmask *mask, char *fmt, ...)
{
- int n;
+ int n, ret = 0;
va_list ap;
char *p, *fn, *m, *end;
int num;
@@ -51,12 +51,18 @@ hidden int sysfs_node_read(struct bitmask *mask, char *fmt, ...)
m = p;
do {
num = strtol(m, &end, 0);
- if (m == end)
- return -1;
- if (num < 0)
- return -2;
- if (num >= numa_num_task_nodes())
- return -1;
+ if (m == end) {
+ ret = -1;
+ goto out;
+ }
+ if (num < 0) {
+ ret = -2;
+ goto out;
+ }
+ if (num >= numa_num_task_nodes()) {
+ ret = -1;
+ goto out;
+ }
numa_bitmask_setbit(mask, num);
/* Continuation not supported by kernel yet. */
@@ -64,6 +70,7 @@ hidden int sysfs_node_read(struct bitmask *mask, char *fmt, ...)
while (isspace(*m) || *m == ',')
m++;
} while (isdigit(*m));
+out:
free(p);
- return 0;
+ return ret;
}
--
2.29.2