]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
test/libradosstriper: always free allocated memory
authorKefu Chai <kchai@redhat.com>
Sat, 8 Jul 2017 12:24:12 +0000 (20:24 +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/libradosstriper/striping.cc

index 2ea02572c3b447bc5f0b7e6fad15b385b242e106..37a892278899a5ceecc2e0867df9af5e0b911d16 100644 (file)
@@ -8,6 +8,7 @@
 #include "include/radosstriper/libradosstriper.h"
 #include "include/radosstriper/libradosstriper.hpp"
 #include "include/ceph_fs.h"
+#include "common/backport14.h"
 #include "test/librados/test.h"
 #include "test/libradosstriper/TestCase.h"
 
@@ -166,13 +167,13 @@ TEST_P(StriperTestRT, StripedRoundtrip) {
       << "_" << testData.size;
   std::string soid = oss.str();
   // writing striped data
-  char* buf1;
+  std::unique_ptr<char[]> buf1;
   bufferlist bl1;
   {
     SCOPED_TRACE("Writing initial object"); 
-    buf1 = (char*) calloc(1, testData.size);
+    buf1 = ceph::make_unique<char[]>(testData.size);
     for (unsigned int i = 0; i < testData.size; i++) buf1[i] = 13*((unsigned char)i);
-    bl1.append(buf1, testData.size);
+    bl1.append(buf1.get(), testData.size);
     ASSERT_EQ(0, striper.write(soid, bl1, testData.size, 0));
     // checking object state from Rados point of view
     ASSERT_NO_FATAL_FAILURE(checkObjectFromRados(soid, bl1, testData.stripe_unit,
@@ -180,15 +181,15 @@ TEST_P(StriperTestRT, StripedRoundtrip) {
                                                  testData.size));
   }
   // adding more data to object and checking again
-  char* buf2;
+  std::unique_ptr<char[]> buf2;
   bufferlist bl2;
   {
     SCOPED_TRACE("Testing append");
-    buf2 = (char*) calloc(1, testData.size);
+    buf2 = ceph::make_unique<char[]>(testData.size);
     for (unsigned int i = 0; i < testData.size; i++) buf2[i] = 17*((unsigned char)i);
-    bl2.append(buf2, testData.size);
+    bl2.append(buf2.get(), testData.size);
     ASSERT_EQ(0, striper.append(soid, bl2, testData.size));
-    bl1.append(buf2, testData.size);
+    bl1.append(buf2.get(), testData.size);
     ASSERT_NO_FATAL_FAILURE(checkObjectFromRados(soid, bl1, testData.stripe_unit,
                                                  testData.stripe_count, testData.object_size,
                                                  testData.size*2));
@@ -258,8 +259,6 @@ TEST_P(StriperTestRT, StripedRoundtrip) {
       free(oid);
     }
   }
-  free(buf1);
-  free(buf2);
 }
 
 const TestData simple_stripe_schemes[] = {