Skip to content

Commit d4ba1fd

Browse files
committed
Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3: Fix #74170: locale information change after mime_content_type
2 parents 5155097 + c62cd9a commit d4ba1fd

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

ext/fileinfo/libmagic/funcs.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,9 @@ file_replace(struct magic_set *ms, const char *pat, const char *rep)
527527
zend_string *repl;
528528
size_t rep_cnt = 0;
529529

530-
(void)setlocale(LC_CTYPE, "C");
531-
532530
opts |= PCRE2_MULTILINE;
533531
convert_libmagic_pattern(&patt, (char*)pat, strlen(pat), opts);
534-
if ((pce = pcre_get_compiled_regex_cache(Z_STR(patt))) == NULL) {
532+
if ((pce = pcre_get_compiled_regex_cache_ex(Z_STR(patt), 0)) == NULL) {
535533
zval_ptr_dtor(&patt);
536534
rep_cnt = -1;
537535
goto out;
@@ -553,7 +551,6 @@ file_replace(struct magic_set *ms, const char *pat, const char *rep)
553551
zend_string_release_ex(res, 0);
554552

555553
out:
556-
(void)setlocale(LC_CTYPE, "");
557554
return rep_cnt;
558555
}
559556

ext/fileinfo/libmagic/readcdf.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,24 @@ cdf_app_to_mime(const char *vbuf, const struct nv *nv)
112112
{
113113
size_t i;
114114
const char *rv = NULL;
115+
char *vbuf_lower;
115116

116-
(void)setlocale(LC_CTYPE, "C");
117-
for (i = 0; nv[i].pattern != NULL; i++)
118-
if (strcasestr(vbuf, nv[i].pattern) != NULL) {
117+
vbuf_lower = zend_str_tolower_dup(vbuf, strlen(vbuf));
118+
for (i = 0; nv[i].pattern != NULL; i++) {
119+
char *pattern_lower;
120+
int found;
121+
122+
pattern_lower = zend_str_tolower_dup(nv[i].pattern, strlen(nv[i].pattern));
123+
found = (strstr(vbuf_lower, pattern_lower) != NULL);
124+
efree(pattern_lower);
125+
126+
if (found) {
119127
rv = nv[i].mime;
120128
break;
121129
}
122-
(void)setlocale(LC_CTYPE, "");
130+
}
131+
132+
efree(vbuf_lower);
123133
return rv;
124134
}
125135

ext/fileinfo/libmagic/softmagic.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -469,27 +469,25 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
469469
private int
470470
check_fmt(struct magic_set *ms, const char *fmt)
471471
{
472-
pcre2_code *pce;
473-
uint32_t capture_count;
472+
pcre_cache_entry *pce;
474473
int rv = -1;
475474
zend_string *pattern;
476475

477476
if (strchr(fmt, '%') == NULL)
478477
return 0;
479478

480-
(void)setlocale(LC_CTYPE, "C");
481479
pattern = zend_string_init("~%[-0-9\\.]*s~", sizeof("~%[-0-9\\.]*s~") - 1, 0);
482-
if ((pce = pcre_get_compiled_regex(pattern, &capture_count)) == NULL) {
480+
if ((pce = pcre_get_compiled_regex_cache_ex(pattern, 0)) == NULL) {
483481
rv = -1;
484482
} else {
485-
pcre2_match_data *match_data = php_pcre_create_match_data(capture_count, pce);
483+
pcre2_code *re = php_pcre_pce_re(pce);
484+
pcre2_match_data *match_data = php_pcre_create_match_data(0, re);
486485
if (match_data) {
487-
rv = pcre2_match(pce, (PCRE2_SPTR)fmt, strlen(fmt), 0, 0, match_data, php_pcre_mctx()) > 0;
486+
rv = pcre2_match(re, (PCRE2_SPTR)fmt, strlen(fmt), 0, 0, match_data, php_pcre_mctx()) > 0;
488487
php_pcre_free_match_data(match_data);
489488
}
490489
}
491-
zend_string_release_ex(pattern, 0);
492-
(void)setlocale(LC_CTYPE, "");
490+
zend_string_release(pattern);
493491
return rv;
494492
}
495493

ext/fileinfo/tests/bug74170.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Bug #74170 locale information change after mime_content_type
3+
--SKIPIF--
4+
<?php
5+
if (!class_exists('finfo'))
6+
die('skip no fileinfo extension');
7+
if (!extension_loaded('intl'))
8+
die('skip intl extension not enabled');
9+
if (setlocale(LC_CTYPE, 'ru_RU.koi8r') === false)
10+
die('skip ru_RU.koi8r locale is not available');
11+
?>
12+
--FILE--
13+
<?php
14+
var_dump(setlocale(LC_CTYPE, 'ru_RU.koi8r'));
15+
var_dump(nl_langinfo(CODESET));
16+
var_dump(mime_content_type(__DIR__ . '/resources/test.ppt'));
17+
var_dump(nl_langinfo(CODESET));
18+
--EXPECT--
19+
string(11) "ru_RU.koi8r"
20+
string(6) "KOI8-R"
21+
string(29) "application/vnd.ms-powerpoint"
22+
string(6) "KOI8-R"

0 commit comments

Comments
 (0)