Skip to content

Commit 7e06d45

Browse files
committed
Merge pull request #1367 from bettio/check-fconv-arg
Fix: add missing is_number check to fconv opcode `fconv` takes also registers as argument, hence they might contain a value that is not a valid number term. These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 6acfcb5 + 1fc7ffd commit 7e06d45

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
- Fixed specifications of nifs from `esp_adc` module
1212
- ESP32: fix `gpio:init/1` on GPIO >= 32
13+
- Adding missing check, passing a non numeric argument to a function expecting a floating point
14+
might lead to a crash in certain situations.
1315

1416
## [0.6.5] - 2024-10-15
1517

src/libAtomVM/opcodesswitch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5912,6 +5912,9 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
59125912
#ifdef IMPL_EXECUTE_LOOP
59135913
TRACE("fconv/2 %lx, fp%i\n", src_value, freg);
59145914
context_ensure_fpregs(ctx);
5915+
if (UNLIKELY(!term_is_number(src_value))) {
5916+
RAISE_ERROR(BADARITH_ATOM);
5917+
}
59155918
ctx->fr[freg] = term_conv_to_float(src_value);
59165919
#endif
59175920

tests/erlang_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ compile_erlang(floatext)
385385
compile_erlang(boxed_is_not_float)
386386
compile_erlang(float_is_float)
387387
compile_erlang(float_is_number)
388+
compile_erlang(fconv_fail_invalid)
388389

389390
compile_erlang(float2list)
390391
compile_erlang(float2bin)
@@ -857,6 +858,7 @@ add_custom_target(erlang_test_modules DEPENDS
857858
boxed_is_not_float.beam
858859
float_is_float.beam
859860
float_is_number.beam
861+
fconv_fail_invalid.beam
860862

861863
float2list.beam
862864
float2bin.beam
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2024 Davide Bettio <davide@uninstall.it>
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(fconv_fail_invalid).
22+
23+
-export([start/0, deg_min_nsew_to_decimal/1]).
24+
25+
start() ->
26+
try ?MODULE:deg_min_nsew_to_decimal({5, nil, e}) of
27+
_Any -> -1
28+
catch
29+
error:badarith -> 0
30+
end.
31+
32+
deg_min_nsew_to_decimal(Coord) ->
33+
{Deg, Min, Nsew} = Coord,
34+
DecimalCoord = Deg + Min / 60,
35+
case Nsew of
36+
n -> DecimalCoord;
37+
s -> -DecimalCoord;
38+
e -> DecimalCoord;
39+
w -> -DecimalCoord
40+
end.

tests/test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ struct Test tests[] = {
435435
TEST_CASE_EXPECTED(boxed_is_not_float, 16),
436436
TEST_CASE_EXPECTED(float_is_float, 32),
437437
TEST_CASE_EXPECTED(float_is_number, 32),
438+
TEST_CASE(fconv_fail_invalid),
438439

439440
TEST_CASE_EXPECTED(float2bin, 31),
440441
TEST_CASE_EXPECTED(float2list, 31),

0 commit comments

Comments
 (0)