Skip to content

Commit d58ef57

Browse files
author
José Valim
committed
Ensure take/1 does not consume more than required
Kudos to @hamiltop for the idea and initial work on this fix. Closes #3381 #3379 Signed-off-by: José Valim <[email protected]>
1 parent 1a94935 commit d58ef57

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/elixir/lib/stream/reducers.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,16 @@ defmodule Stream.Reducers do
132132
defmacro take(f \\ nil) do
133133
quote do
134134
fn(entry, acc(h, n, t) = orig) ->
135-
if n >= 1 do
136-
cont_with_acc(unquote(f), entry, h, n-1, t)
137-
else
138-
{:halt, orig}
135+
case n do
136+
0 ->
137+
{:halt, orig}
138+
1 ->
139+
case cont_with_acc(unquote(f), entry, h, n-1, t) do
140+
{:cont, acc} -> {:halt, acc}
141+
reason -> reason
142+
end
143+
_ ->
144+
cont_with_acc(unquote(f), entry, h, n-1, t)
139145
end
140146
end
141147
end

0 commit comments

Comments
 (0)