netlink: simplify temporary address allocation in rtnl_handle_getlink().

MFC after:	3 days
This commit is contained in:
Alexander V. Chernikov 2023-02-17 17:57:44 +00:00
parent 6694cb1f57
commit 45356a1864

View File

@ -439,9 +439,8 @@ rtnl_handle_getlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *n
NL_LOG(LOG_DEBUG2, "Start dump");
struct ifnet **match_array;
int offset = 0, base_count = 16; /* start with 128 bytes */
match_array = malloc(base_count * sizeof(void *), M_TEMP, M_NOWAIT);
struct ifnet **match_array = NULL;
int offset = 0, base_count = 0;
NLP_LOG(LOG_DEBUG3, nlp, "MATCHING: index=%u type=%d name=%s",
attrs.ifi_index, attrs.ifi_type, attrs.ifla_ifname);
@ -452,14 +451,14 @@ rtnl_handle_getlink(struct nlmsghdr *hdr, struct nlpcb *nlp, struct nl_pstate *n
if (offset >= base_count) {
/* Too many matches, need to reallocate */
struct ifnet **new_array;
int sz = base_count * sizeof(void *);
base_count *= 2;
new_array = malloc(sz * 2, M_TEMP, M_NOWAIT);
/* Start with 128 bytes, do 2x increase on each realloc */
base_count = (base_count != 0) ? base_count * 2 : 16;
new_array = malloc(base_count * sizeof(void *), M_TEMP, M_NOWAIT);
if (new_array == NULL) {
error = ENOMEM;
break;
}
memcpy(new_array, match_array, sz);
memcpy(new_array, match_array, offset * sizeof(void *));
free(match_array, M_TEMP);
match_array = new_array;
}