Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V0.10] Coverity fixes #3423

Draft
wants to merge 10 commits into
base: v0.10
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions common/thread_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <semaphore.h>
#endif
#include "arch.h"
#include "log.h"
#include "thread_calls.h"
#include "os_calls.h"

Expand Down Expand Up @@ -136,11 +137,17 @@ tc_mutex_lock(tbus mutex)
{
#if defined(_WIN32)
WaitForSingleObject((HANDLE)mutex, INFINITE);
return 0;
#else
pthread_mutex_lock((pthread_mutex_t *)mutex);
return 0;
if (mutex != 0)
{
pthread_mutex_lock((pthread_mutex_t *)mutex);
}
else
{
LOG(LOG_LEVEL_ERROR, "Attempt made to lock NULL mutex");
}
#endif
return 0;
}

/*****************************************************************************/
Expand Down
8 changes: 4 additions & 4 deletions fontutils/mkfv1.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,11 @@ convert_mono_glyph(FT_GlyphSlot ft_glyph, unsigned int ucode,
}
}
}
}

if (pa->sans10_compatibility != S10_OFF)
{
implement_sans10_compatibility(g, ucode);
if (pa->sans10_compatibility != S10_OFF)
{
implement_sans10_compatibility(g, ucode);
}
}

return g;
Expand Down
5 changes: 4 additions & 1 deletion sesman/chansrv/chansrv_fuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,10 @@ void xfuse_devredir_cb_rmdir_or_file(struct state_remove *fip,
{
case STATUS_SUCCESS:
case STATUS_NO_SUCH_FILE:
xfs_remove_entry(g_xfs, xinode->inum); /* Remove local copy */
if (xinode != NULL)
{
xfs_remove_entry(g_xfs, xinode->inum); /* Remove local copy */
}
fuse_reply_err(fip->req, 0);
break;

Expand Down
4 changes: 3 additions & 1 deletion sesman/chansrv/chansrv_xfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ xfs_create_xfs_fs(mode_t umask, uid_t uid, gid_t gid)
(xino1 = g_new0(XFS_INODE_ALL, 1)) == NULL ||
(xino2 = g_new0(XFS_INODE_ALL, 1)) == NULL ||
(xino1_name = strdup(".")) == NULL ||
(xino2_name = strdup(".delete-pending")) == NULL)
(xino2_name = strdup(".delete-pending")) == NULL ||
// This keeps Coverity happy (see below)
xfs->free_count < 3)
{
free(xino1);
free(xino2);
Expand Down
2 changes: 1 addition & 1 deletion sesman/chansrv/rail.c
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,7 @@ rail_create_window(Window window_id, Window owner_id)
char *title_bytes = 0;
int title_size = 0;
XWindowAttributes attributes;
int style;
tui32 style;
int ext_style;
int num_window_rects = 1;
int num_visibility_rects = 1;
Expand Down
1 change: 1 addition & 0 deletions sesman/chansrv/sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,7 @@ sound_sndsrvr_source_data_in(struct trans *trans)

if (i < g_bytes_in_stream)
{
//coverity[COPY_PASTE_ERROR:FALSE]
xstream_copyin(s, &g_stream_inp->data[g_stream_inp->size - g_bytes_in_stream], i);
bytes_read += i;
g_bytes_in_stream -= i;
Expand Down
6 changes: 5 additions & 1 deletion sesman/sesexec_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ sesexec_start(struct pre_session_item *psi)
* in the environment */
char buff[64];
g_snprintf(buff, sizeof(buff), "%d", sck[1]);
g_setenv("EICP_FD", buff, 1);
if (g_setenv("EICP_FD", buff, 1) < 0)
{
LOG(LOG_LEVEL_ERROR, "Can't set EICP_FD [%s]",
g_get_strerror());
}

/* [Development] Log all file descriptors not marked cloexec
* other than stdin, stdout, stderr, and the EICP fd in sck[1].
Expand Down
4 changes: 3 additions & 1 deletion sesman/tools/sesadmin.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ print_session(const struct scp_session_info *s)
{
char *username;
const char *uptr;
g_getuser_info_by_uid(s->uid, &username, NULL, NULL, NULL, NULL);
// Don't need to check error return explicitly - can check username
// against NULL
(void)g_getuser_info_by_uid(s->uid, &username, NULL, NULL, NULL, NULL);
uptr = (username == NULL) ? "<unknown>" : username;

printf("Session ID: %d\n", s->sid);
Expand Down
212 changes: 110 additions & 102 deletions tools/devel/tcp_proxy/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ int g_loc_io_count = 0; // bytes read from local port
int g_rem_io_count = 0; // bytes read from remote port

static int g_terminated = 0;
static char g_buf[1024 * 32];


typedef unsigned short tui16;

Expand All @@ -67,29 +65,92 @@ g_tcp_socket_ok(int sck)
return 0;
}

/*****************************************************************************/
static int
copy_sck_to_sck(int from_sck, int to_sck, int hexdump, int local)
{
char buff[1024 * 32];
int rv = -1;

int count = g_tcp_recv(from_sck, buff, sizeof(buff), 0);
if (count > 0 && count <= (int)sizeof(buff))
{
rv = count; // Assume we'll return the amount of data copied
if (local)
{
g_loc_io_count += count;
if (hexdump)
{
LOG_HEXDUMP(LOG_LEVEL_INFO, "from local:", buff, count);
}
}
else
{
g_rem_io_count += count;
if (hexdump)
{
LOG_HEXDUMP(LOG_LEVEL_INFO, "from remote:", buff, count);
}
}


LOG(LOG_LEVEL_DEBUG, "local_io_count: %d\tremote_io_count: %d",
g_loc_io_count, g_rem_io_count);

const char *p = buff;
while ((count > 0) && (!g_terminated))
{
int error = g_tcp_send(to_sck, p, count, 0);

if (error > 0 && error <= count)
{
// We wrote some data
count -= error;
p += error;
}
else if ((error == -1) && g_tcp_last_error_would_block(to_sck))
{
if (g_tcp_can_send(to_sck, 1000))
{
g_tcp_socket_ok(to_sck);
}
}
else
{
count = 0; // Terminate loop
rv = -1; // tell user
}
}
}

return rv;
}

/*****************************************************************************/
static int
main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump)
{
int lis_sck;
int acc_sck;
int con_sck;
int lis_sck = -1;
int acc_sck = -1;
int con_sck = -1;
int sel;
int count;
int sent;
int error;
int i;
int acc_to_con;
int con_to_acc;

acc_to_con = 0;
con_to_acc = 0;
acc_sck = 0;
int acc_to_con = 0;
int con_to_acc = 0;

/* create the listening socket and setup options */
lis_sck = g_tcp_socket();
g_tcp_set_non_blocking(lis_sck);
error = g_tcp_bind(lis_sck, local_port);
if (lis_sck < 0)
{
error = 1;
}
else
{
g_tcp_set_non_blocking(lis_sck);
error = g_tcp_bind(lis_sck, local_port);
}

if (error != 0)
{
Expand Down Expand Up @@ -135,7 +196,7 @@ main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump)

/* stop listening */
g_tcp_close(lis_sck);
lis_sck = 0;
lis_sck = -1;

if (error == 0)
{
Expand All @@ -144,22 +205,26 @@ main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump)
}

/* connect outgoing socket */
con_sck = 0;

if (error == 0)
{
con_sck = g_tcp_socket();
g_tcp_set_non_blocking(con_sck);
error = g_tcp_connect(con_sck, remote_ip, remote_port);
if (con_sck < 0)
{
error = 1;
}
else
{
g_tcp_set_non_blocking(con_sck);
error = g_tcp_connect(con_sck, remote_ip, remote_port);
}

if ((error == -1) && g_tcp_last_error_would_block(con_sck))
{
error = 0;
i = 0;

while (!(g_tcp_can_send(con_sck, 100) && g_tcp_socket_ok(con_sck))
&& (!g_terminated)
&& (i < 100))
while (!g_terminated && i < 100 &&
!g_tcp_can_send(con_sck, 100))
{
g_sleep(100);
i++;
Expand All @@ -170,8 +235,7 @@ main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump)
LOG(LOG_LEVEL_ERROR, "timeout connecting");
error = 1;
}

if (g_terminated)
else if (!g_tcp_socket_ok(con_sck))
{
error = 1;
}
Expand All @@ -183,7 +247,7 @@ main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump)
}
}

