Skip to content

Commit

Permalink
Fix: add missing is_number check to fconv opcode
Browse files Browse the repository at this point in the history
term_conv_to_float expects a valid number term.

Signed-off-by: Davide Bettio <[email protected]>
  • Loading branch information
bettio committed Nov 17, 2024
1 parent 6acfcb5 commit 1fc7ffd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

## [0.6.5] - 2024-10-15

Expand Down
3 changes: 3 additions & 0 deletions src/libAtomVM/opcodesswitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -5912,6 +5912,9 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
#ifdef IMPL_EXECUTE_LOOP
TRACE("fconv/2 %lx, fp%i\n", src_value, freg);
context_ensure_fpregs(ctx);
if (UNLIKELY(!term_is_number(src_value))) {
RAISE_ERROR(BADARITH_ATOM);
}
ctx->fr[freg] = term_conv_to_float(src_value);
#endif

Expand Down
2 changes: 2 additions & 0 deletions tests/erlang_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ compile_erlang(floatext)
compile_erlang(boxed_is_not_float)
compile_erlang(float_is_float)
compile_erlang(float_is_number)
compile_erlang(fconv_fail_invalid)

compile_erlang(float2list)
compile_erlang(float2bin)
Expand Down Expand Up @@ -857,6 +858,7 @@ add_custom_target(erlang_test_modules DEPENDS
boxed_is_not_float.beam
float_is_float.beam
float_is_number.beam
fconv_fail_invalid.beam

float2list.beam
float2bin.beam
Expand Down
40 changes: 40 additions & 0 deletions tests/erlang_tests/fconv_fail_invalid.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
%
% This file is part of AtomVM.
%
% Copyright 2024 Davide Bettio <[email protected]>
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
%
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
%

-module(fconv_fail_invalid).

-export([start/0, deg_min_nsew_to_decimal/1]).

start() ->
try ?MODULE:deg_min_nsew_to_decimal({5, nil, e}) of
_Any -> -1
catch
error:badarith -> 0
end.

deg_min_nsew_to_decimal(Coord) ->
{Deg, Min, Nsew} = Coord,
DecimalCoord = Deg + Min / 60,
case Nsew of
n -> DecimalCoord;
s -> -DecimalCoord;
e -> DecimalCoord;
w -> -DecimalCoord
end.
1 change: 1 addition & 0 deletions tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ struct Test tests[] = {
TEST_CASE_EXPECTED(boxed_is_not_float, 16),
TEST_CASE_EXPECTED(float_is_float, 32),
TEST_CASE_EXPECTED(float_is_number, 32),
TEST_CASE(fconv_fail_invalid),

TEST_CASE_EXPECTED(float2bin, 31),
TEST_CASE_EXPECTED(float2list, 31),
Expand Down

0 comments on commit 1fc7ffd

Please sign in to comment.