[PATCH] array: prevent crash

Michael Santos michael.santos@REDACTED
Thu Nov 25 01:35:46 CET 2010


Add a guard to avoid an infinite loop if a float or negative integer
is used in the record.
---
 lib/stdlib/src/array.erl        |   12 ++++++++----
 lib/stdlib/test/array_SUITE.erl |    8 ++++++--
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/lib/stdlib/src/array.erl b/lib/stdlib/src/array.erl
index 83576c9..72bd977 100644
--- a/lib/stdlib/src/array.erl
+++ b/lib/stdlib/src/array.erl
@@ -766,7 +766,7 @@ set_get_test_() ->
 
 to_list(#array{size = 0}) ->
     [];
-to_list(#array{size = N, elements = E, default = D}) ->
+to_list(#array{size = N, elements = E, default = D}) when is_integer(N), N >= 0 ->
     to_list_1(E, D, N - 1);
 to_list(_) ->
     erlang:error(badarg).
@@ -826,7 +826,9 @@ to_list_test_() ->
 	      to_list(set(N0+1,1,set(0,1,new({default,0}))))),
      ?_assert([1|lists:duplicate(N0*3,0)++[1]] =:= 
 	      to_list(set((N0*3)+1,1,set(0,1,new({default,0}))))),
-     ?_assertError(badarg, to_list(no_array))
+     ?_assertError(badarg, to_list(no_array)),
+     ?_assertError(badarg, to_list(#array{size = -1, elements = 0})),
+     ?_assertError(badarg, to_list(#array{size = 1.1, elements = 0}))
     ].
 -endif.
 
@@ -1021,7 +1023,7 @@ from_list_test_() ->
 
 to_orddict(#array{size = 0}) ->
     [];
-to_orddict(#array{size = N, elements = E, default = D}) ->
+to_orddict(#array{size = N, elements = E, default = D}) when is_integer(N), N >= 0 ->
     I = N - 1,
     to_orddict_1(E, I, D, I);
 to_orddict(_) ->
@@ -1099,7 +1101,9 @@ to_orddict_test_() ->
 	      [{N0*2+1,1} | [{N,undefined}||N<-lists:seq(N0*2+2,N0*10)]] ++
 	      [{N0*10+1,2}] =:= 
 	      to_orddict(set(N0*10+1,2,set(N0*2+1,1,set(0,0,new()))))),
-     ?_assertError(badarg, to_orddict(no_array))     
+     ?_assertError(badarg, to_orddict(no_array)),
+     ?_assertError(badarg, to_orddict(#array{size = -1, elements = 0})),
+     ?_assertError(badarg, to_orddict(#array{size = 1.1, elements = 0}))
     ].
 -endif.
 
diff --git a/lib/stdlib/test/array_SUITE.erl b/lib/stdlib/test/array_SUITE.erl
index e7cfc65..0a93dac 100644
--- a/lib/stdlib/test/array_SUITE.erl
+++ b/lib/stdlib/test/array_SUITE.erl
@@ -416,7 +416,9 @@ to_list_test_() ->
 	      to_list(set(N0+1,1,set(0,1,new({default,0}))))),
      ?_assert([1|lists:duplicate(N0*3,0)++[1]] =:= 
 	      to_list(set((N0*3)+1,1,set(0,1,new({default,0}))))),
-     ?_assertError(badarg, to_list(no_array))
+     ?_assertError(badarg, to_list(no_array)),
+     ?_assertError(badarg, to_list(#array{size = -1, elements = 0})),
+     ?_assertError(badarg, to_list(#array{size = 1.1, elements = 0}))
     ].
 
 sparse_to_list_test_() ->
@@ -488,7 +490,9 @@ to_orddict_test_() ->
 	      [{N0*2+1,1} | [{N,undefined}||N<-lists:seq(N0*2+2,N0*10)]] ++
 	      [{N0*10+1,2}] =:= 
 	      to_orddict(set(N0*10+1,2,set(N0*2+1,1,set(0,0,new()))))),
-     ?_assertError(badarg, to_orddict(no_array))     
+     ?_assertError(badarg, to_orddict(no_array)),
+     ?_assertError(badarg, to_orddict(#array{size = -1, elements = 0})),
+     ?_assertError(badarg, to_orddict(#array{size = 1.1, elements = 0}))
     ].
 
 sparse_to_orddict_test_() ->
-- 
1.7.0.4



More information about the erlang-patches mailing list