Skip to content

Commit

Permalink
Fix coverity issues (#5256)
Browse files Browse the repository at this point in the history
* zend_long

* fix res leak
  • Loading branch information
matyhtf authored Feb 18, 2024
1 parent 5044471 commit 4065b77
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
3 changes: 2 additions & 1 deletion ext-src/php_swoole_http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ static inline void http_server_add_server_array(HashTable *ht, zend_string *key,
zend_hash_add(ht, key, &tmp);
}

static inline void http_server_add_server_array(HashTable *ht, zend_string *key, int value) {
static inline void http_server_add_server_array(HashTable *ht, zend_string *key, zend_long value) {
zval tmp;
ZVAL_LONG(&tmp, value);
zend_hash_add(ht, key, &tmp);
}

static inline void http_server_add_server_array(HashTable *ht, zend_string *key, double value) {
zval tmp;
ZVAL_DOUBLE(&tmp, value);
Expand Down
25 changes: 14 additions & 11 deletions ext-src/swoole_event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,21 @@ static PHP_FUNCTION(swoole_event_add) {
}

auto readable_callback = php_swoole_zval_to_callable(zreadable_callback, "readable_callback");
if ((events & SW_EVENT_READ) && readable_callback == nullptr) {
php_swoole_fatal_error(
E_WARNING, "%s: unable to find readable callback of fd [%d]", ZSTR_VAL(swoole_event_ce->name), socket_fd);
RETURN_FALSE;
}

auto writable_callback = php_swoole_zval_to_callable(zwritable_callback, "writable_callback");
if ((events & SW_EVENT_WRITE) && writable_callback == nullptr) {
php_swoole_fatal_error(
E_WARNING, "%s: unable to find writable callback of fd [%d]", ZSTR_VAL(swoole_event_ce->name), socket_fd);
if (readable_callback) {
delete readable_callback;
}
RETURN_FALSE;
}

EventObject *peo = (EventObject *) ecalloc(1, sizeof(*peo));

Expand All @@ -443,17 +457,6 @@ static PHP_FUNCTION(swoole_event_add) {
socket->set_nonblock();
socket->object = peo;

if ((events & SW_EVENT_READ) && peo->readable_callback == nullptr) {
php_swoole_fatal_error(
E_WARNING, "%s: unable to find readable callback of fd [%d]", ZSTR_VAL(swoole_event_ce->name), socket_fd);
RETURN_FALSE;
}
if ((events & SW_EVENT_WRITE) && peo->writable_callback == nullptr) {
php_swoole_fatal_error(
E_WARNING, "%s: unable to find writable callback of fd [%d]", ZSTR_VAL(swoole_event_ce->name), socket_fd);
RETURN_FALSE;
}

if (swoole_event_add(socket, events) < 0) {
php_swoole_fatal_error(E_WARNING, "swoole_event_add failed");
socket->free();
Expand Down
2 changes: 1 addition & 1 deletion ext-src/swoole_http_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ static int http_request_on_headers_complete(swoole_http_parser *parser) {
ZSTR_LEN(zstr_path) = php_url_decode(ZSTR_VAL(zstr_path), ZSTR_LEN(zstr_path));
http_server_add_server_array(ht, SW_ZSTR_KNOWN(SW_ZEND_STR_PATH_INFO), zstr_path);

http_server_add_server_array(ht, SW_ZSTR_KNOWN(SW_ZEND_STR_REQUEST_TIME), (int) time(nullptr));
http_server_add_server_array(ht, SW_ZSTR_KNOWN(SW_ZEND_STR_REQUEST_TIME), (zend_long) time(nullptr));
http_server_add_server_array(ht, SW_ZSTR_KNOWN(SW_ZEND_STR_REQUEST_TIME_FLOAT), microtime());
http_server_add_server_array(
ht,
Expand Down
7 changes: 4 additions & 3 deletions ext-src/swoole_http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ int php_swoole_http_server_onReceive(Server *serv, RecvData *req) {
HashTable *ht = Z_ARR_P(zserver);

if (serv_sock) {
http_server_add_server_array(ht, SW_ZSTR_KNOWN(SW_ZEND_STR_SERVER_PORT), serv_sock->info.get_port());
http_server_add_server_array(
ht, SW_ZSTR_KNOWN(SW_ZEND_STR_SERVER_PORT), (zend_long) serv_sock->info.get_port());
}
http_server_add_server_array(ht, SW_ZSTR_KNOWN(SW_ZEND_STR_REMOTE_PORT), conn->info.get_port());
http_server_add_server_array(ht, SW_ZSTR_KNOWN(SW_ZEND_STR_REMOTE_PORT), (zend_long) conn->info.get_port());

if (conn->info.type == SW_SOCK_TCP && IN_IS_ADDR_LOOPBACK(&conn->info.addr.inet_v4.sin_addr)) {
http_server_add_server_array(
Expand All @@ -137,7 +138,7 @@ int php_swoole_http_server_onReceive(Server *serv, RecvData *req) {
}
}

http_server_add_server_array(ht, SW_ZSTR_KNOWN(SW_ZEND_STR_MASTER_TIME), (int) conn->last_recv_time);
http_server_add_server_array(ht, SW_ZSTR_KNOWN(SW_ZEND_STR_MASTER_TIME), (zend_long) conn->last_recv_time);
} while (0);

if (swoole_isset_hook((enum swGlobalHookType) PHP_SWOOLE_HOOK_BEFORE_REQUEST)) {
Expand Down
10 changes: 6 additions & 4 deletions ext-src/swoole_http_server_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ static PHP_METHOD(swoole_http_server_coro, onAccept) {
zend::Variable remote_addr = zend::Variable(sock->get_ip());

while (true) {
_recv_request: {
_recv_request : {
sock->get_socket()->recv_wait = 1;
ssize_t retval = sock->recv(buffer->str + buffer->length, buffer->size - buffer->length);
if (sw_unlikely(retval <= 0)) {
Expand Down Expand Up @@ -659,8 +659,9 @@ static PHP_METHOD(swoole_http_server_coro, onAccept) {

zval *zserver = ctx->request.zserver;
http_server_add_server_array(
Z_ARRVAL_P(zserver), SW_ZSTR_KNOWN(SW_ZEND_STR_SERVER_PORT), hs->socket->get_bind_port());
http_server_add_server_array(Z_ARRVAL_P(zserver), SW_ZSTR_KNOWN(SW_ZEND_STR_REMOTE_PORT), sock->get_port());
Z_ARRVAL_P(zserver), SW_ZSTR_KNOWN(SW_ZEND_STR_SERVER_PORT), (zend_long) hs->socket->get_bind_port());
http_server_add_server_array(
Z_ARRVAL_P(zserver), SW_ZSTR_KNOWN(SW_ZEND_STR_REMOTE_PORT), (zend_long) sock->get_port());
http_server_add_server_array(Z_ARRVAL_P(zserver), SW_ZSTR_KNOWN(SW_ZEND_STR_REMOTE_ADDR), remote_addr.ptr());
remote_addr.add_ref();

Expand Down Expand Up @@ -713,7 +714,8 @@ static PHP_METHOD(swoole_http_server_coro, shutdown) {
sock->cancel(SW_EVENT_READ);
zend_hash_index_del(Z_ARRVAL_P(&hs->zclients), index);
}
} ZEND_HASH_FOREACH_END();
}
ZEND_HASH_FOREACH_END();
}

static void http2_server_onRequest(Http2Session *session, Http2Stream *stream) {
Expand Down

0 comments on commit 4065b77

Please sign in to comment.