From: Josh Durgin Date: Mon, 24 Sep 2012 22:27:55 +0000 (-0700) Subject: rbd: fix leak of fd when importing an image from a file X-Git-Tag: v0.53~41^2~28 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=2d44a1a3e1f0cd442c3a7dc533fe59181c72b490;p=ceph.git rbd: fix leak of fd when importing an image from a file CID 719579: Resource leak (RESOURCE_LEAK) At (7): Handle variable "fd" going out of scope leaks the handle. Signed-off-by: Josh Durgin --- diff --git a/src/rbd.cc b/src/rbd.cc index 5d161558183..5f4d85755f6 100644 --- a/src/rbd.cc +++ b/src/rbd.cc @@ -589,6 +589,7 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx, if (r < 0) { r = -errno; cerr << "stat error " << path << std::endl; + close(fd); return r; } if (stat_buf.st_size) @@ -598,6 +599,7 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx, r = get_block_device_size(fd, &size); if (r < 0) { cerr << "unable to get size of file/block device: " << cpp_strerror(r) << std::endl; + close(fd); return r; } } @@ -607,12 +609,14 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx, r = do_create(rbd, io_ctx, imgname, size, order, format, features); if (r < 0) { cerr << "image creation failed" << std::endl; + close(fd); return r; } librbd::Image image; r = rbd.open(io_ctx, image, imgname); if (r < 0) { cerr << "failed to open image" << std::endl; + close(fd); return r; } fsync(fd); /* flush it first, otherwise extents information might not have been flushed yet */ @@ -626,6 +630,7 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx, fiemap = (struct fiemap *)malloc(sizeof(struct fiemap) + sizeof(struct fiemap_extent)); if (!fiemap) { cerr << "Failed to allocate fiemap, not enough memory." << std::endl; + close(fd); return -ENOMEM; } fiemap->fm_start = 0; @@ -720,6 +725,7 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx, else pc.finish(); free(fiemap); + close(fd); return r; }