diff options
author | 2001-12-17 22:29:47 +0000 | |
---|---|---|
committer | 2001-12-17 22:29:47 +0000 | |
commit | 78871eb876677c1cfa6cb2a6202247a39f574ae0 (patch) | |
tree | 700ae2d407e898e8e429b3ec9e8f7a240c082bee | |
parent | sync (diff) | |
download | wireguard-openbsd-78871eb876677c1cfa6cb2a6202247a39f574ae0.tar.xz wireguard-openbsd-78871eb876677c1cfa6cb2a6202247a39f574ae0.zip |
fix memory leak associated with compiled BPF program, sync'd from tcpdump.org. ok itojun@
-rw-r--r-- | lib/libpcap/gencode.c | 18 | ||||
-rw-r--r-- | lib/libpcap/pcap.3 | 13 | ||||
-rw-r--r-- | lib/libpcap/pcap.c | 6 | ||||
-rw-r--r-- | lib/libpcap/pcap.h | 7 |
4 files changed, 34 insertions, 10 deletions
diff --git a/lib/libpcap/gencode.c b/lib/libpcap/gencode.c index 60eb842b4b9..9f494914ba1 100644 --- a/lib/libpcap/gencode.c +++ b/lib/libpcap/gencode.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gencode.c,v 1.13 2001/06/25 23:03:32 provos Exp $ */ +/* $OpenBSD: gencode.c,v 1.14 2001/12/17 22:29:47 dugsong Exp $ */ /* * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998 @@ -22,7 +22,7 @@ */ #ifndef lint static const char rcsid[] = - "@(#) $Header: /home/cvs/src/lib/libpcap/gencode.c,v 1.13 2001/06/25 23:03:32 provos Exp $ (LBL)"; + "@(#) $Header: /home/cvs/src/lib/libpcap/gencode.c,v 1.14 2001/12/17 22:29:47 dugsong Exp $ (LBL)"; #endif #include <sys/types.h> @@ -363,6 +363,20 @@ pcap_compile_nopcap(int snaplen_arg, int linktype_arg, } /* + * Clean up a "struct bpf_program" by freeing all the memory allocated + * in it. + */ +void +pcap_freecode(struct bpf_program *program) +{ + program->bf_len = 0; + if (program->bf_insns != NULL) { + free((char *)program->bf_insns); + program->bf_insns = NULL; + } +} + +/* * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates * which of the jt and jf fields has been resolved and which is a pointer * back to another unresolved block (or nil). At least one of the fields diff --git a/lib/libpcap/pcap.3 b/lib/libpcap/pcap.3 index c2b4565c8f6..0570a9fe705 100644 --- a/lib/libpcap/pcap.3 +++ b/lib/libpcap/pcap.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: pcap.3,v 1.21 2001/12/06 02:50:46 millert Exp $ +.\" $OpenBSD: pcap.3,v 1.22 2001/12/17 22:29:47 dugsong Exp $ .\" .\" Copyright (c) 1994, 1996, 1997 .\" The Regents of the University of California. All rights reserved. @@ -49,6 +49,8 @@ .Fn pcap_compile "pcap_t *p" "struct bpf_program *fp" "char *str" "int optimize" "bpf_u_int32 netmask" .Ft int .Fn pcap_setfilter "pcap_t *p" "struct bpf_program *fp" +.Ft void +.Fn pcap_freecode "struct bpf_program *fp" .Ft "u_char *" .Fn pcap_next "pcap_t *p" "struct pcap_pkthdr *h" .Ft int @@ -251,6 +253,15 @@ is returned on failure; 0 is returned on success. .Pp +.Fn pcap_freecode +is used to free up allocated memory pointed to by a +.Fa bpf_program +struct generated by +.Fn pcap_compile +when that BPF program is no longer needed, for example after it has +been made the filter program for a pcap structure by a call to +.Fn pcap_setfilter . +.Pp .Fn pcap_loop is similar to .Fn pcap_dispatch diff --git a/lib/libpcap/pcap.c b/lib/libpcap/pcap.c index 0fd1adb9213..6e75ef90b10 100644 --- a/lib/libpcap/pcap.c +++ b/lib/libpcap/pcap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pcap.c,v 1.6 1999/07/20 04:49:55 deraadt Exp $ */ +/* $OpenBSD: pcap.c,v 1.7 2001/12/17 22:29:47 dugsong Exp $ */ /* * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998 @@ -35,7 +35,7 @@ #ifndef lint static const char rcsid[] = - "@(#) $Header: /home/cvs/src/lib/libpcap/pcap.c,v 1.6 1999/07/20 04:49:55 deraadt Exp $ (LBL)"; + "@(#) $Header: /home/cvs/src/lib/libpcap/pcap.c,v 1.7 2001/12/17 22:29:47 dugsong Exp $ (LBL)"; #endif #include <sys/types.h> @@ -202,6 +202,6 @@ pcap_close(pcap_t *p) if (p->md.device != NULL) free(p->md.device); #endif - + pcap_freecode(&p->fcode); free(p); } diff --git a/lib/libpcap/pcap.h b/lib/libpcap/pcap.h index 1ef558d9b5f..65aafb0cb14 100644 --- a/lib/libpcap/pcap.h +++ b/lib/libpcap/pcap.h @@ -1,4 +1,4 @@ -/* $OpenBSD: pcap.h,v 1.9 2001/10/02 18:04:35 deraadt Exp $ */ +/* $OpenBSD: pcap.h,v 1.10 2001/12/17 22:29:47 dugsong Exp $ */ /* * Copyright (c) 1993, 1994, 1995, 1996, 1997 @@ -32,7 +32,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#) $Header: /home/cvs/src/lib/libpcap/pcap.h,v 1.9 2001/10/02 18:04:35 deraadt Exp $ (LBL) + * @(#) $Header: /home/cvs/src/lib/libpcap/pcap.h,v 1.10 2001/12/17 22:29:47 dugsong Exp $ (LBL) */ #ifndef lib_pcap_h @@ -119,8 +119,7 @@ int pcap_compile(pcap_t *, struct bpf_program *, char *, int, bpf_u_int32); int pcap_compile_nopcap(int, int, struct bpf_program *, char *, int, bpf_u_int32); -/* XXX */ -int pcap_freecode(pcap_t *, struct bpf_program *); +void pcap_freecode(struct bpf_program *); int pcap_datalink(pcap_t *); int pcap_snapshot(pcap_t *); int pcap_is_swapped(pcap_t *); |