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