102 lines
2.7 KiB
Diff
102 lines
2.7 KiB
Diff
|
From c25329eabeaba048cb6ef1448d1ee040c62c415f Mon Sep 17 00:00:00 2001
|
||
|
From: =?UTF-8?q?Dirk=20M=C3=BCller?= <dirk@dmllr.de>
|
||
|
Date: Tue, 22 Mar 2022 22:28:14 +0100
|
||
|
Subject: [PATCH] Restore locale handling after regex caching
|
||
|
|
||
|
file_regcomp/file_regfree had the side effect of setting and restoring
|
||
|
locale C_TYPE to C to have predictable regex matching. With regcomp
|
||
|
caching file_regfree has been changed to be only called at destruction
|
||
|
time, which means the library changed the locale setting for anything
|
||
|
else as well. Restore old behavior by splitting save/restore into
|
||
|
separate functions which are surrounding regcomp() and regexec() only.
|
||
|
---
|
||
|
src/funcs.c | 39 ++++++++++++++++++++++++++++++++-------
|
||
|
1 file changed, 32 insertions(+), 7 deletions(-)
|
||
|
|
||
|
diff --git a/src/funcs.c b/src/funcs.c
|
||
|
index dcfd352d..7ecaff33 100644
|
||
|
--- a/src/funcs.c
|
||
|
+++ b/src/funcs.c
|
||
|
@@ -658,35 +658,62 @@ out:
|
||
|
return rv;
|
||
|
}
|
||
|
|
||
|
+static void
|
||
|
+file_reg_set_ctype(file_regex_t *rx)
|
||
|
+{
|
||
|
+#ifdef USE_C_LOCALE
|
||
|
+ rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
|
||
|
+ assert(rx->old_lc_ctype != NULL);
|
||
|
+#else
|
||
|
+ (void)setlocale(LC_CTYPE, "C");
|
||
|
+#endif
|
||
|
+}
|
||
|
+
|
||
|
+static void
|
||
|
+file_reg_restore_ctype(file_regex_t *rx)
|
||
|
+{
|
||
|
+#ifdef USE_C_LOCALE
|
||
|
+ (void)uselocale(rx->old_lc_ctype);
|
||
|
+#else
|
||
|
+ (void)setlocale(LC_CTYPE, rx->old_lc_ctype);
|
||
|
+#endif
|
||
|
+}
|
||
|
+
|
||
|
protected int
|
||
|
file_regcomp(file_regex_t *rx, const char *pat, int flags)
|
||
|
{
|
||
|
#ifdef USE_C_LOCALE
|
||
|
rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
|
||
|
assert(rx->c_lc_ctype != NULL);
|
||
|
- rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
|
||
|
- assert(rx->old_lc_ctype != NULL);
|
||
|
#else
|
||
|
rx->old_lc_ctype = setlocale(LC_CTYPE, NULL);
|
||
|
assert(rx->old_lc_ctype != NULL);
|
||
|
rx->old_lc_ctype = strdup(rx->old_lc_ctype);
|
||
|
assert(rx->old_lc_ctype != NULL);
|
||
|
- (void)setlocale(LC_CTYPE, "C");
|
||
|
#endif
|
||
|
rx->pat = pat;
|
||
|
|
||
|
- return rx->rc = regcomp(&rx->rx, pat, flags);
|
||
|
+ file_reg_set_ctype(rx);
|
||
|
+ rx->rc = regcomp(&rx->rx, pat, flags);
|
||
|
+ file_reg_restore_ctype(rx);
|
||
|
+
|
||
|
+ return rx->rc;
|
||
|
}
|
||
|
|
||
|
protected int
|
||
|
file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
|
||
|
regmatch_t* pmatch, int eflags)
|
||
|
{
|
||
|
+ int rc;
|
||
|
assert(rx->rc == 0);
|
||
|
/* XXX: force initialization because glibc does not always do this */
|
||
|
if (nmatch != 0)
|
||
|
memset(pmatch, 0, nmatch * sizeof(*pmatch));
|
||
|
- return regexec(&rx->rx, str, nmatch, pmatch, eflags);
|
||
|
+ file_reg_set_ctype(rx);
|
||
|
+ rc = regexec(&rx->rx, str, nmatch, pmatch, eflags);
|
||
|
+ file_reg_restore_ctype(rx);
|
||
|
+
|
||
|
+ return rc;
|
||
|
}
|
||
|
|
||
|
protected void
|
||
|
@@ -695,10 +722,8 @@ file_regfree(file_regex_t *rx)
|
||
|
if (rx->rc == 0)
|
||
|
regfree(&rx->rx);
|
||
|
#ifdef USE_C_LOCALE
|
||
|
- (void)uselocale(rx->old_lc_ctype);
|
||
|
freelocale(rx->c_lc_ctype);
|
||
|
#else
|
||
|
- (void)setlocale(LC_CTYPE, rx->old_lc_ctype);
|
||
|
free(rx->old_lc_ctype);
|
||
|
#endif
|
||
|
}
|
||
|
--
|
||
|
2.35.1
|
||
|
|