From 7f018719155dcd79ae10aaeebd3fe218ea773300 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 2 Mar 2024 11:04:22 +0000 Subject: [PATCH] ext/gettext: bind_textdomain_codeset should not accept empty domains. the man page specifies that for bind_textdomain_codeset, like bindtextdomain, the domain should not be an empty string. close GH-13571 --- NEWS | 4 ++++ UPGRADING | 3 +++ ext/gettext/gettext.c | 7 ++++++- .../gettext_bind_textdomain_codeset-retval.phpt | 15 +++++++++++++-- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index e23921cba6f..5153e955322 100644 --- a/NEWS +++ b/NEWS @@ -47,6 +47,10 @@ PHP NEWS - FTP: . Removed the deprecated inet_ntoa call support. (David Carlier) +- Gettext: + . bind_textdomain_codeset now throws an exception on empty domain. + (David Carlier) + - IMAP: . Moved to PECL. (Derick Rethans) diff --git a/UPGRADING b/UPGRADING index 6121904badd..78956ae29a6 100644 --- a/UPGRADING +++ b/UPGRADING @@ -324,6 +324,9 @@ PHP 8.4 UPGRADE NOTES . DOMDocument::registerNodeClass() now has a tentative return type of true. 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. + - Intl: . IntlDateFormatter::__construct() throws a ValueError if the locale is invalid. . NumberFormatter::__construct() throws a ValueError if the locale is invalid. diff --git a/ext/gettext/gettext.c b/ext/gettext/gettext.c index 05f41552c63..b11f49d3e51 100644 --- a/ext/gettext/gettext.c +++ b/ext/gettext/gettext.c @@ -171,7 +171,7 @@ PHP_FUNCTION(bindtextdomain) PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, domain_len) - if (domain[0] == '\0') { + if (!domain_len) { zend_argument_value_error(1, "cannot be empty"); RETURN_THROWS(); } @@ -283,6 +283,11 @@ 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/gettext_bind_textdomain_codeset-retval.phpt b/ext/gettext/tests/gettext_bind_textdomain_codeset-retval.phpt index 47c821648fc..e6df576edf0 100644 --- a/ext/gettext/tests/gettext_bind_textdomain_codeset-retval.phpt +++ b/ext/gettext/tests/gettext_bind_textdomain_codeset-retval.phpt @@ -4,13 +4,24 @@ test if bind_textdomain_codeset() returns correct value gettext --FILE-- getMessage() . PHP_EOL; + } + + try { + bind_textdomain_codeset("", "UTF-8"); + } catch (ValueError $e) { + echo $e->getMessage() . PHP_EOL; + } var_dump(bind_textdomain_codeset('messages', "UTF-8")); echo "Done\n"; ?> --EXPECT-- -bool(false) +bind_textdomain_codeset(): Argument #1 ($domain) cannot be empty +bind_textdomain_codeset(): Argument #1 ($domain) cannot be empty string(5) "UTF-8" Done --CREDITS--