vault icon indicating copy to clipboard operation
vault copied to clipboard

fix incorrect use of loop variable

Open renatolabs opened this issue 2 years ago • 3 comments

This fixes a couple of references to loop variables in parallel tests and deferred functions. When running a parallel test (calling t.Parallel()) combined with the table-driven pattern, it's necessary to copy the test case loop variable, otherwise only the last test case is exercised. This is documented in the testing package:

https://pkg.go.dev/testing#hdr-Subtests_and_Sub_benchmarks

defer statements that invoke a closure should also not reference a loop variable directly as the referenced value will change in each iteration of the loop.

Issues were automatically found with the loopvarcapture linter.

renatolabs avatar Aug 24 '22 16:08 renatolabs

CLA assistant check
All committers have signed the CLA.

hashicorp-cla avatar Aug 24 '22 16:08 hashicorp-cla

Hi @renatolabs, thanks for your submission! Please could I get you to add a change log entry (https://github.com/hashicorp/vault/blob/main/CONTRIBUTING.md#changelog-entries)?

peteski22 avatar Sep 07 '22 13:09 peteski22

Hi @renatolabs, thanks for your submission! Please could I get you to add a change log entry (https://github.com/hashicorp/vault/blob/main/CONTRIBUTING.md#changelog-entries)?

Done, thank you for the reminder!

renatolabs avatar Sep 07 '22 20:09 renatolabs

Seems like the test failures are not flukes. I'm able to reproduce them when running the pull-request locally with make test. I'd be ok to approve the changes if we can get a green test run.

I tried the proposed changes on the tip of hashicorp/vault and it seems to pass. Would it be possible to rebase to a newer commit to see if it does the trick? Thanks again.

maxcoulombe avatar Sep 26 '22 15:09 maxcoulombe

Seems like the test failures are not flukes. I'm able to reproduce them when running the pull-request locally with make test. I'd be ok to approve the changes if we can get a green test run.

I tried the proposed changes on the tip of hashicorp/vault and it seems to pass. Would it be possible to rebase to a newer commit to see if it does the trick? Thanks again.

Rebased 👍

renatolabs avatar Sep 26 '22 20:09 renatolabs

Looks like there's a panic, which I think is introduced by this change:

[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x43f9231]

goroutine 24844 [running]:
github.com/hashicorp/vault/command.(*ServerCommand).Run.func2(0xc000095400?)
	/home/circleci/go/src/github.com/hashicorp/vault/command/server.go:1333 +0x31
github.com/hashicorp/vault/command.(*ServerCommand).Run(0xc00120b860, {0xc002ab21e0, 0x2, 0x2})
	/home/circleci/go/src/github.com/hashicorp/vault/command/server.go:1773 +0x41e5
github.com/hashicorp/vault/command.TestServer_ReloadListener.func1()
	/home/circleci/go/src/github.com/hashicorp/vault/command/server_test.go:140 +0x5f
created by github.com/hashicorp/vault/command.TestServer_ReloadListener
	/home/circleci/go/src/github.com/hashicorp/vault/command/server_test.go:139 +0x56c

ncabatoff avatar Sep 29 '22 17:09 ncabatoff

I suppose this is related to this section

// There is always one nil seal. We need to skip it so we don't start an empty Finalize-Seal-Shamir
// section.
if seal == nil {
    continue
}

which exists in command/operator_diagnose.go but not in command/server.go. So in that sense, this commit exposed (rather than introduced) the issue.

But I'll let folks with more knowledge of the codebase to weigh in; let me know if you want me to any changes here!

renatolabs avatar Sep 30 '22 14:09 renatolabs

But I'll let folks with more knowledge of the codebase to weigh in; let me know if you want me to any changes here!

Well, I agree with your analysis: you've exposed a bug, not introduced one. And I agree it makes sense to check for nil seals in that loop so we don't panic.

ncabatoff avatar Oct 03 '22 13:10 ncabatoff

But I'll let folks with more knowledge of the codebase to weigh in; let me know if you want me to any changes here!

Well, I agree with your analysis: you've exposed a bug, not introduced one. And I agree it makes sense to check for nil seals in that loop so we don't panic.

Added a similar check to command/server.go 👍

renatolabs avatar Oct 03 '22 20:10 renatolabs

Thanks @renatolabs !

ncabatoff avatar Oct 04 '22 13:10 ncabatoff