Skip to content

Commit fc11f69

Browse files
authored
Merge pull request #1197 from Gu1nness/fix/dont-null-pointer-exception
feat: Prevent SEGFAULT in Cast operations
2 parents 16ceda5 + 753d11d commit fc11f69

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
@@ -5,6 +5,7 @@ import (
55

66
"github.com/google/go-cmp/cmp"
77

8+
"github.com/percona/percona-backup-mongodb/pbm/storage"
89
"github.com/percona/percona-backup-mongodb/pbm/storage/azure"
910
"github.com/percona/percona-backup-mongodb/pbm/storage/fs"
1011
"github.com/percona/percona-backup-mongodb/pbm/storage/gcs"
@@ -207,6 +208,18 @@ func TestIsSameStorage(t *testing.T) {
207208
})
208209
}
209210

211+
func TestCastError(t *testing.T) {
212+
t.Run("S3", func(t *testing.T) {
213+
cfg := StorageConf{Type: storage.S3}
214+
215+
err := cfg.Cast()
216+
if err == nil {
217+
t.Errorf("Cast did not raise an error")
218+
}
219+
220+
})
221+
}
222+
210223
func boolPtr(b bool) *bool {
211224
return &b
212225
}

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 filesystem configuration with filesystem 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
@@ -44,7 +44,7 @@ const (
4444

4545
//nolint:lll
4646
type Config struct {
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"`
@@ -200,6 +200,9 @@ func (cfg *Config) IsSameStorage(other *Config) bool {
200200
}
201201

202202
func (cfg *Config) Cast() error {
203+
if cfg == nil {
204+
return errors.New("missing S3 configuration with S3 storage type")
205+
}
203206
if cfg.Region == "" {
204207
cfg.Region = defaultS3Region
205208
}

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)