Skip to content

Commit d01272a

Browse files
authored
Fix panic of empty value in filter config (pingcap#799)
Fix panic of empty value in filter config Verify config about filters with empty schema or table name.
1 parent 041c049 commit d01272a

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

drainer/config.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,43 @@ func (cfg *Config) configFromFile(path string) error {
226226
return util.StrictDecodeFile(path, "drainer", cfg)
227227
}
228228

229+
func (cfg *Config) validateFilter() error {
230+
for _, db := range cfg.SyncerCfg.DoDBs {
231+
if len(db) == 0 {
232+
return errors.New("empty schema name in `replicate-do-db` config")
233+
}
234+
}
235+
236+
dbs := strings.Split(cfg.SyncerCfg.IgnoreSchemas, ",")
237+
for _, db := range dbs {
238+
if len(db) == 0 {
239+
return errors.New("empty schema name in `ignore-schemas` config")
240+
}
241+
}
242+
243+
for _, tb := range cfg.SyncerCfg.DoTables {
244+
if len(tb.Schema) == 0 {
245+
return errors.New("empty schema name in `replicate-do-table` config")
246+
}
247+
248+
if len(tb.Table) == 0 {
249+
return errors.New("empty table name in `replicate-do-table` config")
250+
}
251+
}
252+
253+
for _, tb := range cfg.SyncerCfg.IgnoreTables {
254+
if len(tb.Schema) == 0 {
255+
return errors.New("empty schema name in `ignore-table` config")
256+
}
257+
258+
if len(tb.Table) == 0 {
259+
return errors.New("empty table name in `ignore-table` config")
260+
}
261+
}
262+
263+
return nil
264+
}
265+
229266
// validate checks whether the configuration is valid
230267
func (cfg *Config) validate() error {
231268
if err := validateAddr(cfg.ListenAddr); err != nil {
@@ -254,7 +291,7 @@ func (cfg *Config) validate() error {
254291
}
255292
}
256293

257-
return nil
294+
return cfg.validateFilter()
258295
}
259296

260297
func (cfg *Config) adjustConfig() error {

drainer/config_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
. "github.com/pingcap/check"
2626
"github.com/pingcap/parser/mysql"
2727
dsync "github.com/pingcap/tidb-binlog/drainer/sync"
28+
"github.com/pingcap/tidb-binlog/pkg/filter"
2829
"github.com/pingcap/tidb-binlog/pkg/util"
2930
pkgzk "github.com/pingcap/tidb-binlog/pkg/zk"
3031
"github.com/samuel/go-zookeeper/zk"
@@ -67,6 +68,38 @@ func (t *testDrainerSuite) TestConfig(c *C) {
6768
c.Assert(cfg.SyncerCfg.SQLMode, Equals, mysql.SQLMode(0))
6869
}
6970

71+
func (t *testDrainerSuite) TestValidateFilter(c *C) {
72+
cfg := NewConfig()
73+
c.Assert(cfg.validateFilter(), IsNil)
74+
75+
cfg = NewConfig()
76+
cfg.SyncerCfg.DoDBs = []string{""}
77+
c.Assert(cfg.validateFilter(), NotNil)
78+
79+
cfg = NewConfig()
80+
cfg.SyncerCfg.IgnoreSchemas = "a,,c"
81+
c.Assert(cfg.validateFilter(), NotNil)
82+
83+
emptyScheme := []filter.TableName{{Schema: "", Table: "t"}}
84+
emptyTable := []filter.TableName{{Schema: "s", Table: ""}}
85+
86+
cfg = NewConfig()
87+
cfg.SyncerCfg.DoTables = emptyScheme
88+
c.Assert(cfg.validateFilter(), NotNil)
89+
90+
cfg = NewConfig()
91+
cfg.SyncerCfg.DoTables = emptyTable
92+
c.Assert(cfg.validateFilter(), NotNil)
93+
94+
cfg = NewConfig()
95+
cfg.SyncerCfg.IgnoreTables = emptyScheme
96+
c.Assert(cfg.validateFilter(), NotNil)
97+
98+
cfg = NewConfig()
99+
cfg.SyncerCfg.IgnoreTables = emptyTable
100+
c.Assert(cfg.validateFilter(), NotNil)
101+
}
102+
70103
func (t *testDrainerSuite) TestValidate(c *C) {
71104
cfg := NewConfig()
72105

pkg/filter/filter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ func NewFilter(ignoreDBs []string, ignoreTables []TableName, doDBs []string, doT
4848
func (s *Filter) addOneRegex(originStr string) {
4949
if _, ok := s.reMap[originStr]; !ok {
5050
var re *regexp.Regexp
51-
if originStr[0] != '~' {
52-
re = regexp.MustCompile(fmt.Sprintf("(?i)^%s$", originStr))
53-
} else {
51+
if len(originStr) > 0 && originStr[0] == '~' {
5452
re = regexp.MustCompile(fmt.Sprintf("(?i)%s", originStr[1:]))
53+
} else { // must match completely
54+
re = regexp.MustCompile(fmt.Sprintf("(?i)^%s$", originStr))
5555
}
5656
s.reMap[originStr] = re
5757
}

pkg/filter/filter_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ func (t *testFilterSuite) TestFilter(c *C) {
4949
filter = NewFilter(nil, ignoreTables, nil, nil)
5050
c.Assert(filter.SkipSchemaAndTable("ignore", "ignore"), IsTrue)
5151
c.Assert(filter.SkipSchemaAndTable("not_ignore", "not_ignore"), IsFalse)
52+
53+
// with empty string
54+
filter = NewFilter(nil, nil, []string{""} /*doDBs*/, nil)
55+
c.Assert(filter.SkipSchemaAndTable("", "any"), IsFalse)
56+
c.Assert(filter.SkipSchemaAndTable("any", ""), IsTrue)
5257
}

0 commit comments

Comments
 (0)