Skip to content

Commit 3178b34

Browse files
committed
feat: Prevent SEGFAULT in Cast operations
1 parent 29437a3 commit 3178b34

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

pbm/config/config_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"testing"
55

6+
"github.com/percona/percona-backup-mongodb/pbm/storage"
67
"github.com/percona/percona-backup-mongodb/pbm/storage/azure"
78
"github.com/percona/percona-backup-mongodb/pbm/storage/gcs"
89
"github.com/percona/percona-backup-mongodb/pbm/storage/s3"
@@ -125,6 +126,18 @@ func TestIsSameStorage(t *testing.T) {
125126
})
126127
}
127128

129+
func TestCastError(t *testing.T) {
130+
t.Run("S3", func(t *testing.T) {
131+
cfg := StorageConf{Type: storage.S3}
132+
133+
err := cfg.Cast()
134+
if err == nil {
135+
t.Errorf("Cast did not raise an error")
136+
}
137+
138+
})
139+
}
140+
128141
func boolPtr(b bool) *bool {
129142
return &b
130143
}

pbm/storage/fs/fs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ func (cfg *Config) Equal(other *Config) bool {
4747
}
4848

4949
func (cfg *Config) Cast() error {
50+
if cfg == nil {
51+
return errors.New("Missing blackhole configuration with blackhole storage type.")
52+
}
5053
if cfg.Path == "" {
5154
return errors.New("path can't be empty")
5255
}

pbm/storage/s3/s3.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const (
4444
//nolint:lll
4545
type Config struct {
4646
Provider string `bson:"provider,omitempty" json:"provider,omitempty" yaml:"provider,omitempty"`
47-
Region string `bson:"region" json:"region" yaml:"region"`
47+
Region string `bson:"region,omitempty" json:"region,omitempty" yaml:"region,omitempty"`
4848
EndpointURL string `bson:"endpointUrl,omitempty" json:"endpointUrl" yaml:"endpointUrl,omitempty"`
4949
EndpointURLMap map[string]string `bson:"endpointUrlMap,omitempty" json:"endpointUrlMap,omitempty" yaml:"endpointUrlMap,omitempty"`
5050
ForcePathStyle *bool `bson:"forcePathStyle,omitempty" json:"forcePathStyle,omitempty" yaml:"forcePathStyle,omitempty"`
@@ -207,6 +207,9 @@ func (cfg *Config) IsSameStorage(other *Config) bool {
207207
}
208208

209209
func (cfg *Config) Cast() error {
210+
if cfg == nil {
211+
return errors.New("Missing S3 configuration with S3 storage type.")
212+
}
210213
if cfg.Region == "" {
211214
cfg.Region = defaultS3Region
212215
}

pbm/storage/s3/s3_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ func TestConfig(t *testing.T) {
149149
t.Error("expected not to be equal when updating credentials")
150150
}
151151
})
152+
153+
t.Run("Cast succeeds", func(t *testing.T) {
154+
if opts.Region != "" {
155+
t.Error("Start value is not ''")
156+
}
157+
opts.Cast()
158+
159+
if opts.Region != "us-east-1" {
160+
t.Error("Default value should be set on Cast")
161+
}
162+
})
152163
}
153164

154165
func TestRetryer(t *testing.T) {

0 commit comments

Comments
 (0)