Skip to content

Commit f60ffed

Browse files
committed
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
1 parent 160a4a6 commit f60ffed

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

ext/gettext/gettext.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ PHP_FUNCTION(bindtextdomain)
167167
char *domain;
168168
size_t domain_len;
169169
zend_string *dir = NULL;
170-
char *retval, dir_name[MAXPATHLEN];
170+
char *retval, dir_name[MAXPATHLEN], *btd_result;
171171

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

183183
if (dir == NULL) {
184-
RETURN_STRING(bindtextdomain(domain, NULL));
184+
btd_result = bindtextdomain(domain, NULL);
185+
if (btd_result == NULL) {
186+
/* POSIX-compliant implementations can return
187+
* NULL if an error occured. On musl you will
188+
* also get NULL if the domain is not yet
189+
* bound, because musl has no default directory
190+
* to return in that case. */
191+
RETURN_FALSE;
192+
}
193+
RETURN_STRING(btd_result);
185194
}
186195

187196
if (ZSTR_LEN(dir) != 0 && !zend_string_equals_literal(dir, "0")) {

0 commit comments

Comments
 (0)