Skip to content

Commit d11fcf8

Browse files
authored
Merge pull request libgit2#3953 from arthurschreiber/arthur/fix-regcomp-locale-issues
Make sure we use the `C` locale for `regcomp` on macOS.
2 parents e8ab13b + ab96ca5 commit d11fcf8

File tree

8 files changed

+54
-12
lines changed

8 files changed

+54
-12
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Mo
2020

2121
INCLUDE(CheckLibraryExists)
2222
INCLUDE(CheckFunctionExists)
23+
INCLUDE(CheckSymbolExists)
2324
INCLUDE(CheckStructHasMember)
2425
INCLUDE(AddCFlagIfSupported)
2526
INCLUDE(FindPkgConfig)
@@ -507,6 +508,11 @@ ELSE ()
507508
ENDIF ()
508509
ENDIF()
509510

511+
CHECK_SYMBOL_EXISTS(regcomp_l "xlocale.h" HAVE_REGCOMP_L)
512+
IF (HAVE_REGCOMP_L)
513+
ADD_DEFINITIONS(-DHAVE_REGCOMP_L)
514+
ENDIF ()
515+
510516
CHECK_FUNCTION_EXISTS(futimens HAVE_FUTIMENS)
511517
IF (HAVE_FUTIMENS)
512518
ADD_DEFINITIONS(-DHAVE_FUTIMENS)

