From 7195c04d986ecd26c25c13e3c180790a2e85a723 Mon Sep 17 00:00:00 2001 From: Richard Yao Date: Mon, 12 Sep 2022 15:34:10 -0400 Subject: [PATCH] Fix file descriptor handling in zdb_copy_object() Coverity found a file descriptor leak. Eyeballing it showed that we had no handling for the `open()` call failing either. We can address both of these at once. Reviewed-by: Brian Behlendorf Reviewed-by: Neal Gompa Signed-off-by: Richard Yao Closes #13862 --- cmd/zdb/zdb.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/zdb/zdb.c b/cmd/zdb/zdb.c index 5389520e803..0fc4f0d0d1b 100644 --- a/cmd/zdb/zdb.c +++ b/cmd/zdb/zdb.c @@ -4737,6 +4737,8 @@ zdb_copy_object(objset_t *os, uint64_t srcobj, char *destfile) } int fd = open(destfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd == -1) + return (errno); /* * We cap the size at 1 mebibyte here to prevent * allocation failures and nigh-infinite printing if the @@ -4746,6 +4748,7 @@ zdb_copy_object(objset_t *os, uint64_t srcobj, char *destfile) offset = 0; char *buf = kmem_alloc(oursize, KM_NOSLEEP); if (buf == NULL) { + (void) close(fd); return (ENOMEM); } @@ -4755,6 +4758,7 @@ zdb_copy_object(objset_t *os, uint64_t srcobj, char *destfile) if (err != 0) { (void) printf("got error %u from dmu_read\n", err); kmem_free(buf, oursize); + (void) close(fd); return (err); } if (dump_opt['v'] > 3) { -- 2.45.0