Skip to content

Commit aec66c4

Browse files
committed
feat: Prevent SEGFAULT in Cast operations
1 parent 45a8e9d commit aec66c4

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/fs"
89
"github.com/percona/percona-backup-mongodb/pbm/storage/gcs"
@@ -153,6 +154,18 @@ func TestIsSameStorage(t *testing.T) {
153154
})
154155
}
155156

157+
func TestCastError(t *testing.T) {
158+
t.Run("S3", func(t *testing.T) {
159+
cfg := StorageConf{Type: storage.S3}
160+
161+
err := cfg.Cast()
162+
if err == nil {
163+
t.Errorf("Cast did not raise an error")
164+
}
165+
166+
})
167+
}
168+
156169
func boolPtr(b bool) *bool {
157170
return &b
158171
}

pbm/storage/fs/fs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ func (cfg *Config) IsSameStorage(other *Config) bool {
6464
}
6565

6666
func (cfg *Config) Cast() error {
67+
if cfg == nil {
68+
return errors.New("Missing blackhole configuration with blackhole storage type.")
69+
}
6770
if cfg.Path == "" {
6871
return errors.New("path can't be empty")
6972
}

pbm/storage/s3/s3.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const (
4747
//nolint:lll
4848
type Config struct {
4949
Provider string `bson:"provider,omitempty" json:"provider,omitempty" yaml:"provider,omitempty"`
50-
Region string `bson:"region" json:"region" yaml:"region"`
50+
Region string `bson:"region,omitempty" json:"region,omitempty" yaml:"region,omitempty"`
5151
EndpointURL string `bson:"endpointUrl,omitempty" json:"endpointUrl" yaml:"endpointUrl,omitempty"`
5252
EndpointURLMap map[string]string `bson:"endpointUrlMap,omitempty" json:"endpointUrlMap,omitempty" yaml:"endpointUrlMap,omitempty"`
5353
ForcePathStyle *bool `bson:"forcePathStyle,omitempty" json:"forcePathStyle,omitempty" yaml:"forcePathStyle,omitempty"`
@@ -203,6 +203,9 @@ func (cfg *Config) IsSameStorage(other *Config) bool {
203203
}
204204

205205
func (cfg *Config) Cast() error {
206+
if cfg == nil {
207+
return errors.New("Missing S3 configuration with S3 storage type.")
208+
}
206209
if cfg.Region == "" {
207210
cfg.Region = defaultS3Region
208211
}

pbm/storage/s3/s3_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ func TestConfig(t *testing.T) {
162162
t.Error("expected not to be equal when updating credentials")
163163
}
164164
})
165+
166+
t.Run("Cast succeeds", func(t *testing.T) {
167+
if opts.Region != "" {
168+
t.Error("Start value is not ''")
169+
}
170+
opts.Cast()
171+
172+
if opts.Region != "us-east-1" {
173+
t.Error("Default value should be set on Cast")
174+
}
175+
})
165176
}
166177

167178
func TestRetryer(t *testing.T) {

0 commit comments

Comments
 (0)