Skip to content

Support musl gettext (libintl) functions in tests #17168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ php
/ext/*/configure.ac
/ext/*/run-tests.php

# Generated by ./configure if libc might be musl
/ext/gettext/tests/locale/en_US

# ------------------------------------------------------------------------------
# Generated by Windows build system
# ------------------------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions ext/gettext/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ if test "$PHP_GETTEXT" != "no"; then
AC_CHECK_LIB(c, bindtextdomain, [
GETTEXT_LIBS=
GETTEXT_CHECK_IN_LIB=c

dnl If libintl.h is provided by libc, it's possible that libc is musl.
dnl The gettext family of functions under musl ignores the codeset
dnl suffix on directories like "en_US.UTF-8"; instead they look only
dnl in "en_US". To accomodate that, we symlink some test data from one
dnl to the other.
AC_MSG_NOTICE([symlinking en_US.UTF-8 messages to en_US in case you are on musl])
_linkdest="${srcdir%/}"/ext/gettext/tests/locale/en_US
AS_IF([test ! -e "${_linkdest}"],[
ln -s en_US.UTF-8 "${_linkdest}"
])
],[
AC_MSG_ERROR(Unable to find required gettext library)
])
Expand Down
13 changes: 11 additions & 2 deletions ext/gettext/gettext.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ PHP_FUNCTION(bindtextdomain)
char *domain;
size_t domain_len;
zend_string *dir = NULL;
char *retval, dir_name[MAXPATHLEN];
char *retval, dir_name[MAXPATHLEN], *btd_result;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "sS!", &domain, &domain_len, &dir) == FAILURE) {
RETURN_THROWS();
Expand All @@ -181,7 +181,16 @@ PHP_FUNCTION(bindtextdomain)
}

if (dir == NULL) {
RETURN_STRING(bindtextdomain(domain, NULL));
btd_result = bindtextdomain(domain, NULL);
if (btd_result == NULL) {
/* POSIX-compliant implementations can return
* NULL if an error occured. On musl you will
* also get NULL if the domain is not yet
* bound, because musl has no default directory
* to return in that case. */
RETURN_FALSE;
}
RETURN_STRING(btd_result);
}

if (ZSTR_LEN(dir) != 0 && !zend_string_equals_literal(dir, "0")) {
Expand Down
28 changes: 19 additions & 9 deletions ext/gettext/tests/bug53251.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@ if (getenv('SKIP_REPEAT')) die('skip gettext leaks global state across requests'
?>
--FILE--
<?php
var_dump(is_string(bindtextdomain('foo', null)));
$results[] = is_string(bindtextdomain('foo', null));
$dir = bindtextdomain('foo', '.');
var_dump(bindtextdomain('foo', null) === $dir);
$results[] = bindtextdomain('foo', null) === $dir;
$results[] = bind_textdomain_codeset('foo', null);
$results[] = bind_textdomain_codeset('foo', 'UTF-8');
$results[] = bind_textdomain_codeset('foo', null);

var_dump(bind_textdomain_codeset('foo', null));
var_dump(bind_textdomain_codeset('foo', 'UTF-8'));
var_dump(bind_textdomain_codeset('foo', null));
$expected = [true, true, false, "UTF-8", "UTF-8"];

// musl's bindtextdomain() has no default directory to return when
// "foo" is unbound, so in the first call, you will get false instead
// of a string.
//
// bind_textdomain_codeset() always returns false on musl
// because musl only supports UTF-8. For more information:
//
// * https://github.com/php/doc-en/issues/4311,
// * https://github.com/php/php-src/issues/17163
//
$expected_musl = [false, true, false, false, false];

var_dump($results === $expected || $results === $expected_musl);
?>
--EXPECT--
bool(true)
bool(true)
bool(false)
string(5) "UTF-8"
string(5) "UTF-8"
12 changes: 10 additions & 2 deletions ext/gettext/tests/gettext_bind_textdomain_codeset-retval.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ gettext
--FILE--
<?php
var_dump(bind_textdomain_codeset(false,false));
var_dump(bind_textdomain_codeset('messages', "UTF-8"));

// bind_textdomain_codeset() always returns false on musl
// because musl only supports UTF-8. For more information:
//
// * https://github.com/php/doc-en/issues/4311,
// * https://github.com/php/php-src/issues/17163
//
$result = bind_textdomain_codeset('messages', "UTF-8");
var_dump($result === false || $result === "UTF-8");

echo "Done\n";
?>
--EXPECT--
bool(false)
string(5) "UTF-8"
bool(true)
Done
--CREDITS--
Florian Holzhauer [email protected]
Expand Down
Loading