1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

s4:lib: Fix a possible fd leak in gp_get_file()

Found by covscan.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=13567

Pair-Programmed-With: Justin Stephenson <jstephen@redhat.com>
Signed-off-by: Andreas Schneider <asn@samba.org>
Signed-off-by: Justin Stephenson <jstephen@redhat.com>
Reviewed-by: Jeremy Allison <jra@samba.org>
This commit is contained in:
Andreas Schneider 2018-08-09 16:42:43 +02:00 committed by Jeremy Allison
parent b7b4fc51d0
commit d4fb124adf

View File

@ -215,6 +215,7 @@ static NTSTATUS gp_get_file (struct smbcli_tree *tree, const char *remote_src,
fh_local = open(local_dst, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fh_local == -1) {
DEBUG(0, ("Failed to open local file: %s\n", local_dst));
smbcli_close(tree, fh_remote);
return NT_STATUS_UNSUCCESSFUL;
}
@ -224,11 +225,17 @@ static NTSTATUS gp_get_file (struct smbcli_tree *tree, const char *remote_src,
NT_STATUS_IS_ERR(smbcli_getattrE(tree, fh_remote,
&attr, &file_size, NULL, NULL, NULL))) {
DEBUG(0, ("Failed to get remote file size: %s\n", smbcli_errstr(tree)));
smbcli_close(tree, fh_remote);
close(fh_local);
return NT_STATUS_UNSUCCESSFUL;
}
buf = talloc_zero_array(tree, uint8_t, buf_size);
NT_STATUS_HAVE_NO_MEMORY(buf);
if (buf == NULL) {
smbcli_close(tree, fh_remote);
close(fh_local);
return NT_STATUS_NO_MEMORY;
}
/* Copy the contents of the file */
while (1) {
@ -240,27 +247,28 @@ static NTSTATUS gp_get_file (struct smbcli_tree *tree, const char *remote_src,
if (write(fh_local, buf, n) != n) {
DEBUG(0, ("Short write while copying file.\n"));
smbcli_close(tree, fh_remote);
close(fh_local);
talloc_free(buf);
return NT_STATUS_UNSUCCESSFUL;
}
nread += n;
}
/* Bytes read should match the file size, or the copy was incomplete */
if (nread != file_size) {
DEBUG(0, ("Remote/local file size mismatch after copying file: "
"%s (remote %zu, local %zu).\n",
remote_src, file_size, nread));
close(fh_local);
talloc_free(buf);
return NT_STATUS_UNSUCCESSFUL;
}
/* Close the files */
smbcli_close(tree, fh_remote);
close(fh_local);
talloc_free(buf);
/* Bytes read should match the file size, or the copy was incomplete */
if (nread != file_size) {
DEBUG(0, ("Remote/local file size mismatch after copying file: "
"%s (remote %zu, local %zu).\n",
remote_src, file_size, nread));
return NT_STATUS_UNSUCCESSFUL;
}
return NT_STATUS_OK;
}