From 2d2934241c56970594517ba3e5dba0025b9d5283 Mon Sep 17 00:00:00 2001 From: matt335672 <30179339+matt335672@users.noreply.github.com> Date: Sat, 1 Feb 2025 17:27:20 +0000 Subject: [PATCH] Coverity CID 468110/468131 file descriptors cannot be zero when not in use. This commit enforces that for the test TCP proxy. --- tools/devel/tcp_proxy/main.c | 55 +++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/tools/devel/tcp_proxy/main.c b/tools/devel/tcp_proxy/main.c index 137a64ee..350037b5 100644 --- a/tools/devel/tcp_proxy/main.c +++ b/tools/devel/tcp_proxy/main.c @@ -71,25 +71,28 @@ g_tcp_socket_ok(int sck) 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) { @@ -135,7 +138,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) { @@ -144,13 +147,18 @@ 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)) { @@ -280,9 +288,18 @@ main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump) } } - 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;