From c1a07b2ca1e456e1ac01406592966f90338934d2 Mon Sep 17 00:00:00 2001 From: Alan King Date: Wed, 18 Dec 2024 14:52:23 -0500 Subject: [PATCH] [#288] Fix memleaks discovered by ASAN --- src/main.cpp | 4 ++++ src/storage_tiering.cpp | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 01b4289..e29ef65 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,7 @@ #include "irods/private/storage_tiering/exec_as_user.hpp" #include +#include #include #undef LIST @@ -118,6 +119,7 @@ namespace { } dataObjInp_t data_obj_inp{}; + const auto free_cond_input = irods::at_scope_exit{[&data_obj_inp] { clearKeyVal(&data_obj_inp.condInput); }}; rstrcpy(data_obj_inp.objPath, _object_path.c_str(), MAX_NAME_LEN); data_obj_inp.createMode = getDefFileMode(); addKeyVal(&data_obj_inp.condInput, RESC_NAME_KW, _source_resource.c_str()); @@ -149,6 +151,7 @@ namespace { } dataObjInp_t obj_inp{}; + const auto free_cond_input = irods::at_scope_exit{[&obj_inp] { clearKeyVal(&obj_inp.condInput); }}; rstrcpy( obj_inp.objPath, _object_path.c_str(), @@ -192,6 +195,7 @@ namespace { const_cast(_attribute.c_str()), const_cast(ts.c_str()), ""}; + const auto free_cond_input = irods::at_scope_exit{[&avuOp] { clearKeyVal(&avuOp.condInput); }}; if (_comm->clientUser.authInfo.authFlag >= LOCAL_PRIV_USER_AUTH) { addKeyVal(&avuOp.condInput, ADMIN_KW, ""); diff --git a/src/storage_tiering.cpp b/src/storage_tiering.cpp index 2133fa4..accded9 100644 --- a/src/storage_tiering.cpp +++ b/src/storage_tiering.cpp @@ -757,8 +757,25 @@ namespace irods { }; execMyRuleInp_t exec_inp{}; - rstrcpy(exec_inp.myRule, rule_obj.dump().c_str(), META_STR_LEN); msParamArray_t* out_arr{}; + // Capture out_arr pointer by reference because it is still nullptr at this point. + const auto free_inputs_and_outputs = irods::at_scope_exit{[&exec_inp, &out_arr] { + clearKeyVal(&exec_inp.condInput); + + if (exec_inp.inpParamArray) { + // The second parameter with a value of 1 instructs the function to free the "inOutStruct". + clearMsParamArray(exec_inp.inpParamArray, 1); + std::free(exec_inp.inpParamArray); + } + + if (out_arr) { + // The second parameter with a value of 1 instructs the function to free the "inOutStruct". + clearMsParamArray(out_arr, 1); + std::free(out_arr); + } + }}; + + rstrcpy(exec_inp.myRule, rule_obj.dump().c_str(), META_STR_LEN); addKeyVal( &exec_inp.condInput , irods::KW_CFG_INSTANCE_NAME