Skip to content

Commit

Permalink
fix coverity issues[2] (#3740)
Browse files Browse the repository at this point in the history
* fix coverity issues[2]

* fix coverity issues[2]
  • Loading branch information
matyhtf committed Oct 14, 2020
1 parent 05e15eb commit 77fd918
Show file tree
Hide file tree
Showing 21 changed files with 56 additions and 67 deletions.
6 changes: 3 additions & 3 deletions include/swoole_server.h
Expand Up @@ -535,9 +535,9 @@ class Server {
/**
* worker(worker and task_worker) process chroot / user / group
*/
std::string chroot;
std::string user;
std::string group;
std::string chroot_;
std::string user_;
std::string group_;

/**
* run as a daemon process
Expand Down
12 changes: 0 additions & 12 deletions include/swoole_socket.h
Expand Up @@ -84,18 +84,6 @@ struct Address {
socklen_t len;
enum swSocket_type type;

Address() {
addr = {};
len = 0;
type = SW_SOCK_TCP;
}

Address(enum swSocket_type _type, const std::string &_host, int _port) {
if (!assign(_type, _host, _port)) {
throw std::bad_exception();
}
}

bool assign(enum swSocket_type _type, const std::string &_host, int _port);
const char *get_ip() {
return get_addr();
Expand Down
2 changes: 1 addition & 1 deletion include/swoole_timer.h
Expand Up @@ -26,7 +26,7 @@
#define SW_TIMER_MIN_MS 1
#define SW_TIMER_MIN_SEC 0.001
#define SW_TIMER_MAX_MS LONG_MAX
#define SW_TIMER_MAX_SEC ((double) (LONG_MAX / 1000))
#define SW_TIMER_MAX_SEC ((double) LONG_MAX / 1000)

namespace swoole {

Expand Down
8 changes: 4 additions & 4 deletions php_swoole_mysql_proto.h
Expand Up @@ -620,8 +620,8 @@ class login_packet : public client_packet
public:
login_packet(
greeting_packet *greeting_packet,
const std::string user,
const std::string password,
const std::string &user,
const std::string &password,
std::string database,
char charset
);
Expand All @@ -638,7 +638,7 @@ class auth_switch_request_packet : public server_packet
class auth_switch_response_packet : public client_packet
{
public:
auth_switch_response_packet(auth_switch_request_packet *req, const std::string password);
auth_switch_response_packet(auth_switch_request_packet *req, const std::string &password);
};

class auth_signature_request_packet : public server_packet
Expand Down Expand Up @@ -673,7 +673,7 @@ class auth_signature_prepared_packet : public client_packet
class auth_signature_response_packet : public client_packet
{
public:
auth_signature_response_packet(raw_data_packet *raw_data_pakcet, const std::string password, const char *auth_plugin_data);
auth_signature_response_packet(raw_data_packet *raw_data_pakcet, const std::string &password, const char *auth_plugin_data);
};

class lcb_packet : public server_packet
Expand Down
2 changes: 1 addition & 1 deletion src/core/base.cc
Expand Up @@ -311,7 +311,7 @@ pid_t swoole_fork(int flags) {

void swoole_dump_ascii(const char *data, size_t size) {
for (size_t i = 0; i < size; i++) {
printf("%d ", (unsigned) data[i]);
printf("%u ", (unsigned) data[i]);
}
printf("\n");
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/heap.cc
Expand Up @@ -159,7 +159,7 @@ void *Heap::peek() {

void Heap::print() {
for (uint32_t i = 1; i < num; i++) {
printf("#%d\tpriority=%ld, data=%p\n", i, (long) nodes[i]->priority, nodes[i]->data);
printf("#%u\tpriority=%ld, data=%p\n", i, (long) nodes[i]->priority, nodes[i]->data);
}
}
} // namespace swoole
5 changes: 3 additions & 2 deletions src/network/dns.cc
Expand Up @@ -174,6 +174,7 @@ std::vector<std::string> swoole::coroutine::dns_lookup(const char *domain, doubl

uchar rdata[10][254];
uint32_t type[10];
sw_memset_zero(rdata, sizeof(rdata));

char *temp;
steps = 0;
Expand Down Expand Up @@ -308,8 +309,8 @@ static int domain_encode(const char *src, int n, char *dest) {
*/
static void domain_decode(char *str) {
size_t i, j;
for (i = 0; i < strlen((const char *) str); i++) {
unsigned int len = str[i];
for (i = 0; i < strlen(str); i++) {
uint32_t len = str[i];
for (j = 0; j < len; j++) {
str[i] = str[i + 1];
i++;
Expand Down
2 changes: 1 addition & 1 deletion src/network/socket.cc
Expand Up @@ -418,7 +418,7 @@ int Socket::handle_sendfile() {
if (ret <= 0) {
switch (catch_error(errno)) {
case SW_ERROR:
swSysWarn("sendfile(%s, %ld, %d) failed", task->filename, (long) task->offset, sendn);
swSysWarn("sendfile(%s, %ld, %zu) failed", task->filename, (long) task->offset, sendn);
buffer->pop();
return SW_OK;
case SW_CLOSE:
Expand Down
4 changes: 2 additions & 2 deletions src/os/msg_queue.cc
Expand Up @@ -79,7 +79,7 @@ ssize_t swMsgQueue_push(swMsgQueue *q, swQueue_data *in, size_t length) {
} else if (errno == EAGAIN) {
return -1;
} else {
swSysWarn("msgsnd(%d, %uz, %ld) failed", q->msg_id, length, in->mtype);
swSysWarn("msgsnd(%d, %lu, %ld) failed", q->msg_id, length, in->mtype);
return -1;
}
} else {
Expand Down Expand Up @@ -107,7 +107,7 @@ int swMsgQueue_set_capacity(swMsgQueue *q, size_t queue_bytes) {
}
__stat.msg_qbytes = queue_bytes;
if (msgctl(q->msg_id, IPC_SET, &__stat)) {
swSysWarn("msgctl(msqid=%d, IPC_SET, msg_qbytes=%uz) failed", q->msg_id, queue_bytes);
swSysWarn("msgctl(msqid=%d, IPC_SET, msg_qbytes=%lu) failed", q->msg_id, queue_bytes);
return -1;
}
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/server/manager.cc
Expand Up @@ -65,7 +65,7 @@ static void swManager_kill_timeout_process(Timer *timer, TimerNode *tnode) {
continue;
}
if (swoole_kill(pid, SIGKILL) < 0) {
swSysWarn("swKill(%d, SIGKILL) [%d] failed", pid, i);
swSysWarn("swKill(%d, SIGKILL) [%u] failed", pid, worker_id);
} else {
swoole_error_log(SW_LOG_WARNING,
SW_ERROR_SERVER_WORKER_EXIT_TIMEOUT,
Expand Down
4 changes: 2 additions & 2 deletions src/server/master.cc
Expand Up @@ -261,7 +261,7 @@ dtls::Session *Server::accept_dtls_connection(ListenPort *port, Address *sa) {

_cleanup:
if (conn) {
sw_memset_zero(conn, sizeof(*conn));
conn = {};
}
if (session) {
delete session;
Expand Down Expand Up @@ -1620,7 +1620,7 @@ Connection *Server::add_connection(ListenPort *ls, Socket *_socket, int server_f
unlock();

Connection *connection = &(connection_list[fd]);
sw_memset_zero(connection, sizeof(*connection));
*connection = {};
_socket->object = connection;
_socket->removed = 1;
_socket->buffer_size = ls->socket_buffer_size;
Expand Down
2 changes: 1 addition & 1 deletion src/server/process.cc
Expand Up @@ -106,7 +106,7 @@ static int swFactoryProcess_create_pipes(Factory *factory) {
int kernel_buffer_size = SW_UNIXSOCK_MAX_BUF_SIZE;

if (swPipeUnsock_create(&object->pipes[i], 1, SOCK_DGRAM) < 0) {
sw_free(object->pipes);
delete[] object->pipes;
object->pipes = nullptr;
return SW_ERR;
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/reactor_thread.cc
Expand Up @@ -289,7 +289,7 @@ int Server::close_connection(Reactor *reactor, Socket *socket) {
}
serv->unlock();

sw_memset_zero(conn, sizeof(Connection));
*conn = {};
return Reactor::_close(reactor, socket);
}

Expand Down
8 changes: 3 additions & 5 deletions src/server/static_handler.cc
Expand Up @@ -149,14 +149,12 @@ bool StaticHandler::hit() {

if (S_ISLNK(file_stat.st_mode)) {
char buf[PATH_MAX];
ssize_t byte = ::readlink(task.filename, buf, PATH_MAX - 1);
ssize_t byte = ::readlink(task.filename, buf, sizeof(buf) - 1);
if (byte <= 0) {
return false;
}

strcpy(task.filename, buf);
task.filename[byte] = 0;

buf[byte] = 0;
swoole_strlcpy(task.filename, buf, sizeof(task.filename));
goto check_stat;
}

Expand Down
40 changes: 21 additions & 19 deletions src/server/worker.cc
Expand Up @@ -234,12 +234,12 @@ int Server::accept_task(EventData *task) {
switch (task->info.type) {
case SW_SERVER_EVENT_RECV_DATA: {
Connection *conn = get_connection_verify(task->info.fd);
if (conn && task->info.len > 0) {
sw_atomic_fetch_sub(&conn->recv_queued_bytes, task->info.len);
swTraceLog(SW_TRACE_SERVER, "[Worker] len=%d, qb=%d\n", task->info.len, conn->recv_queued_bytes);
}
conn->last_dispatch_time = task->info.time;
if (!Worker_discard_data(this, conn, task)) {
if (task->info.len > 0) {
sw_atomic_fetch_sub(&conn->recv_queued_bytes, task->info.len);
swTraceLog(SW_TRACE_SERVER, "[Worker] len=%d, qb=%d\n", task->info.len, conn->recv_queued_bytes);
}
conn->last_dispatch_time = task->info.time;
Worker_do_task(this, worker, task, onReceive);
}
break;
Expand All @@ -264,10 +264,12 @@ int Server::accept_task(EventData *task) {
// SSL client certificate
if (task->info.len > 0) {
Connection *conn = get_connection_verify_no_ssl(task->info.fd);
char *cert_data = nullptr;
size_t length = get_packet(this, task, &cert_data);
conn->ssl_client_cert = new String(cert_data, length);
conn->ssl_client_cert_pid = SwooleG.pid;
if (conn) {
char *cert_data = nullptr;
size_t length = get_packet(this, task, &cert_data);
conn->ssl_client_cert = new String(cert_data, length);
conn->ssl_client_cert_pid = SwooleG.pid;
}
}
#endif
if (onConnect) {
Expand Down Expand Up @@ -328,30 +330,30 @@ void Server::worker_start_callback() {

if (is_root) {
// get group info
if (!group.empty()) {
_group = getgrnam(group.c_str());
if (!group_.empty()) {
_group = getgrnam(group_.c_str());
if (!_group) {
swWarn("get group [%s] info failed", group.c_str());
swWarn("get group [%s] info failed", group_.c_str());
}
}
// get user info
if (!user.empty()) {
_passwd = getpwnam(user.c_str());
if (!user_.empty()) {
_passwd = getpwnam(user_.c_str());
if (!_passwd) {
swWarn("get user [%s] info failed", user.c_str());
swWarn("get user [%s] info failed", user_.c_str());
}
}
// set process group
if (_group && setgid(_group->gr_gid) < 0) {
swSysWarn("setgid to [%s] failed", group.c_str());
swSysWarn("setgid to [%s] failed", group_.c_str());
}
// set process user
if (_passwd && setuid(_passwd->pw_uid) < 0) {
swSysWarn("setuid to [%s] failed", user.c_str());
swSysWarn("setuid to [%s] failed", user_.c_str());
}
// chroot
if (!chroot.empty() && ::chroot(chroot.c_str()) != 0) {
swSysWarn("chroot to [%s] failed", chroot.c_str());
if (!chroot_.empty() && ::chroot(chroot_.c_str()) != 0) {
swSysWarn("chroot to [%s] failed", chroot_.c_str());
}
}

Expand Down
2 changes: 1 addition & 1 deletion swoole_client_coro.cc
Expand Up @@ -285,7 +285,7 @@ bool php_swoole_client_set(Socket *cli, zval *zset) {
if (php_swoole_array_get_value(vht, "write_timeout", ztmp)) {
cli->set_timeout(zval_get_double(ztmp), Socket::TIMEOUT_WRITE);
}
std::string _bind_address("");
std::string _bind_address;
int _bind_port = 0;
if (php_swoole_array_get_value(vht, "bind_port", ztmp)) {
zend_long v = zval_get_long(ztmp);
Expand Down
2 changes: 1 addition & 1 deletion swoole_coroutine.cc
Expand Up @@ -1000,7 +1000,7 @@ PHP_METHOD(swoole_coroutine, list) {
sw_zend_call_method_with_1_params(return_value,
swoole_coroutine_iterator_ce,
&swoole_coroutine_iterator_ce->constructor,
(const char *) "__construct",
"__construct",
nullptr,
&zlist);
zval_ptr_dtor(&zlist);
Expand Down
3 changes: 1 addition & 2 deletions swoole_coroutine_system.cc
Expand Up @@ -163,6 +163,7 @@ static void co_socket_read(int fd, zend_long length, INTERNAL_FUNCTION_PARAMETER
}

static void co_socket_write(int fd, char *str, size_t l_str, INTERNAL_FUNCTION_PARAMETERS) {
TmpSocket *sock;
ssize_t ret = write(fd, str, l_str);
if (ret < 0) {
if (errno == EAGAIN) {
Expand All @@ -174,8 +175,6 @@ static void co_socket_write(int fd, char *str, size_t l_str, INTERNAL_FUNCTION_P
RETURN_LONG(ret);
}

TmpSocket *sock;

_yield:
sock = (TmpSocket *) ecalloc(1, sizeof(TmpSocket));

Expand Down
8 changes: 4 additions & 4 deletions swoole_mysql_proto.cc
Expand Up @@ -501,8 +501,8 @@ greeting_packet::greeting_packet(const char *data) : server_packet(data) {
};

login_packet::login_packet(greeting_packet *greeting_packet,
const std::string user,
const std::string password,
const std::string &user,
const std::string &password,
std::string database,
char charset) {
char *p = data.body;
Expand Down Expand Up @@ -569,7 +569,7 @@ auth_switch_request_packet::auth_switch_request_packet(const char *data) : serve
swTraceLog(SW_TRACE_MYSQL_CLIENT, "auth switch plugin name=%s", auth_method_name.c_str());
}

auth_switch_response_packet::auth_switch_response_packet(auth_switch_request_packet *req, const std::string password) {
auth_switch_response_packet::auth_switch_response_packet(auth_switch_request_packet *req, const std::string &password) {
// if auth switch is triggered, password can't be empty
// create auth switch response packet
set_header(mysql_auth_encrypt_dispatch(data.body, req->auth_method_name, req->auth_method_data, password.c_str()),
Expand All @@ -580,7 +580,7 @@ auth_switch_response_packet::auth_switch_response_packet(auth_switch_request_pac
// Caching sha2 authentication. Public key request and send encrypted password
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
auth_signature_response_packet::auth_signature_response_packet(raw_data_packet *raw_data_pakcet,
const std::string password,
const std::string &password,
const char *auth_plugin_data) {
#ifndef SW_MYSQL_RSA_SUPPORT
{
Expand Down
6 changes: 3 additions & 3 deletions swoole_server.cc
Expand Up @@ -2098,13 +2098,13 @@ static PHP_METHOD(swoole_server, set) {
php_swoole_set_global_option(vht);

if (php_swoole_array_get_value(vht, "chroot", ztmp)) {
serv->chroot = zend::String(ztmp).to_std_string();
serv->chroot_ = zend::String(ztmp).to_std_string();
}
if (php_swoole_array_get_value(vht, "user", ztmp)) {
serv->user = zend::String(ztmp).to_std_string();
serv->user_ = zend::String(ztmp).to_std_string();
}
if (php_swoole_array_get_value(vht, "group", ztmp)) {
serv->group = zend::String(ztmp).to_std_string();
serv->group_ = zend::String(ztmp).to_std_string();
}
if (php_swoole_array_get_value(vht, "daemonize", ztmp)) {
serv->daemonize = zval_is_true(ztmp);
Expand Down
1 change: 1 addition & 0 deletions thirdparty/multipart_parser.c
Expand Up @@ -18,6 +18,7 @@ static void multipart_log(const char * format, ...)
fprintf(stderr, "[HTTP_MULTIPART_PARSER] %s:%d: ", __FILE__, __LINE__);
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
va_end(args);
#endif
}

Expand Down

0 comments on commit 77fd918

Please sign in to comment.