Skip to content
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

Consider to make sure append always copies the input slice #10458

Closed
bep opened this issue Nov 19, 2022 · 2 comments
Closed

Consider to make sure append always copies the input slice #10458

bep opened this issue Nov 19, 2022 · 2 comments
Assignees
Milestone

Comments

@bep
Copy link
Member

bep commented Nov 19, 2022

Go's append (and reflect.Append) reuses the input slice if there is enough capacity, else it creates a new one and copies the content over.

The reason we have not seen this bug before is probably

  1. Because in most common cases where you use append you're working on a local slice and the current behaviour makes sense.
  2. append is used on slices returned from where etc.
  3. append is called enough elements so the capacity needs to be expanded.

Note that the other collection funcs (where, union etc.) always creates a new slice.

See https://discourse.gohugo.io/t/appending-a-page-to-a-page-collection-append-or-union/41532/10

@bep bep self-assigned this Nov 19, 2022
@bep bep added this to the v0.106.0 milestone Nov 19, 2022
@bep bep changed the title Investigate append issue Make sure append always copies the input slice Nov 20, 2022
@bep
Copy link
Member Author

bep commented Nov 20, 2022

@jmooring I will fixed this pretty fast, but I suspect that you can do:

{{ $pages := slice | append site.RegularPages }}
... now it should be safe to append more to the new slice.

@bep bep changed the title Make sure append always copies the input slice Consider to make sure append always copies the input slice Nov 20, 2022
@bep bep modified the milestones: v0.106.0, v0.108.0 Nov 25, 2022
@bep bep modified the milestones: v0.108.0, v0.109.0 Dec 14, 2022
@bep bep modified the milestones: v0.109.0, v0.111.0 Jan 26, 2023
@bep bep modified the milestones: v0.111.0, v0.112.0 Feb 15, 2023
@bep bep modified the milestones: v0.112.0, v0.113.0 Apr 15, 2023
@bep bep modified the milestones: v0.113.0, v0.115.0, v0.114.0 Jun 13, 2023
@bep bep closed this as completed in f73c567 Jun 14, 2023
@github-actions
Copy link

github-actions bot commented Jul 6, 2023

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant