File 0623-stdlib-fix-lists-sublist-3-if-Start-length-L-1.patch of Package erlang

From 2d312773d995eb2177037c7530d12fb7c55f8308 Mon Sep 17 00:00:00 2001
From: Shanti Chellaram <shanti@shanti.im>
Date: Thu, 20 Aug 2020 22:18:50 -0400
Subject: [PATCH] stdlib: fix lists:sublist/3 if Start>length(L)+1

lists:sublist/3 fails when Start (the 2nd argument) was greater than
len(List)+1, due to nthtail/2 throwing an exception. This change
replaces the use of nthtail/2 with a recursive function definition.
---
 lib/stdlib/src/lists.erl        | 8 ++++++--
 lib/stdlib/test/lists_SUITE.erl | 2 ++
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/lib/stdlib/src/lists.erl b/lib/stdlib/src/lists.erl
index 06c90c0280..f44ed726ca 100644
--- a/lib/stdlib/src/lists.erl
+++ b/lib/stdlib/src/lists.erl
@@ -341,8 +341,12 @@ max([],    Max)              -> Max.
       Len :: non_neg_integer(),
       T :: term().
 
-sublist(List, S, L) when is_integer(L), L >= 0 ->
-    sublist(nthtail(S-1, List), L).
+sublist(List, 1, L) when is_list(List), is_integer(L), L >= 0 ->
+    sublist(List, L);
+sublist([], S, _L) when is_integer(S), S >= 2 ->
+    [];
+sublist([_H|T], S, L) when is_integer(S), S >= 2 ->
+    sublist(T, S-1, L).
 
 -spec sublist(List1, Len) -> List2 when
       List1 :: [T],
diff --git a/lib/stdlib/test/lists_SUITE.erl b/lib/stdlib/test/lists_SUITE.erl
index 837ab4e97e..88b07df663 100644
--- a/lib/stdlib/test/lists_SUITE.erl
+++ b/lib/stdlib/test/lists_SUITE.erl
@@ -2306,6 +2306,7 @@ sublist_2_e(Config) when is_list(Config) ->
 sublist_3(Config) when is_list(Config) ->
     ?line [] = lists:sublist([], 1, 0),
     ?line [] = lists:sublist([], 1, 1),
+    ?line [] = lists:sublist([], 2, 0),
     ?line [] = lists:sublist([a], 1, 0),
     ?line [a] = lists:sublist([a], 1, 1),
     ?line [a] = lists:sublist([a], 1, 2),
@@ -2319,6 +2320,7 @@ sublist_3(Config) when is_list(Config) ->
     ?line [] = lists:sublist([a], 2, 1),
     ?line [] = lists:sublist([a], 2, 2),
     ?line [] = lists:sublist([a], 2, 79),
+    ?line [] = lists:sublist([a], 3, 1),
     ?line [] = lists:sublist([a,b|c], 1, 0),
     ?line [] = lists:sublist([a,b|c], 2, 0),
     ?line [a] = lists:sublist([a,b|c], 1, 1),
-- 
2.26.2

openSUSE Build Service is sponsored by