<?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>230140</bug_id>
          
          <creation_ts>2021-09-09 17:33:47 -0700</creation_ts>
          <short_desc>Use of memcpy with overlapping memory pointers</short_desc>
          <delta_ts>2021-09-12 17:49:09 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Media</component>
          <version>Other</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="Jean-Yves Avenard [:jya]">jean-yves.avenard</reporter>
          <assigned_to name="Jean-Yves Avenard [:jya]">jean-yves.avenard</assigned_to>
          <cc>darin</cc>
    
    <cc>ddkilzer</cc>
    
    <cc>eric.carlson</cc>
    
    <cc>ews-watchlist</cc>
    
    <cc>glenn</cc>
    
    <cc>jer.noble</cc>
    
    <cc>philipj</cc>
    
    <cc>sergio</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1792182</commentid>
    <comment_count>0</comment_count>
    <who name="Jean-Yves Avenard [:jya]">jean-yves.avenard</who>
    <bug_when>2021-09-09 17:33:47 -0700</bug_when>
    <thetext>Problem introduced in bug 229251; the memory buffer content is shifted to the left; the use of memcpy under these circumstances is fine. But it triggers Asan &quot;AddressSanitizer detected: memcpy-param-overlap&quot;

Should change memcpy to memmove here to keep asan happy.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792183</commentid>
    <comment_count>1</comment_count>
    <who name="Jean-Yves Avenard [:jya]">jean-yves.avenard</who>
    <bug_when>2021-09-09 17:33:55 -0700</bug_when>
    <thetext>rdar://82946555</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792190</commentid>
    <comment_count>2</comment_count>
      <attachid>437814</attachid>
    <who name="Jean-Yves Avenard [:jya]">jean-yves.avenard</who>
    <bug_when>2021-09-09 17:56:05 -0700</bug_when>
    <thetext>Created attachment 437814
Patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792280</commentid>
    <comment_count>3</comment_count>
      <attachid>437814</attachid>
    <who name="David Kilzer (:ddkilzer)">ddkilzer</who>
    <bug_when>2021-09-10 02:14:36 -0700</bug_when>
    <thetext>Comment on attachment 437814
Patch

r=me</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792285</commentid>
    <comment_count>4</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2021-09-10 03:23:38 -0700</bug_when>
    <thetext>Committed r282263 (241541@main): &lt;https://commits.webkit.org/241541@main&gt;

All reviewed patches have been landed. Closing bug and clearing flags on attachment 437814.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792454</commentid>
    <comment_count>5</comment_count>
      <attachid>437814</attachid>
    <who name="Darin Adler">darin</who>
    <bug_when>2021-09-10 11:19:30 -0700</bug_when>
    <thetext>Comment on attachment 437814
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=437814&amp;action=review

&gt; Source/WebCore/ChangeLog:11
&gt; +        We use memcpy with overlapping pointers which triggers Asan. In practice,
&gt; +        with how memcpy was used the behaviour wasn&apos;t undefined and so would have
&gt; +        been fine.

