<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Alex Astva</title>
    <description>The latest articles on Forem by Alex Astva (@alexastva).</description>
    <link>https://forem.com/alexastva</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F55595%2F99a02785-3624-43b8-9789-a4563c9eb1c2.jpg</url>
      <title>Forem: Alex Astva</title>
      <link>https://forem.com/alexastva</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alexastva"/>
    <language>en</language>
    <item>
      <title>How to shoot yourself in the foot in C and C++. Haiku OS Cookbook</title>
      <dc:creator>Alex Astva</dc:creator>
      <pubDate>Fri, 26 Jul 2019 07:09:48 +0000</pubDate>
      <link>https://forem.com/alexastva/how-to-shoot-yourself-in-the-foot-in-c-and-c-haiku-os-cookbook-1i5j</link>
      <guid>https://forem.com/alexastva/how-to-shoot-yourself-in-the-foot-in-c-and-c-haiku-os-cookbook-1i5j</guid>
      <description>&lt;p&gt;This story goes back to 2015, when Haiku OS and PVS-Studio static analyzer developers decided to join forces and improve this OS code quality. At first it was more like an experiment, as there was no Linux analyzer at that time and the team had to work only with the compiled executable analyzer file. The entire infrastructure for parsing compiler parameters, running preprocessor, analysis paralleling and so on was taken from the &lt;a href="https://www.viva64.com/en/m/0031/" rel="noopener noreferrer"&gt;Compiler Monitoring UI&lt;/a&gt; utility in C#, which was ported in parts to the Mono platform in order to be run in Linux.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FEAU3U7xXUAAN4EQ%3Fformat%3Djpg%26name%3Dlarge" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FEAU3U7xXUAAN4EQ%3Fformat%3Djpg%26name%3Dlarge" alt="HaikuOS"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This story goes back to 2015, when Haiku OS and PVS-Studio static analyzer developers decided to join forces and improve this OS code quality. At first it was more like an experiment, as there was no Linux analyzer at that time and the team had to work only with the compiled executable analyzer file. The entire infrastructure for parsing compiler parameters, running preprocessor, analysis paralleling and so on was taken from the &lt;a href="https://www.viva64.com/en/m/0031/" rel="noopener noreferrer"&gt;Compiler Monitoring UI&lt;/a&gt; utility in C#, which was ported in parts to the Mono platform in order to be run in Linux.&lt;/p&gt;

&lt;p&gt;Now the Haiku project is built using the cross compiler under various OSs, except Windows. Once again, I'd like to mention the convenience and documentation completeness related to Haiku OS building and thank Haiku OS developers for their help in building the project.&lt;/p&gt;

&lt;p&gt;Interestingly, the nature of programming errors is such that they don't disappear if you don't search for them and don't pay attention to the code quality. Haiku developers tried to use &lt;a href="https://scan.coverity.com/projects/haiku-32bit" rel="noopener noreferrer"&gt;Coverity Scan&lt;/a&gt;, but, most sadly, the last analysis run was almost two years ago. Even though the analysis has been configured in 2014 using Coverity, it didn't stop us from writing two long articles on errors review in 2015 (&lt;a href="https://www.viva64.com/en/b/0317/" rel="noopener noreferrer"&gt;part 1&lt;/a&gt;, &lt;a href="https://www.viva64.com/en/b/0318/" rel="noopener noreferrer"&gt;part 2&lt;/a&gt;). Now four years later, a new article about checking this project appears.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;here will be some interesting errors from the project, a more complete report can be checked out in the article "&lt;a href="https://www.viva64.com/en/b/0644/" rel="noopener noreferrer"&gt;&lt;em&gt;How to shoot yourself in the foot in C and C++. Haiku OS Cookbook&lt;/em&gt;&lt;/a&gt;"&lt;/em&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So let's move on to the errors:&lt;/p&gt;

