Skip to content

Commit dc5414d

Browse files
committed
sql/opt: fix RLS metadata clearing
This commit addresses two issues in the handling of RLS metadata in the sql/opt package: - The RLS metadata was not being properly cleared after calling the Clear() function. This function is only used in test. - Emitting RLS information during explain could theoretically result in a nil pointer dereference. While this path cannot be reached with a nil pointer under current conditions, the code was caught by an LLM. This change adds a defensive safeguard. Closes #153192 Release note: none Epic: none
1 parent dae24c3 commit dc5414d

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

pkg/sql/opt/exec/explain/emit.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,14 +1509,16 @@ func (e *emitter) emitPolicies(ob *OutputBuilder, table cat.Table, n *Node) {
15091509
ob.AddField("policies", "row-level security enabled, no policies applied.")
15101510
} else {
15111511
var sb strings.Builder
1512-
policies := table.Policies()
1513-
for _, grp := range [][]cat.Policy{policies.Permissive, policies.Restrictive} {
1514-
for _, policy := range grp {
1515-
if applied.Policies.Contains(policy.ID) {
1516-
if sb.Len() > 0 {
1517-
sb.WriteString(", ")
1512+
if table != nil {
1513+
policies := table.Policies()
1514+
for _, grp := range [][]cat.Policy{policies.Permissive, policies.Restrictive} {
1515+
for _, policy := range grp {
1516+
if applied.Policies.Contains(policy.ID) {
1517+
if sb.Len() > 0 {
1518+
sb.WriteString(", ")
1519+
}
1520+
sb.WriteString(policy.Name.Normalize())
15181521
}
1519-
sb.WriteString(policy.Name.Normalize())
15201522
}
15211523
}
15221524
}

pkg/sql/opt/memo/memo_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,12 @@ func TestMemoIsStale(t *testing.T) {
672672
stale()
673673
evalCtx.SessionData().UserProto = oldUser
674674
notStale()
675+
676+
// User changes (after RLS was reinitialized)
675677
o.Memo().Metadata().ClearRLSEnabled()
678+
evalCtx.SessionData().UserProto = newUser
679+
notStale()
680+
evalCtx.SessionData().UserProto = oldUser
676681
notStale()
677682

678683
// Stale row_security.

pkg/sql/opt/row_level_security.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ func (r *RowLevelSecurityMeta) MaybeInit(user username.SQLUsername, hasAdminRole
4848

4949
// Clear unsets the initialized property. This is used as a test helper.
5050
func (r *RowLevelSecurityMeta) Clear() {
51-
r = &RowLevelSecurityMeta{}
51+
if r == nil {
52+
return
53+
}
54+
*r = RowLevelSecurityMeta{}
5255
}
5356

5457
// AddTableUse indicates that an RLS-enabled table was encountered while

0 commit comments

Comments
 (0)