<story><title>Clang emits memcpy for std::swap, which can introduce undefined behavior</title><url>https://llvm.org/bugs/show_bug.cgi?id=27498</url></story><parent_chain><item><author>kazinator</author><text>The compiler is the implementation; it cannot &lt;i&gt;cause&lt;/i&gt; &amp;quot;undefined behavior&amp;quot;.&lt;p&gt;What it can be is &amp;quot;nonconforming&amp;quot;.&lt;p&gt;A correct program which hits that situation continues to have well-defined behavior (which we know from the text of the program and the standard).  Just the implementation isn&amp;#x27;t handling the requirements correctly; it is not conforming.&lt;p&gt;An implementation is not bound by the standard in its own use of the standard library functions. The implementation can use a function like memcpy in ways that would be formally undefined if they occured in user code; its implementation of memcpy just has to harmonize with that use, so that the intended behavior is ensured.
This is because implementations can add their own requirements to areas that the standard leaves undefined. For instance, an implementation can add the requirement to its memcpy that the copy may overlap, if the destination has a lower address. Then, the implementation can generate code which uses memcpy that way, or make internal uses of memcpy from other library functions which use it that way. It&amp;#x27;s just using its own (possibly not publicly documented) extension.</text></item></parent_chain><comment><author>xorblurb</author><text>&amp;gt; An implementation is not bound by the standard in its own use of the standard library functions. The implementation can use a function like memcpy in ways that would be formally undefined if they occured in user code; its implementation of memcpy just has to harmonize with that use, so that the intended behavior is ensured. This is because implementations can add their own requirements to areas that the standard leaves undefined. For instance, an implementation can add the requirement to its memcpy that the copy may overlap, if the destination has a lower address. Then, the implementation can generate code which uses memcpy that way, or make internal uses of memcpy from other library functions which use it that way. It&amp;#x27;s just using its own (possibly not publicly documented) extension.&lt;p&gt;Yes. But right now they do not do that, and it&amp;#x27;s not their implementation of memcpy they rely on, and they are exactly doing the kind of things they disallow their users to do, so the whole picture is just plain insane. They just call libc memcpy in some cases, and their rational is that &amp;quot;it happens to work&amp;quot;. Except nobody is actually sure about that, it might be already buggy on PPC, and the guarantee is not documented anywhere. Completely insane - especially given their very own approach in similar situations. As usual, they are prioritizing &amp;quot;optimizations&amp;quot; over correctness, and lets not even think about defense in depth. It&amp;#x27;s a brand new level of insanity, even beyond their usual &amp;quot;the standard allows us to do that&amp;quot; excuse, because here it does not even allow that, and it seems that neither do officially all the used implementations!&lt;p&gt;Software engineers must loose the habit of taking all kind of crazy risks that can only lead to marginally faster execution of newly introduced bugs. That does not serve any purpose.</text></comment>
<story><title>Clang emits memcpy for std::swap, which can introduce undefined behavior</title><url>https://llvm.org/bugs/show_bug.cgi?id=27498</url></story><parent_chain><item><author>kazinator</author><text>The compiler is the implementation; it cannot &lt;i&gt;cause&lt;/i&gt; &amp;quot;undefined behavior&amp;quot;.&lt;p&gt;What it can be is &amp;quot;nonconforming&amp;quot;.&lt;p&gt;A correct program which hits that situation continues to have well-defined behavior (which we know from the text of the program and the standard).  Just the implementation isn&amp;#x27;t handling the requirements correctly; it is not conforming.&lt;p&gt;An implementation is not bound by the standard in its own use of the standard library functions. The implementation can use a function like memcpy in ways that would be formally undefined if they occured in user code; its implementation of memcpy just has to harmonize with that use, so that the intended behavior is ensured.
This is because implementations can add their own requirements to areas that the standard leaves undefined. For instance, an implementation can add the requirement to its memcpy that the copy may overlap, if the destination has a lower address. Then, the implementation can generate code which uses memcpy that way, or make internal uses of memcpy from other library functions which use it that way. It&amp;#x27;s just using its own (possibly not publicly documented) extension.</text></item></parent_chain><comment><author>electrograv</author><text>Are you saying that Clang guarantees its own memcpy implementation will be used? I don&amp;#x27;t know; but it seems odd that if this is fully intended that the compiler&amp;#x2F;linker would generate warnings from its own handling of perfectly valid C code (as shown in the bug).</text></comment>