From a20dc8ddfdec5e1de233f3c9c63e1dd85cdcc19d Mon Sep 17 00:00:00 2001 From: imtbkcat Date: Fri, 20 Mar 2020 21:19:47 +0800 Subject: [PATCH] fix select current_role error --- executor/simple_test.go | 16 ++++++++++++++++ privilege/privileges/cache.go | 9 ++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/executor/simple_test.go b/executor/simple_test.go index 548b36d94042f..2e365de48053d 100644 --- a/executor/simple_test.go +++ b/executor/simple_test.go @@ -59,6 +59,22 @@ func (s *testSuite3) TestDo(c *C) { tk.MustQuery("select @a").Check(testkit.Rows("1")) } +func (s *testSuite3) TestSetRoleAllCorner(c *C) { + // For user with no role, `SET ROLE ALL` should active + // a empty slice, rather than nil. + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("create user set_role_all") + se, err := session.CreateSession4Test(s.store) + c.Check(err, IsNil) + defer se.Close() + c.Assert(se.Auth(&auth.UserIdentity{Username: "set_role_all", Hostname: "localhost"}, nil, nil), IsTrue) + ctx := context.Background() + _, err = se.Execute(ctx, `set role all`) + c.Assert(err, IsNil) + _, err = se.Execute(ctx, `select current_role`) + c.Assert(err, IsNil) +} + func (s *testSuite3) TestCreateRole(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("create user testCreateRole;") diff --git a/privilege/privileges/cache.go b/privilege/privileges/cache.go index f1110edfc5490..0d3af938f9e4a 100644 --- a/privilege/privileges/cache.go +++ b/privilege/privileges/cache.go @@ -1253,11 +1253,10 @@ func (p *MySQLPrivilege) getAllRoles(user, host string) []*auth.RoleIdentity { key := user + "@" + host edgeTable, ok := p.RoleGraph[key] ret := make([]*auth.RoleIdentity, 0, len(edgeTable.roleList)) - if !ok { - return nil - } - for _, r := range edgeTable.roleList { - ret = append(ret, r) + if ok { + for _, r := range edgeTable.roleList { + ret = append(ret, r) + } } return ret }