Skip to content

Commit 7e3d58c

Browse files
committed
Remove memory leak from fail path on invalid loader.
1 parent bfc0cbb commit 7e3d58c

File tree

5 files changed

+231
-7
lines changed

5 files changed

+231
-7
lines changed

source/loader/source/loader.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ void loader_initialize_proxy()
172172
log_write("metacall", LOG_LEVEL_ERROR, "Loader invalid proxy insertion <%p>", (void *) proxy);
173173

174174
loader_impl_destroy(proxy);
175-
176-
free(host);
177175
}
178176

179177
/* Insert into destruction list */
@@ -353,16 +351,20 @@ loader_impl loader_create_impl(const loader_naming_tag tag)
353351

354352
if (impl != NULL)
355353
{
356-
if (set_insert(l->impl_map, (set_key)loader_impl_tag(impl), impl) == 0)
354+
if (set_insert(l->impl_map, (set_key)loader_impl_tag(impl), impl) != 0)
357355
{
358-
return impl;
359-
}
356+
log_write("metacall", LOG_LEVEL_ERROR, "Loader implementation insertion error (%s)", tag);
360357

361-
log_write("metacall", LOG_LEVEL_ERROR, "Loader implementation insertion error (%s)", tag);
358+
loader_impl_destroy(impl);
362359

363-
loader_impl_destroy(impl);
360+
return NULL;
361+
}
362+
363+
return impl;
364364
}
365365

366+
free(host);
367+
366368
return NULL;
367369
}
368370

source/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ add_subdirectory(metacall_initialize_ex_test)
168168
add_subdirectory(metacall_reinitialize_test)
169169
add_subdirectory(metacall_initialize_destroy_multiple_test)
170170
add_subdirectory(metacall_initialize_destroy_multiple_node_test)
171+
add_subdirectory(metacall_invalid_loader_test)
171172
add_subdirectory(metacall_fork_test)
172173
add_subdirectory(metacall_return_monad_test)
173174
add_subdirectory(metacall_callback_complex_test)
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Check if this loader is enabled
2+
if(NOT OPTION_BUILD_DIST_LIBS)
3+
return()
4+
endif()
5+
6+
#
7+
# Executable name and options
8+
#
9+
10+
# Target name
11+
set(target metacall-invalid-loader-test)
12+
message(STATUS "Test ${target}")
13+
14+
#
15+
# Compiler warnings
16+
#
17+
18+
include(Warnings)
19+
20+
#
21+
# Compiler security
22+
#
23+
24+
include(SecurityFlags)
25+
26+
#
27+
# Sources
28+
#
29+
30+
set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
31+
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
32+
33+
set(sources
34+
${source_path}/main.cpp
35+
${source_path}/metacall_invalid_loader_test.cpp
36+
)
37+
38+
# Group source files
39+
set(header_group "Header Files (API)")
40+
set(source_group "Source Files")
41+
source_group_by_path(${include_path} "\\\\.h$|\\\\.hpp$"
42+
${header_group} ${headers})
43+
source_group_by_path(${source_path} "\\\\.cpp$|\\\\.c$|\\\\.h$|\\\\.hpp$"
44+
${source_group} ${sources})
45+
46+
#
47+
# Create executable
48+
#
49+
50+
# Build executable
51+
add_executable(${target}
52+
${sources}
53+
)
54+
55+
# Create namespaced alias
56+
add_executable(${META_PROJECT_NAME}::${target} ALIAS ${target})
57+
58+
#
59+
# Project options
60+
#
61+
62+
set_target_properties(${target}
63+
PROPERTIES
64+
${DEFAULT_PROJECT_OPTIONS}
65+
FOLDER "${IDE_FOLDER}"
66+
)
67+
68+
#
69+
# Include directories
70+
#
71+
72+
target_include_directories(${target}
73+
PRIVATE
74+
${DEFAULT_INCLUDE_DIRECTORIES}
75+
${PROJECT_BINARY_DIR}/source/include
76+
)
77+
78+
#
79+
# Libraries
80+
#
81+
82+
target_link_libraries(${target}
83+
PRIVATE
84+
${DEFAULT_LIBRARIES}
85+
86+
GTest
87+
88+
${META_PROJECT_NAME}::metacall_distributable
89+
)
90+
91+
#
92+
# Compile definitions
93+
#
94+
95+
target_compile_definitions(${target}
96+
PRIVATE
97+
${DEFAULT_COMPILE_DEFINITIONS}
98+
)
99+
100+
#
101+
# Compile options
102+
#
103+
104+
target_compile_options(${target}
105+
PRIVATE
106+
${DEFAULT_COMPILE_OPTIONS}
107+
)
108+
109+
#
110+
# Linker options
111+
#
112+
113+
target_link_libraries(${target}
114+
PRIVATE
115+
${DEFAULT_LINKER_OPTIONS}
116+
)
117+
118+
#
119+
# Define test
120+
#
121+
122+
add_test(NAME ${target}
123+
COMMAND $<TARGET_FILE:${target}>
124+
)
125+
126+
#
127+
# Define test properties
128+
#
129+
130+
set_property(TEST ${target}
131+
PROPERTY LABELS ${target}
132+
)
133+
134+
include(TestEnvironmentVariables)
135+
136+
test_environment_variables(${target}
137+
""
138+
${TESTS_ENVIRONMENT_VARIABLES}
139+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* MetaCall Library by Parra Studios
3+
* A library for providing a foreign function interface calls.
4+
*
5+
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include <gtest/gtest.h>
22+
23+
int main(int argc, char * argv[])
24+
{
25+
::testing::InitGoogleTest(&argc, argv);
26+
27+
return RUN_ALL_TESTS();
28+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* MetaCall Library by Parra Studios
3+
* A library for providing a foreign function interface calls.
4+
*
5+
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include <gtest/gtest.h>
22+
23+
#include <metacall/metacall.h>
24+
#include <metacall/metacall_loaders.h>
25+
26+
class metacall_invalid_loader_test : public testing::Test
27+
{
28+
public:
29+
};
30+
31+
TEST_F(metacall_invalid_loader_test, DefaultConstructor)
32+
{
33+
metacall_print_info();
34+
35+
ASSERT_EQ((int) 0, (int) metacall_initialize());
36+
37+
static const char * invalid_scripts[] =
38+
{
39+
"a.invalid"
40+
};
41+
42+
EXPECT_EQ((int) 1, (int) metacall_load_from_file("invalid", invalid_scripts, sizeof(invalid_scripts) / sizeof(invalid_scripts[0]), NULL));
43+
44+
static const char invalid_buffer[] =
45+
{
46+
"invalid"
47+
};
48+
49+
EXPECT_EQ((int) 1, (int) metacall_load_from_memory("invalid", invalid_buffer, sizeof(invalid_buffer), NULL));
50+
51+
ASSERT_EQ((int) 1, (int) metacall_is_initialized("invalid"));
52+
53+
ASSERT_EQ((int) 0, (int) metacall_destroy());
54+
}

0 commit comments

Comments
 (0)