diff --git a/executor/simple_test.go b/executor/simple_test.go index 81150aa317d46..0f2429fc76010 100644 --- a/executor/simple_test.go +++ b/executor/simple_test.go @@ -58,6 +58,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 7a29ab21bb110..394911c0580aa 100644 --- a/privilege/privileges/cache.go +++ b/privilege/privileges/cache.go @@ -1161,11 +1161,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 }