[php-src] master: ext/gettext: updating apis accepting domain behavior.
| From: | David Carlier | Date: | Wed, 13 Mar 2024 20:51:19 +0000 |
| Subject: | [php-src] master: ext/gettext: updating apis accepting domain behavior. | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-127725@lists.php.net to get a copy of this message | ||
Author: David Carlier (devnexen)
Date: 2024-03-13T20:33:47Z
Commit: https://github.com/php/php-src/commit/b3410360ccd34ac2c354fd6fef228fb8833f0bad
Raw diff: https://github.com/php/php-src/commit/b3410360ccd34ac2c354fd6fef228fb8833f0bad.diff
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
Changed paths:
M NEWS
M UPGRADING
M ext/gettext/gettext.c
M ext/gettext/tests/dcngettext.phpt
M ext/gettext/tests/gettext_textdomain-retval.phpt
Diff:
diff --git a/NEWS b/NEWS
index cc6b7561ec4b7..bf8838e9ea4ad 100644
--- a/NEWS
+++ b/NEWS
@@ -52,8 +52,8 @@ PHP NEWS
. Removed the deprecated inet_ntoa call support. (David Carlier)
- Gettext:
- . bind_textdomain_codeset now throws an exception on empty domain.
- (David Carlier)
+ . bind_textdomain_codeset, textdomain and d(*)gettext functions
+ now throw an exception on empty domain. (David Carlier)
- Hash:
. Changed return type of hash_update() to true. (nielsdos)
diff --git a/UPGRADING b/UPGRADING
index 09b277339ed3c..69a7a2bbb06de 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -323,7 +323,8 @@ PHP 8.4 UPGRADE NOTES
Previously, the return type was bool but only true could be returned in practice.
- Gettext:
- . bind_textdomain_codeset now throws an exception if the domain's argument is empty.
+ . bind_textdomain_codeset, textdomain and d(*)gettext functions now throw an exception
+ if the domain argument is empty.
- Hash:
. Changed the return type of hash_update() to true. It was already the case that only
diff --git a/ext/gettext/gettext.c b/ext/gettext/gettext.c
index 7c9e8897e7b65..8518a80520b55 100644
--- a/ext/gettext/gettext.c
+++ b/ext/gettext/gettext.c
@@ -54,6 +54,9 @@ ZEND_GET_MODULE(php_gettext)
if (UNEXPECTED(domain_len > PHP_GETTEXT_MAX_DOMAIN_LENGTH)) { \
zend_argument_value_error(_arg_num, "is too long"); \
RETURN_THROWS(); \
+ } else if (domain_len == 0) { \
+ zend_argument_value_error(_arg_num, "cannot be empty"); \
+ RETURN_THROWS(); \
}
#define PHP_GETTEXT_LENGTH_CHECK(_arg_num, check_len) \
@@ -85,8 +88,11 @@ PHP_FUNCTION(textdomain)
RETURN_THROWS();
}
- if (domain != NULL && ZSTR_LEN(domain) != 0 && !zend_string_equals_literal(domain,
"0")) {
+ if (domain != NULL) {
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
+ }
+
+ if (domain != NULL && !zend_string_equals_literal(domain, "0")) {
domain_name = ZSTR_VAL(domain);
}
@@ -179,11 +185,6 @@ PHP_FUNCTION(bindtextdomain)
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, domain_len)
- if (!domain_len) {
- zend_argument_value_error(1, "cannot be empty");
- RETURN_THROWS();
- }
-
if (dir == NULL) {
RETURN_STRING(bindtextdomain(domain, NULL));
}
@@ -292,11 +293,6 @@ PHP_FUNCTION(bind_textdomain_codeset)
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, domain_len)
- if (!domain_len) {
- zend_argument_value_error(1, "cannot be empty");
- RETURN_THROWS();
- }
-
retval = bind_textdomain_codeset(domain, codeset);
if (!retval) {
diff --git a/ext/gettext/tests/dcngettext.phpt b/ext/gettext/tests/dcngettext.phpt
index 2f356b1cf0c6a..3b8123413e252 100644
--- a/ext/gettext/tests/dcngettext.phpt
+++ b/ext/gettext/tests/dcngettext.phpt
@@ -13,8 +13,18 @@ var_dump(dcngettext(1,1,1,1,1));
var_dump(dcngettext("test","test","test",1,1));
var_dump(dcngettext("test","test","test",0,1));
var_dump(dcngettext("test","test","test",-1,-1));
-var_dump(dcngettext("","","",1,1));
-var_dump(dcngettext("","","",0,1));
+
+try {
+ dcngettext("","","",1,1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ dcngettext("","","",0,1);
+} catch (\ValueError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
echo "Done\n";
?>
@@ -23,6 +33,6 @@ string(1) "1"
string(4) "test"
string(4) "test"
string(4) "test"
-string(0) ""
-string(0) ""
+dcngettext(): Argument #1 ($domain) cannot be empty
+dcngettext(): Argument #1 ($domain) cannot be empty
Done
diff --git a/ext/gettext/tests/gettext_textdomain-retval.phpt
b/ext/gettext/tests/gettext_textdomain-retval.phpt
index a7c6b0becfd52..172a0069afc7f 100644
--- a/ext/gettext/tests/gettext_textdomain-retval.phpt
+++ b/ext/gettext/tests/gettext_textdomain-retval.phpt
@@ -18,11 +18,18 @@ bindtextdomain ("messages", "./locale");
echo textdomain('test'), "\n";
echo textdomain(null), "\n";
echo textdomain('foo'), "\n";
+
+try {
+ textdomain('');
+} catch (\ValueError $e) {
+ echo $e->getMessage();
+}
?>
--EXPECT--
test
test
foo
+textdomain(): Argument #1 ($domain) cannot be empty
--CREDITS--
Christian Weiske, cweiske@php.net
PHP Testfest Berlin 2009-05-09