[libnftnl,1/2] flowtable: device array dynamic allocation
diff mbox series

Message ID 20191018094947.9531-1-pablo@netfilter.org
State Changes Requested
Delegated to: Pablo Neira
Headers show
Series
  • [libnftnl,1/2] flowtable: device array dynamic allocation
Related show

Commit Message

Pablo Neira Ayuso Oct. 18, 2019, 9:49 a.m. UTC
Remove artificial upper limit of 8 devices per flowtable.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 src/flowtable.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

Patch
diff mbox series

diff --git a/src/flowtable.c b/src/flowtable.c
index 1f7ba3052d4f..54e1bea25775 100644
--- a/src/flowtable.c
+++ b/src/flowtable.c
@@ -358,30 +358,31 @@  static int nftnl_flowtable_parse_hook_cb(const struct nlattr *attr, void *data)
 static int nftnl_flowtable_parse_devs(struct nlattr *nest,
 				      struct nftnl_flowtable *c)
 {
+	const char **dev_array;
+	int len = 0, size = 8;
 	struct nlattr *attr;
-	char *dev_array[8];
-	int len = 0, i;
+
+	dev_array = calloc(8, sizeof(char *));
+	if (!dev_array)
+		return -1;
 
 	mnl_attr_for_each_nested(attr, nest) {
 		if (mnl_attr_get_type(attr) != NFTA_DEVICE_NAME)
 			goto err;
 		dev_array[len++] = strdup(mnl_attr_get_str(attr));
-		if (len >= 8)
-			break;
-	}
+		if (len >= size) {
+			dev_array = realloc(dev_array, size * 2);
+			if (!dev_array)
+				goto err;
 
-	if (!len)
-		return -1;
-
-	c->dev_array = calloc(len + 1, sizeof(char *));
-	if (!c->dev_array)
-		goto err;
+			size *= 2;
+			memset(&dev_array[len], 0, size - len);
+		}
+	}
 
+	c->dev_array = dev_array;
 	c->dev_array_len = len;
 
-	for (i = 0; i < len; i++)
-		c->dev_array[i] = dev_array[i];
-
 	return 0;
 err:
 	while (len--)