&lt;h2&gt;
  
  
  Formal security
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.viva64.com/en/w/v597/" rel="noopener noreferrer"&gt;V597&lt;/a&gt; The compiler could delete the 'memset' function call, which is used to flush 'f_key' object. The memset_s() function should be used to erase the private data. dst_api.c 1018&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifndef SAFE_FREE
#define SAFE_FREE(a) \
do{if(a != NULL){memset(a,0, sizeof(*a)); free(a); a=NULL;}} while (0)
&lt;/span&gt;&lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="cp"&gt;#endif
&lt;/span&gt;
&lt;span class="n"&gt;DST_KEY&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="nf"&gt;dst_free_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DST_KEY&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f_key&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dk_func&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dk_func&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dk_KEY_struct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dk_func&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dk_KEY_struct&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;EREPORT&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="s"&gt;"dst_free_key(): Unknown key alg %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dk_alg&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dk_KEY_struct&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dk_KEY_struct&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dk_KEY_struct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dk_key_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;SAFE_FREE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;dk_key_name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;SAFE_FREE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f_key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The analyzer has detected suspicious code, meant for secure private data clearing. Unfortunately, the &lt;em&gt;SAFE_FREE*macro which expands into the *memset&lt;/em&gt;, &lt;em&gt;free*calls and *NULL*assignment doesn't make the code safer, as it's all removed by the compiler right when optimizing with *O2&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;By the way, it is nothing else but &lt;a href="https://cwe.mitre.org/data/definitions/14.html" rel="noopener noreferrer"&gt;CWE-14&lt;/a&gt;: Compiler Removal of Code to Clear Buffers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Miscellaneous
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.viva64.com/en/w/v645/" rel="noopener noreferrer"&gt;V645&lt;/a&gt; The 'strncat' function call could lead to the 'output' buffer overflow. The bounds should not contain the size of the buffer, but a number of characters it can hold. NamespaceDump.cpp 101&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="nf"&gt;dump_acpi_namespace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acpi_ns_device_info&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;indenting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;320&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;tabs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="n"&gt;strlcat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tabs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"|--- "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tabs&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(....)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;uint32&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;acpi&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;get_object_type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;snprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="s"&gt;"%s%s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tabs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;ACPI_TYPE_INTEGER&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;strncat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"     INTEGER"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;ACPI_TYPE_STRING&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;strncat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"     STRING"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;....&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference between *strlcat*and *strncat*functions is not very obvious for someone who is unfamiliar with the description of these functions. The *strlcat*function expects the size of the entire buffer as the third argument while the *strncat*function - the size of the free space in a buffer, which requires evaluating a needed value before calling the function. But developers often forget or don't know about it. Passing the entire buffer size to the *strncat*function can lead to buffer overflow, as the function will consider this value as an acceptable number of characters to copy. The *strlcat*function doesn't have such a problem. But you have to pass strings, ending with terminal null so that it worked properly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Errors with the free function
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.viva64.com/en/w/v575/" rel="noopener noreferrer"&gt;V575&lt;/a&gt; The null pointer is passed into 'free' function. Inspect the first argument. PackageFileHeapWriter.cpp 166&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;_GetBuffer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fBufferSize&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;fBuffers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;bad_alloc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Someone made an error here. The ||operator has to be used instead of &amp;amp;&amp;amp;. Only in this case the &lt;em&gt;std::bad_alloc()*exception will be thrown in case if memory allocation (using the *malloc&lt;/em&gt; function) failed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Errors with the delete operator
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.viva64.com/en/w/v611/" rel="noopener noreferrer"&gt;V611&lt;/a&gt; The memory was allocated using 'new T[]' operator but was released using the 'delete' operator. Consider inspecting this code. It's probably better to use 'delete [] fOutBuffer;'. Check lines: 26, 45. PCL6Rasterizer.h 26&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PCL6Rasterizer&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Rasterizer&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;PCL6Rasterizer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;fOutBuffer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;fOutBuffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;InitializeBuffer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fOutBuffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;uchar&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fOutBufferSize&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;uchar&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;fOutBuffer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt;    &lt;span class="n"&gt;fOutBufferSize&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a common error to use the &lt;em&gt;delete*operator instead of *delete[].&lt;/em&gt; It is easiest to make a mistake when writing a class, as the destructor's code is often far from the memory locations. Here, the programmer incorrectly frees the memory stored by the *fOutBuffer*pointer in the destructor.&lt;/p&gt;

