Skip to content

Commit

Permalink
br: fix get snapshot response pointer deref panic (#54510) (#54712)
Browse files Browse the repository at this point in the history
close #54511
  • Loading branch information
ti-chi-bot committed Aug 28, 2024
1 parent 6ea9184 commit 8607475
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion br/pkg/aws/ebs.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (e *EC2Session) WaitSnapshotsCreated(snapIDMap map[string]string, progress
log.Info("snapshot completed", zap.String("id", *s.SnapshotId))
totalVolumeSize += *s.VolumeSize
} else if *s.State == ec2.SnapshotStateError {
log.Error("snapshot failed", zap.String("id", *s.SnapshotId), zap.String("error", (*s.StateMessage)))
log.Error("snapshot failed", zap.String("id", *s.SnapshotId), zap.String("error", utils.GetOrZero(s.StateMessage)))
return 0, errors.Errorf("snapshot %s failed", *s.SnapshotId)
} else {
log.Debug("snapshot creating...", zap.Stringer("snap", s))
Expand Down
19 changes: 19 additions & 0 deletions br/pkg/aws/ebs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,25 @@ func TestWaitSnapshotsCreated(t *testing.T) {
expectedSize: 0,
expectErr: true,
},
{
desc: "snapshot failed w/out state message",
snapshotsOutput: ec2.DescribeSnapshotsOutput{
Snapshots: []*ec2.Snapshot{
{
SnapshotId: awsapi.String("snap-1"),
VolumeSize: awsapi.Int64(1),
State: awsapi.String(ec2.SnapshotStateCompleted),
},
{
SnapshotId: awsapi.String("snap-2"),
State: awsapi.String(ec2.SnapshotStateError),
StateMessage: nil,
},
},
},
expectedSize: 0,
expectErr: true,
},
{
desc: "snapshots pending",
snapshotsOutput: ec2.DescribeSnapshotsOutput{
Expand Down
1 change: 1 addition & 0 deletions br/pkg/utils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"math.go",
"misc.go",
"permission.go",
"pointer.go",
"pprof.go",
"progress.go",
"retry.go",
Expand Down
13 changes: 13 additions & 0 deletions br/pkg/utils/pointer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2022 PingCAP, Inc. Licensed under Apache-2.0.

package utils

// GetOrZero returns the value pointed to by p, or a zero value of
// its type if p is nil.
func GetOrZero[T any](p *T) T {
var zero T
if p == nil {
return zero
}
return *p
}

0 comments on commit 8607475

Please sign in to comment.