Skip to content

Commit 83b700e

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 php#13696
1 parent fa8c6a5 commit 83b700e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

ext/gettext/gettext.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#ifdef HAVE_LIBINTL
2424

2525
#include <stdio.h>
26+
#include <stdlib.h> /* secure_getenv() */
2627
#include <locale.h>
2728
#include "ext/standard/info.h"
2829
#include "php_gettext.h"
@@ -180,7 +181,7 @@ PHP_FUNCTION(dcgettext)
180181
PHP_FUNCTION(bindtextdomain)
181182
{
182183
zend_string *domain, *dir = NULL;
183-
char *retval, dir_name[MAXPATHLEN];
184+
char *retval, dir_name[MAXPATHLEN], *btd_result;
184185

185186
ZEND_PARSE_PARAMETERS_START(1, 2)
186187
Z_PARAM_STR(domain)
@@ -191,7 +192,16 @@ PHP_FUNCTION(bindtextdomain)
191192
PHP_GETTEXT_DOMAIN_LENGTH_CHECK(1, ZSTR_LEN(domain))
192193

193194
if (dir == NULL) {
194-
RETURN_STRING(bindtextdomain(ZSTR_VAL(domain), NULL));
195+
btd_result = bindtextdomain(ZSTR_VAL(domain), NULL);
196+
if (btd_result == NULL) {
197+
/* POSIX-compliant implementations can return
198+
* NULL if an error occured. On musl you will
199+
* also get NULL if the domain is not yet
200+
* bound, because musl has no default directory
201+
* to return in that case. */
202+
RETURN_FALSE;
203+
}
204+
RETURN_STRING(btd_result);
195205
}
196206

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

0 commit comments

Comments
 (0)