<?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>306673</bug_id>
          
          <creation_ts>2026-01-30 17:33:57 -0800</creation_ts>
          <short_desc>[ITP] Crash in ResourceLoadStatisticsStore::ensureResourceStatisticsForRegistrableDomain due to unsafe std::optional access</short_desc>
          <delta_ts>2026-02-02 18:37:42 -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>Website Storage</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <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>sihui_liu</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2176764</commentid>
    <comment_count>0</comment_count>
    <who name="David Kilzer (:ddkilzer)">ddkilzer</who>
    <bug_when>2026-01-30 17:33:57 -0800</bug_when>
    <thetext>ResourceLoadStatisticsStore::ensureResourceStatisticsForRegistrableDomain() crashes with `std::bad_optional_access` when calling `.value()` on an empty `std::optional&lt;unsigned&gt;` returned by `domainID(domain)`.

The crash occurs when:
1. `insertObservedDomain()` successfully inserts a domain into the database
2. `domainID(domain)` immediately fails to find the just-inserted domain, returning `std::nullopt`  
3. Calling `.value()` on the empty optional throws `std::bad_optional_access`
4. C++ runtime calls `abort()`, terminating the process

Stack trace:
```
0   libsystem_c.dylib:  abort
1   libc++.1.dylib:  std::bad_optional_access
2   libc++.1.dylib:  std::optional&lt;unsigned&gt;::value
3   WebKit:  WebKit::ResourceLoadStatisticsStore::domainID
4   WebKit:  WebKit::ResourceLoadStatisticsStore::insertObservedDomain
5   WebKit:  WebKit::ResourceLoadStatisticsStore::ensureResourceStatisticsForRegistrableDomain
6   WebKit:  WebKit::ResourceLoadStatisticsStore::setIsScheduledForAllScriptWrittenStorageRemoval
[...]
```

&lt;rdar://167532652&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2176765</commentid>
    <comment_count>1</comment_count>
    <who name="David Kilzer (:ddkilzer)">ddkilzer</who>
    <bug_when>2026-01-30 17:33:58 -0800</bug_when>
    <thetext>The fix replaces the unsafe `.value()` call with proper optional checking and error recovery. Also fixes a misleading error message.

Testing notes: Unable to write a test since it would require removing a just-inserted domain from the database using an unknown mechanism external to ensureResourceStatisticsForRegistrableDomain().

No change in behavior for successful operations, only improves error handling for edge cases.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2176772</commentid>
    <comment_count>2</comment_count>
    <who name="David Kilzer (:ddkilzer)">ddkilzer</who>
    <bug_when>2026-01-30 18:16:08 -0800</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/57608</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2177379</commentid>
    <comment_count>3</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2026-02-02 18:37:40 -0800</bug_when>
    <thetext>Committed 306672@main (215b7246a621): &lt;https://commits.webkit.org/306672@main&gt;

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

    </bug>

</bugzilla>