]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
erasure-code: use new/delete to alloc/free coefficients array
authorKefu Chai <tchaikov@gmail.com>
Fri, 29 Mar 2024 23:38:54 +0000 (07:38 +0800)
committerKefu Chai <tchaikov@gmail.com>
Sat, 30 Mar 2024 01:46:46 +0000 (09:46 +0800)
before this change, we allocate coefficients table with
`malloc()` in `ErasureCodeIsaDefault::prepare()`, but free them using
`delete`. this is identified by LeakSanitizer, and it reports

```
==3135332==ERROR: AddressSanitizer: alloc-dealloc-mismatch (malloc vs operator delete) on 0x60700002a870
    #0 0x5627c6ef721d in operator delete(void*) (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_erasure_code_plugin_isa+0x1ca21d) (BuildId: 7922906370e5183d67f55211a868c0b0e22b4a2c)
    #1 0x7fbbe38e858f in ErasureCodeIsaTableCache::~ErasureCodeIsaTableCache() /home/jenkins-build/build/workspace/ceph-pull-requests/src/erasure-code/isa/ErasureCodeIsaTableCache.cc:65:13
    #2 0x7fbbe390be40 in ErasureCodePluginIsa::~ErasureCodePluginIsa() /home/jenkins-build/build/workspace/ceph-pull-requests/src/erasure-code/isa/ErasureCodePluginIsa.h:24:7
    #3 0x7fbbe390be68 in ErasureCodePluginIsa::~ErasureCodePluginIsa() /home/jenkins-build/build/workspace/ceph-pull-requests/src/erasure-code/isa/ErasureCodePluginIsa.h:24:7
    #4 0x5627c7063b52 in ceph::ErasureCodePluginRegistry::~ErasureCodePluginRegistry() /home/jenkins-build/build/workspace/ceph-pull-requests/src/erasure-code/ErasureCodePlugin.cc:49:5
    #5 0x7fbbeccb6494 in __run_exit_handlers stdlib/./stdlib/exit.c:113:8
    #6 0x7fbbeccb660f in exit stdlib/./stdlib/exit.c:143:3
    #7 0x7fbbecc9ad96 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:74:3
    #8 0x7fbbecc9ae3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #9 0x5627c6e38da4 in _start (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_erasure_code_plugin_isa+0x10bda4) (BuildId: 7922906370e5183d67f55211a868c0b0e22b4a2c)
```

so, in this change, we use `new []` and `delete []` to allocate and free
them, to be more consistent, and to silence this warning.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/erasure-code/isa/ErasureCodeIsa.cc
src/erasure-code/isa/ErasureCodeIsaTableCache.cc

index 1c2eadfdd0fec27f3e3f283ef31402e5ac05b517..305667e72e9f3f339740edacc880594415bee398 100644 (file)
@@ -379,7 +379,10 @@ ErasureCodeIsaDefault::prepare()
     dout(10) << "[ cache tables ] creating coeff for k=" <<
       k << " m=" << m << dendl;
     // build encoding coefficients which need to be computed once for each (k,m)
-    encode_coeff = (unsigned char*) malloc(k * (m + k));
+    //
+    // the coeff array is freed by ErasureCodeIsaTableCache::setEncodingCoefficient
+    // or ErasureCodeIsaTableCache::~ErasureCodeIsaTableCache()
+    encode_coeff = new unsigned char[k * (m + k)];
 
     if (matrixtype == kVandermonde)
       gf_gen_rs_matrix(encode_coeff, k + m, k);
@@ -398,7 +401,7 @@ ErasureCodeIsaDefault::prepare()
     dout(10) << "[ cache tables ] creating tables for k=" <<
       k << " m=" << m << dendl;
     // build encoding table which needs to be computed once for each (k,m)
-    encode_tbls = (unsigned char*) malloc(k * (m + k)*32);
+    encode_tbls = new unsigned char[k * (m + k)*32];
     ec_init_tables(k, m, &encode_coeff[k * k], encode_tbls);
 
     // either our new created table is stored or if it has been
index 8a3318aa118aaa751316c205159cb5939ff4bfef..aad44d733f4655b3e0cc91778684d93c27167500 100644 (file)
@@ -62,7 +62,7 @@ ErasureCodeIsaTableCache::~ErasureCodeIsaTableCache()
       for (table_it = tables_it->second.begin(); table_it != tables_it->second.end(); ++table_it) {
         if (table_it->second) {
           if (*(table_it->second)) {
-            delete *(table_it->second);
+            delete[] *(table_it->second);
           }
           delete table_it->second;
         }
@@ -75,7 +75,7 @@ ErasureCodeIsaTableCache::~ErasureCodeIsaTableCache()
       for (table_it = tables_it->second.begin(); table_it != tables_it->second.end(); ++table_it) {
         if (table_it->second) {
           if (*(table_it->second)) {
-            delete *(table_it->second);
+            delete[] *(table_it->second);
           }
           delete table_it->second;
         }
@@ -211,7 +211,7 @@ ErasureCodeIsaTableCache::setEncodingCoefficient(int matrix, int k, int m, unsig
   if (*ec_out_coeff) {
     // somebody might have deposited these coefficients in the meanwhile, so clean
     // the input coefficients and return the stored ones
-    free (ec_in_coeff);
+    delete[] ec_in_coeff;
     return *ec_out_coeff;
   } else {
     // we store the provided input coefficients and return these