Skip to content

Commit 71f22ec

Browse files
committed
Add missing call to memory_ensure_free for erlang:md5/1 nif
Also add a test for erlang:md5/1. Signed-off-by: Paul Guyot <[email protected]>
1 parent aafa6c7 commit 71f22ec

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

src/platforms/esp32/components/avm_sys/platform_nifs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ static term nif_rom_md5(Context *ctx, int argc, term argv[])
381381
MD5Update(&md5, (const unsigned char *) term_binary_data(data), term_binary_size(data));
382382
MD5Final(digest, &md5);
383383
#endif
384-
384+
if (UNLIKELY(memory_ensure_free(ctx, term_binary_data_size_in_terms(MD5_DIGEST_LENGTH) + BINARY_HEADER_SIZE) != MEMORY_GC_OK)) {
385+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
386+
}
385387
return term_from_literal_binary(digest, MD5_DIGEST_LENGTH, &ctx->heap, ctx->global);
386388
}
387389

src/platforms/esp32/test/main/test_erl_sources/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,20 @@ function(compile_erlang module_name)
3636
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${CMAKE_CURRENT_BINARY_DIR}/${module_name}.beam")
3737
endfunction()
3838

39+
compile_erlang(test_md5)
3940
compile_erlang(test_socket)
4041
compile_erlang(test_time_and_processes)
4142

4243
add_custom_command(
4344
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/esp32_test_modules.avm"
4445
COMMAND HostAtomVM-prefix/src/HostAtomVM-build/tools/packbeam/PackBEAM -i esp32_test_modules.avm
4546
HostAtomVM-prefix/src/HostAtomVM-build/libs/atomvmlib.avm
47+
test_md5.beam
4648
test_socket.beam
4749
test_time_and_processes.beam
4850
DEPENDS
4951
HostAtomVM
52+
"${CMAKE_CURRENT_BINARY_DIR}/test_md5.beam"
5053
"${CMAKE_CURRENT_BINARY_DIR}/test_socket.beam"
5154
"${CMAKE_CURRENT_BINARY_DIR}/test_time_and_processes.beam"
5255
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2023 Paul Guyot <[email protected]>
5+
%
6+
% Licensed under the Apache License, Version 2.0 (the "License");
7+
% you may not use this file except in compliance with the License.
8+
% You may obtain a copy of the License at
9+
%
10+
% http://www.apache.org/licenses/LICENSE-2.0
11+
%
12+
% Unless required by applicable law or agreed to in writing, software
13+
% distributed under the License is distributed on an "AS IS" BASIS,
14+
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
% See the License for the specific language governing permissions and
16+
% limitations under the License.
17+
%
18+
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
%
20+
21+
-module(test_md5).
22+
23+
-export([start/0]).
24+
25+
start() ->
26+
<<62, 37, 150, 10, 121, 219, 198, 155, 103, 76, 212, 236, 103, 167, 44, 98>> = erlang:md5(
27+
<<"Hello world">>
28+
),
29+
ok.

src/platforms/esp32/test/main/test_main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ term avm_test_case(const char *test_module)
177177
return ret_value;
178178
}
179179

180+
TEST_CASE("test_md5", "[test_run]")
181+
{
182+
term ret_value = avm_test_case("test_md5.beam");
183+
TEST_ASSERT(ret_value == OK_ATOM);
184+
}
185+
180186
TEST_CASE("test_time_and_processes", "[test_run]")
181187
{
182188
term ret_value = avm_test_case("test_time_and_processes.beam");

0 commit comments

Comments
 (0)