1 /*	$NetBSD: regfree.c,v 1.19 2021/02/26 19:24:47 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
7  * Copyright (c) 1992, 1993, 1994
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Henry Spencer.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)regfree.c	8.3 (Berkeley) 3/20/94
38  */
39 
40 #if HAVE_NBTOOL_CONFIG_H
41 #include "nbtool_config.h"
42 #endif
43 
44 #include <sys/cdefs.h>
45 #if 0
46 static char sccsid[] = "@(#)regfree.c	8.3 (Berkeley) 3/20/94";
47 __FBSDID("$FreeBSD: head/lib/libc/regex/regfree.c 326025 2017-11-20 19:49:47Z pfg $");
48 #endif
49 __RCSID("$NetBSD: regfree.c,v 1.19 2021/02/26 19:24:47 christos Exp $");
50 
51 #include "namespace.h"
52 #include <sys/types.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <limits.h>
56 #include <regex.h>
57 
58 #ifdef __weak_alias
__weak_alias(regfree,_regfree)59 __weak_alias(regfree,_regfree)
60 #endif
61 
62 #include "utils.h"
63 #include "regex2.h"
64 
65 /*
66  - regfree - free everything
67  = extern void regfree(regex_t *);
68  */
69 void
70 regfree(regex_t *preg)
71 {
72 	struct re_guts *g;
73 	unsigned int i;
74 
75 	_DIAGASSERT(preg != NULL);
76 
77 	_DIAGASSERT(preg->re_magic == MAGIC1);
78 	if (preg->re_magic != MAGIC1)	/* oops */
79 		return;			/* nice to complain, but hard */
80 
81 	g = preg->re_g;
82 	if (g == NULL || g->magic != MAGIC2)	/* oops again */
83 		return;
84 	preg->re_magic = 0;		/* mark it invalid */
85 	g->magic = 0;			/* mark it invalid */
86 
87 	if (g->strip != NULL)
88 		free(g->strip);
89 	if (g->sets != NULL) {
90 		for (i = 0; i < g->ncsets; i++) {
91 			free(g->sets[i].ranges);
92 			free(g->sets[i].wides);
93 			free(g->sets[i].types);
94 		}
95 		free(g->sets);
96 	}
97 	if (g->must != NULL)
98 		free(g->must);
99 	if (g->charjump != NULL)
100 		free(&g->charjump[CHAR_MIN]);
101 	if (g->matchjump != NULL)
102 		free(g->matchjump);
103 	free(g);
104 }
105