Skip to content

Fixing edge cases in Enum.slide/3 #12052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2022
Merged

Conversation

sabiwara
Copy link
Contributor

@sabiwara sabiwara commented Aug 4, 2022

The first bug can happen on empty lists:

iex> Enum.slide([], 1, 2)
** (FunctionClauseError) no function clause matching in Enum.slide_list_start/4

    The following arguments were given to Enum.slide_list_start/4:

        # 1
        []

        # 2
        1

        # 3
        2

        # 4
        2

    Attempted function clauses (showing 2 out of 2):

        defp slide_list_start([h | t], start, middle, last) when start > 0 and start <= middle and middle <= last
        defp slide_list_start(list, 0, middle, last)

    (elixir 1.13.4) lib/enum.ex:2665: Enum.slide_list_start/4

The second bug is on a weird edge case because an assumed invariant (start <= stop) isn't actually checked (but only happens for some precise values of the insertion index):

iex> Enum.slide([1, 2, 3, 4], 2..1//1, -3)
** (FunctionClauseError) no function clause matching in Enum.slide_list_start/4

    The following arguments were given to Enum.slide_list_start/4:

        # 1
        [1, 2, 3, 4]

        # 2
        1

        # 3
        2

        # 4
        1

    Attempted function clauses (showing 2 out of 2):

        defp slide_list_start([h | t], start, middle, last) when start > 0 and start <= middle and middle <= last
        defp slide_list_start(list, 0, middle, last)

    (elixir 1.13.4) lib/enum.ex:2665: Enum.slide_list_start/4

Copy link
Member

@whatyouhide whatyouhide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catches @sabiwara 💟

@josevalim josevalim merged commit 57f3931 into elixir-lang:main Aug 4, 2022
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

josevalim pushed a commit that referenced this pull request Aug 4, 2022
* Fix bug in Enum.slide/3: empty lists

* Fix edge case in Enum.slide/3
@sabiwara sabiwara deleted the fix-slide-bug branch August 5, 2022 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants