Skip to content

Commit 6a88e09

Browse files
committed
erasure-code: use new/delete to alloc/free coefficients array
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 ceph#2 0x7fbbe390be40 in ErasureCodePluginIsa::~ErasureCodePluginIsa() /home/jenkins-build/build/workspace/ceph-pull-requests/src/erasure-code/isa/ErasureCodePluginIsa.h:24:7 ceph#3 0x7fbbe390be68 in ErasureCodePluginIsa::~ErasureCodePluginIsa() /home/jenkins-build/build/workspace/ceph-pull-requests/src/erasure-code/isa/ErasureCodePluginIsa.h:24:7 ceph#4 0x5627c7063b52 in ceph::ErasureCodePluginRegistry::~ErasureCodePluginRegistry() /home/jenkins-build/build/workspace/ceph-pull-requests/src/erasure-code/ErasureCodePlugin.cc:49:5 ceph#5 0x7fbbeccb6494 in __run_exit_handlers stdlib/./stdlib/exit.c:113:8 ceph#6 0x7fbbeccb660f in exit stdlib/./stdlib/exit.c:143:3 ceph#7 0x7fbbecc9ad96 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:74:3 ceph#8 0x7fbbecc9ae3f in __libc_start_main csu/../csu/libc-start.c:392:3 ceph#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 <[email protected]>
1 parent 8d7564a commit 6a88e09

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/erasure-code/isa/ErasureCodeIsa.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,10 @@ ErasureCodeIsaDefault::prepare()
379379
dout(10) << "[ cache tables ] creating coeff for k=" <<
380380
k << " m=" << m << dendl;
381381
// build encoding coefficients which need to be computed once for each (k,m)
382-
encode_coeff = (unsigned char*) malloc(k * (m + k));
382+
//
383+
// the coeff array is freed by ErasureCodeIsaTableCache::setEncodingCoefficient
384+
// or ErasureCodeIsaTableCache::~ErasureCodeIsaTableCache()
385+
encode_coeff = new unsigned char[k * (m + k)];
383386

384387
if (matrixtype == kVandermonde)
385388
gf_gen_rs_matrix(encode_coeff, k + m, k);
@@ -398,7 +401,7 @@ ErasureCodeIsaDefault::prepare()
398401
dout(10) << "[ cache tables ] creating tables for k=" <<
399402
k << " m=" << m << dendl;
400403
// build encoding table which needs to be computed once for each (k,m)
401-
encode_tbls = (unsigned char*) malloc(k * (m + k)*32);
404+
encode_tbls = new unsigned char[k * (m + k)*32];
402405
ec_init_tables(k, m, &encode_coeff[k * k], encode_tbls);
403406

404407
// either our new created table is stored or if it has been

src/erasure-code/isa/ErasureCodeIsaTableCache.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ ErasureCodeIsaTableCache::~ErasureCodeIsaTableCache()
6262
for (table_it = tables_it->second.begin(); table_it != tables_it->second.end(); ++table_it) {
6363
if (table_it->second) {
6464
if (*(table_it->second)) {
65-
delete *(table_it->second);
65+
delete[] *(table_it->second);
6666
}
6767
delete table_it->second;
6868
}
@@ -75,7 +75,7 @@ ErasureCodeIsaTableCache::~ErasureCodeIsaTableCache()
7575
for (table_it = tables_it->second.begin(); table_it != tables_it->second.end(); ++table_it) {
7676
if (table_it->second) {
7777
if (*(table_it->second)) {
78-
delete *(table_it->second);
78+
delete[] *(table_it->second);
7979
}
8080
delete table_it->second;
8181
}
@@ -211,7 +211,7 @@ ErasureCodeIsaTableCache::setEncodingCoefficient(int matrix, int k, int m, unsig
211211
if (*ec_out_coeff) {
212212
// somebody might have deposited these coefficients in the meanwhile, so clean
213213
// the input coefficients and return the stored ones
214-
free (ec_in_coeff);
214+
delete[] ec_in_coeff;
215215
return *ec_out_coeff;
216216
} else {
217217
// we store the provided input coefficients and return these

0 commit comments

Comments
 (0)