&lt;p&gt;Follow our &lt;a href="https://www.viva64.com/en/b/" rel="noopener noreferrer"&gt;PVS-Studio team blog&lt;/a&gt; to see another Haiku OS errors review coming out soon for those who read the first part up to the end.   The full analyzer report will be sent to developers before posting this errors review, so some errors might be fixed by the time you're reading this. To pass the time between the articles, I suggest downloading and trying &lt;a href="https://www.viva64.com/en/pvs-studio-download/" rel="noopener noreferrer"&gt;PVS-Studio&lt;/a&gt; for your project.&lt;/p&gt;

</description>
      <category>haikuos</category>
      <category>osdev</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Every 4000 lines of Android code contain a potential vulnerability</title>
      <dc:creator>Alex Astva</dc:creator>
      <pubDate>Fri, 03 Aug 2018 13:56:09 +0000</pubDate>
      <link>https://forem.com/alexastva/every-4000-lines-of-android-code-contain-a-potential-vulnerability-37ak</link>
      <guid>https://forem.com/alexastva/every-4000-lines-of-android-code-contain-a-potential-vulnerability-37ak</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.viva64.com%2Fmedia%2Fimages%2Fcontent%2Fb%2F0579_Android%2Fimage2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.viva64.com%2Fmedia%2Fimages%2Fcontent%2Fb%2F0579_Android%2Fimage2.png" alt="Every 4000 lines of Android code contain a potential vulnerability"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Last year, developers of the static code analyzer PVS-Studio already cited the result of their research on the operating system &lt;a href="https://www.viva64.com/en/b/0519/" rel="noopener noreferrer"&gt;Tizen&lt;/a&gt; example and now again the choice fell on another, no less popular OS, Android. Looking ahead, I want to note that I didn't expect such a huge number of mistakes in such a popular product!&lt;/p&gt;

&lt;p&gt;In the article, mistakes carried in even such a high-quality codeare striped on examples, in fact even those tested by Coverity.&lt;/p&gt;

&lt;p&gt;In total, the developers described about 490 potential vulnerabilities. For example, CWE-14: Compiler Removal of Code to Clear Buffers, which is a serious type of potential vulnerability and leads to access to private data.&lt;/p&gt;

&lt;p&gt;Among the others you can find:&lt;br&gt;
• CWE-119: Improper Restriction of Operations within the Bounds of a Memory Buffer&lt;br&gt;
• CWE-393: Return of Wrong Status Code&lt;br&gt;
• CWE-480: Use of Incorrect Operator&lt;br&gt;
• CWE-561: Dead Code&lt;br&gt;
• CWE-690: Unchecked Return Value to NULL Pointer Dereference&lt;br&gt;
• CWE-762: Mismatched Memory Management Routines&lt;/p&gt;

&lt;p&gt;And many other interesting examples of almost 2 million lines of code in C and C ++! Wow!&lt;/p&gt;

