summaryrefslogtreecommitdiff
path: root/sbin/pfctl/pfctl_radix.c
diff options
context:
space:
mode:
authorCedric Berger <cedric@cvs.openbsd.org>2003-09-24 09:12:36 +0000
committerCedric Berger <cedric@cvs.openbsd.org>2003-09-24 09:12:36 +0000
commit67dad3bb6434822c3d946151d6e0a8d4e5e13b97 (patch)
treef64f95b55749000226092d97d23b4a645fc9952a /sbin/pfctl/pfctl_radix.c
parentd27a0490ffcf2f4a9b5fa8fee0bd500d6e3c13d4 (diff)
Fix realloc usage and make sure we don't increase buffer size on failure.
ok henning@ mcbride@
Diffstat (limited to 'sbin/pfctl/pfctl_radix.c')
-rw-r--r--sbin/pfctl/pfctl_radix.c28
1 files changed, 13 insertions, 15 deletions
diff --git a/sbin/pfctl/pfctl_radix.c b/sbin/pfctl/pfctl_radix.c
index 71dbe3174b4..788522c530d 100644
--- a/sbin/pfctl/pfctl_radix.c
+++ b/sbin/pfctl/pfctl_radix.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: pfctl_radix.c,v 1.20 2003/08/22 21:50:34 david Exp $ */
+/* $OpenBSD: pfctl_radix.c,v 1.21 2003/09/24 09:12:35 cedric Exp $ */
/*
* Copyright (c) 2002 Cedric Berger
@@ -512,6 +512,7 @@ pfr_buf_next(struct pfr_buffer *b, const void *prev)
int
pfr_buf_grow(struct pfr_buffer *b, int minsize)
{
+ caddr_t p;
size_t bs;
if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX) {
@@ -522,29 +523,26 @@ pfr_buf_grow(struct pfr_buffer *b, int minsize)
return (0);
bs = buf_esize[b->pfrb_type];
if (!b->pfrb_msize) {
- b->pfrb_msize = minsize;
- if (b->pfrb_msize < 64)
- b->pfrb_msize = 64;
- b->pfrb_caddr = calloc(bs, b->pfrb_msize);
+ if (minsize < 64)
+ minsize = 64;
+ b->pfrb_caddr = calloc(bs, minsize);
if (b->pfrb_caddr == NULL)
return (-1);
+ b->pfrb_msize = minsize;
} else {
- int omsize = b->pfrb_msize;
-
if (minsize == 0)
- b->pfrb_msize *= 2;
- else
- b->pfrb_msize = minsize;
- if (b->pfrb_msize < 0 || b->pfrb_msize >= SIZE_T_MAX / bs) {
+ minsize = b->pfrb_msize * 2;
+ if (minsize < 0 || minsize >= SIZE_T_MAX / bs) {
/* msize overflow */
errno = ENOMEM;
return (-1);
}
- b->pfrb_caddr = realloc(b->pfrb_caddr, b->pfrb_msize * bs);
- if (b->pfrb_caddr == NULL)
+ p = realloc(b->pfrb_caddr, minsize * bs);
+ if (p == NULL)
return (-1);
- bzero(((caddr_t)b->pfrb_caddr) + omsize * bs,
- (b->pfrb_msize-omsize) * bs);
+ bzero(p + b->pfrb_msize * bs, (minsize - b->pfrb_msize) * bs);
+ b->pfrb_caddr = p;
+ b->pfrb_msize = minsize;
}
return (0);
}