From 07ef39656b1ad87dc7dc7993bbd6ca5d77575ba0 Mon Sep 17 00:00:00 2001 From: claudio Date: Tue, 10 Sep 2024 08:53:20 +0000 Subject: [PATCH] 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@ --- usr.sbin/bgpd/rde_community.c | 9 +++++---- 1 file 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 @@ -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)); } /* -- 2.20.1