From owner-freebsd-bugs@FreeBSD.ORG Sun Jan 23 11:20:25 2005 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 83F3A16A4CF for ; Sun, 23 Jan 2005 11:20:23 +0000 (GMT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8626443D46 for ; Sun, 23 Jan 2005 11:20:23 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.13.1/8.13.1) with ESMTP id j0NBKNhm022302 for ; Sun, 23 Jan 2005 11:20:23 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.13.1/8.13.1/Submit) id j0NBKNdW022301; Sun, 23 Jan 2005 11:20:23 GMT (envelope-from gnats) Date: Sun, 23 Jan 2005 11:20:23 GMT Message-Id: <200501231120.j0NBKNdW022301@freefall.freebsd.org> To: freebsd-bugs@FreeBSD.org From: Dmitrij Tejblum Subject: Re: bin/76075: arp program failures due to ARP table growth X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Dmitrij Tejblum List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 23 Jan 2005 11:20:25 -0000 The following reply was made to PR bin/76075; it has been noted by GNATS. From: Dmitrij Tejblum To: Maxim Konovalov Cc: bug-followup@freebsd.org Subject: Re: bin/76075: arp program failures due to ARP table growth Date: Sun, 23 Jan 2005 14:17:16 +0300 (MSK) > What is your opinion about an enclosed diff (a bit more adaptable > algorithm stolen from killall(1))? Your patch has a few small mistakes. E.g. it reallocates the buffer right after it was allocated without any attempt to fill it. The following variant is better in my opinion. But anyway, something like it should be OK. --- arp.c Tue Jan 11 02:00:45 2005 +++ arp.c Sun Jan 23 13:40:36 2005 @@ -456,12 +456,12 @@ { int mib[6]; size_t needed; - char *lim, *buf, *next; + char *lim, *buf, *newbuf, *next; struct rt_msghdr *rtm; struct sockaddr_inarp *sin2; struct sockaddr_dl *sdl; char ifname[IF_NAMESIZE]; - int found_entry = 0; + int st, found_entry = 0; mib[0] = CTL_NET; mib[1] = PF_ROUTE; @@ -473,9 +473,16 @@ err(1, "route-sysctl-estimate"); if (needed == 0) /* empty table */ return 0; - if ((buf = malloc(needed)) == NULL) - err(1, "malloc"); - if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) + buf = NULL; + do { + needed += needed / 2; + newbuf = realloc(buf, needed); + if (newbuf == NULL) + err(1, "realloc"); /* also free all allocated memory */ + buf = newbuf; + st = sysctl(mib, 6, buf, &needed, NULL, 0); + } while (st == -1 && errno == ENOMEM); + if (st == -1) err(1, "actual retrieval of routing table"); lim = buf + needed; for (next = buf; next < lim; next += rtm->rtm_msglen) {