Fix: LibOverride: Memory leak after recent std::string conversion

An allocated char pointer was reassigned without being freed.
Fix by replacing the error-prone combination of C strings and
std::string with a complete conversion to std::string. Also change
the logic a bit to allow moving the path string in some cases
instead of copying it.

Pull Request: https://projects.blender.org/blender/blender/pulls/118046
This commit is contained in:
Hans Goudey
2024-02-09 19:29:03 +01:00
committed by Hans Goudey
parent 93e72912e1
commit d1d3d998ea

View File

@@ -7,6 +7,7 @@
*/
#include <cstring>
#include <fmt/format.h>
#include <optional>
#include <CLG_log.h>
@@ -61,14 +62,14 @@ static CLG_LogRef LOG = {"rna.access_compare_override"};
static ID *rna_property_override_property_real_id_owner(Main * /*bmain*/,
PointerRNA *ptr,
PropertyRNA *prop,
char **r_rna_path)
std::optional<std::string> *r_rna_path)
{
ID *id = ptr->owner_id;
ID *owner_id = id;
const char *rna_path_prefix = nullptr;
if (r_rna_path != nullptr) {
*r_rna_path = nullptr;
*r_rna_path = std::nullopt;
}
if (id == nullptr) {
@@ -101,10 +102,12 @@ static ID *rna_property_override_property_real_id_owner(Main * /*bmain*/,
return owner_id;
}
if (const std::optional<std::string> rna_path = RNA_path_from_ID_to_property(ptr, prop)) {
*r_rna_path = BLI_strdup(rna_path->c_str());
if (rna_path_prefix != nullptr) {
*r_rna_path = BLI_sprintfN("%s%s", rna_path_prefix, rna_path->c_str());
if (std::optional<std::string> rna_path = RNA_path_from_ID_to_property(ptr, prop)) {
if (rna_path_prefix) {
r_rna_path->emplace(fmt::format("{}{}", rna_path_prefix, *rna_path));
}
else {
r_rna_path->emplace(std::move(*rna_path));
}
return owner_id;
@@ -1655,13 +1658,12 @@ IDOverrideLibraryProperty *RNA_property_override_property_find(Main *bmain,
PropertyRNA *prop,
ID **r_owner_id)
{
char *rna_path;
std::optional<std::string> rna_path;
*r_owner_id = rna_property_override_property_real_id_owner(bmain, ptr, prop, &rna_path);
if (rna_path != nullptr) {
if (rna_path) {
IDOverrideLibraryProperty *op = BKE_lib_override_library_property_find(
(*r_owner_id)->override_library, rna_path);
MEM_freeN(rna_path);
(*r_owner_id)->override_library, rna_path->c_str());
return op;
}
return nullptr;
@@ -1672,17 +1674,16 @@ IDOverrideLibraryProperty *RNA_property_override_property_get(Main *bmain,
PropertyRNA *prop,
bool *r_created)
{
char *rna_path;
std::optional<std::string> rna_path;
if (r_created != nullptr) {
*r_created = false;
}
ID *id = rna_property_override_property_real_id_owner(bmain, ptr, prop, &rna_path);
if (rna_path != nullptr) {
if (rna_path) {
IDOverrideLibraryProperty *op = BKE_lib_override_library_property_get(
id->override_library, rna_path, r_created);
MEM_freeN(rna_path);
id->override_library, rna_path->c_str(), r_created);
return op;
}
return nullptr;