From 63f97a3d0301005568cf235eccb54610704666f6 Mon Sep 17 00:00:00 2001 From: Norbert Pocs Date: Wed, 4 Aug 2021 09:22:08 +0200 Subject: [PATCH] Fix some compiler warnings Covscan analyzer was used Signed-off-by: Norbert Pocs Reviewed-by: Jakub Jelen Reviewed-by: Andreas Schneider --- examples/libssh_scp.c | 2 +- examples/sshd_direct-tcpip.c | 12 +++++++++++- src/getpass.c | 12 ++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/examples/libssh_scp.c b/examples/libssh_scp.c index b81059b4..6fdf8a4f 100644 --- a/examples/libssh_scp.c +++ b/examples/libssh_scp.c @@ -233,7 +233,7 @@ static int open_location(struct location *loc, int flag) { loc->file = fopen(loc->path, flag == READ ? "r":"w"); if (!loc->file) { if (errno == EISDIR) { - if (chdir(loc->path)) { + if (loc->path != NULL && chdir(loc->path)) { fprintf(stderr, "Error changing directory to %s: %s\n", loc->path, strerror(errno)); diff --git a/examples/sshd_direct-tcpip.c b/examples/sshd_direct-tcpip.c index 589c0889..606f999f 100644 --- a/examples/sshd_direct-tcpip.c +++ b/examples/sshd_direct-tcpip.c @@ -92,7 +92,11 @@ cleanup_push(struct cleanup_node_struct** head_ref, // Allocate memory for node struct cleanup_node_struct *new_node = malloc(sizeof *new_node); - new_node->next = (*head_ref); + if (head_ref != NULL) { + new_node->next = *head_ref; + } else { + new_node->next = NULL; + } // Copy new_data new_node->data = new_data; @@ -518,6 +522,12 @@ message_callback(UNUSED_PARAM(ssh_session session), pFd = malloc(sizeof *pFd); cb_chan = malloc(sizeof *cb_chan); event_fd_data = malloc(sizeof *event_fd_data); + if (pFd == NULL || cb_chan == NULL || event_fd_data == NULL) { + SAFE_FREE(pFd); + SAFE_FREE(cb_chan); + SAFE_FREE(event_fd_data); + return 1; + } (*pFd) = socket_fd; event_fd_data->channel = channel; diff --git a/src/getpass.c b/src/getpass.c index 99627665..c00d0f54 100644 --- a/src/getpass.c +++ b/src/getpass.c @@ -255,7 +255,11 @@ int ssh_getpass(const char *prompt, /* disable nonblocking I/O */ if (fd & O_NDELAY) { - fcntl(0, F_SETFL, fd & ~O_NDELAY); + ok = fcntl(0, F_SETFL, fd & ~O_NDELAY); + if (ok < 0) { + perror("fcntl"); + return -1; + } } ok = ssh_gets(prompt, buf, len, verify); @@ -267,7 +271,11 @@ int ssh_getpass(const char *prompt, /* close fd */ if (fd & O_NDELAY) { - fcntl(0, F_SETFL, fd); + ok = fcntl(0, F_SETFL, fd); + if (ok < 0) { + perror("fcntl"); + return -1; + } } if (!ok) {