I don’t understand the comment that memcpy would have been fine? Could you elaborate, just to satisfy my curiosity?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792455</commentid>
    <comment_count>6</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2021-09-10 11:20:52 -0700</bug_when>
    <thetext>(In reply to Jean-Yves Avenard [:jya] from comment #0)
&gt; the memory buffer content is shifted to
&gt; the left; the use of memcpy under these circumstances is fine

I see now: You are saying that typically memcpy works fine as long as you are moving left. Never thought of it that way, but I suppose as a practical matter that is so, but I am glad we fixed this because I don’t think it is OK to rely on that.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792484</commentid>
    <comment_count>7</comment_count>
      <attachid>437814</attachid>
    <who name="Darin Adler">darin</who>
    <bug_when>2021-09-10 12:18:10 -0700</bug_when>
    <thetext>Comment on attachment 437814
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=437814&amp;action=review

&gt; Source/WebCore/ChangeLog:10
&gt; +        with how memcpy was used the behaviour wasn&apos;t undefined and so would have

What you probably should have said is that even though technically the behavior was undefined, in practice real world implementations of memcpy work with overlapping ranges as long as you are moving &quot;left&quot;.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792661</commentid>
    <comment_count>8</comment_count>
      <attachid>437814</attachid>
    <who name="David Kilzer (:ddkilzer)">ddkilzer</who>
    <bug_when>2021-09-11 00:11:05 -0700</bug_when>
    <thetext>Comment on attachment 437814
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=437814&amp;action=review

&gt;&gt; Source/WebCore/ChangeLog:11
&gt;&gt; +        been fine.
&gt; 
&gt; I don’t understand the comment that memcpy would have been fine? Could you elaborate, just to satisfy my curiosity?

On Darwin platforms, memcpy() is implemented identically to memmove(), so there is no undefined behavior for overlapping regions.

This is not true on other platforms like Linux, though.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792819</commentid>
    <comment_count>9</comment_count>
      <attachid>437814</attachid>
    <who name="Darin Adler">darin</who>
    <bug_when>2021-09-12 16:45:30 -0700</bug_when>
    <thetext>Comment on attachment 437814
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=437814&amp;action=review

&gt;&gt;&gt; Source/WebCore/ChangeLog:11
&gt;&gt;&gt; +        been fine.
&gt;&gt; 
&gt;&gt; I don’t understand the comment that memcpy would have been fine? Could you elaborate, just to satisfy my curiosity?
&gt; 
&gt; On Darwin platforms, memcpy() is implemented identically to memmove(), so there is no undefined behavior for overlapping regions.
&gt; 
&gt; This is not true on other platforms like Linux, though.

Let’s not quibble, but &quot;undefined behavior&quot; is not about what the functions actually do in practice, but about what the C standard guarantees they will do.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1792821</commentid>
    <comment_count>10</comment_count>
      <attachid>437814</attachid>
    <who name="Jean-Yves Avenard [:jya]">jean-yves.avenard</who>
    <bug_when>2021-09-12 17:49:09 -0700</bug_when>
    <thetext>Comment on attachment 437814
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=437814&amp;action=review

&gt;&gt;&gt;&gt; Source/WebCore/ChangeLog:11
&gt;&gt;&gt;&gt; +        been fine.
&gt;&gt;&gt; 
&gt;&gt;&gt; I don’t understand the comment that memcpy would have been fine? Could you elaborate, just to satisfy my curiosity?
&gt;&gt; 
&gt;&gt; On Darwin platforms, memcpy() is implemented identically to memmove(), so there is no undefined behavior for overlapping regions.
&gt;&gt; 
&gt;&gt; This is not true on other platforms like Linux, though.
&gt; 
&gt; Let’s not quibble, but &quot;undefined behavior&quot; is not about what the functions actually do in practice, but about what the C standard guarantees they will do.

undefined behaviour is the easiest explanation that could be given if the user isn&apos;t paying attention on this matter.
I was merely pointing that there was no logic flaw in the original code: that is the end result would be wrong with rubbish data. Considering:
1- the code is only on mac, where memmove is always used
2- I&apos;m not aware of any libc&apos;s memcpy implementation or any compiler optimisation on any supported platforms where copying when dst &lt; src would cause unexpected results. memmove == memcpy whenever dst &lt; src. glibc calls memcpy explicitly  from memmove if that is so.

When I wrote the comment, my concern was that the code could be dangerous or produce rubbish, and I wrote it with that concern in mind. I&apos;d be more explicit next time. sorry for that.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>437814</attachid>
            <date>2021-09-09 17:56:05 -0700</date>
            <delta_ts>2021-09-10 03:23:39 -0700</delta_ts>
            <desc>Patch</desc>
            <filename>bug-230140-20210910105604.patch</filename>
            <type>text/plain</type>
            <size>2074</size>
            <attacher name="Jean-Yves Avenard [:jya]">jean-yves.avenard</attacher>
            
              <data encoding="base64">U3VidmVyc2lvbiBSZXZpc2lvbjogMjgyMTk2CmRpZmYgLS1naXQgYS9Tb3VyY2UvV2ViQ29yZS9D
aGFuZ2VMb2cgYi9Tb3VyY2UvV2ViQ29yZS9DaGFuZ2VMb2cKaW5kZXggMjMyYTBhNjVlOTFmODkx
MjllMmFjNWEzZGQ5Y2VmNzgxNzYzMGJlNy4uYTM2ODI4NzYzYTc3OTRjOTc4MmY5MGE1Y2YwN2Rl
MzI1YmNkMTUxNCAxMDA2NDQKLS0tIGEvU291cmNlL1dlYkNvcmUvQ2hhbmdlTG9nCisrKyBiL1Nv
dXJjZS9XZWJDb3JlL0NoYW5nZUxvZwpAQCAtMSwzICsxLDE5IEBACisyMDIxLTA5LTA5ICBKZWFu
LVl2ZXMgQXZlbmFyZCAgPGp5YUBhcHBsZS5jb20+CisKKyAgICAgICAgVXNlIG9mIG1lbWNweSB3
aXRoIG92ZXJsYXBwaW5nIG1lbW9yeSBwb2ludGVycworICAgICAgICBodHRwczovL2J1Z3Mud2Vi
a2l0Lm9yZy9zaG93X2J1Zy5jZ2k/aWQ9MjMwMTQwCisgICAgICAgIHJkYXI6Ly84Mjk0NjU1NQor
CisgICAgICAgIFJldmlld2VkIGJ5IE5PQk9EWSAoT09QUyEpLgorCisgICAgICAgIFdlIHVzZSBt
ZW1jcHkgd2l0aCBvdmVybGFwcGluZyBwb2ludGVycyB3aGljaCB0cmlnZ2VycyBBc2FuLiBJbiBw
cmFjdGljZSwKKyAgICAgICAgd2l0aCBob3cgbWVtY3B5IHdhcyB1c2VkIHRoZSBiZWhhdmlvdXIg
d2Fzbid0IHVuZGVmaW5lZCBhbmQ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