terraform-provider-cloudflare icon indicating copy to clipboard operation
terraform-provider-cloudflare copied to clipboard

Fix array manipulation bug with the zone settings resource.

Open turbomaze opened this issue 2 years ago • 5 comments

This PR addresses a potential bug with the cloudflare_zone_settings_override resource. I was reviewing the implementation before provisioning it, and I encountered a bit of array manipulation code that might contain a bug. I haven't observed a problem in practice, but out of caution, I don't want to apply this resource until I'm sure it won't cause any problems for my zone.

The potential bug relates to how zone settings that must be fetched as single zone settings are removed from the zone settings list. I think there's an off-by-one type of error happening as the zone settings array is mutated.

NOTE: the code in this PR has never been tested or even ran

turbomaze avatar Sep 22 '22 21:09 turbomaze

changelog detected :white_check_mark:

github-actions[bot] avatar Sep 22 '22 21:09 github-actions[bot]

@turbomaze are you able to add a test case that demonstrates the issue and the fix here? i'm not quite sure if this is actually changing the behaviour.

jacobbednarz avatar Sep 22 '22 21:09 jacobbednarz

Unfortunately I won't be able to dig into the actual provider code to put together a proper test case. But here's some Go code demonstrating the issue and the resolution:

package main

import "fmt"

func main() {
	// current behavior, buggy
	arr := []int{0, 1, 2, 3, 4}
	indexesToCut := []int{0, 1}
	fmt.Println("expected", []int{2, 3, 4})

	for _, indexToCut := range indexesToCut {
		arr = append(arr[:indexToCut], arr[indexToCut+1:]...)
	}
	fmt.Println("current", arr)

	// proposed behavior, not buggy
	arr = []int{0, 1, 2, 3, 4}
	indexesToCut = []int{0, 1}

	offset := 0
	for _, indexToCut := range indexesToCut {
		adjustedIndexToCut := indexToCut - offset
		arr = append(arr[:adjustedIndexToCut], arr[adjustedIndexToCut+1:]...)
		offset += 1
	}

	fmt.Println("proposed", arr)
}

Output:

expected [2 3 4]
current [1 3 4]
proposed [2 3 4]

turbomaze avatar Sep 22 '22 21:09 turbomaze

@turbomaze after you add the CHANGELOG entry (https://github.com/cloudflare/terraform-provider-cloudflare/pull/1925#issuecomment-1255585508) we can get this merged.

jacobbednarz avatar Sep 25 '22 22:09 jacobbednarz

@turbomaze after you add the CHANGELOG entry (#1925 (comment)) we can get this merged.

Should be good now, thanks

turbomaze avatar Sep 26 '22 00:09 turbomaze

This functionality has been released in v3.25.0 of the Terraform Cloudflare Provider.

Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

github-actions[bot] avatar Oct 04 '22 22:10 github-actions[bot]