Skip to content

Commit d319098

Browse files
committed
Fix #53251: bindtextdomain with null dir doesn't return old value
Apparently, users expect `bindtextdomain` and `bind_textdomain_codeset` with `null` as second argument to work like their C counterparts, namely to return the previously set value. Thus, we support that. Closes GH-6631.
1 parent 527bcb1 commit d319098

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2021, PHP 8.0.3
44

5+
- Gettext:
6+
. Fixed bug #53251 (bindtextdomain with null dir doesn't return old value).
7+
(cmb)
8+
59
- Opcache:
610
. Fixed bug #80634 (write_property handler of internal classes is skipped on
711
preloaded JITted code). (Dmitry)

ext/gettext/gettext.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ PHP_FUNCTION(dcgettext)
163163
/* {{{ Bind to the text domain domain_name, looking for translations in dir. Returns the current domain */
164164
PHP_FUNCTION(bindtextdomain)
165165
{
166-
char *domain, *dir;
166+
char *domain, *dir = NULL;
167167
size_t domain_len, dir_len;
168168
char *retval, dir_name[MAXPATHLEN];
169169

170-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &domain, &domain_len, &dir, &dir_len) == FAILURE) {
170+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss!", &domain, &domain_len, &dir, &dir_len) == FAILURE) {
171171
RETURN_THROWS();
172172
}
173173

@@ -178,6 +178,10 @@ PHP_FUNCTION(bindtextdomain)
178178
RETURN_THROWS();
179179
}
180180

181+
if (dir == NULL) {
182+
RETURN_STRING(bindtextdomain(domain, NULL));
183+
}
184+
181185
if (dir[0] != '\0' && strcmp(dir, "0")) {
182186
if (!VCWD_REALPATH(dir, dir_name)) {
183187
RETURN_FALSE;
@@ -272,10 +276,10 @@ PHP_FUNCTION(dcngettext)
272276
/* {{{ Specify the character encoding in which the messages from the DOMAIN message catalog will be returned. */
273277
PHP_FUNCTION(bind_textdomain_codeset)
274278
{
275-
char *domain, *codeset, *retval = NULL;
279+
char *domain, *codeset = NULL, *retval = NULL;
276280
size_t domain_len, codeset_len;
277281

278-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &domain, &domain_len, &codeset, &codeset_len) == FAILURE) {
282+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss!", &domain, &domain_len, &codeset, &codeset_len) == FAILURE) {
279283
RETURN_THROWS();
280284
}
281285

ext/gettext/gettext.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function dgettext(string $domain, string $message): string {}
1313

1414
function dcgettext(string $domain, string $message, int $category): string {}
1515

16-
function bindtextdomain(string $domain, string $directory): string|false {}
16+
function bindtextdomain(string $domain, ?string $directory): string|false {}
1717

1818
#ifdef HAVE_NGETTEXT
1919
function ngettext(string $singular, string $plural, int $count): string {}
@@ -28,5 +28,5 @@ function dcngettext(string $domain, string $singular, string $plural, int $count
2828
#endif
2929

3030
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
31-
function bind_textdomain_codeset(string $domain, string $codeset): string|false {}
31+
function bind_textdomain_codeset(string $domain, ?string $codeset): string|false {}
3232
#endif

ext/gettext/gettext_arginfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 7d0fe93cb15576756edc5aad71deadae67046690 */
2+
* Stub hash: 3fbd90b87dfcbc5a1a0a2aea8d0cc45516e221ce */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_textdomain, 0, 1, IS_STRING, 0)
55
ZEND_ARG_TYPE_INFO(0, domain, IS_STRING, 1)
@@ -24,7 +24,7 @@ ZEND_END_ARG_INFO()
2424

2525
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_bindtextdomain, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
2626
ZEND_ARG_TYPE_INFO(0, domain, IS_STRING, 0)
27-
ZEND_ARG_TYPE_INFO(0, directory, IS_STRING, 0)
27+
ZEND_ARG_TYPE_INFO(0, directory, IS_STRING, 1)
2828
ZEND_END_ARG_INFO()
2929

3030
#if defined(HAVE_NGETTEXT)
@@ -57,7 +57,7 @@ ZEND_END_ARG_INFO()
5757
#if defined(HAVE_BIND_TEXTDOMAIN_CODESET)
5858
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_bind_textdomain_codeset, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
5959
ZEND_ARG_TYPE_INFO(0, domain, IS_STRING, 0)
60-
ZEND_ARG_TYPE_INFO(0, codeset, IS_STRING, 0)
60+
ZEND_ARG_TYPE_INFO(0, codeset, IS_STRING, 1)
6161
ZEND_END_ARG_INFO()
6262
#endif
6363

ext/gettext/tests/bug53251.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Bug #53251 (bindtextdomain with null dir doesn't return old value)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gettext')) die('skip gettext extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
var_dump(is_string(bindtextdomain('foo', null)));
10+
$dir = bindtextdomain('foo', '.');
11+
var_dump(bindtextdomain('foo', null) === $dir);
12+
13+
var_dump(bind_textdomain_codeset('foo', null));
14+
var_dump(bind_textdomain_codeset('foo', 'UTF-8'));
15+
var_dump(bind_textdomain_codeset('foo', null));
16+
17+
?>
18+
--EXPECT--
19+
bool(true)
20+
bool(true)
21+
bool(false)
22+
string(5) "UTF-8"
23+
string(5) "UTF-8"

0 commit comments

Comments
 (0)