From f1d7586eb28394d222429b2db771bb136e28fa89 Mon Sep 17 00:00:00 2001 From: Ivano Bilenchi Date: Wed, 4 Dec 2024 12:21:23 +0100 Subject: [PATCH] Fix memory leak in 'cowl_has_key_axiom' --- .pre-commit-config.yaml | 2 +- data/test_import.owl | 2 ++ include/cowl_has_key_axiom.h | 15 ++++----------- include/cowl_vector.h | 4 ++-- src/cowl_has_key_axiom.c | 26 ++++++++++++++++++++++++++ test/tests/cowl_ontology_tests.c | 4 ++-- 6 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 src/cowl_has_key_axiom.c diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 09e62c3a..5a65c8d1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,6 +8,6 @@ repos: - id: mixed-line-ending - id: end-of-file-fixer - repo: https://github.com/pre-commit/mirrors-clang-format - rev: v19.1.3 + rev: v19.1.4 hooks: - id: clang-format diff --git a/data/test_import.owl b/data/test_import.owl index 88771766..f599ed25 100644 --- a/data/test_import.owl +++ b/data/test_import.owl @@ -34,6 +34,8 @@ EquivalentObjectProperties(:ObjectPropertyA ObjectInverseOf(:ObjectPropertyB)) SubClassOf(:ClassA ObjectAllValuesFrom(:ObjectPropertyA owl:Thing)) SubClassOf(:ClassA ObjectSomeValuesFrom(:ObjectPropertyA owl:Thing)) +HasKey(:ClassA (:ObjectPropertyA) ()) +HasKey(:ClassA () (:DataPropertyA)) ############################ diff --git a/include/cowl_has_key_axiom.h b/include/cowl_has_key_axiom.h index 740c85f4..5bab8db5 100644 --- a/include/cowl_has_key_axiom.h +++ b/include/cowl_has_key_axiom.h @@ -17,8 +17,6 @@ #include "cowl_attrs.h" #include "cowl_impl.h" #include "cowl_macros.h" -#include "cowl_object_type.h" -#include "cowl_vector.h" COWL_BEGIN_DECLS @@ -46,20 +44,15 @@ cowl_struct_decl(CowlHasKeyAxiom); * Returns a 'has key' axiom. * * @param cls_exp The class expression, instances of which this axiom acts as the key for. - * @param obj_props Object property expressions that make up the key. - * @param data_props Data property expressions that make up the key. + * @param obj_props @type{optional} Object property expressions that make up the key. + * @param data_props @type{optional} Data property expressions that make up the key. * @param annot @type{optional} The annotations. * @return Axiom, or NULL on error. */ +COWL_API COWL_RETAINED -COWL_INLINE CowlHasKeyAxiom *cowl_has_key_axiom(CowlAnyClsExp *cls_exp, CowlVector *obj_props, - CowlVector *data_props, CowlVector *annot) { - if (!obj_props) obj_props = cowl_vector_empty(); - if (!data_props) data_props = cowl_vector_empty(); - return (CowlHasKeyAxiom *)cowl_get_impl_3_annot(COWL_OT_A_HAS_KEY, cls_exp, obj_props, - data_props, annot); -} + CowlVector *data_props, CowlVector *annot); /** * Gets the class expression, instances of which this axiom acts as the key for. diff --git a/include/cowl_vector.h b/include/cowl_vector.h index 8aec4ceb..486ca832 100644 --- a/include/cowl_vector.h +++ b/include/cowl_vector.h @@ -50,7 +50,7 @@ cowl_struct_decl(CowlVector); /** * Returns a vector. * - * @param vec Underlying raw vector. + * @param data Underlying raw vector. * @return Vector, or NULL on error. * * @note You must not use the raw vector after passing it to this function. @@ -58,7 +58,7 @@ cowl_struct_decl(CowlVector); */ COWL_API COWL_RETAINED -CowlVector *cowl_vector(UVec(CowlObjectPtr) *vec); +CowlVector *cowl_vector(UVec(CowlObjectPtr) *data); /** * Returns a vector with no elements. diff --git a/src/cowl_has_key_axiom.c b/src/cowl_has_key_axiom.c new file mode 100644 index 00000000..80784937 --- /dev/null +++ b/src/cowl_has_key_axiom.c @@ -0,0 +1,26 @@ +/** + * @author Ivano Bilenchi + * + * @copyright Copyright (c) 2024 SisInf Lab, Polytechnic University of Bari + * @copyright + * @copyright SPDX-License-Identifier: EPL-2.0 + * + * @file + */ + +#include "cowl_has_key_axiom.h" +#include "cowl_any.h" +#include "cowl_impl.h" +#include "cowl_object.h" +#include "cowl_object_type.h" +#include "cowl_vector.h" + +CowlHasKeyAxiom *cowl_has_key_axiom(CowlAnyClsExp *cls_exp, CowlVector *obj_props, + CowlVector *data_props, CowlVector *annot) { + obj_props = obj_props ? cowl_retain(obj_props) : cowl_vector_empty(); + data_props = data_props ? cowl_retain(data_props) : cowl_vector_empty(); + CowlAny *ret = cowl_get_impl_3_annot(COWL_OT_A_HAS_KEY, cls_exp, obj_props, data_props, annot); + cowl_release(obj_props); + cowl_release(data_props); + return ret; +} diff --git a/test/tests/cowl_ontology_tests.c b/test/tests/cowl_ontology_tests.c index 9f4c5e78..13a7e491 100644 --- a/test/tests/cowl_ontology_tests.c +++ b/test/tests/cowl_ontology_tests.c @@ -26,7 +26,7 @@ static CowlOntology *onto = NULL; static ulib_uint const test_onto_imports_count = 1; -static ulib_uint const test_onto_axiom_count = 571; +static ulib_uint const test_onto_axiom_count = 573; static ulib_uint const test_primitives_count[] = { 106, 48, 72, 27, 18, 1, 46, 12 }; static ulib_uint const test_primitive_axiom_count[] = { 16, 4, 4, 2, 2, 1, 2, 8 }; @@ -66,7 +66,7 @@ static void axiom_counts_by_type_init(void) { axiom_counts_by_type[COWL_AT_FUNC_DATA_PROP] = 1; axiom_counts_by_type[COWL_AT_DATA_PROP_DOMAIN] = 66; axiom_counts_by_type[COWL_AT_DATA_PROP_RANGE] = 66; - axiom_counts_by_type[COWL_AT_HAS_KEY] = 1; + axiom_counts_by_type[COWL_AT_HAS_KEY] = 3; axiom_counts_by_type[COWL_AT_ANNOT_ASSERT] = 19; axiom_counts_by_type[COWL_AT_SUB_ANNOT_PROP] = 1; axiom_counts_by_type[COWL_AT_ANNOT_PROP_DOMAIN] = 1;