Skip to content

Commit b341036

Browse files
committed
ext/gettext: updating apis accepting domain behavior.
to be more in line with the proper usage ; normally domain should not be empty strings. Close GH-13691
1 parent c2d20f4 commit b341036

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

NEWS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ PHP NEWS
5252
. Removed the deprecated inet_ntoa call support. (David Carlier)
5353

5454
- Gettext:
55-
. bind_textdomain_codeset now throws an exception on empty domain.
56-
(David Carlier)
55+
. bind_textdomain_codeset, textdomain and d(*)gettext functions
56+
now throw an exception on empty domain. (David Carlier)
5757

5858
- Hash:
5959
. Changed return type of hash_update() to true. (nielsdos)

UPGRADING

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ PHP 8.4 UPGRADE NOTES
323323
Previously, the return type was bool but only true could be returned in practice.
324324

325325
- Gettext:
326-
. bind_textdomain_codeset now throws an exception if the domain's argument is empty.
326+
. bind_textdomain_codeset, textdomain and d(*)gettext functions now throw an exception
327+
if the domain argument is empty.
327328

328329
- Hash:
329330
. Changed the return type of hash_update() to true. It was already the case that only

ext/gettext/gettext.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ ZEND_GET_MODULE(php_gettext)
5454
if (UNEXPECTED(domain_len > PHP_GETTEXT_MAX_DOMAIN_LENGTH)) { \
5555
zend_argument_value_error(_arg_num, "is too long"); \
5656
RETURN_THROWS(); \
57+
} else if (domain_len == 0) { \
58+
zend_argument_value_error(_arg_num, "cannot be empty"); \
59+
RETURN_THROWS(); \
5760
}
5861

5962
#define PHP_GETTEXT_LENGTH_CHECK(_arg_num, check_len) \
@@ -85,8 +88,11 @@ PHP_FUNCTION(textdomain)
8588
RETURN_THROWS();
8689
}
8790

88-
if (domain != NULL && ZSTR_LEN(domain) != 0 && !zend_string_equals_literal(domain, "0")) {
91+
if (domain != NULL) {
8992
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
93+
}
94+
95+
if (domain != NULL && !zend_string_equals_literal(domain, "0")) {
9096
domain_name = ZSTR_VAL(domain);
9197
}
9298

@@ -179,11 +185,6 @@ PHP_FUNCTION(bindtextdomain)
179185

180186
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, domain_len)
181187

182-
if (!domain_len) {
183-
zend_argument_value_error(1, "cannot be empty");
184-
RETURN_THROWS();
185-
}
186-
187188
if (dir == NULL) {
188189
RETURN_STRING(bindtextdomain(domain, NULL));
189190
}
@@ -292,11 +293,6 @@ PHP_FUNCTION(bind_textdomain_codeset)
292293

293294
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, domain_len)
294295

295-
if (!domain_len) {
296-
zend_argument_value_error(1, "cannot be empty");
297-
RETURN_THROWS();
298-
}
299-
300296
retval = bind_textdomain_codeset(domain, codeset);
301297

302298
if (!retval) {

ext/gettext/tests/dcngettext.phpt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,18 @@ var_dump(dcngettext(1,1,1,1,1));
1313
var_dump(dcngettext("test","test","test",1,1));
1414
var_dump(dcngettext("test","test","test",0,1));
1515
var_dump(dcngettext("test","test","test",-1,-1));
16-
var_dump(dcngettext("","","",1,1));
17-
var_dump(dcngettext("","","",0,1));
16+
17+
try {
18+
dcngettext("","","",1,1);
19+
} catch (\ValueError $e) {
20+
echo $e->getMessage() . PHP_EOL;
21+
}
22+
23+
try {
24+
dcngettext("","","",0,1);
25+
} catch (\ValueError $e) {
26+
echo $e->getMessage() . PHP_EOL;
27+
}
1828

1929
echo "Done\n";
2030
?>
@@ -23,6 +33,6 @@ string(1) "1"
2333
string(4) "test"
2434
string(4) "test"
2535
string(4) "test"
26-
string(0) ""
27-
string(0) ""
36+
dcngettext(): Argument #1 ($domain) cannot be empty
37+
dcngettext(): Argument #1 ($domain) cannot be empty
2838
Done

ext/gettext/tests/gettext_textdomain-retval.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@ bindtextdomain ("messages", "./locale");
1818
echo textdomain('test'), "\n";
1919
echo textdomain(null), "\n";
2020
echo textdomain('foo'), "\n";
21+
22+
try {
23+
textdomain('');
24+
} catch (\ValueError $e) {
25+
echo $e->getMessage();
26+
}
2127
?>
2228
--EXPECT--
2329
test
2430
test
2531
foo
32+
textdomain(): Argument #1 ($domain) cannot be empty
2633
--CREDITS--
2734
Christian Weiske, cweiske@php.net
2835
PHP Testfest Berlin 2009-05-09

0 commit comments

Comments
 (0)