src/config.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ int git_config_iterator_glob_new(git_config_iterator **out, const git_config *cf
478478
iter = git__calloc(1, sizeof(all_iter));
479479
GITERR_CHECK_ALLOC(iter);
480480

481-
if ((result = regcomp(&iter->regex, regexp, REG_EXTENDED)) != 0) {
481+
if ((result = p_regcomp(&iter->regex, regexp, REG_EXTENDED)) != 0) {
482482
giterr_set_regex(&iter->regex, result);
483483
git__free(iter);
484484
return -1;
@@ -512,7 +512,7 @@ int git_config_backend_foreach_match(
512512
int error = 0;
513513

514514
if (regexp != NULL) {
515-
if ((error = regcomp(&regex, regexp, REG_EXTENDED)) != 0) {
515+
if ((error = p_regcomp(&regex, regexp, REG_EXTENDED)) != 0) {
516516
giterr_set_regex(&regex, error);
517517
regfree(&regex);
518518
return -1;
@@ -1003,7 +1003,7 @@ int git_config_multivar_iterator_new(git_config_iterator **out, const git_config
10031003
goto on_error;
10041004

10051005
if (regexp != NULL) {
1006-
error = regcomp(&iter->regex, regexp, REG_EXTENDED);
1006+
error = p_regcomp(&iter->regex, regexp, REG_EXTENDED);
10071007
if (error != 0) {
10081008
giterr_set_regex(&iter->regex, error);
10091009
error = -1;

src/config_file.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ static int config_set_multivar(
570570
if ((result = git_config__normalize_name(name, &key)) < 0)
571571
return result;
572572

573-
result = regcomp(&preg, regexp, REG_EXTENDED);
573+
result = p_regcomp(&preg, regexp, REG_EXTENDED);
574574
if (result != 0) {
575575
giterr_set_regex(&preg, result);
576576
result = -1;
@@ -657,7 +657,7 @@ static int config_delete_multivar(git_config_backend *cfg, const char *name, con
657657

658658
refcounted_strmap_free(map);
659659

660-
result = regcomp(&preg, regexp, REG_EXTENDED);
660+
result = p_regcomp(&preg, regexp, REG_EXTENDED);
661661
if (result != 0) {
662662
giterr_set_regex(&preg, result);
663663
result = -1;
@@ -1957,4 +1957,3 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
19571957
git_buf_free(&reader->buffer);
19581958
return result;
19591959
}
1960-

src/diff_driver.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static int diff_driver_add_patterns(
114114
if (error < 0)
115115
break;
116116

117-
if ((error = regcomp(&pat->re, buf.ptr, regex_flags)) != 0) {
117+
if ((error = p_regcomp(&pat->re, buf.ptr, regex_flags)) != 0) {
118118
/*
119119
* TODO: issue a warning
120120
*/
@@ -210,7 +210,7 @@ static int git_diff_driver_builtin(
210210
goto done;
211211

212212
if (ddef->words &&
213-
(error = regcomp(
213+
(error = p_regcomp(
214214
&drv->word_pattern, ddef->words, ddef->flags | REG_EXTENDED)))
215215
{
216216
error = giterr_set_regex(&drv->word_pattern, error);
@@ -314,7 +314,7 @@ static int git_diff_driver_load(
314314
goto done;
315315
if (!ce || !ce->value)
316316
/* no diff.<driver>.wordregex, so just continue */;
317-
else if (!(error = regcomp(&drv->word_pattern, ce->value, REG_EXTENDED)))
317+
else if (!(error = p_regcomp(&drv->word_pattern, ce->value, REG_EXTENDED)))
318318
found_driver = true;
319319
else {
320320
/* TODO: warn about bad regex instead of failure */
@@ -519,4 +519,3 @@ void git_diff_find_context_clear(git_diff_find_context_payload *payload)
519519
payload->driver = NULL;
520520
}
521521
}
522-

src/revparse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static int build_regex(regex_t *regex, const char *pattern)
5050
return GIT_EINVALIDSPEC;
5151
}
5252

53-
error = regcomp(regex, pattern, REG_EXTENDED);
53+
error = p_regcomp(regex, pattern, REG_EXTENDED);
5454
if (!error)
5555
return 0;
5656

src/unix/posix.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,14 @@ GIT_INLINE(int) p_futimes(int f, const struct p_timeval t[2])
8080
# define p_futimes futimes
8181
#endif
8282

83+
#ifdef HAVE_REGCOMP_L
84+
#include <xlocale.h>
85+
GIT_INLINE(int) p_regcomp(regex_t *preg, const char *pattern, int cflags)
86+
{
87+
return regcomp_l(preg, pattern, cflags, (locale_t) 0);
88+
}
89+
#else
90+
# define p_regcomp regcomp
91+
#endif
92+
8393
#endif

src/win32/posix.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,7 @@ extern int p_lstat_posixly(const char *filename, struct stat *buf);
5757
extern struct tm * p_localtime_r(const time_t *timer, struct tm *result);
5858
extern struct tm * p_gmtime_r(const time_t *timer, struct tm *result);
5959

60+
/* Use the bundled regcomp */
61+
#define p_regcomp regcomp
62+
6063
#endif

tests/core/posix.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "clar_libgit2.h"
1313
#include "posix.h"
14+
#include "userdiff.h"
1415

1516
void test_core_posix__initialize(void)
1617
{
@@ -39,7 +40,7 @@ void test_core_posix__inet_pton(void)
3940
struct in_addr addr;
4041
struct in6_addr addr6;
4142
size_t i;
42-
43+
4344
struct in_addr_data {
4445
const char *p;
4546
const uint8_t n[4];
@@ -146,3 +147,27 @@ void test_core_posix__utimes(void)
146147

147148
p_unlink("foo");
148149
}
150+
151+
void test_core_posix__p_regcomp_compile_single_byte_regexps(void)
152+
{
153+
regex_t preg;
154+
155+
cl_must_pass(p_regcomp(&preg, "[\xc0-\xff][\x80-\xbf]", REG_EXTENDED));
156+
157+
regfree(&preg);
158+
}
159+
160+
void test_core_posix__p_regcomp_compile_userdiff_regexps(void)
161+
{
162+
regex_t preg;
163+
size_t idx;
164+
165+
for (idx = 0; idx < ARRAY_SIZE(builtin_defs); ++idx) {
166+
git_diff_driver_definition ddef = builtin_defs[idx];
167+
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+
}
171+
172+
regfree(&preg);
173+
}

0 commit comments

Comments
 (0)