From 009bbc0546dd6007cfe987a8ab5f4be0b78c0204 Mon Sep 17 00:00:00 2001 From: Norbert Pocs Date: Fri, 19 May 2023 12:02:52 +0200 Subject: [PATCH] sftp.c: Avoid null dereference Issue found by covscan (gcc analyzer) Signed-off-by: Norbert Pocs Reviewed-by: Jakub Jelen --- src/sftp.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/sftp.c b/src/sftp.c index c23d9538..c98fe655 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -1755,6 +1755,10 @@ static int sftp_handle_close(sftp_session sftp, ssh_string handle) int sftp_close(sftp_file file){ int err = SSH_NO_ERROR; + if (file == NULL) { + return err; + } + SAFE_FREE(file->name); if (file->handle){ err = sftp_handle_close(file->sftp,file->handle); @@ -1917,7 +1921,7 @@ void sftp_file_set_blocking(sftp_file handle){ /* Read from a file using an opened sftp file handle. */ ssize_t sftp_read(sftp_file handle, void *buf, size_t count) { - sftp_session sftp = handle->sftp; + sftp_session sftp; sftp_message msg = NULL; sftp_status_message status; ssh_string datastring; @@ -1926,6 +1930,11 @@ ssize_t sftp_read(sftp_file handle, void *buf, size_t count) { uint32_t id; int rc; + if (handle == NULL) { + return -1; + } + sftp = handle->sftp; + if (handle->eof) { return 0; } @@ -2147,7 +2156,7 @@ int sftp_async_read(sftp_file file, void *data, uint32_t size, uint32_t id){ } ssize_t sftp_write(sftp_file file, const void *buf, size_t count) { - sftp_session sftp = file->sftp; + sftp_session sftp; sftp_message msg = NULL; sftp_status_message status; ssh_buffer buffer; @@ -2156,6 +2165,11 @@ ssize_t sftp_write(sftp_file file, const void *buf, size_t count) { size_t packetlen; int rc; + if (file == NULL) { + return -1; + } + sftp = file->sftp; + buffer = ssh_buffer_new(); if (buffer == NULL) { ssh_set_error_oom(sftp->session);