master hovercats/oakiss / pkg / libnl / patch / 0006-Avoid-use-of-alloca-and-statement-expressions.patch
 1From f64cfa931a450ffb08cbbdeb6652b424f6bb8187 Mon Sep 17 00:00:00 2001
 2From: Michael Forney <mforney@mforney.org>
 3Date: Thu, 30 Jan 2020 14:12:35 -0800
 4Subject: [PATCH] Avoid use of alloca and statement expressions
 5
 6---
 7 include/netlink-private/utils.h | 21 ---------------------
 8 lib/genl/mngt.c                 | 13 +++++++++----
 9 2 files changed, 9 insertions(+), 25 deletions(-)
10
11diff --git a/include/netlink-private/utils.h b/include/netlink-private/utils.h
12index 1456797..e26399f 100644
13--- a/include/netlink-private/utils.h
14+++ b/include/netlink-private/utils.h
15@@ -93,27 +93,6 @@ extern const char *nl_strerror_l(int err);
16 
17 /*****************************************************************************/
18 
19-#define _nl_malloc_maybe_a(alloca_maxlen, bytes, to_free) \
20-	({ \
21-		const size_t _bytes = (bytes); \
22-		__typeof__ (to_free) _to_free = (to_free); \
23-		__typeof__ (*_to_free) _ptr; \
24-		\
25-		_NL_STATIC_ASSERT ((alloca_maxlen) <= 500); \
26-		_nl_assert (_to_free && !*_to_free); \
27-		\
28-		if (_bytes <= (alloca_maxlen)) { \
29-			_ptr = alloca (_bytes); \
30-		} else { \
31-			_ptr = malloc (_bytes); \
32-			*_to_free = _ptr; \
33-		}; \
34-		\
35-		_ptr; \
36-	})
37-
38-/*****************************************************************************/
39-
40 static inline char *
41 _nl_strncpy_trunc(char *dst, const char *src, size_t len)
42 {
43diff --git a/lib/genl/mngt.c b/lib/genl/mngt.c
44index ff50e1d..8f92122 100644
45--- a/lib/genl/mngt.c
46+++ b/lib/genl/mngt.c
47@@ -54,7 +54,7 @@ static int cmd_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *nlh,
48 	int err;
49 	struct genlmsghdr *ghdr;
50 	struct genl_cmd *cmd;
51-	struct nlattr **tb;
52+	struct nlattr **tb, *tb_local[32];
53 
54 	ghdr = genlmsg_hdr(nlh);
55 
56@@ -64,9 +64,14 @@ static int cmd_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *nlh,
57 	if (cmd->c_msg_parser == NULL)
58 		return -NLE_OPNOTSUPP;
59 
60-	tb = _nl_malloc_maybe_a (300, (((size_t) cmd->c_maxattr) + 1u) * sizeof (struct nlattr *), &tb_free);
61-	if (!tb)
62-		return -NLE_NOMEM;
63+	if (cmd->c_maxattr > ARRAY_SIZE(tb_local) - 1) {
64+		tb = malloc(((size_t) cmd->c_maxattr + 1u) * sizeof (struct nlattr *));
65+		if (!tb)
66+			return -NLE_NOMEM;
67+		tb_free = tb;
68+	} else {
69+		tb = tb_local;
70+	}
71 
72 	err = nlmsg_parse(nlh,
73 	                  GENL_HDRSIZE(ops->o_hdrsize),
74-- 
752.25.0
76