    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Static Analysis in GCC and Clang</title>
        <link>https://members.accu.org/index.php/articles/2811</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 32, #3 - July 2020</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c412/">323</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-412/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+412/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Static Analysis in GCC and Clang</h1>
<p><strong>Author:</strong>&nbsp;Bob Schmidt</p>
<p>
<strong>Date:</strong> 05 July 2020 17:27:42 +01:00 or Sun, 05 July 2020 17:27:42 +01:00</p>
<p><strong>Summary:</strong>&nbsp;Silas S. Brown shares some experiences of analysing code.</p>
<p><strong>Body:</strong>&nbsp;<p>Did you know that GCC version 10 (available in Debian 10, Fedora 32, etc) has a flag called <code>-fanalyzer</code> that turns on static analysis?</p>

<p>Clang has had a â€˜Clang Static Analyzerâ€™ since at least 2011, although this is not always included in the default Clang distribution (in GNU/Linux itâ€™s usually packaged separately under â€˜clang-toolsâ€™ or â€˜clang-analyzerâ€™). It can be run with the command <code>scan-build make</code> as long as your Makefile uses <code>$(CC)</code> and <code>$(CXX)</code> to compile C and C++ with the default compiler.</p>

<p>Static analysis can be likened to having extra-paranoid warnings. It can â€˜false positiveâ€™ and warn about things that are not in fact problems. I was incorrectly warned by GCC 10.1.1 that Listing 1 leaks memory (it didnâ€™t seem to realise the static global was kept for later), and Clang gave me a â€˜use after freeâ€™ warning on a branch that gives a pointer to <code>realloc()</code> but uses the original pointer if <code>realloc()</code> returned <code>NULL</code> (Clangâ€™s analyser seemed to think <code>realloc()</code> could both return <code>NULL</code> and also free the pointer, which is not according to the standard). Clang also warned me about null pointer dereferencing because it hadnâ€™t realised a function Iâ€™d called would, in the event of null pointer, return a false value that would stop the code from going down the branch that uses the pointer. (That function was in another module, so the analyser couldnâ€™t check it and had to assume it might return true when given null.) But the output of a static analyser can still be worth checking and thinking about: it found a couple of unused assignments in my code for example, and one place where there could be a division by zero given invalid user input.</p>

<pre class="programlisting">
  #include &lt;stdlib.h&gt;
  typedef struct { int a; } S;
  static S *sList;
  static int sLen;
  int alloc_sList(int n) {
    sList = calloc(n,sizeof(S));
    if(!sList) n = 0;
    sLen = n;
    return n;
  }</pre>
  
<p class="bio"><span class="author"><b>Silas S. Brown</b></span> Silas is a partially-sighted Computer Science post-doc in Cambridge who currently works in part-time assistant tuition and part-time for Oracle. He has been an ACCU member since 1994.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
