<?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>287431</bug_id>
          
          <creation_ts>2025-02-10 13:25:29 -0800</creation_ts>
          <short_desc>REGRESSION(288121@main): [WebAudio] DirectConvolver::process() tries to use use negative indexes on a std::span</short_desc>
          <delta_ts>2025-02-11 03:45:45 -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>Web Audio</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>DoNotImportToRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          <blocked>266396</blocked>
          <everconfirmed>1</everconfirmed>
          <reporter name="Adrian Perez">aperez</reporter>
          <assigned_to name="Adrian Perez">aperez</assigned_to>
          <cc>cdumez</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2093595</commentid>
    <comment_count>0</comment_count>
    <who name="Adrian Perez">aperez</who>
    <bug_when>2025-02-10 13:25:29 -0800</bug_when>
    <thetext>In 288121@main (bug #284897) the convolution code was changed from using a &quot;float*&quot; to a &quot;std::span&lt;float&gt;&quot;. The code previously used a float* pointing to the *middle* of a buffer, which was defined as:

    float* inputP = m_buffer.data() + m_inputBlockSize;

and then changed to:

    auto inputP = m_buffer.span().subspan(m_inputBlockSize);

In both cases above &quot;inputP&quot; points to the middle of &quot;m_buffer&quot;, because this is always initialized to have double the amount of elements of &quot;m_inputBlockSize&quot;:

    DirectConvolver::DirectConvolver(size_t inputBlockSize)
        : m_inputBlockSize(inputBlockSize)
        , m_buffer(inputBlockSize * 2)
    {
    }

Later, there is a loop which is roughly this, plus unrolled cases for a few kernel sizes, but the issue is the same in all of them:

    #define CONVOLVE_ONE_SAMPLE                                    \
        sum += inputP[i - j] * kernelP[j]; \
        j++;

    size_t i = 0;                                                                                                                                                                                                         
    while (i &lt; source.size()) {
        size_t j = 0;
        float sum = 0;
        while (j &lt; kernelSize) {
            CONVOLVE_ONE_SAMPLE
        }
        destination[i++] = sum;
    }

Inside the macro, the calculated &quot;i - j&quot; index *will be negative* most of the time, to pick elements from the *left* half of &quot;m_buffer&quot;. While this worked fine with the raw &quot;float*&quot;, indexing a &quot;std::span&lt;float&gt;&quot; will coerce the negative index into a &quot;size_t&quot; with the values wrapping around (due to underflow) and resulting into huge indexes. This triggers an assertion when the C++ library assertions are enabled (for example with the patch for bug #266396).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2093607</commentid>
    <comment_count>1</comment_count>
    <who name="Adrian Perez">aperez</who>
    <bug_when>2025-02-10 13:35:36 -0800</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/40371</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2093755</commentid>
    <comment_count>2</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2025-02-11 01:02:35 -0800</bug_when>
    <thetext>Committed 290202@main (19e127b1e23d): &lt;https://commits.webkit.org/290202@main&gt;

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

    </bug>

</bugzilla>