]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rbd: fix leak of fd when importing an image from a file
authorJosh Durgin <josh.durgin@inktank.com>
Mon, 24 Sep 2012 22:27:55 +0000 (15:27 -0700)
committerSamuel Just <sam.just@inktank.com>
Tue, 25 Sep 2012 21:09:28 +0000 (14:09 -0700)
CID 719579: Resource leak (RESOURCE_LEAK)
At (7): Handle variable "fd" going out of scope leaks the handle.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
src/rbd.cc

index 5d1615581837a5f0a5315564f9b073120aa18492..5f4d85755f6de117bb1eec766533642ec4dca9bb 100644 (file)
@@ -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;
 }