Skip to content

Commit 1f165fd

Browse files
committed
Fix the crash caused by inconsistent return types
1 parent bbbde61 commit 1f165fd

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

ext-src/php_swoole_cxx.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,11 @@ static inline void array_set(zval *arg, const char *key, size_t l_key, const cha
711711
add_assoc_zval_ex(arg, key, l_key, &ztmp);
712712
}
713713

714+
static inline void array_set(zval *arg, zend_ulong index, zval *zvalue) {
715+
Z_TRY_ADDREF_P(zvalue);
716+
zend_hash_index_add(Z_ARRVAL_P(arg), index, zvalue);
717+
}
718+
714719
static inline void array_add(zval *arg, zval *zvalue) {
715720
Z_TRY_ADDREF_P(zvalue);
716721
add_next_index_zval(arg, zvalue);

ext-src/swoole_http_server_coro.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ static PHP_METHOD(swoole_http_server_coro, onAccept) {
544544

545545
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 1, 1)
546546
Z_PARAM_OBJECT(zconn)
547-
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
547+
ZEND_PARSE_PARAMETERS_END_EX(RETURN_NULL());
548548

549549
Coroutine *co = Coroutine::get_current();
550550
Socket *sock = php_swoole_get_socket(zconn);
@@ -557,11 +557,10 @@ static PHP_METHOD(swoole_http_server_coro, onAccept) {
557557

558558
#ifdef SW_USE_OPENSSL
559559
if (sock->ssl_is_enable() && !sock->ssl_handshake()) {
560-
RETURN_FALSE;
560+
RETURN_NULL();
561561
}
562562
#endif
563-
Z_TRY_ADDREF_P(zconn);
564-
zend_hash_index_add(Z_ARRVAL_P(&hs->zclients), co->get_cid(), zconn);
563+
zend::array_set(&hs->zclients, co->get_cid(), zconn);
565564
zend::Variable remote_addr = zend::Variable(sock->get_addr());
566565

567566
while (true) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
swoole_http_server_coro: crash - bad return type
3+
--SKIPIF--
4+
<?php require __DIR__ . '/../include/skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
require __DIR__ . '/../include/bootstrap.php';
8+
9+
$port = get_one_free_port();
10+
11+
Co::set(['log_file' => '/dev/null']);
12+
13+
go(function () use ($port) {
14+
$server = new Co\Http\Server("127.0.0.1", $port, true);
15+
$server->set([
16+
'open_tcp_nodelay' => true,
17+
'ssl_cert_file' => SSL_FILE_DIR.'/server.crt',
18+
'ssl_key_file' => SSL_FILE_DIR.'/server.key',
19+
]);
20+
$server->handle('/', function ($request, $response) {
21+
$response->end("<h1>Index</h1>");
22+
});
23+
$server->handle('/stop', function ($request, $response) use ($server) {
24+
$response->end("<h1>Stop</h1>");
25+
$server->shutdown();
26+
});
27+
$server->start();
28+
});
29+
30+
go(function () use ($port) {
31+
try {
32+
echo httpGetBody("http://127.0.0.1:{$port}/") . PHP_EOL;
33+
} catch (Throwable $e) {
34+
Assert::contains($e->getMessage(), 'Connection reset by peer');
35+
echo "Bad Client\n";
36+
}
37+
echo httpGetBody("https://127.0.0.1:{$port}/") . PHP_EOL;
38+
echo httpGetBody("https://127.0.0.1:{$port}/stop?hello=1") . PHP_EOL;
39+
});
40+
Swoole\Event::wait();
41+
42+
?>
43+
--EXPECT--
44+
Bad Client
45+
<h1>Index</h1>
46+
<h1>Stop</h1>

0 commit comments

Comments
 (0)