Skip to content

Conversation

@Mark-C-Hall
Copy link

Summary

Fixes a nil pointer dereference crash that occurs during terraform init -migrate-state when the destination backend returns an error other than ErrDefaultWorkspaceNotSupported.

Root Cause: In meta_backend_migrate.go, when StateMgr() returns an error, the code only handles ErrDefaultWorkspaceNotSupported in the if-branch. For any other error (e.g., 403 permission denied), destinationState remains nil, and the subsequent call to destinationState.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

  1. Configure a GCS backend where you have permission to create the bucket but not write to it
  2. Run terraform init to create local state
  3. Run terraform init -migrate-state
  4. Before fix: Crash with panic: runtime error: invalid memory address or nil pointer dereference
  5. After fix: Clean error message: Error loading state: googleapi: Error 403: ... does not have storage.objects.list access

Related Issues

🤖 Generated with Claude Code

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
@Mark-C-Hall Mark-C-Hall requested a review from a team as a code owner December 23, 2025 18:42
@hashicorp-cla-app
Copy link

hashicorp-cla-app bot commented Dec 23, 2025

CLA assistant check
All committers have signed the CLA.

@hashicorp-cla-app
Copy link

CLA assistant check

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.

@crw
Copy link
Contributor

crw commented Dec 29, 2025

Thanks for this submission! I will raise it in triage if it is not handled before then.

Copy link
Member

@radeksimko radeksimko left a 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'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Terraform crashes with nil pointer dereference during backend migration when permission errors occur

3 participants