Fix common realloc memory leak mistakes found by cppcheck

test-fix-ec-unknown-state-51
Vitaliy Filippov 2024-01-13 01:29:22 +03:00
parent 5280d1d561
commit 5d9d6f32a0
1 changed files with 12 additions and 8 deletions

View File

@ -558,13 +558,14 @@ void blockstore_impl_t::process_list(blockstore_op_t *op)
if (stable_count >= stable_alloc) if (stable_count >= stable_alloc)
{ {
stable_alloc *= 2; stable_alloc *= 2;
stable = (obj_ver_id*)realloc(stable, sizeof(obj_ver_id) * stable_alloc); obj_ver_id* nst = (obj_ver_id*)realloc(stable, sizeof(obj_ver_id) * stable_alloc);
if (!stable) if (!nst)
{ {
op->retval = -ENOMEM; op->retval = -ENOMEM;
FINISH_OP(op); FINISH_OP(op);
return; return;
} }
stable = nst;
} }
stable[stable_count++] = { stable[stable_count++] = {
.oid = clean_it->first, .oid = clean_it->first,
@ -642,8 +643,8 @@ void blockstore_impl_t::process_list(blockstore_op_t *op)
if (stable_count >= stable_alloc) if (stable_count >= stable_alloc)
{ {
stable_alloc += 32768; stable_alloc += 32768;
stable = (obj_ver_id*)realloc(stable, sizeof(obj_ver_id) * stable_alloc); obj_ver_id *nst = (obj_ver_id*)realloc(stable, sizeof(obj_ver_id) * stable_alloc);
if (!stable) if (!nst)
{ {
if (unstable) if (unstable)
free(unstable); free(unstable);
@ -651,6 +652,7 @@ void blockstore_impl_t::process_list(blockstore_op_t *op)
FINISH_OP(op); FINISH_OP(op);
return; return;
} }
stable = nst;
} }
stable[stable_count++] = dirty_it->first; stable[stable_count++] = dirty_it->first;
} }
@ -666,8 +668,8 @@ void blockstore_impl_t::process_list(blockstore_op_t *op)
if (unstable_count >= unstable_alloc) if (unstable_count >= unstable_alloc)
{ {
unstable_alloc += 32768; unstable_alloc += 32768;
unstable = (obj_ver_id*)realloc(unstable, sizeof(obj_ver_id) * unstable_alloc); obj_ver_id *nst = (obj_ver_id*)realloc(unstable, sizeof(obj_ver_id) * unstable_alloc);
if (!unstable) if (!nst)
{ {
if (stable) if (stable)
free(stable); free(stable);
@ -675,6 +677,7 @@ void blockstore_impl_t::process_list(blockstore_op_t *op)
FINISH_OP(op); FINISH_OP(op);
return; return;
} }
unstable = nst;
} }
unstable[unstable_count++] = dirty_it->first; unstable[unstable_count++] = dirty_it->first;
} }
@ -694,8 +697,8 @@ void blockstore_impl_t::process_list(blockstore_op_t *op)
if (stable_count+unstable_count > stable_alloc) if (stable_count+unstable_count > stable_alloc)
{ {
stable_alloc = stable_count+unstable_count; stable_alloc = stable_count+unstable_count;
stable = (obj_ver_id*)realloc(stable, sizeof(obj_ver_id) * stable_alloc); obj_ver_id *nst = (obj_ver_id*)realloc(stable, sizeof(obj_ver_id) * stable_alloc);
if (!stable) if (!nst)
{ {
if (unstable) if (unstable)
free(unstable); free(unstable);
@ -703,6 +706,7 @@ void blockstore_impl_t::process_list(blockstore_op_t *op)
FINISH_OP(op); FINISH_OP(op);
return; return;
} }
stable = nst;
} }
// Copy unstable entries // Copy unstable entries
for (int i = 0; i < unstable_count; i++) for (int i = 0; i < unstable_count; i++)