while ((!g_terminated) && (error == 0))
while (!g_terminated)
{
sel = g_tcp_select(con_sck, acc_sck);

Expand All @@ -196,93 +260,37 @@ main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump)
if (sel & 1)
{
// can read from con_sck w/o blocking
count = g_tcp_recv(con_sck, g_buf, 1024 * 16, 0);
error = count < 1;

if (error == 0)
count = copy_sck_to_sck(con_sck, acc_sck, hexdump, 1);
if (count < 0)
{
g_loc_io_count += count;
con_to_acc += count;

if (hexdump)
{
LOG_HEXDUMP(LOG_LEVEL_INFO, "from remove, the socket from connect", g_buf, count);
}

LOG(LOG_LEVEL_DEBUG, "local_io_count: %d\tremote_io_count: %d",
g_loc_io_count, g_rem_io_count);
sent = 0;

while ((sent < count) && (error == 0) && (!g_terminated))
{
i = g_tcp_send(acc_sck, g_buf + sent, count - sent, 0);

if ((i == -1) && g_tcp_last_error_would_block(acc_sck))
{
if (g_tcp_can_send(acc_sck, 1000))
{
g_tcp_socket_ok(acc_sck);
}
}
else if (i < 1)
{
error = 1;
}
else
{
sent += i;
}
}
break;
}
con_to_acc += count;
}

if (sel & 2)
{
// can read from acc_sck w/o blocking
count = g_tcp_recv(acc_sck, g_buf, 1024 * 16, 0);
error = count < 1;

if (error == 0)
count = copy_sck_to_sck(acc_sck, con_sck, hexdump, 0);
if (count < 0)
{
g_rem_io_count += count;
acc_to_con += count;

if (hexdump)
{
LOG_HEXDUMP(LOG_LEVEL_INFO, "from accepted, the socket from accept", g_buf, count);
}

LOG(LOG_LEVEL_DEBUG, "local_io_count: %d\tremote_io_count: %d",
g_loc_io_count, g_rem_io_count);
sent = 0;

while ((sent < count) && (error == 0) && (!g_terminated))
{
i = g_tcp_send(con_sck, g_buf + sent, count - sent, 0);

if ((i == -1) && g_tcp_last_error_would_block(con_sck))
{
if (g_tcp_can_send(con_sck, 1000))
{
g_tcp_socket_ok(con_sck);
}
}
else if (i < 1)
{
error = 1;
}
else
{
sent += i;
}
}
}
break;
};
acc_to_con += count;
}
}

g_tcp_close(lis_sck);
g_tcp_close(con_sck);
g_tcp_close(acc_sck);
if (lis_sck >= 0)
{
g_tcp_close(lis_sck);
}
if (con_sck >= 0)
{
g_tcp_close(con_sck);
}
if (acc_sck >= 0)
{
g_tcp_close(acc_sck);
}
LOG(LOG_LEVEL_INFO, "acc_to_con %d", acc_to_con);
LOG(LOG_LEVEL_INFO, "con_to_acc %d", con_to_acc);
return 0;
Expand Down
Loading
Loading