&lt;p&gt;Source - &lt;a href="https://www.viva64.com/en/b/0579/" rel="noopener noreferrer"&gt;https://www.viva64.com/en/b/0579/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>cpp</category>
      <category>devops</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Amazon Lumberyard: A Scream of Anguish</title>
      <dc:creator>Alex Astva</dc:creator>
      <pubDate>Mon, 09 Jul 2018 14:33:17 +0000</pubDate>
      <link>https://forem.com/alexastva/amazon-lumberyard-a-scream-of-anguish-230e</link>
      <guid>https://forem.com/alexastva/amazon-lumberyard-a-scream-of-anguish-230e</guid>
      <description>&lt;p&gt;Video games are among the most popular software products. Now a new game engine, Amazon Lumberyard, has joined this huge industry. The project is currently in the beta phase and still has time to get rid of the bugs and improve. Its authors have a lot of work to do to make sure they don't disappoint millions of gamers and game developers in the nearest future.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flt0lpyqa05bgr6jo9fzi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flt0lpyqa05bgr6jo9fzi.png" alt="Amazon Lumberyard copy-past bugs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aws.amazon.com/lumberyard/" rel="noopener noreferrer"&gt;Amazon Lumberyard&lt;/a&gt; is a free cross-platform triple-A game engine developed by Amazon and based on the architecture of &lt;a href="https://www.cryengine.com/" rel="noopener noreferrer"&gt;CryEngine&lt;/a&gt;, which was licensed from Crytek in 2015. I already checked CryEngine twice, in &lt;a href="https://www.viva64.com/en/b/0417/" rel="noopener noreferrer"&gt;august 2016&lt;/a&gt; and &lt;a href="https://www.viva64.com/en/b/0495/" rel="noopener noreferrer"&gt;april 2017&lt;/a&gt;, and I'm sorry to say it, but the code quality had decreased since the first check. I was wondering the other day how Amazon had used the engine and took a look at the new product. I must admit they did make a great environment. The user documentation and environment deployment software are really awesome too. But the code is messed up again! I hope Amazon can afford to allocate much more resources for that project and will start caring about code quality at last. By writing this review I hope to draw their attention to this problem and persuade them to take a new approach to the development process. &lt;/p&gt;

&lt;p&gt;The source files under analysis refer to the latest Amazon Lumberyard version, 1.14.0.1, and were downloaded from the &lt;a href="https://github.com/aws/lumberyard/" rel="noopener noreferrer"&gt;Github&lt;/a&gt; repository. &lt;a href="https://robertsspaceindustries.com/" rel="noopener noreferrer"&gt;Star Citizen&lt;/a&gt; is one of the first games to be built on the Lumberyard engine. If you are looking to play it, then welcome to take a peek under the hood with me.&lt;/p&gt;

&lt;p&gt;Read more - &lt;a href="https://www.viva64.com/en/b/0574/" rel="noopener noreferrer"&gt;https://www.viva64.com/en/b/0574/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>amazonlumberyard</category>
      <category>cpp</category>
      <category>programming</category>
    </item>
    <item>
      <title>Couple of words about development of a new static analyzer for Java code</title>
      <dc:creator>Alex Astva</dc:creator>
      <pubDate>Fri, 22 Jun 2018 09:00:30 +0000</pubDate>
      <link>https://forem.com/alexastva/couple-of-words-about-development-of-a-new-static-analyzer-for-java-code-3dmf</link>
      <guid>https://forem.com/alexastva/couple-of-words-about-development-of-a-new-static-analyzer-for-java-code-3dmf</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.viva64.com%2Fmedia%2Fimages%2Fcontent%2Fb%2F0572_Java_analyzer%2Fimage4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.viva64.com%2Fmedia%2Fimages%2Fcontent%2Fb%2F0572_Java_analyzer%2Fimage4.png" alt="How Dr. Frankenstein assembled the analyzer from pieces"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio static analyzer team, which until recently was searching for bugs and potential vulnerabilities only in C, C++, and C# code, has prepared a new version of their tool for Java code as well. Despite the fact that in the Java world there is already a number of static analysis tools, developers believe that their analyzer can be powerful and will be a good competition. &lt;/p&gt;

&lt;p&gt;One of developers wrote in &lt;a href="https://www.viva64.com/en/b/0572/" rel="noopener noreferrer"&gt;his article&lt;/a&gt; about the way how PVS-Studio for Java was created. First of all, it was necessary to figure out the development process of a syntax tree and semantic model. As these elements are basic, static analyzer is built around them.  In addition to them, the analyzer also requires data flow analysis, which  enables you to calculate the possible values of variables and expressions in each point of the program and, thanks to that, find errors. Also the analyzer needs the annotation mechanism, diagnostic rules, integration, testing, and other techniques, explained in detail in the article. &lt;/p&gt;

&lt;p&gt;Traditionally, the author gave examples of errors from different open source projects, that the Java analyzer has detected.  The author also noted that in the future articles with a more detailed report on each project will be available. So far you can review errors from the Hibernate, Hive, JavaParser, Jenkins projects and several others.&lt;/p&gt;

