Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

SlicesAsSets option behaves poorly with duplicate items in the slice #36

Open
tzneal opened this issue Mar 4, 2022 · 3 comments
Open

Comments

@tzneal
Copy link

tzneal commented Mar 4, 2022

If two slices differ only by an item that is repeated in both, it's trivial to construct hash collisions.

package main

import (
	"fmt"

	hashstructure "github.com/mitchellh/hashstructure/v2"
)

func main() {
	// clearly two different lists
	list1 := []string{"a", "b", "c", "e", "e"}
	list2 := []string{"a", "b", "c", "d", "d"}

	// with the same hash
	fmt.Println(hashstructure.Hash(list1, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true}))
	fmt.Println(hashstructure.Hash(list2, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true}))
}

// output
// 12638153115695167423 <nil>
// 12638153115695167423 <nil>
tzneal added a commit to tzneal/hashstructure that referenced this issue Mar 4, 2022
@jpalawaga
Copy link

The code sample looks correct to me—if the slice list members are the same and the set option is turned on, then the two arrays should produce the same hash.

If this happens with the set option off, then there is clearly a bug. Or perhaps, I do not understand the set option

@tzneal
Copy link
Author

tzneal commented Oct 3, 2022

They're not the same though, look at the last two members of each slice.

list1 := []string{"a", "b", "c", "e", "e"}
list2 := []string{"a", "b", "c", "d", "d"}

@jpalawaga
Copy link

My apologies, I apparently can't see straight.

michaelbeaumont added a commit to michaelbeaumont/hashstructure that referenced this issue Jan 5, 2023
michaelbeaumont added a commit to michaelbeaumont/hashstructure that referenced this issue Jan 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants