Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:Ledest:erlang:18
erlang
2104-Correct-error-handling-for-list_to_integer...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 2104-Correct-error-handling-for-list_to_integer-2.patch of Package erlang
From 66fc4da8610eb4c0ddb2b17d248472432d7c7b7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org> Date: Fri, 27 Nov 2020 05:30:52 +0100 Subject: [PATCH] Correct error handling for list_to_integer/2 Passing in some atoms as base to list_to_integer/2 would not raise a `badarg` exception. For example, `list_to_integer("10", true)` would return the value 4. --- erts/emulator/beam/bif.c | 12 ++++---- erts/emulator/test/num_bif_SUITE.erl | 45 ++++++++++++++++------------ 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/erts/emulator/beam/bif.c b/erts/emulator/beam/bif.c index 5beab81b98..4f1f114eba 100644 --- a/erts/emulator/beam/bif.c +++ b/erts/emulator/beam/bif.c @@ -3105,13 +3105,15 @@ BIF_RETTYPE list_to_integer_2(BIF_ALIST_2) int base; i = erts_list_length(BIF_ARG_1); - if (i < 0) - BIF_ERROR(BIF_P, BADARG); - + if (i < 0 || is_not_small(BIF_ARG_2)) { + BIF_ERROR(BIF_P, BADARG); + } + base = signed_val(BIF_ARG_2); - if (base < 2 || base > 36) - BIF_ERROR(BIF_P, BADARG); + if (base < 2 || base > 36) { + BIF_ERROR(BIF_P, BADARG); + } if (erts_list_to_integer(BIF_P, BIF_ARG_1, base, &res, &dummy) != LTI_ALL_INTEGER) { diff --git a/erts/emulator/test/num_bif_SUITE.erl b/erts/emulator/test/num_bif_SUITE.erl index 6b834705cf..fded36431e 100644 --- a/erts/emulator/test/num_bif_SUITE.erl +++ b/erts/emulator/test/num_bif_SUITE.erl @@ -544,7 +544,7 @@ t_string_to_integer(Config) when is_list(Config) -> test_sti(268435455), test_sti(-268435455), - % Interesting values around 2-pows, such as MIN_SMALL and MAX_SMALL. + %% Interesting values around 2-pows, such as MIN_SMALL and MAX_SMALL. lists:foreach(fun(Bits) -> N = 1 bsl Bits, test_sti(N - 1), @@ -553,11 +553,11 @@ t_string_to_integer(Config) when is_list(Config) -> end, lists:seq(16, 130)), - %% Bignums. + %% Bignums test_sti(123456932798748738738,16), test_sti(list_to_integer(lists:duplicate(2000, $1))), - %% unalign string + %% Unaligned string Str = <<"10">>, UnalignStr = <<0:3, (id(Str))/binary, 0:5>>, <<_:3, SomeStr:2/binary, _:5>> = id(UnalignStr), @@ -568,32 +568,39 @@ t_string_to_integer(Config) when is_list(Config) -> {'EXIT', {badarg, _}} = (catch binary_to_integer(Value)), {'EXIT', {badarg, _}} = - (catch erlang:list_to_integer(Value)) + (catch list_to_integer(Value)) end,[atom,1.2,0.0,[$1,[$2]]]), - % Default base error cases + %% Default base error cases lists:foreach(fun(Value) -> {'EXIT', {badarg, _}} = - (catch erlang:binary_to_integer( - list_to_binary(Value))), + (catch binary_to_integer(list_to_binary(Value))), {'EXIT', {badarg, _}} = - (catch erlang:list_to_integer(Value)) + (catch list_to_integer(Value)) end,["1.0"," 1"," -1","","+"]), - % Custom base error cases + %% Custom base error cases lists:foreach(fun({Value,Base}) -> {'EXIT', {badarg, _}} = - (catch binary_to_integer( - list_to_binary(Value),Base)), + (catch binary_to_integer(list_to_binary(Value), Base)), {'EXIT', {badarg, _}} = - (catch erlang:list_to_integer(Value,Base)) - end,[{" 1",1},{" 1",37},{"2",2},{"B",11},{"b",11},{":", 16}, - {"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111z",16}, - {"1z111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",16}, - {"111z11111111",16}]), - - %% log2 calculation overflow bug in do_integer_to_list (OTP-12624) - %% Would crash with segv + (catch list_to_integer(Value, Base)) + end, + [{" 1",1},{" 1",37},{"2",2},{"B",11},{"b",11},{":", 16}, + {"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111z",16}, + {"1z111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",16}, + {"111z11111111",16}, + %% Untagging atoms at the beginning of atom.names + %% would produce a base in the valid range. + {"10",true}, %Base 4 + {"10",'_'}, %Base 8 + {"10",nonode@nohost}, %Base 12 + {"10",'$end_of_table'}, %Base 16 + {"10",''} %Base 20 + ]), + + %% log2 calculation overflow bug in do_integer_to_list (OTP-12624). + %% Would crash with segementation fault. 0 = list_to_integer(lists:duplicate(10000000,$0)), ok. -- 2.26.2
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor