Skip to content

Commit 1863014

Browse files
committed
Split off php_set_sock_blocking() and s.is_blocked to a separate function
This makes it harder to forget the check and keeps the variable and function call consistent. Closes phpGH-18604.
1 parent 31ebb42 commit 1863014

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ PHP NEWS
3636
failure). (nielsdos)
3737
. Fix leak of accel_globals->key. (nielsdos)
3838

39+
- OpenSSL:
40+
. Fix missing checks against php_set_blocking() in xp_ssl.c. (nielsdos)
41+
3942
- PDO_OCI:
4043
. Fixed bug GH-18494 (PDO OCI segfault in statement GC). (nielsdos)
4144

ext/openssl/xp_ssl.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,6 +1901,15 @@ static int php_openssl_capture_peer_certs(php_stream *stream,
19011901
}
19021902
/* }}} */
19031903

1904+
static zend_result php_openssl_set_blocking(php_openssl_netstream_data_t *sslsock, int block)
1905+
{
1906+
zend_result result = php_set_sock_blocking(sslsock->s.socket, block);
1907+
if (EXPECTED(SUCCESS == result)) {
1908+
sslsock->s.is_blocked = block;
1909+
}
1910+
return result;
1911+
}
1912+
19041913
static int php_openssl_enable_crypto(php_stream *stream,
19051914
php_openssl_netstream_data_t *sslsock,
19061915
php_stream_xport_crypto_param *cparam) /* {{{ */
@@ -1929,8 +1938,7 @@ static int php_openssl_enable_crypto(php_stream *stream,
19291938
sslsock->state_set = 1;
19301939
}
19311940

1932-
if (SUCCESS == php_set_sock_blocking(sslsock->s.socket, 0)) {
1933-
sslsock->s.is_blocked = 0;
1941+
if (SUCCESS == php_openssl_set_blocking(sslsock, 0)) {
19341942
/* The following mode are added only if we are able to change socket
19351943
* to non blocking mode which is also used for read and write */
19361944
SSL_set_mode(sslsock->ssl_handle, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
@@ -1983,8 +1991,8 @@ static int php_openssl_enable_crypto(php_stream *stream,
19831991
}
19841992
} while (retry);
19851993

1986-
if (sslsock->s.is_blocked != blocked && SUCCESS == php_set_sock_blocking(sslsock->s.socket, blocked)) {
1987-
sslsock->s.is_blocked = blocked;
1994+
if (sslsock->s.is_blocked != blocked) {
1995+
php_openssl_set_blocking(sslsock, blocked);
19881996
}
19891997

19901998
if (n == 1) {
@@ -2067,8 +2075,8 @@ static ssize_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, si
20672075
timeout = &sslsock->s.timeout;
20682076
}
20692077

2070-
if (timeout && php_set_sock_blocking(sslsock->s.socket, 0) == SUCCESS) {
2071-
sslsock->s.is_blocked = 0;
2078+
if (timeout) {
2079+
php_openssl_set_blocking(sslsock, 0);
20722080
}
20732081

20742082
if (!sslsock->s.is_blocked && timeout && (timeout->tv_sec > 0 || (timeout->tv_sec == 0 && timeout->tv_usec))) {
@@ -2092,9 +2100,7 @@ static ssize_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, si
20922100
if (php_openssl_compare_timeval(elapsed_time, *timeout) > 0 ) {
20932101
/* If the socket was originally blocking, set it back. */
20942102
if (began_blocked) {
2095-
if (php_set_sock_blocking(sslsock->s.socket, 1) == SUCCESS) {
2096-
sslsock->s.is_blocked = 1;
2097-
}
2103+
php_openssl_set_blocking(sslsock, 1);
20982104
}
20992105
sslsock->s.timeout_event = 1;
21002106
return -1;
@@ -2189,8 +2195,8 @@ static ssize_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, si
21892195
}
21902196

21912197
/* And if we were originally supposed to be blocking, let's reset the socket to that. */
2192-
if (began_blocked && php_set_sock_blocking(sslsock->s.socket, 1) == SUCCESS) {
2193-
sslsock->s.is_blocked = 1;
2198+
if (began_blocked) {
2199+
php_openssl_set_blocking(sslsock, 1);
21942200
}
21952201

21962202
return 0 > nr_bytes ? 0 : nr_bytes;
@@ -2496,8 +2502,8 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val
24962502
timeout = &tv;
24972503
}
24982504

2499-
if (timeout && php_set_sock_blocking(sslsock->s.socket, 0) == SUCCESS) {
2500-
sslsock->s.is_blocked = 0;
2505+
if (timeout) {
2506+
php_openssl_set_blocking(sslsock, 0);
25012507
}
25022508

25032509
if (!sslsock->s.is_blocked && timeout && (timeout->tv_sec > 0 || (timeout->tv_sec == 0 && timeout->tv_usec))) {
@@ -2521,9 +2527,7 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val
25212527
if (php_openssl_compare_timeval(elapsed_time, *timeout) > 0 ) {
25222528
/* If the socket was originally blocking, set it back. */
25232529
if (began_blocked) {
2524-
if (php_set_sock_blocking(sslsock->s.socket, 1) == SUCCESS) {
2525-
sslsock->s.is_blocked = 1;
2526-
}
2530+
php_openssl_set_blocking(sslsock, 1);
25272531
}
25282532
sslsock->s.timeout_event = 1;
25292533
return PHP_STREAM_OPTION_RETURN_ERR;
@@ -2574,9 +2578,7 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val
25742578

25752579
if (began_blocked && !sslsock->s.is_blocked) {
25762580
// Set it back to blocking
2577-
if (php_set_sock_blocking(sslsock->s.socket, 1) == SUCCESS) {
2578-
sslsock->s.is_blocked = 1;
2579-
}
2581+
php_openssl_set_blocking(sslsock, 1);
25802582
}
25812583
} else {
25822584
#ifdef PHP_WIN32

0 commit comments

Comments
 (0)