Skip to content

Commit 3611797

Browse files
Fix the existence check for regcomp_l.
`xlocale.h` only defines `regcomp_l` if `regex.h` was included as well. Also change the test cases to actually test `p_regcomp` works with a multibyte locale.
1 parent 45dc219 commit 3611797

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ ELSE ()
508508
ENDIF ()
509509
ENDIF()
510510

511-
CHECK_SYMBOL_EXISTS(regcomp_l "xlocale.h" HAVE_REGCOMP_L)
511+
CHECK_SYMBOL_EXISTS(regcomp_l "regex.h;xlocale.h" HAVE_REGCOMP_L)
512512
IF (HAVE_REGCOMP_L)
513513
ADD_DEFINITIONS(-DHAVE_REGCOMP_L)
514514
ENDIF ()

tests/core/posix.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# endif
1010
#endif
1111

12+
#include <locale.h>
13+
1214
#include "clar_libgit2.h"
1315
#include "posix.h"
1416
#include "userdiff.h"
@@ -148,26 +150,46 @@ void test_core_posix__utimes(void)
148150
p_unlink("foo");
149151
}
150152

151-
void test_core_posix__p_regcomp_compile_single_byte_regexps(void)
153+
void test_core_posix__p_regcomp_ignores_global_locale_ctype(void)
152154
{
153155
regex_t preg;
156+
int error = 0;
157+
158+
const char* oldlocale = setlocale(LC_CTYPE, NULL);
154159

155-
cl_must_pass(p_regcomp(&preg, "[\xc0-\xff][\x80-\xbf]", REG_EXTENDED));
160+
if (!setlocale(LC_CTYPE, "UTF-8") &&
161+
!setlocale(LC_CTYPE, "c.utf8") &&
162+
!setlocale(LC_CTYPE, "en_US.UTF-8"))
163+
cl_skip();
156164

165+
if (MB_CUR_MAX == 1) {
166+
setlocale(LC_CTYPE, oldlocale);
167+
cl_fail("Expected locale to be switched to multibyte");
168+
}
169+
170+
p_regcomp(&preg, "[\xc0-\xff][\x80-\xbf]", REG_EXTENDED);
157171
regfree(&preg);
172+
173+
setlocale(LC_CTYPE, oldlocale);
174+
175+
cl_must_pass(error);
158176
}
159177

160178
void test_core_posix__p_regcomp_compile_userdiff_regexps(void)
161179
{
162-
regex_t preg;
163180
size_t idx;
164181

165182
for (idx = 0; idx < ARRAY_SIZE(builtin_defs); ++idx) {
166183
git_diff_driver_definition ddef = builtin_defs[idx];
184+
int error = 0;
185+
regex_t preg;
167186

168-
cl_must_pass(p_regcomp(&preg, ddef.fns, REG_EXTENDED | ddef.flags));
169-
cl_must_pass(p_regcomp(&preg, ddef.words, REG_EXTENDED));
170-
}
187+
error = p_regcomp(&preg, ddef.fns, REG_EXTENDED | ddef.flags);
188+
regfree(&preg);
189+
cl_must_pass(error);
171190

172-
regfree(&preg);
191+
error = p_regcomp(&preg, ddef.words, REG_EXTENDED);
192+
regfree(&preg);
193+
cl_must_pass(error);
194+
}
173195
}

0 commit comments

Comments
 (0)