-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Fix nil pointer dereference in backend state migration #38028
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
base: main
Are you sure you want to change the base?
Fix nil pointer dereference in backend state migration #38028
Conversation
Fixes a nil pointer dereference panic that occurs during backend migration when StateMgr returns an error other than ErrDefaultWorkspaceNotSupported. The bug occurred because the code only checked for the specific ErrDefaultWorkspaceNotSupported error. When any other error occurred (such as permission errors like storage.objects.get access denied), destinationState remained nil, but the code continued and attempted to call destinationState.RefreshState(), causing a panic. This fix adds an else clause to catch and return all other errors from StateMgr, preventing the nil pointer dereference and providing users with a clear error message instead of a crash. Fixes hashicorp#24100
|
Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement Learn more about why HashiCorp requires a CLA and what the CLA includes Have you signed the CLA already but the status is still pending? Recheck it. |
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
|
Thanks for this submission! I will raise it in triage if it is not handled before then. |
radeksimko
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation looks good to me, as I mentioned in the linked issue. I have raised some further changes (#38033) that should enable us to test this scenario more thoroughly and prevent similar issues in the future. I will let that PR be reviewed first by other maintainers and come back to this.
Thank you for the fix and for the patience.
| @@ -0,0 +1,5 @@ | |||
| kind: BUG FIXES | |||
| body: 'backend: Fix nil pointer dereference crash during `terraform init -migrate-state` when the destination backend returns a permission error' | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| body: 'backend: Fix nil pointer dereference crash during `terraform init -migrate-state` when the destination backend returns a permission error' | |
| body: 'backend: Fix nil pointer dereference crash during `terraform init` when the destination backend returns an error' |
As far as I can tell this is reproducible without the flag and applies to any kind of error.
Summary
Fixes a nil pointer dereference crash that occurs during
terraform init -migrate-statewhen the destination backend returns an error other thanErrDefaultWorkspaceNotSupported.Root Cause: In
meta_backend_migrate.go, whenStateMgr()returns an error, the code only handlesErrDefaultWorkspaceNotSupportedin the if-branch. For any other error (e.g., 403 permission denied),destinationStateremains nil, and the subsequent call todestinationState.RefreshState()causes a panic.Fix: Added an else clause to return the error immediately for any non-workspace-related errors, preventing the nil pointer dereference.
Fixes #38027
Test Plan
terraform initto create local stateterraform init -migrate-statepanic: runtime error: invalid memory address or nil pointer dereferenceError loading state: googleapi: Error 403: ... does not have storage.objects.list accessRelated Issues
🤖 Generated with Claude Code