Skip to content
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

Preventing panics of RecoverableError casts #2267

Merged
merged 4 commits into from
Feb 1, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions nomad/node_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,9 @@ func (n *Node) DeriveVaultToken(args *structs.DeriveVaultTokenRequest,
// setErr is a helper for setting the recoverable error on the reply and
// logging it
setErr := func(e error, recoverable bool) {
reply.Error = structs.NewRecoverableError(e, recoverable).(*structs.RecoverableError)
if rerr, ok := structs.NewRecoverableError(e, recoverable).(*structs.RecoverableError); ok {
Copy link
Member

Choose a reason for hiding this comment

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

This approach will still log an error even when e is nil.

Instead I would just check if e is nil and return early:

setErr := func(...) {
    if e == nil { return }

    // ...existing code...
}

Copy link
Author

Choose a reason for hiding this comment

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

Oh, right. nil is "castable" to *structs.RecoverableError.

reply.Error = rerr
}
n.srv.logger.Printf("[ERR] nomad.client: DeriveVaultToken failed (recoverable %v): %v", recoverable, e)
}

Expand Down Expand Up @@ -1133,8 +1135,8 @@ func (n *Node) DeriveVaultToken(args *structs.DeriveVaultTokenRequest,

if rerr, ok := createErr.(*structs.RecoverableError); ok {
reply.Error = rerr
} else {
reply.Error = structs.NewRecoverableError(createErr, false).(*structs.RecoverableError)
} else if rerr, ok = structs.NewRecoverableError(createErr, false).(*structs.RecoverableError); ok {
reply.Error = rerr
}

return nil
Expand Down