Skip to content

Commit 152dfa8

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
2 parents 9bd9e3a + b7fdfb7 commit 152dfa8

File tree

7 files changed

+114
-27
lines changed

7 files changed

+114
-27
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ PHP NEWS
5757
. Fixed bug GH-19801 (leaks in var_dump() and debug_zval_dump()).
5858
(alexandre-daubois)
5959

60+
- Streams:
61+
. Fixed bug GH-19248 (Use strerror_r instead of strerror in main).
62+
(Jakub Zelenka)
63+
6064
- XMLReader:
6165
. Fixed bug GH-20009 (XMLReader leak on RelaxNG schema failure). (nielsdos)
6266

main/network.c

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,28 @@ PHPAPI socklen_t php_sockaddr_size(php_sockaddr_storage *addr)
10361036
}
10371037
/* }}} */
10381038

1039+
#ifdef PHP_WIN32
1040+
char *php_socket_strerror_s(long err, char *buf, size_t bufsize)
1041+
{
1042+
if (buf == NULL) {
1043+
char ebuf[1024];
1044+
errno_t res = strerror_s(ebuf, sizeof(ebuf), err);
1045+
if (res == 0) {
1046+
buf = estrdup(ebuf);
1047+
} else {
1048+
buf = estrdup("Unknown error");
1049+
}
1050+
} else {
1051+
errno_t res = strerror_s(buf, bufsize, err);
1052+
if (res != 0) {
1053+
strncpy(buf, "Unknown error", bufsize);
1054+
buf[bufsize?(bufsize-1):0] = 0;
1055+
}
1056+
}
1057+
return buf;
1058+
}
1059+
#endif
1060+
10391061
/* Given a socket error code, if buf == NULL:
10401062
* emallocs storage for the error message and returns
10411063
* else
@@ -1045,16 +1067,40 @@ PHPAPI socklen_t php_sockaddr_size(php_sockaddr_storage *addr)
10451067
PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize)
10461068
{
10471069
#ifndef PHP_WIN32
1048-
char *errstr;
1049-
1050-
errstr = strerror(err);
1070+
# ifdef HAVE_STRERROR_R
1071+
if (buf == NULL) {
1072+
char ebuf[1024];
1073+
# ifdef STRERROR_R_CHAR_P
1074+
char *errstr = strerror_r(err, ebuf, sizeof(ebuf));
1075+
buf = estrdup(errstr);
1076+
# else
1077+
errno_t res = strerror_r(err, ebuf, sizeof(ebuf));
1078+
if (res == 0) {
1079+
buf = estrdup(ebuf);
1080+
} else {
1081+
buf = estrdup("Unknown error");
1082+
}
1083+
# endif
1084+
} else {
1085+
# ifdef STRERROR_R_CHAR_P
1086+
buf = strerror_r(err, buf, bufsize);
1087+
# else
1088+
errno_t res = strerror_r(err, buf, bufsize);
1089+
if (res != 0) {
1090+
strncpy(buf, "Unknown error", bufsize);
1091+
buf[bufsize?(bufsize-1):0] = 0;
1092+
}
1093+
# endif
1094+
}
1095+
# else
1096+
char *errstr = strerror(err);
10511097
if (buf == NULL) {
10521098
buf = estrdup(errstr);
10531099
} else {
10541100
strncpy(buf, errstr, bufsize);
10551101
buf[bufsize?(bufsize-1):0] = 0;
10561102
}
1057-
return buf;
1103+
# endif
10581104
#else
10591105
char *sysbuf = php_win32_error_to_msg(err);
10601106
if (!sysbuf[0]) {
@@ -1069,19 +1115,31 @@ PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize)
10691115
}
10701116

10711117
php_win32_error_msg_free(sysbuf);
1072-
1073-
return buf;
10741118
#endif
1119+
return buf;
10751120
}
10761121
/* }}} */
10771122

10781123
/* {{{ php_socket_error_str */
10791124
PHPAPI zend_string *php_socket_error_str(long err)
10801125
{
10811126
#ifndef PHP_WIN32
1082-
char *errstr;
1083-
1084-
errstr = strerror(err);
1127+
# ifdef HAVE_STRERROR_R
1128+
char ebuf[1024];
1129+
# ifdef STRERROR_R_CHAR_P
1130+
char *errstr = strerror_r(err, ebuf, sizeof(ebuf));
1131+
# else
1132+
const char *errstr;
1133+
errno_t res = strerror_r(err, ebuf, sizeof(ebuf));
1134+
if (res == 0) {
1135+
errstr = ebuf;
1136+
} else {
1137+
errstr = "Unknown error";
1138+
}
1139+
# endif
1140+
# else
1141+
char *errstr = strerror(err);
1142+
# endif
10851143
return zend_string_init(errstr, strlen(errstr), 0);
10861144
#else
10871145
zend_string *ret;

main/php_network.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
* unless buf is not NULL.
6565
* Also works sensibly for win32 */
6666
BEGIN_EXTERN_C()
67+
#ifdef PHP_WIN32
68+
char *php_socket_strerror_s(long err, char *buf, size_t bufsize);
69+
#else
70+
#define php_socket_strerror_s php_socket_strerror
71+
#endif
6772
PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize);
6873
PHPAPI zend_string *php_socket_error_str(long err);
6974
END_EXTERN_C()

