diff mbox

[wwwdocs] Update gcc-4.9/porting_to.html w.r.t null pointer checks

Message ID 20140313102019.GA23158@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely March 13, 2014, 10:20 a.m. UTC
Committed.
diff mbox

Patch

Index: htdocs/gcc-4.9/porting_to.html
===================================================================
RCS file: /cvs/gcc/wwwdocs/htdocs/gcc-4.9/porting_to.html,v
retrieving revision 1.4
diff -u -r1.4 porting_to.html
--- htdocs/gcc-4.9/porting_to.html	7 Mar 2014 19:45:14 -0000	1.4
+++ htdocs/gcc-4.9/porting_to.html	13 Mar 2014 10:17:07 -0000
@@ -60,6 +60,36 @@ 
 <code>#pragma omp end declare target</code> directive, this is now a parsing
 error.</p>
 
+<h3>Null pointer checks may be optimized away more aggressively</h3>
+
+<p> GCC might now optimize away the null pointer check in code like:</p>
+
+<pre><code>
+  int copy (int* dest, int* src, size_t nbytes) {
+    memmove (dest, src, nbytes);
+    if (src != NULL)
+      return *src;
+    return 0;
+  }
+</code></pre>
+
+<p>The pointers passed to <code>memmove</code> (and similar functions in
+<code>&lt;string.h&gt;</code>) must be non-null even when
+<code>nbytes==0</code>, so GCC can use that information to remove the check
+after the <code>memmove</code> call. Calling <code>copy(p, NULL, 0)</code>
+can therefore deference a null pointer and crash.</p>
+
+<p>The example above needs to be fixed to avoid the invalid
+<code>memmove</code> call, for example:</p>
+
+<pre><code>
+    if (nbytes != 0)
+      memmove (dest, src, nbytes);
+</code></pre>
+
+<p>This optimization can also affect implicit null pointer checks such as
+the one done by the C++ runtime for the <code>delete[]</code> operator.</p>
+
 <h2>C language issues</h2>
 
 <h3>Right operand of comma operator without effect</h3>