Blame SOURCES/file-5.39-regex-caching-2.patch

8a9804
diff --git a/src/apprentice.c b/src/apprentice.c
8a9804
index 21eac1e..781c5e1 100644
8a9804
--- a/src/apprentice.c
8a9804
+++ b/src/apprentice.c
8a9804
@@ -513,6 +513,9 @@ file_ms_free(struct magic_set *ms)
8a9804
 	free(ms->o.pbuf);
8a9804
 	free(ms->o.buf);
8a9804
 	free(ms->c.li);
8a9804
+#ifdef USE_C_LOCALE
8a9804
+	freelocale(ms->c_lc_ctype);
8a9804
+#endif
8a9804
 	free(ms);
8a9804
 }
8a9804
 
8a9804
@@ -551,6 +554,10 @@ file_ms_alloc(int flags)
8a9804
 	ms->elf_notes_max = FILE_ELF_NOTES_MAX;
8a9804
 	ms->regex_max = FILE_REGEX_MAX;
8a9804
 	ms->bytes_max = FILE_BYTES_MAX;
8a9804
+#ifdef USE_C_LOCALE
8a9804
+       ms->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
8a9804
+       assert(ms->c_lc_ctype != NULL);
8a9804
+#endif
8a9804
 	return ms;
8a9804
 free:
8a9804
 	free(ms);
8a9804
@@ -624,6 +631,7 @@ mlist_free_one(struct mlist *ml)
8a9804
 		if (ml->magic_rxcomp[i]) {
8a9804
 			file_regfree(ml->magic_rxcomp[i]);
8a9804
 			free(ml->magic_rxcomp[i]);
8a9804
+			ml->magic_rxcomp[i] = NULL;
8a9804
 		}
8a9804
 	}
8a9804
 	free(ml->magic_rxcomp);
8a9804
@@ -2714,7 +2722,8 @@ getvalue(struct magic_set *ms, struct magic *m, const char **p, int action)
8a9804
 		}
