From f5994494aca9e592047474efc05deb31b6789d0d Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Fri, 22 Jan 2021 13:08:51 +0100 Subject: [PATCH] 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. --- ext/gettext/gettext.c | 12 ++++++++---- ext/gettext/tests/bug53251.phpt | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 ext/gettext/tests/bug53251.phpt diff --git a/ext/gettext/gettext.c b/ext/gettext/gettext.c index 003787f5c08b4..f68057925995e 100644 --- a/ext/gettext/gettext.c +++ b/ext/gettext/gettext.c @@ -255,11 +255,11 @@ PHP_NAMED_FUNCTION(zif_dcgettext) Bind to the text domain domain_name, looking for translations in dir. Returns the current domain */ PHP_NAMED_FUNCTION(zif_bindtextdomain) { - char *domain, *dir; + char *domain, *dir = NULL; size_t domain_len, dir_len; char *retval, dir_name[MAXPATHLEN]; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &domain, &domain_len, &dir, &dir_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss!", &domain, &domain_len, &dir, &dir_len) == FAILURE) { return; } @@ -270,6 +270,10 @@ PHP_NAMED_FUNCTION(zif_bindtextdomain) RETURN_FALSE; } + if (dir == NULL) { + RETURN_STRING(bindtextdomain(domain, NULL)); + } + if (dir[0] != '\0' && strcmp(dir, "0")) { if (!VCWD_REALPATH(dir, dir_name)) { RETURN_FALSE; @@ -368,10 +372,10 @@ PHP_NAMED_FUNCTION(zif_dcngettext) Specify the character encoding in which the messages from the DOMAIN message catalog will be returned. */ PHP_NAMED_FUNCTION(zif_bind_textdomain_codeset) { - char *domain, *codeset, *retval = NULL; + char *domain, *codeset = NULL, *retval = NULL; size_t domain_len, codeset_len; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &domain, &domain_len, &codeset, &codeset_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss!", &domain, &domain_len, &codeset, &codeset_len) == FAILURE) { return; } diff --git a/ext/gettext/tests/bug53251.phpt b/ext/gettext/tests/bug53251.phpt new file mode 100644 index 0000000000000..b3e2e9e66c00f --- /dev/null +++ b/ext/gettext/tests/bug53251.phpt @@ -0,0 +1,23 @@ +--TEST-- +Bug #53251 (bindtextdomain with null dir doesn't return old value) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) +bool(true) +bool(false) +string(5) "UTF-8" +string(5) "UTF-8"