--- php-7.4.29/ext/posix/posix.c	2022-04-12 10:55:39.000000000 +0000
+++ php-7.4.29/ext/posix/posix.c	2022-05-27 16:10:31.374379930 +0000
@@ -1094,8 +1094,11 @@ PHP_FUNCTION(posix_getgrnam)
 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
 
 #if defined(ZTS) && defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
+	errno = 0;
 	buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
-	if (buflen < 1) {
+	if (buflen == -1 && errno == 0) {
+		buflen = 1024;
+	} else if (buflen < 1) {
 		RETURN_FALSE;
 	}
 	buf = emalloc(buflen);
@@ -1151,8 +1154,11 @@ PHP_FUNCTION(posix_getgrgid)
 
 #if defined(ZTS) && defined(HAVE_GETGRGID_R) && defined(_SC_GETGR_R_SIZE_MAX)
 
+	errno = 0;
 	grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
-	if (grbuflen < 1) {
+	if (grbuflen == -1 && errno == 0) {
+		grbuflen = 1024;
+	} else if (grbuflen < 1) {
 		RETURN_FALSE;
 	}
 
@@ -1226,8 +1232,11 @@ PHP_FUNCTION(posix_getpwnam)
 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
 
 #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R)
+	errno = 0;
 	buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
-	if (buflen < 1) {
+	if (buflen == -1 && errno == 0) {
+		buflen = 1024;
+	} else if (buflen < 1) {
 		RETURN_FALSE;
 	}
 	buf = emalloc(buflen);
@@ -1282,8 +1291,11 @@ PHP_FUNCTION(posix_getpwuid)
 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
 
 #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWUID_R)
+	errno = 0;
 	pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
-	if (pwbuflen < 1) {
+	if (pwbuflen == -1 && errno == 0) {
+		pwbuflen = 1024;
+	} else if (pwbuflen < 1) {
 		RETURN_FALSE;
 	}
 	pwbuf = emalloc(pwbuflen);
--- php-7.4.29/ext/standard/filestat.c	2022-04-12 10:55:45.000000000 +0000
+++ php-7.4.29/ext/standard/filestat.c	2022-05-27 16:12:23.176939839 +0000
@@ -310,15 +310,25 @@ PHPAPI int php_get_gid_by_name(const cha
 #if defined(ZTS) && defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
 		struct group gr;
 		struct group *retgrptr;
+		errno = 0;
 		long grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
 		char *grbuf;
 
-		if (grbuflen < 1) {
+		if (grbuflen == -1 && errno == 0) {
+			grbuflen = 1024;
+		} else if (grbuflen < 1) {
 			return FAILURE;
 		}
 
 		grbuf = emalloc(grbuflen);
-		if (getgrnam_r(name, &gr, grbuf, grbuflen, &retgrptr) != 0 || retgrptr == NULL) {
+try_again:
+		retgrptr = &gr;
+		if (getgrnam_r(name, &gr, grbuf, grbuflen, &retgrptr) || retgrptr == NULL) {
+			if (errno == ERANGE) {
+				grbuflen *= 2;
+				grbuf = erealloc(grbuf, grbuflen);
+				goto try_again;
+			}
 			efree(grbuf);
 			return FAILURE;
 		}
@@ -446,15 +456,25 @@ PHPAPI uid_t php_get_uid_by_name(const c
 #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R)
 		struct passwd pw;
 		struct passwd *retpwptr = NULL;
+		errno = 0;
 		long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
 		char *pwbuf;
 
-		if (pwbuflen < 1) {
+		if (pwbuflen == -1 && errno == 0) {
+			pwbuflen = 1024;
+		} else if (pwbuflen < 1) {
 			return FAILURE;
 		}
 
 		pwbuf = emalloc(pwbuflen);
-		if (getpwnam_r(name, &pw, pwbuf, pwbuflen, &retpwptr) != 0 || retpwptr == NULL) {
+try_again:
+		retpwptr = &pw;
+		if (getpwnam_r(name, &pw, pwbuf, pwbuflen, &retpwptr) || retpwptr == NULL) {
+			if (errno == ERANGE) {
+				pwbuflen *= 2;
+				pwbuf = erealloc(pwbuf, pwbuflen);
+				goto try_again;
+			}
 			efree(pwbuf);
 			return FAILURE;
 		}
--- php-7.4.29/main/fopen_wrappers.c	2022-04-12 10:55:38.000000000 +0000
+++ php-7.4.29/main/fopen_wrappers.c	2022-05-27 16:13:13.063261295 +0000
@@ -375,10 +375,13 @@ PHPAPI int php_fopen_primary_script(zend
 			struct passwd *pw;
 #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
 			struct passwd pwstruc;
+			errno = 0;
 			long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
 			char *pwbuf;
 
-			if (pwbuflen < 1) {
+			if (pwbuflen == -1 && errno == 0) {
+				pwbuflen = 1024;
+			} else if (pwbuflen < 1) {
 				return FAILURE;
 			}
 
@@ -391,7 +394,14 @@ PHPAPI int php_fopen_primary_script(zend
 			memcpy(user, path_info + 2, length);
 			user[length] = '\0';
 #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
-			if (getpwnam_r(user, &pwstruc, pwbuf, pwbuflen, &pw)) {
+try_again:
+			pw = &pwstruc;
+			if (getpwnam_r(user, pw, pwbuf, pwbuflen, &pw) || pw == NULL) {
+				if (errno == ERANGE) {
+					pwbuflen *= 2;
+					pwbuf = erealloc(pwbuf, pwbuflen);
+					goto try_again;
+				}
 				efree(pwbuf);
 				return FAILURE;
 			}
--- php-7.4.29/main/main.c	2022-04-12 10:55:38.000000000 +0000
+++ php-7.4.29/main/main.c	2022-05-27 16:14:26.862749793 +0000
@@ -1534,23 +1534,27 @@ PHPAPI char *php_get_current_user(void)
 		struct passwd *pwd;
 #if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
 		struct passwd _pw;
-		struct passwd *retpwptr = NULL;
+		errno = 0;
 		int pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
 		char *pwbuf;
 
-		if (pwbuflen < 1) {
+		if (pwbuflen == -1 && errno == 0) {
+			pwbuflen = 1024;
+		} else if (pwbuflen < 1) {
 			return "";
 		}
 		pwbuf = emalloc(pwbuflen);
-		if (getpwuid_r(pstat->st_uid, &_pw, pwbuf, pwbuflen, &retpwptr) != 0) {
-			efree(pwbuf);
-			return "";
-		}
-		if (retpwptr == NULL) {
+try_again:
+		pwd = &_pw;
+		if (getpwuid_r(pstat->st_uid, pwd, pwbuf, pwbuflen, &pwd) || pwd == NULL) {
+			if (errno == ERANGE) {
+				pwbuflen *= 2;
+				pwbuf = erealloc(pwbuf, pwbuflen);
+				goto try_again;
+			}
 			efree(pwbuf);
 			return "";
 		}
-		pwd = &_pw;
 #else
 		if ((pwd=getpwuid(pstat->st_uid))==NULL) {
 			return "";