ext/gettext/gettext.c: handle NULLs from bindtextdomain()

According to POSIX, bindtextdomain() returns "the implementation-
defined default directory pathname used by the gettext family of
functions" when its second parameter is NULL (i.e. when you are
querying the directory corresponding to some text domain and that
directory has not yet been set). Its PHP counterpart is feeding
that result direclty to RETURN_STRING, but this can go wrong in
two ways:

  1. If an error occurs, even POSIX-compliant implementations
     may return NULL.

  2. At least one non-compliant implementation (musl) lacks
     a default directory and returns NULL whenever the domain
     has not yet been bound.

In either of those cases, PHP segfaults on the NULL string. In this
commit we check for the NULL, and RETURN_FALSE when it happens rather
than crashing.

This partially addresses GH #13696
This commit is contained in:
Michael Orlitzky 2024-12-19 09:11:10 -05:00 committed by Arnaud Le Blanc
parent 53b69ba8cf
commit 0221ceeccd
No known key found for this signature in database
GPG key ID: 0098C05DD15ABC13

View file

@ -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();
@ -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")) {