]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
test/ceph_test_librgw_file_nfsns: always free allocated memory 16227/head
authorKefu Chai <kchai@redhat.com>
Sat, 8 Jul 2017 12:25:12 +0000 (20:25 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 8 Jul 2017 12:38:59 +0000 (20:38 +0800)
use unique_ptr to manage allocated memory, so we can free it even if the
test fails. this silences the warnings like:

Potential leak of memory pointed to by 'buf2'

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/test/librgw_file_nfsns.cc

index 1731491ba720226d2bf28c684e508100b004797b..347aee4c6fb356cfb9fbb4bfa5660b25d3d95dd1 100644 (file)
@@ -24,6 +24,7 @@
 #include "rgw/rgw_lib_frontend.h" // direct requests
 
 #include "gtest/gtest.h"
+#include "common/backport14.h"
 #include "common/ceph_argparse.h"
 #include "common/debug.h"
 #include "global/global_init.h"
@@ -737,22 +738,21 @@ TEST(LibRGW, READF_DIRS1) {
       ofstream of;
       of.open(readf_out_name, ios::out|ios::app|ios::binary);
       int bufsz = 1024 * 1024 * sizeof(char);
-      char *buffer = (char*) malloc(bufsz);
+      auto buffer = ceph::make_unique<char[]>(bufsz);
 
       uint64_t offset = 0;
       uint64_t length = bufsz;
       for (int ix = 0; ix < 6; ++ix) {
        size_t nread = 0;
-       memset(buffer, 0, length); // XXX
-       rc = rgw_read(fs, fobj.fh, offset, length, &nread, buffer,
+       memset(buffer.get(), 0, length); // XXX
+       rc = rgw_read(fs, fobj.fh, offset, length, &nread, buffer.get(),
                      RGW_READ_FLAG_NONE);
        ASSERT_EQ(rc, 0);
        ASSERT_EQ(nread, length);
-       of.write(buffer, length);
+       of.write(buffer.get(), length);
        offset += nread;
       }
       of.close();
-      free(buffer);
       rgw_fh_rele(fs, fobj.fh, 0 /* flags */);
     }
   }