summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudio Jeker <claudio@cvs.openbsd.org>2024-09-10 08:53:21 +0000
committerClaudio Jeker <claudio@cvs.openbsd.org>2024-09-10 08:53:21 +0000
commit130a57e6bd9a3c605515484079196ac3303cd26b (patch)
tree7de11296116b82cd5facee5659e671dafc0c2d95
parent50d612c0017c7e1a6d8a14a8cd1d1858c8852053 (diff)
community_copy needs to check if nentries is 0 and handle that specially.
Calling malloc / reallocarray with a 0 size is not portable and the memcpy with a possible NULL pointer as source and 0 len is seen as UB by newer C standards (grmbl). OK tb@
-rw-r--r--usr.sbin/bgpd/rde_community.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/usr.sbin/bgpd/rde_community.c b/usr.sbin/bgpd/rde_community.c
index 0b89858ed73..2ab30de7ed7 100644
--- a/usr.sbin/bgpd/rde_community.c
+++ b/usr.sbin/bgpd/rde_community.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rde_community.c,v 1.15 2024/01/24 14:51:12 claudio Exp $ */
+/* $OpenBSD: rde_community.c,v 1.16 2024/09/10 08:53:20 claudio Exp $ */
/*
* Copyright (c) 2019 Claudio Jeker <claudio@openbsd.org>
@@ -715,18 +715,19 @@ communities_copy(struct rde_community *to, struct rde_community *from)
memset(to, 0, sizeof(*to));
/* ignore from->size and allocate the perfect amount */
- to->size = from->size;
+ to->size = from->nentries;
to->nentries = from->nentries;
to->flags = from->flags;
+ if (to->nentries == 0)
+ return;
+
if ((to->communities = reallocarray(NULL, to->size,
sizeof(struct community))) == NULL)
fatal(__func__);
memcpy(to->communities, from->communities,
to->nentries * sizeof(struct community));
- memset(to->communities + to->nentries, 0, sizeof(struct community) *
- (to->size - to->nentries));
}
/*