community_copy needs to check if nentries is 0 and handle that specially.
authorclaudio <claudio@openbsd.org>
Tue, 10 Sep 2024 08:53:20 +0000 (08:53 +0000)
committerclaudio <claudio@openbsd.org>
Tue, 10 Sep 2024 08:53:20 +0000 (08:53 +0000)
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

index 0b89858..2ab30de 100644 (file)
@@ -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));
 }
 
 /*