Skip to content

Commit

Permalink
br: fix get snapshot response pointer deref panic (#54510) (#54714)
Browse files Browse the repository at this point in the history
close #54511
  • Loading branch information
ti-chi-bot authored Aug 5, 2024
1 parent ef0bc1e commit 52f415d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions br/pkg/aws/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
deps = [
"//br/pkg/config",
"//br/pkg/glue",
"//br/pkg/utils",
"//pkg/util",
"@com_github_aws_aws_sdk_go//aws",
"@com_github_aws_aws_sdk_go//aws/awserr",
Expand Down
3 changes: 2 additions & 1 deletion br/pkg/aws/ebs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/pingcap/log"
"github.com/pingcap/tidb/br/pkg/config"
"github.com/pingcap/tidb/br/pkg/glue"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/pkg/util"
"go.uber.org/atomic"
"go.uber.org/zap"
Expand Down Expand Up @@ -252,7 +253,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 @@ -11,6 +11,7 @@ go_library(
"key.go",
"misc.go",
"permission.go",
"pointer.go",
"pprof.go",
"progress.go",
"register.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 52f415d

Please sign in to comment.