<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>306226</bug_id>
          
          <creation_ts>2026-01-25 13:43:23 -0800</creation_ts>
          <short_desc>Fix -Wlifetime-safety-permissive warning in XPathNodeSet::findRootNode()</short_desc>
          <delta_ts>2026-01-26 14:00:13 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>XML</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=306244</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="David Kilzer (:ddkilzer)">ddkilzer</reporter>
          <assigned_to name="David Kilzer (:ddkilzer)">ddkilzer</assigned_to>
          <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2174937</commentid>
    <comment_count>0</comment_count>
    <who name="David Kilzer (:ddkilzer)">ddkilzer</who>
    <bug_when>2026-01-25 13:43:23 -0800</bug_when>
    <thetext>The compiler warning `-Wlifetime-safety-permissive` was triggered in `findRootNode()` when building with upstream clang in Source/WebCore/xml/XPathNodeSet.cpp:

```
OpenSource/Source/WebCore/xml/XPathNodeSet.cpp:191:20: error: object whose reference is captured does not live long enough [-Werror,-Wlifetime-safety-permissive]
  191 |             node = parent.get();
      |                    ^~~~~~
OpenSource/Source/WebCore/xml/XPathNodeSet.cpp:191:31: note: destroyed here
  191 |             node = parent.get();
      |                               ^
OpenSource/Source/WebCore/xml/XPathNodeSet.cpp:190:32: note: later used here
  190 |         while (RefPtr parent = node-&gt;parentNode())
      |                                ^~~~
```

The issue is in the while loop where `RefPtr parent` is declared in the condition, causing it to be destroyed and reconstructed on every iteration. After `node = parent.get()` executes, `parent` is destroyed, but then `node-&gt;parentNode()` is called in the next iteration with `node` potentially pointing to a destroyed object.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2174938</commentid>
    <comment_count>1</comment_count>
    <who name="David Kilzer (:ddkilzer)">ddkilzer</who>
    <bug_when>2026-01-25 13:43:24 -0800</bug_when>
    <thetext>The specific lifetime issue is fixed by changing from a `while` loop to a `for` loop pattern to avoid recreating the `RefPtr parent` variable on each iteration.

Current problematic code:

```
while (RefPtr parent = node-&gt;parentNode())
    node = parent.get();
```

Proposed fix:

```
for (RefPtr parent = current-&gt;parentNode(); parent; parent = current-&gt;parentNode())
    current = WTF::move(parent);
```

Why this works:

The `for` loop declares `parent` once and reuses it via assignment, maintaining object lifetime across iterations. When `current-&gt;parentNode()` is called in the increment expression, the previous `parent` value is still alive, preventing the lifetime safety issue.

Additional changes to support this fix:
1. Return `RefPtr&lt;Node&gt;` instead of `Node*` for proper lifetime management
2. Use `RefPtr&lt;Node&gt; current` throughout to maintain object lifetime  
3. Replace `&amp;node-&gt;document()` with `current-&gt;protectedDocument()` (returns `Ref&lt;Document&gt;`)
4. Properly transfer ownership with `WTF::move(parent)`</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2174939</commentid>
    <comment_count>2</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2026-01-25 13:43:31 -0800</bug_when>
    <thetext>&lt;rdar://problem/168880148&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2174953</commentid>
    <comment_count>3</comment_count>
    <who name="David Kilzer (:ddkilzer)">ddkilzer</who>
    <bug_when>2026-01-25 15:16:39 -0800</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/57221</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2175250</commentid>
    <comment_count>4</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2026-01-26 14:00:10 -0800</bug_when>
    <thetext>Committed 306240@main (7a9af69f9b93): &lt;https://commits.webkit.org/306240@main&gt;

Reviewed commits have been landed. Closing PR #57221 and removing active labels.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>