main/rfc1867.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,8 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler)
10481048
if (wlen == (size_t)-1) {
10491049
/* write failed */
10501050
#if DEBUG_FILE_UPLOAD
1051-
sapi_module.sapi_error(E_NOTICE, "write() failed - %s", strerror(errno));
1051+
char errstr[256];
1052+
sapi_module.sapi_error(E_NOTICE, "write() failed - %s", php_socket_strerror_s(errno, errstr, sizeof(errstr)));
10521053
#endif
10531054
cancel_upload = PHP_UPLOAD_ERROR_F;
10541055
} else if (wlen < blen) {

main/streams/plain_wrapper.c

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,9 @@ static ssize_t php_stdiop_write(php_stream *stream, const char *buf, size_t coun
368368
return bytes_written;
369369
}
370370
if (!(stream->flags & PHP_STREAM_FLAG_SUPPRESS_ERRORS)) {
371-
php_error_docref(NULL, E_NOTICE, "Write of %zu bytes failed with errno=%d %s", count, errno, strerror(errno));
371+
char errstr[256];
372+
php_error_docref(NULL, E_NOTICE, "Write of %zu bytes failed with errno=%d %s",
373+
count, errno, php_socket_strerror_s(errno, errstr, sizeof(errstr)));
372374
}
373375
}
374376
} else {
@@ -444,7 +446,9 @@ static ssize_t php_stdiop_read(php_stream *stream, char *buf, size_t count)
444446
/* TODO: Should this be treated as a proper error or not? */
445447
} else {
446448
if (!(stream->flags & PHP_STREAM_FLAG_SUPPRESS_ERRORS)) {
447-
php_error_docref(NULL, E_NOTICE, "Read of %zu bytes failed with errno=%d %s", count, errno, strerror(errno));
449+
char errstr[256];
450+
php_error_docref(NULL, E_NOTICE, "Read of %zu bytes failed with errno=%d %s",
451+
count, errno, php_socket_strerror_s(errno, errstr, sizeof(errstr)));
448452
}
449453

450454
/* TODO: Remove this special-case? */
@@ -1278,7 +1282,9 @@ static int php_plain_files_unlink(php_stream_wrapper *wrapper, const char *url,
12781282
ret = VCWD_UNLINK(url);
12791283
if (ret == -1) {
12801284
if (options & REPORT_ERRORS) {
1281-
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
1285+
char errstr[256];
1286+
php_error_docref1(NULL, url, E_WARNING, "%s",
1287+
php_socket_strerror_s(errno, errstr, sizeof(errstr)));
12821288
}
12831289
return 0;
12841290
}
@@ -1324,6 +1330,7 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_f
13241330

13251331
if (ret == -1) {
13261332
#ifndef PHP_WIN32
1333+
char errstr[256];
13271334
# ifdef EXDEV
13281335
if (errno == EXDEV) {
13291336
zend_stat_t sb;
@@ -1344,15 +1351,17 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_f
13441351
* access to the file in the meantime.
13451352
*/
13461353
if (VCWD_CHOWN(url_to, sb.st_uid, sb.st_gid)) {
1347-
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
1354+
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s",
1355+
php_socket_strerror_s(errno, errstr, sizeof(errstr)));
13481356
if (errno != EPERM) {
13491357
success = 0;
13501358
}
13511359
}
13521360

13531361
if (success) {
13541362
if (VCWD_CHMOD(url_to, sb.st_mode)) {
1355-
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
1363+
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s",
1364+
php_socket_strerror_s(errno, errstr, sizeof(errstr)));
13561365
if (errno != EPERM) {
13571366
success = 0;
13581367
}
@@ -1363,10 +1372,12 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_f
13631372
VCWD_UNLINK(url_from);
13641373
}
13651374
} else {
1366-
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
1375+
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s",
1376+
php_socket_strerror_s(errno, errstr, sizeof(errstr)));
13671377
}
13681378
} else {
1369-
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
1379+
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s",
1380+
php_socket_strerror_s(errno, errstr, sizeof(errstr)));
13701381
}
13711382
# if !defined(ZTS) && !defined(TSRM_WIN32)
13721383
umask(oldmask);
@@ -1379,7 +1390,8 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_f
13791390
#ifdef PHP_WIN32
13801391
php_win32_docref2_from_error(GetLastError(), url_from, url_to);
13811392
#else
1382-
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
1393+
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s",
1394+
php_socket_strerror_s(errno, errstr, sizeof(errstr)));
13831395
#endif
13841396
return 0;
13851397
}
@@ -1459,11 +1471,12 @@ static int php_plain_files_mkdir(php_stream_wrapper *wrapper, const char *dir, i
14591471
if (!p) {
14601472
p = buf;
14611473
}
1474+
char errstr[256];
14621475
while (true) {
14631476
int ret = VCWD_MKDIR(buf, (mode_t) mode);
14641477
if (ret < 0 && errno != EEXIST) {
14651478
if (options & REPORT_ERRORS) {
1466-
php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
1479+
php_error_docref(NULL, E_WARNING, "%s", php_socket_strerror_s(errno, errstr, sizeof(errstr)));
14671480
}
14681481
return 0;
14691482
}
@@ -1483,7 +1496,7 @@ static int php_plain_files_mkdir(php_stream_wrapper *wrapper, const char *dir, i
14831496
/* issue a warning to client when the last directory was created failed */
14841497
if (ret < 0) {
14851498
if (options & REPORT_ERRORS) {
1486-
php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
1499+
php_error_docref(NULL, E_WARNING, "%s", php_socket_strerror_s(errno, errstr, sizeof(errstr)));
14871500
}
14881501
return 0;
14891502
}
@@ -1502,15 +1515,16 @@ static int php_plain_files_rmdir(php_stream_wrapper *wrapper, const char *url, i
15021515
return 0;
15031516
}
15041517

1518+
char errstr[256];
15051519
#ifdef PHP_WIN32
15061520
if (!php_win32_check_trailing_space(url, strlen(url))) {
1507-
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(ENOENT));
1521+
php_error_docref1(NULL, url, E_WARNING, "%s", php_socket_strerror_s(ENOENT, errstr, sizeof(errstr)));
15081522
return 0;
15091523
}
15101524
#endif
15111525

15121526
if (VCWD_RMDIR(url) < 0) {
1513-
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(errno));
1527+
php_error_docref1(NULL, url, E_WARNING, "%s", php_socket_strerror_s(errno, errstr, sizeof(errstr)));
15141528
return 0;
15151529
}
15161530

@@ -1529,10 +1543,11 @@ static int php_plain_files_metadata(php_stream_wrapper *wrapper, const char *url
15291543
#endif
15301544
mode_t mode;
15311545
int ret = 0;
1546+
char errstr[256];
15321547

15331548
#ifdef PHP_WIN32
15341549
if (!php_win32_check_trailing_space(url, strlen(url))) {
1535-
php_error_docref1(NULL, url, E_WARNING, "%s", strerror(ENOENT));
1550+
php_error_docref1(NULL, url, E_WARNING, "%s", php_socket_strerror_s(ENOENT, errstr, sizeof(errstr)));
15361551
return 0;
15371552
}
15381553
#endif
@@ -1551,7 +1566,8 @@ static int php_plain_files_metadata(php_stream_wrapper *wrapper, const char *url
15511566
if (VCWD_ACCESS(url, F_OK) != 0) {
15521567
FILE *file = VCWD_FOPEN(url, "w");
15531568
if (file == NULL) {
1554-
php_error_docref1(NULL, url, E_WARNING, "Unable to create file %s because %s", url, strerror(errno));
1569+
php_error_docref1(NULL, url, E_WARNING, "Unable to create file %s because %s", url,
1570+
php_socket_strerror_s(errno, errstr, sizeof(errstr)));
15551571
return 0;
15561572
}
15571573
fclose(file);
@@ -1594,7 +1610,8 @@ static int php_plain_files_metadata(php_stream_wrapper *wrapper, const char *url
15941610
return 0;
15951611
}
15961612
if (ret == -1) {
1597-
php_error_docref1(NULL, url, E_WARNING, "Operation failed: %s", strerror(errno));
1613+
php_error_docref1(NULL, url, E_WARNING, "Operation failed: %s",
1614+
php_socket_strerror_s(errno, errstr, sizeof(errstr)));
15981615
return 0;
15991616
}
16001617
php_clear_stat_cache(0, NULL, 0);

main/streams/streams.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ static void php_stream_display_wrapper_errors(php_stream_wrapper *wrapper, const
203203
free_msg = 1;
204204
} else {
205205
if (wrapper == &php_plain_files_wrapper) {
206-
msg = strerror(errno); /* TODO: not ts on linux */
206+
char errstr[256];
207+
msg = php_socket_strerror_s(errno, errstr, sizeof(errstr));
207208
} else {
208209
msg = "operation failed";
209210
}

main/streams/xp_socket.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,9 +678,10 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t *
678678

679679
if (sock->socket == SOCK_ERR) {
680680
if (xparam->want_errortext) {
681+
char errstr[256];
681682
xparam->outputs.error_text = strpprintf(0, "Failed to create unix%s socket %s",
682683
stream->ops == &php_stream_unix_socket_ops ? "" : "datagram",
683-
strerror(errno));
684+
php_socket_strerror_s(errno, errstr, sizeof(errstr)));
684685
}
685686
return -1;
686687
}

0 commit comments

Comments
 (0)