8a9804
 		if (m->type == FILE_REGEX) {
8a9804
 			file_regex_t rx;
8a9804
-			int rc = file_regcomp(&rx, m->value.s, REG_EXTENDED);
8a9804
+			int rc = file_regcomp(ms, &rx, m->value.s,
8a9804
+			    REG_EXTENDED);
8a9804
 			if (rc) {
8a9804
 				if (ms->flags & MAGIC_CHECK)
8a9804
 					file_regerror(&rx, rc, ms);
8a9804
diff --git a/src/file.h b/src/file.h
8a9804
index c0b5a7c..f049446 100644
8a9804
--- a/src/file.h
8a9804
+++ b/src/file.h
8a9804
@@ -173,13 +173,6 @@
8a9804
 
8a9804
 typedef struct {
8a9804
 	const char *pat;
8a9804
-#if defined(HAVE_NEWLOCALE) && defined(HAVE_USELOCALE) && defined(HAVE_FREELOCALE)
8a9804
-#define USE_C_LOCALE
8a9804
-	locale_t old_lc_ctype;
8a9804
-	locale_t c_lc_ctype;
8a9804
-#else
8a9804
-	char *old_lc_ctype;
8a9804
-#endif
8a9804
 	int rc;
8a9804
 	regex_t rx;
8a9804
 } file_regex_t;
8a9804
@@ -487,6 +480,10 @@ struct magic_set {
8a9804
 #define	FILE_INDIR_MAX			50
8a9804
 #define	FILE_NAME_MAX			50
8a9804
 #define	FILE_REGEX_MAX			8192
8a9804
+#if defined(HAVE_NEWLOCALE) && defined(HAVE_USELOCALE) && defined(HAVE_FREELOCALE)
8a9804
+#define USE_C_LOCALE
8a9804
+       locale_t c_lc_ctype;
8a9804
+#endif
8a9804
 };
8a9804
 
8a9804
 /* Type for Unicode characters */
8a9804
@@ -574,9 +571,10 @@ protected int buffer_fill(const struct buffer *);
8a9804
 
8a9804
 
8a9804
 
8a9804
-protected int file_regcomp(file_regex_t *, const char *, int);
8a9804
-protected int file_regexec(file_regex_t *, const char *, size_t, regmatch_t *,
8a9804
+protected int file_regcomp(struct magic_set *, file_regex_t *, const char *,
8a9804
     int);
8a9804
+protected int file_regexec(struct magic_set *, file_regex_t *, const char *,
8a9804
+    size_t, regmatch_t *, int);
8a9804
 protected void file_regfree(file_regex_t *);
8a9804
 protected void file_regerror(file_regex_t *, int, struct magic_set *);
8a9804
 
8a9804
diff --git a/src/funcs.c b/src/funcs.c
8a9804
index 6320cf2..1391a44 100644
8a9804
--- a/src/funcs.c
8a9804
+++ b/src/funcs.c
8a9804
@@ -613,13 +613,13 @@ file_replace(struct magic_set *ms, const char *pat, const char *rep)
8a9804
 	file_regex_t rx;
8a9804
 	int rc, rv = -1;
8a9804
 
8a9804
-	rc = file_regcomp(&rx, pat, REG_EXTENDED);
8a9804
+	rc = file_regcomp(ms, &rx, pat, REG_EXTENDED);
8a9804
 	if (rc) {
8a9804
 		file_regerror(&rx, rc, ms);
8a9804
 	} else {
8a9804
 		regmatch_t rm;
8a9804
 		int nm = 0;
8a9804
-		while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
8a9804
+		while (file_regexec(ms, &rx, ms->o.buf, 1, &rm, 0) == 0) {
8a9804
 			ms->o.buf[rm.rm_so] = '\0';
8a9804
 			if (file_printf(ms, "%s%s", rep,
8a9804
 			    rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
8a9804
@@ -634,34 +634,52 @@ out:
8a9804
 }
8a9804
 
8a9804
 protected int
8a9804
-file_regcomp(file_regex_t *rx, const char *pat, int flags)
8a9804
+file_regcomp(struct magic_set *ms, file_regex_t *rx, const char *pat, int flags)
8a9804
 {
8a9804
 #ifdef USE_C_LOCALE
8a9804
-	rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
8a9804
-	assert(rx->c_lc_ctype != NULL);
8a9804
-	rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
8a9804
-	assert(rx->old_lc_ctype != NULL);
8a9804
+	locale_t old = uselocale(ms->c_lc_ctype);
8a9804
+	assert(old != NULL);
8a9804
 #else
8a9804
-	rx->old_lc_ctype = setlocale(LC_CTYPE, NULL);
8a9804
-	assert(rx->old_lc_ctype != NULL);
8a9804
-	rx->old_lc_ctype = strdup(rx->old_lc_ctype);
8a9804
-	assert(rx->old_lc_ctype != NULL);
8a9804
+	char old[1024];
8a9804
+	strlcpy(old, setlocale(LC_CTYPE, NULL), sizeof(old));
8a9804
 	(void)setlocale(LC_CTYPE, "C");
8a9804
 #endif
8a9804
 	rx->pat = pat;
8a9804
 
8a9804
-	return rx->rc = regcomp(&rx->rx, pat, flags);
8a9804
+	rx->rc = regcomp(&rx->rx, pat, flags);
8a9804
+
8a9804
+#ifdef USE_C_LOCALE
8a9804
+	uselocale(old);
8a9804
+#else
8a9804
+	(void)setlocale(LC_CTYPE, old);
8a9804
+#endif
8a9804
+	return rx->rc;
8a9804
 }
8a9804
 
8a9804
 protected int
8a9804
-file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
8a9804
-    regmatch_t* pmatch, int eflags)
8a9804
+file_regexec(struct magic_set *ms, file_regex_t *rx, const char *str,
8a9804
+    size_t nmatch, regmatch_t* pmatch, int eflags)
8a9804
 {
8a9804
+#ifdef USE_C_LOCALE
8a9804
+	locale_t old = uselocale(ms->c_lc_ctype);
8a9804
+	assert(old != NULL);
8a9804
+#else
8a9804
+	char old[1024];
8a9804
+	strlcpy(old, setlocale(LC_CTYPE, NULL), sizeof(old));
8a9804
+	(void)setlocale(LC_CTYPE, "C");
8a9804
+#endif
8a9804
+	int rc;
8a9804
 	assert(rx->rc == 0);
8a9804
 	/* XXX: force initialization because glibc does not always do this */
8a9804
 	if (nmatch != 0)
8a9804
 		memset(pmatch, 0, nmatch * sizeof(*pmatch));
8a9804
-	return regexec(&rx->rx, str, nmatch, pmatch, eflags);
8a9804
+	rc = regexec(&rx->rx, str, nmatch, pmatch, eflags);
8a9804
+#ifdef USE_C_LOCALE
8a9804
+	uselocale(old);
8a9804
+#else
8a9804
+	(void)setlocale(LC_CTYPE, old);
8a9804
+#endif
8a9804
+	return rc;
8a9804
 }
8a9804
 
8a9804
 protected void
8a9804
@@ -669,13 +687,6 @@ file_regfree(file_regex_t *rx)
8a9804
 {
8a9804
 	if (rx->rc == 0)
8a9804
 		regfree(&rx->rx);
8a9804
-#ifdef USE_C_LOCALE
8a9804
-	(void)uselocale(rx->old_lc_ctype);
8a9804
-	freelocale(rx->c_lc_ctype);
8a9804
-#else
8a9804
-	(void)setlocale(LC_CTYPE, rx->old_lc_ctype);
8a9804
-	free(rx->old_lc_ctype);
8a9804
-#endif
8a9804
 }
8a9804
 
8a9804
 protected void
8a9804
diff --git a/src/softmagic.c b/src/softmagic.c
8a9804
index 43338fc..b4052a6 100644
8a9804
--- a/src/softmagic.c
8a9804
+++ b/src/softmagic.c
8a9804
@@ -478,11 +478,11 @@ check_fmt(struct magic_set *ms, const char *fmt)
8a9804
 	if (strchr(fmt, '%') == NULL)
8a9804
 		return 0;
8a9804
 
8a9804
-	rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
8a9804
+	rc = file_regcomp(ms, &rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
8a9804
 	if (rc) {
8a9804
 		file_regerror(&rx, rc, ms);
8a9804
 	} else {
8a9804
-		rc = file_regexec(&rx, fmt, 0, 0, 0);
8a9804
+		rc = file_regexec(ms, &rx, fmt, 0, 0, 0);
8a9804
 		rv = !rc;
8a9804
 	}
8a9804
 	file_regfree(&rx);
8a9804
@@ -2003,11 +2003,12 @@ alloc_regex(struct magic_set *ms, struct magic *m)
8a9804
 		return NULL;
8a9804
 	}
8a9804
 
8a9804
-	rc = file_regcomp(rx, m->value.s, REG_EXTENDED | REG_NEWLINE |
8a9804
+	rc = file_regcomp(ms, rx, m->value.s, REG_EXTENDED | REG_NEWLINE |
8a9804
 	    ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
8a9804
 	if (rc == 0)
8a9804
 		return rx;
8a9804
 
8a9804
+fprintf(stderr, "regcomp %s %d\n", m->value.s, rc);
8a9804
 	file_regerror(rx, rc, ms);
8a9804
 	file_regfree(rx);
8a9804
 	free(rx);
8a9804
@@ -2225,7 +2226,7 @@ magiccheck(struct magic_set *ms, struct magic *m, file_regex_t **m_cache)
8a9804
 		    search = CCAST(char *, "");
8a9804
 		    copy = NULL;
8a9804
 		}
8a9804
-		rc = file_regexec(rx, RCAST(const char *, search),
8a9804
+		rc = file_regexec(ms, rx, RCAST(const char *, search),
8a9804
 		    1, &pmatch, 0);
8a9804
 		free(copy);
8a9804
 		switch (rc) {