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

Don't use append() on slices with unclear origin #2155

Merged
merged 1 commit into from
Oct 24, 2023

Conversation

mtrmac
Copy link
Collaborator

@mtrmac mtrmac commented Oct 23, 2023

If some code creates a slice with extra capacity, and provides it to more than one user which calls append(), those two users will write conflicting data to the same backing array.

So, use append(slices.Clone(...), ...) whenever the origin of the array backing a slice is unclear (or even in cases where it can be traced but the code is a bit remote, making it hard to maintain).

A demonstration of the problem:

package main

import (
	"fmt"
	"sync"
	"time"
)

func goroutine(wg *sync.WaitGroup, sharedSlice []int, i int) {
	defer wg.Done()

	privateSlice := append(sharedSlice, i)
	oldValue := privateSlice[len(privateSlice)-1]
	// This is already slow enough that the "full" value is frequently overwritten by another goroutine
	fmt.Printf("goroutine %d: immediate %d; full %#v@%v\n", i, oldValue, privateSlice, cap(privateSlice))

	time.Sleep(100 * time.Millisecond) // Give ample time for other goroutines to run

	newValue := privateSlice[len(privateSlice)-1]
	if newValue != i {
		panic(fmt.Sprintf("goroutine %d: immediate %d, after sleep %d; full %#v@%v", i, oldValue, newValue, privateSlice, cap(privateSlice)))
	}
}

func main() {
	sharedSlice := []int{0, 0, 0, 0}
	// Creates a backing array with extra capacity. (Alternatively, this could use the 3-argument version of make(),
	// but this demonstrates that this happens completely naturally just using append().)
	sharedSlice = append(sharedSlice, 0, 0)
	fmt.Printf("shared: %#v@%d\n", sharedSlice, cap(sharedSlice))

	wg := sync.WaitGroup{}
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go goroutine(&wg, sharedSlice, i)
	}
	wg.Wait()
}

Previously containers/ocicrypt#93 .

If some code creates a slice with extra capacity, and
provides it to more than one user which calls append(),
those two users will write conflicting data to the same
backing array.

So, use append(slices.Clone(...), ...) whenever the origin
of the array backing a slice is unclear (or even in cases
where it can be traced but the code is a bit remote, making it
hard to maintain).

A demonstration of the problem:

> package main
>
> import (
> 	"fmt"
> 	"sync"
> 	"time"
> )
>
> func goroutine(wg *sync.WaitGroup, sharedSlice []int, i int) {
> 	defer wg.Done()
>
> 	privateSlice := append(sharedSlice, i)
> 	oldValue := privateSlice[len(privateSlice)-1]
> 	// This is already slow enough that the "full" value is frequently overwritten by another goroutine
> 	fmt.Printf("goroutine %d: immediate %d; full %#v@%v\n", i, oldValue, privateSlice, cap(privateSlice))
>
> 	time.Sleep(100 * time.Millisecond) // Give ample time for other goroutines to run
>
> 	newValue := privateSlice[len(privateSlice)-1]
> 	if newValue != i {
> 		panic(fmt.Sprintf("goroutine %d: immediate %d, after sleep %d; full %#v@%v", i, oldValue, newValue, privateSlice, cap(privateSlice)))
> 	}
> }
>
> func main() {
> 	sharedSlice := []int{0, 0, 0, 0}
> 	// Creates a backing array with extra capacity. (Alternatively, this could use the 3-argument version of make(),
> 	// but this demonstrates that this happens completely naturally just using append().)
> 	sharedSlice = append(sharedSlice, 0, 0)
> 	fmt.Printf("shared: %#v@%d\n", sharedSlice, cap(sharedSlice))
>
> 	wg := sync.WaitGroup{}
> 	for i := 0; i < 10; i++ {
> 		wg.Add(1)
> 		go goroutine(&wg, sharedSlice, i)
> 	}
> 	wg.Wait()
> }

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
@rhatdan
Copy link
Member

rhatdan commented Oct 23, 2023

LGTM

Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

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

LGTM

@vrothberg vrothberg merged commit d788540 into containers:main Oct 24, 2023
9 checks passed
@mtrmac mtrmac deleted the append branch October 24, 2023 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants