Skip to content

Commit 2677d43

Browse files
committed
Fix #75696: posix_getgrnam fails to print details of group
According to the POSIX specification of `getgrnam_r()` the result of `sysconf(_SC_GETGR_R_SIZE_MAX)` is an initial value suggested for the size of the buffer, and `ERANGE` signals that insufficient storage was supplied. So if we get `ERANGE`, we try again with a buffer twice as big, and so on, instead of failing.
1 parent 7fb7869 commit 2677d43

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2018, PHP 7.1.23
44

5+
- POSIX:
6+
Fixed bug #75696 (posix_getgrnam fails to print details of group). (cmb)
57

68
13 Sep 2018, PHP 7.1.22
79

ext/posix/posix.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,9 +1074,15 @@ PHP_FUNCTION(posix_getgrnam)
10741074
RETURN_FALSE;
10751075
}
10761076
buf = emalloc(buflen);
1077+
try_again:
10771078
g = &gbuf;
10781079

10791080
if (getgrnam_r(name, g, buf, buflen, &g) || g == NULL) {
1081+
if (errno == ERANGE) {
1082+
buflen *= 2;
1083+
buf = erealloc(buf, buflen);
1084+
goto try_again;
1085+
}
10801086
POSIX_G(last_error) = errno;
10811087
efree(buf);
10821088
RETURN_FALSE;

ext/posix/tests/bug75696.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #75696 (posix_getgrnam fails to print details of group)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('posix')) die('skip posix extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$gid = posix_getgid();
10+
$name = posix_getgrgid($gid)['name'];
11+
$info = posix_getgrnam($name);
12+
var_dump(is_array($info));
13+
?>
14+
===DONE===
15+
--EXPECT--
16+
bool(true)
17+
===DONE===

0 commit comments

Comments
 (0)