From 38bb2cf62e4c4a974d32e7ea5fe9f383994f5937 Mon Sep 17 00:00:00 2001 From: Giulio Iotti Date: Sat, 14 Mar 2015 15:34:55 +0200 Subject: [PATCH] do not allow empty database name, closes #1950 --- database.go | 4 ++++ server.go | 3 +++ server_test.go | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/database.go b/database.go index 1a03cc634c4..60e63c05da1 100644 --- a/database.go +++ b/database.go @@ -81,6 +81,10 @@ func (db *database) UnmarshalJSON(data []byte) error { return err } + if o.Name == "" { + return ErrDatabaseNameRequired + } + // Copy over properties from intermediate type. db.name = o.Name db.defaultRetentionPolicy = o.DefaultRetentionPolicy diff --git a/server.go b/server.go index f8f9d03dff4..f5facd8e449 100644 --- a/server.go +++ b/server.go @@ -747,6 +747,9 @@ func (s *Server) Databases() (a []string) { // CreateDatabase creates a new database. func (s *Server) CreateDatabase(name string) error { + if name == "" { + return ErrDatabaseNameRequired + } c := &createDatabaseCommand{Name: name} _, err := s.broadcast(createDatabaseMessageType, c) return err diff --git a/server_test.go b/server_test.go index 69849156eb0..eea7638feaf 100644 --- a/server_test.go +++ b/server_test.go @@ -290,6 +290,11 @@ func TestServer_CreateDatabase(t *testing.T) { s := OpenServer(NewMessagingClient()) defer s.Close() + // Attempt creating empty name database. + if err := s.CreateDatabase(""); err != influxdb.ErrDatabaseNameRequired { + t.Fatal("expected error on empty database name") + } + // Create the "foo" database. if err := s.CreateDatabase("foo"); err != nil { t.Fatal(err)