]> xenbits.xensource.com Git - people/julieng/freebsd.git/commitdiff
Reallocate a maxlen-long buffer only when the current maxlen is
authorhrs <hrs@FreeBSD.org>
Tue, 6 Oct 2015 08:43:48 +0000 (08:43 +0000)
committerhrs <hrs@FreeBSD.org>
Tue, 6 Oct 2015 08:43:48 +0000 (08:43 +0000)
shorter than the required length.  Note that it rarely happens
because maxlen is almost always 128 which covers struct sockaddr_storage.

usr.sbin/rpcbind/rpcb_svc_com.c

index 326224a6062ee8a3eaa53b64b194e806ed75256b..2ee2a4c7d87e7402382c3ac6d0186da803ac6ccc 100644 (file)
@@ -1051,17 +1051,19 @@ netbufcmp(struct netbuf *n1, struct netbuf *n2)
 static bool_t
 netbuf_copybuf(struct netbuf *dst, const struct netbuf *src)
 {
+       assert(src->len <= src->maxlen);
 
-       if (dst->len != src->len || dst->buf == NULL) {
+       if (dst->maxlen < src->len || dst->buf == NULL) {
                if (dst->buf != NULL)
                        free(dst->buf);
-               if ((dst->buf = malloc(src->len)) == NULL)
+               if ((dst->buf = calloc(1, src->maxlen)) == NULL)
                        return (FALSE);
-
-               dst->maxlen = dst->len = src->len;
+               dst->maxlen = src->maxlen;
        }
 
+       dst->len = src->len;
        memcpy(dst->buf, src->buf, src->len);
+
        return (TRUE);
 }