&lt;p&gt;Besides that, when the first alpha version of the Java analyzer is available, developers suggest participating in its testing for those who would like to.  To do this, write to their support and they will contact you.&lt;/p&gt;

&lt;p&gt;Full article - &lt;a href="https://www.viva64.com/en/b/0572/" rel="noopener noreferrer"&gt;https://www.viva64.com/en/b/0572/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>staticcodeanalyzer</category>
      <category>programming</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Static Analysis in Video Game Development: Top 10 Software Bugs</title>
      <dc:creator>Alex Astva</dc:creator>
      <pubDate>Fri, 04 May 2018 13:32:44 +0000</pubDate>
      <link>https://forem.com/alexastva/static-analysis-in-video-game-development-top-10-software-bugs-3m31</link>
      <guid>https://forem.com/alexastva/static-analysis-in-video-game-development-top-10-software-bugs-3m31</guid>
      <description>&lt;p&gt;If you are a software developer working in the video game industry and wondering what else you could do to improve the quality of your product or make the development process easier and you don’t use static analysis – it’s just the right time to start doing so. You doubt that? OK, I’ll try to convince you. And if you are just looking to see what coding mistakes are common with video-game and game-engine developers, then you’re, again, at the right place: I have picked the most interesting ones for you.&lt;/p&gt;

&lt;p&gt;One of the best ways to prove that static analysis is a useful method is probably through examples showing it in action. That’s what the PVS-Studio team does while checking open-source projects. After publishing of various articles, we compiled our top-10 mistakes from the program code in the video-game industry, and now we suggest you read this article - &lt;a href="https://www.viva64.com/en/b/0570/"&gt;https://www.viva64.com/en/b/0570/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;List of projects considered in the article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;X-Ray Engine&lt;/li&gt;
&lt;li&gt;CryEngine V&lt;/li&gt;
&lt;li&gt;Space Engineers&lt;/li&gt;
&lt;li&gt;Quake III Arena GPL&lt;/li&gt;
&lt;li&gt;Unity&lt;/li&gt;
&lt;li&gt;Godot&lt;/li&gt;
&lt;li&gt;Doom 3&lt;/li&gt;
&lt;li&gt;Xenko
-Unreal Engine 4&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Although video-game development includes a lot of steps, coding remains one of the basic ones. Even if you don’t write thousands of code lines, you have to use various tools whose quality determines how comfortable the process is and what the ultimate result will be. Static analysis is a very useful tool when developing, and one more option to help you improve the quality of your code (and thus of the final product).&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>opensource</category>
      <category>csharp</category>
      <category>gamedev</category>
    </item>
    <item>
      <title>Static Code Analyzer: the Additional Insurance of the Medical Software</title>
      <dc:creator>Alex Astva</dc:creator>
      <pubDate>Wed, 21 Mar 2018 08:24:47 +0000</pubDate>
      <link>https://forem.com/alexastva/static-code-analyzer-the-additional-insurance-of-the-medical-software-3d94</link>
      <guid>https://forem.com/alexastva/static-code-analyzer-the-additional-insurance-of-the-medical-software-3d94</guid>
      <description>&lt;p&gt;Software bugs can lead not only to material losses, but also can damage human's health. For example, actors on the stage of a theatre can get injured if suddenly one of the scenery begins to go down on the stage at the wrong time. However, the connection between the errors in code and the health damage of medical software is more obvious. Let's talk about this topic.&lt;/p&gt;

