Skip to content

Commit b1119de

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: [ci skip] NEWS for GH-13922 Support sysconf(_SC_GETPW_R_SIZE_MAX) == -1 (#13922)
2 parents 04418ed + d7ef2c2 commit b1119de

File tree

3 files changed

+68
-19
lines changed

3 files changed

+68
-19
lines changed

ext/standard/filestat.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,25 @@ PHPAPI zend_result php_get_gid_by_name(const char *name, gid_t *gid)
279279
struct group *retgrptr;
280280
long grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
281281
char *grbuf;
282+
int err;
282283

283284
if (grbuflen < 1) {
284-
return FAILURE;
285+
grbuflen = 1024;
285286
}
286-
287+
# if ZEND_DEBUG
288+
/* Test retry logic */
289+
grbuflen = 1;
290+
# endif
287291
grbuf = emalloc(grbuflen);
288-
if (getgrnam_r(name, &gr, grbuf, grbuflen, &retgrptr) != 0 || retgrptr == NULL) {
292+
293+
try_again:
294+
err = getgrnam_r(name, &gr, grbuf, grbuflen, &retgrptr);
295+
if (err != 0 || retgrptr == NULL) {
296+
if (err == ERANGE) {
297+
grbuflen *= 2;
298+
grbuf = erealloc(grbuf, grbuflen);
299+
goto try_again;
300+
}
289301
efree(grbuf);
290302
return FAILURE;
291303
}
@@ -405,13 +417,25 @@ PHPAPI zend_result php_get_uid_by_name(const char *name, uid_t *uid)
405417
struct passwd *retpwptr = NULL;
406418
long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
407419
char *pwbuf;
420+
int err;
408421

409422
if (pwbuflen < 1) {
410-
return FAILURE;
423+
pwbuflen = 1024;
411424
}
412-
425+
# if ZEND_DEBUG
426+
/* Test retry logic */
427+
pwbuflen = 1;
428+
# endif
413429
pwbuf = emalloc(pwbuflen);
414-
if (getpwnam_r(name, &pw, pwbuf, pwbuflen, &retpwptr) != 0 || retpwptr == NULL) {
430+
431+
try_again:
432+
err = getpwnam_r(name, &pw, pwbuf, pwbuflen, &retpwptr);
433+
if (err != 0 || retpwptr == NULL) {
434+
if (err == EAGAIN) {
435+
pwbuflen *= 2;
436+
pwbuf = erealloc(pwbuf, pwbuflen);
437+
goto try_again;
438+
}
415439
efree(pwbuf);
416440
return FAILURE;
417441
}

main/fopen_wrappers.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -374,26 +374,38 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle)
374374

375375
if (s) { /* if there is no path name after the file, do not bother */
376376
char user[32]; /* to try open the directory */
377+
378+
length = s - (path_info + 2);
379+
if (length > sizeof(user) - 1) {
380+
length = sizeof(user) - 1;
381+
}
382+
memcpy(user, path_info + 2, length);
383+
user[length] = '\0';
384+
377385
struct passwd *pw;
378386
#if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
379387
struct passwd pwstruc;
380388
long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
381389
char *pwbuf;
390+
int err;
382391

383392
if (pwbuflen < 1) {
384-
return FAILURE;
393+
pwbuflen = 1024;
385394
}
386-
395+
# if ZEND_DEBUG
396+
/* Test retry logic */
397+
pwbuflen = 1;
398+
# endif
387399
pwbuf = emalloc(pwbuflen);
388-
#endif
389-
length = s - (path_info + 2);
390-
if (length > sizeof(user) - 1) {
391-
length = sizeof(user) - 1;
392-
}
393-
memcpy(user, path_info + 2, length);
394-
user[length] = '\0';
395-
#if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
396-
if (getpwnam_r(user, &pwstruc, pwbuf, pwbuflen, &pw)) {
400+
401+
try_again:
402+
err = getpwnam_r(user, &pwstruc, pwbuf, pwbuflen, &pw);
403+
if (err) {
404+
if (err == ERANGE) {
405+
pwbuflen *= 2;
406+
pwbuf = erealloc(pwbuf, pwbuflen);
407+
goto try_again;
408+
}
397409
efree(pwbuf);
398410
return FAILURE;
399411
}

main/main.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,12 +1471,25 @@ PHPAPI char *php_get_current_user(void)
14711471
struct passwd *retpwptr = NULL;
14721472
int pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
14731473
char *pwbuf;
1474+
int err;
14741475

14751476
if (pwbuflen < 1) {
1476-
return "";
1477+
pwbuflen = 1024;
14771478
}
1479+
# if ZEND_DEBUG
1480+
/* Test retry logic */
1481+
pwbuflen = 1;
1482+
# endif
14781483
pwbuf = emalloc(pwbuflen);
1479-
if (getpwuid_r(pstat->st_uid, &_pw, pwbuf, pwbuflen, &retpwptr) != 0) {
1484+
1485+
try_again:
1486+
err = getpwuid_r(pstat->st_uid, &_pw, pwbuf, pwbuflen, &retpwptr);
1487+
if (err != 0) {
1488+
if (err == ERANGE) {
1489+
pwbuflen *= 2;
1490+
pwbuf = erealloc(pwbuf, pwbuflen);
1491+
goto try_again;
1492+
}
14801493
efree(pwbuf);
14811494
return "";
14821495
}

0 commit comments

Comments
 (0)