&lt;p&gt;This article focuses on the teams of developers who create the programs for a medical equipment. I hope they will not stay indifferent and will check their code. Let's recall two famous cases where errors in programs, related to medicine, became the reason for bad news.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.viva64.com%2Fmedia%2Fimages%2Fcontent%2Fb%2F0564_Medic%2Fimage4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.viva64.com%2Fmedia%2Fimages%2Fcontent%2Fb%2F0564_Medic%2Fimage4.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Firstly, it is a &lt;a href="https://hownot2code.com/2016/10/22/killer-bug-therac-25-quick-and-dirty/" rel="noopener noreferrer"&gt;series of tragic events&lt;/a&gt; caused by the errors in the Therac-25 device of radiation therapy. This device has caused at least six overdoses of radiation within the period from June 1985 to January 1987, some patients received doses of tens of thousands of rad. At least two people died directly from the radiation overdoses. Software bugs of the device were the reason of the tragedies and the main problem was the incorrect security strategy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.viva64.com%2Fmedia%2Fimages%2Fcontent%2Fb%2F0564_Medic%2Fimage6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.viva64.com%2Fmedia%2Fimages%2Fcontent%2Fb%2F0564_Medic%2Fimage6.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Secondly, the software bugs can also cause harm indirectly. &lt;a href="http://www.pnas.org/content/early/2016/06/27/1602413113" rel="noopener noreferrer"&gt;For example&lt;/a&gt;, the bugs in the software for MRI-scanners raise the questions about the 40 000 researches. For several decades, neuroscientists and cognitive psychologists has used statistical programs AFNI, SPM and FSL to analyze &lt;a href="https://en.wikipedia.org/wiki/Functional_magnetic_resonance_imaging" rel="noopener noreferrer"&gt;fMRI&lt;/a&gt; data. As it turned out, because of the incorrect algorithms, these programs might return up to 70% of false positive results instead of the projected 5%.&lt;/p&gt;

&lt;p&gt;As you can see, the code errors can lead not only to troubles, such as a crash or a loss of data, but also to much more serious consequences, which influence the life and the health of many people throughout many years.&lt;/p&gt;

&lt;p&gt;Moreover, the developer is responsible not only for his own code, but for the code of the used libraries. This situation is completely real, when the artifacts appear when creating an image/video due to an error from a third-party library and this will lead to confusion when diagnosing.&lt;/p&gt;

&lt;p&gt;This is not an abstract theoretical problem. I myself faced a situation, in which when porting programs to 64-bit system an error causing incorrect handling of MRI data began to reveal itself. Fortunately, the error showed itself very clearly: a large fragment of the image was absent. However, the error might not be that noticeable and consist in the incorrect displaying of some details and it'll be much harder to detect it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.viva64.com%2Fmedia%2Fimages%2Fcontent%2Fb%2F0564_Medic%2Fimage8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.viva64.com%2Fmedia%2Fimages%2Fcontent%2Fb%2F0564_Medic%2Fimage8.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;More information about this error is available in the article "&lt;a href="https://www.viva64.com/en/b/0465/" rel="noopener noreferrer"&gt;PVS-Studio project - 10 years of failures and successes&lt;/a&gt;". It is this and some other &lt;a href="https://www.viva64.com/en/t/0002/" rel="noopener noreferrer"&gt;64-bit errors&lt;/a&gt; that inspired the creation of the Viva64 tool, which then turned into a PVS-Studio static code analyzer.&lt;/p&gt;

&lt;p&gt;It is impossible to predict where the errors can be and what errors can lead to trouble. The error can be complex and not necessarily spoil life, lurking in the algorithm of data processing and displaying. I can imagine a situation where because of an error in the comparison function, data of the wrong patient will be selected for processing, or the program, describing the condition of the patient, will not notice some differences in the data structure.&lt;/p&gt;

&lt;p&gt;I invite all readers to start using the PVS-Studio static code analyzer. Yes, the analyzer, like any other tool, does not guarantee the absence of errors in your programs. However, it becomes an additional line of defense on the field of the battle against the bugs. It can help detect a lot of errors at the early stages of development and may help to save someone's health.&lt;/p&gt;

&lt;p&gt;Read more - &lt;a href="https://hownot2code.com/2018/03/21/pvs-studio-the-additional-insurance-of-the-medical-software/" rel="noopener noreferrer"&gt;https://hownot2code.com/2018/03/21/pvs-studio-the-additional-insurance-of-the-medical-software/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>medicalsoftware</category>
      <category>informationsecurity</category>
      <category>testing</category>
      <category>development</category>
    </item>
  </channel>
</rss>
