<?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>DEV Community: Unicorn Developer</title>
    <description>The latest articles on DEV Community by Unicorn Developer (@pvsdev).</description>
    <link>https://dev.to/pvsdev</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1110345%2F0615f05a-4746-4c6f-89ff-a4ef95008a77.png</url>
      <title>DEV Community: Unicorn Developer</title>
      <link>https://dev.to/pvsdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pvsdev"/>
    <language>en</language>
    <item>
      <title>Let’s make a programming language. Variables — Key points</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Tue, 18 Aug 2026 13:00:38 +0000</pubDate>
      <link>https://dev.to/pvsdev/lets-make-a-programming-language-variables-key-points-3c6p</link>
      <guid>https://dev.to/pvsdev/lets-make-a-programming-language-variables-key-points-3c6p</guid>
      <description>&lt;p&gt;We continue the development of our toy programming language that has evolved from handling single expressions to supporting full programs composed of declaration lists. In this episode, we'll explore variable implementation, declarations, and type handling in a simple programming language interpreter/compiler.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzybvpvun3quzuk0ukl3c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzybvpvun3quzuk0ukl3c.png" alt=" " width="800" height="451"&gt;&lt;/a&gt;&lt;br&gt;
  &lt;iframe src="https://www.youtube.com/embed/Sn8fVb_O90Y"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;About the speaker&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yuri Minaev is an experienced C++ developer, architect at PVS Studio, and a recognized voice in the C++ community who has spoken at CppCast, C++ on Sea, and CppCon. Over the course of ten sessions, he’ll guide you through each stage of building your own programming language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parsing and name resolution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Introducing variable names requires more than parsing. The semantic analyzer, or SEMA, manages the symbol table, scopes, and types. The symbol table associates names with variable declarators, allowing the language to resolve references and detect undeclared identifiers and duplicate definitions. Declarations are recorded in the current scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types and implicit conversions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SEMA extracts types from expressions using a visitor. The language has &lt;code&gt;error&lt;/code&gt;, &lt;code&gt;void&lt;/code&gt;, &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;float&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;, and &lt;code&gt;function&lt;/code&gt; types. Functions and void values cannot initialize variables. When an operation combines different types, the language calculates a common type. For example, int and float produce float, so int is implicitly cast. Logical and relational expressions produce boolean results, while arithmetic expressions use their operands' common type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Building the syntax tree&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Implicit casts are added to the syntax tree during construction, so the evaluator will not need to calculate types itself. It can walk the tree and execute the represented operations and conversions. Errors propagate through the tree, and invalid programs are never evaluated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Want more?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want to watch other talks, follow this &lt;a href="https://youtube.com/playlist?list=PLGVoaOmC1PBw&amp;amp;si=4TeC8SI6XXTPBIvF" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can also sign up for our upcoming webinars: &lt;a href="https://pvs-studio.com/en/webinar/32/?utm_source=devto&amp;amp;utm_medium=pvs&amp;amp;utm_campaign=webinar&amp;amp;utm_content=web6&amp;amp;utm_term=recap" rel="noopener noreferrer"&gt;Let's make a programming language. Functions&lt;/a&gt;. We hope to see you there!&lt;/p&gt;

&lt;p&gt;If you'd like to learn more about PVS-Studio analyzer, check out our &lt;a href="https://pvs-studio.com/en/?utm_source=devto&amp;amp;utm_medium=pvs&amp;amp;utm_campaign=webinar&amp;amp;utm_content=web6&amp;amp;utm_term=recap" rel="noopener noreferrer"&gt;website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;See ya!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Static and dynamic analysis help when unit tests fail</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 14 Aug 2026 13:15:28 +0000</pubDate>
      <link>https://dev.to/pvsdev/static-and-dynamic-analysis-help-when-unit-tests-fail-4ii3</link>
      <guid>https://dev.to/pvsdev/static-and-dynamic-analysis-help-when-unit-tests-fail-4ii3</guid>
      <description>&lt;p&gt;No one disputes the value of static or dynamic code analysis. However, some developers see that value as rather abstract and stop at writing unit tests. I recently came across an example from the C world that nicely illustrates how unit tests can sometimes fail to catch even the most common types of errors.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9cbu4m0i25jta7uc6jph.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9cbu4m0i25jta7uc6jph.png" alt="1401_Beyond_Unit_Tests_Static_and_Dynamic_Analysis/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I've already discussed the bug I'm about to show you in the article "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0845/" rel="noopener noreferrer"&gt;A beautiful error in the implementation of the string concatenation function&lt;/a&gt;." I'll briefly recap it, and then we'll look at it from the perspective of early detection.&lt;/p&gt;

&lt;h2&gt;
  
  
  String concatenation bug
&lt;/h2&gt;

&lt;p&gt;The LFortran project had the following function for concatenating two strings into a new buffer:&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="nf"&gt;_lfortran_strcat&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;s1&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;s2&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;dest&lt;/span&gt;&lt;span class="p"&gt;)&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;cntr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;trmn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'\0'&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;s1_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s1&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;s2_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s2&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;trmn_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;trmn&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;dest_char&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&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;s1_len&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;s2_len&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;trmn_size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;s1_len&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&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;dest_char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;cntr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;cntr&lt;/span&gt;&lt;span class="o"&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;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;s2_len&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&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;dest_char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;cntr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;cntr&lt;/span&gt;&lt;span class="o"&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;dest_char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;cntr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;trmn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dest_char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&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;This is a classic error where the allocated buffer is one byte smaller than required. The &lt;a href="https://pvs-studio.com/en/blog/terms/0088/" rel="noopener noreferrer"&gt;terminal null&lt;/a&gt; isn't taken into account; or rather, it is, but its size is calculated incorrectly.&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;char&lt;/span&gt; &lt;span class="n"&gt;trmn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'\0'&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;trmn_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;trmn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;trmn&lt;/code&gt; character here gets interpreted as an empty string, whose length is zero. As a result, the &lt;code&gt;trmn_size&lt;/code&gt; variable, whose name suggests that it stores the size of the terminal null, always equals &lt;code&gt;0&lt;/code&gt;. So, the terminal null is written past the end of the allocated buffer.&lt;/p&gt;

&lt;p&gt;This is how we can fix the code (by adding &lt;code&gt;+1&lt;/code&gt; when calculating the argument of the &lt;code&gt;malloc&lt;/code&gt; function):&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="nf"&gt;_lfortran_strcat&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;s1&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;s2&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;dest&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;s1&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;||&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s1&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;||&lt;/span&gt;
        &lt;span class="n"&gt;s2&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;||&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s2&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;||&lt;/span&gt; &lt;span class="n"&gt;dest&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="c1"&gt;// Some error handling appropriate for this project.&lt;/span&gt;
      &lt;span class="p"&gt;....&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;s1_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s1&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;s2_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s2&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;dest_char&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&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;s1_len&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s2_len&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;dest_char&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="c1"&gt;// Some error handling appropriate for this project.&lt;/span&gt;
      &lt;span class="p"&gt;....&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;memcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dest_char&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1_len&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;memcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dest_char&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s1_len&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2_len&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;dest_char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s1_len&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s2_len&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'\0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dest_char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&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;h2&gt;
  
  
  Granularity
&lt;/h2&gt;

&lt;p&gt;The error is simple and straightforward. Buffer overflows are a common issue in C programs. What else is there to discuss? The bug has been found; case closed.&lt;/p&gt;

&lt;p&gt;What got me thinking was a reader's comment about how tricky this error can be because of the granularity of memory allocation.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;malloc&lt;/code&gt; function doesn't actually allocate exactly as much memory as requested. It obtains large blocks from the OS and splits them into smaller chunks, adding some metadata along the way. The exact block size depends on the implementation.&lt;/p&gt;

&lt;p&gt;Even if we call &lt;code&gt;malloc(1)&lt;/code&gt;, the allocator may still return a pointer to a block that is, say, 32 bytes in size (minus the metadata fields, leaving 24 usable bytes). This helps maintain memory alignment, typically the 16-byte one, and simplifies memory management.&lt;/p&gt;

&lt;p&gt;The returned pointer must be properly aligned. Since &lt;code&gt;malloc&lt;/code&gt; doesn't know what types we'll store in the allocated memory, it has to account for the strictest alignment requirement that might arise. In reality, on x86-64, &lt;code&gt;malloc&lt;/code&gt; allocates addresses that are multiples of 16. That's why the allocator always rounds the requested block size up to the next alignment boundary.&lt;/p&gt;

&lt;p&gt;I described the process very roughly and simplified some details. The important point is that, in reality, the code we looked at usually allocates more memory than it actually needs. If the blocks are multiples of 16 bytes, writing the terminal null into the next block will happen only when the resulting string size is also a multiple of 16. In other words, the bug has a 1-in-16 chance of actually happening (or perhaps not 16, that's just one possible value).&lt;/p&gt;

&lt;p&gt;Now, I'd like to mention undefined behavior. Technically, the code is incorrect because it accesses memory outside the array bounds. We can't reliably predict how it will work, how the bug will behave, etc.&lt;/p&gt;

&lt;p&gt;However, undefined behavior can also mean that incorrect code happens to work as expected. In this case, the memory manager may allocate more memory than the code requests, making it appear as if everything is working correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations of unit tests
&lt;/h2&gt;

&lt;p&gt;We can write unit tests like these:&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="nf"&gt;Test1&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"a"&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;b&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="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;_lfortran_strcat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;q&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;ok&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s+%s=%s %s&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;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s"&gt;"ok"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"err"&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;q&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="nf"&gt;Test2&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"12"&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"345"&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;q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;_lfortran_strcat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;q&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;ok&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"12345"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s+%s=%s %s&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;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s"&gt;"ok"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"err"&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;q&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;And still miss the issue. &lt;a href="https://godbolt.org/z/Gn3d3odET" rel="noopener noreferrer"&gt;The tests pass successfully&lt;/a&gt;:&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="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;345&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;12345&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tests with short strings won't reveal the problem. Some developers might not even think to try working with long strings. Why would they? At first glance, such tests don't seem to add much value. Those devs will probably create tests for edge cases, such as empty strings, but they aren't relevant to finding the bug we're discussing.&lt;/p&gt;

&lt;p&gt;Even with long strings, the chance of spilling into the neighboring block is only 1 in N (where N might be 16, for example). They could concatenate two huge strings, each containing 111111 characters, and everything would still work fine because the resulting 222222-character string doesn't end on a 16-byte block boundary.&lt;/p&gt;

&lt;h2&gt;
  
  
  More unit tests
&lt;/h2&gt;

&lt;p&gt;Don't get me wrong: I'm not criticizing unit tests. They're a great tool! However, some errors can easily hide from them, and this is exactly such a case.&lt;/p&gt;

&lt;p&gt;The issue is that it's easy to miss where the code crosses the invisible boundary of an allocated memory block. The following test creates reasonably long strings, 37 characters each. Do you think this test will cause the program to crash or create another visible problem?&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="nf"&gt;Test3&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"123"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&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;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;memset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'\0'&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;q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;_lfortran_strcat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;q&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;ok&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%u %s %s&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;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s"&gt;"ok"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"err"&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;b&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;q&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;It's impossible to say whether it will or not because we're dealing with undefined behavior. In reality, I compiled it with &lt;code&gt;gcc&lt;/code&gt; using the &lt;code&gt;-O2&lt;/code&gt; optimization level and didn't notice any visible symptoms of the error, even though the memory blocks should have been corrupted. Yet the tests still &lt;a href="https://godbolt.org/z/xK7hzTso4" rel="noopener noreferrer"&gt;make everything look fine&lt;/a&gt;:&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="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;12&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;13&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;14&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;17&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;19&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;21&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;22&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;23&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;24&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;26&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;27&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;28&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;29&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;31&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;32&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;33&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;span class="mi"&gt;34&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="n"&gt;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The program will crash when &lt;code&gt;i&lt;/code&gt; reaches &lt;a href="https://godbolt.org/z/qTdManMb4" rel="noopener noreferrer"&gt;38&lt;/a&gt; in the &lt;code&gt;for (unsigned i = 1; i != 38; ++i)&lt;/code&gt; loop:&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="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;invalid&lt;/span&gt; &lt;span class="n"&gt;pointer&lt;/span&gt;
&lt;span class="n"&gt;Program&lt;/span&gt; &lt;span class="n"&gt;terminated&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;SIGSEGV&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't look for any special meaning in the number &lt;code&gt;38&lt;/code&gt;; it's just how it turned out. The most interesting thing here is how long the error was hiding from unit tests!&lt;/p&gt;

&lt;h2&gt;
  
  
  Early error detection
&lt;/h2&gt;

&lt;p&gt;Such an error can be detected immediately using &lt;a href="https://pvs-studio.com/en/blog/terms/0046/" rel="noopener noreferrer"&gt;static&lt;/a&gt; or &lt;a href="https://pvs-studio.com/en/blog/terms/0070/" rel="noopener noreferrer"&gt;dynamic&lt;/a&gt; analysis.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwryh3o3b8shh1mvtoh6n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwryh3o3b8shh1mvtoh6n.png" alt="1401_Beyond_Unit_Tests_Static_and_Dynamic_Analysis/image3.png" width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Picrute 1—Early error detection using dynamic analysis (AddressSanitizer) or static analysis (PVS-Studio).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/pvs-studio/" rel="noopener noreferrer"&gt;PVS-Studio&lt;/a&gt; static analyzer immediately issues a warning about an anomaly in the code: &lt;a href="https://pvs-studio.com/en/docs/warnings/v742/" rel="noopener noreferrer"&gt;V742&lt;/a&gt; Function receives an address of a 'char' type variable instead of pointer to a buffer. Inspect the first argument.&lt;/p&gt;

&lt;p&gt;Or you can use a dynamic analysis tool, such as &lt;a href="https://github.com/google/sanitizers/wiki/addresssanitizer" rel="noopener noreferrer"&gt;AddressSanitizer&lt;/a&gt; (gcc key:fsanitize=address). The simplest test, &lt;code&gt;Test1&lt;/code&gt;, &lt;a href="https://godbolt.org/z/MzKjK3Kss" rel="noopener noreferrer"&gt;will immediately expose&lt;/a&gt; the issue.&lt;/p&gt;

&lt;p&gt;Unit tests and dynamic analysis work hand in hand. The sanitizer detects an issue while the unit test runs. Without the test, the error may not surface until much later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Static and dynamic analysis complement unit tests and other bug-finding techniques well. There's no single best method or tool, though. Static and dynamic analyzers have their own limitations and complement each other. And unit tests can uncover logic errors that analyzers are unlikely to catch.&lt;/p&gt;

&lt;p&gt;Use them all. Although it'll require some investment at first, it'll pay off over time by catching a large percentage of bugs at an early stage in the development process. The earlier you find a bug, the easier and cheaper it is to fix (the &lt;a href="https://pvs-studio.com/en/blog/terms/7013/" rel="noopener noreferrer"&gt;shift-left testing&lt;/a&gt; approach).&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Static analysis can enhance your unit tests by detecting errors within the tests themselves: &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1179/" rel="noopener noreferrer"&gt;Finding errors in unit tests&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Each approach has its own advantages and disadvantages: &lt;a href="https://pvs-studio.com/en/blog/posts/0643/" rel="noopener noreferrer"&gt;What's the Use of Dynamic Analysis When You Have Static Analysis?&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>programming</category>
      <category>unittest</category>
      <category>security</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Webinar on common mistakes Go developers make</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 07 Aug 2026 12:28:17 +0000</pubDate>
      <link>https://dev.to/pvsdev/a-webinar-on-common-mistakes-go-developers-make-1ohj</link>
      <guid>https://dev.to/pvsdev/a-webinar-on-common-mistakes-go-developers-make-1ohj</guid>
      <description>&lt;p&gt;Even experienced Go developers can make mistakes. Typos, duplicated code, incorrect condition checks, and other issues may find their way into production code just like they do in any other programming language.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffjlbn0komo7mn4ozwfoh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffjlbn0komo7mn4ozwfoh.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Many teams rely on tools such as go vet to catch common issues. But is that enough? Some bugs remain invisible to standard tools and approaches, making them much harder to spot before they affect performance or behavior.&lt;/p&gt;

&lt;p&gt;If you've ever wondered why these issues still slip through, our upcoming live talk is for you!&lt;/p&gt;

&lt;h2&gt;
  
  
  What we'll cover
&lt;/h2&gt;

&lt;p&gt;During the webinar, Gleb Aslamov, Senior Developer Advocate at PVS-Studio, will cover common mistakes found in real-world Go code from well-known open-source projects. The speaker will also discuss the kinds of errors that standard tools and development practices fail to detect—and how to deal with them.&lt;/p&gt;

&lt;p&gt;Throughout the webinar, Gleb will also explain why these mistakes happen, why conventional tooling doesn't always detect them, and how developers can find them sooner.&lt;/p&gt;

&lt;h2&gt;
  
  
  Join us live
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/webinar/33/?utm_source=devto&amp;amp;utm_medium=pvs&amp;amp;utm_campaign=webinar&amp;amp;utm_content=go_mistakes" rel="noopener noreferrer"&gt;Follow this link&lt;/a&gt; to join us on &lt;strong&gt;August 27, 2026, at 1:00 p.m. UTC+&lt;/strong&gt; to learn more about common errors and pitfalls in Go. &lt;/p&gt;

&lt;p&gt;Be sure to check your email and confirm your registration to access the webinar link!&lt;/p&gt;

</description>
      <category>go</category>
      <category>webinar</category>
      <category>programming</category>
      <category>opensource</category>
    </item>
    <item>
      <title>How PVS-Studio helps enhance quality of embedded projects</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Wed, 05 Aug 2026 13:30:26 +0000</pubDate>
      <link>https://dev.to/pvsdev/how-pvs-studio-helps-enhance-quality-of-embedded-projects-2hk3</link>
      <guid>https://dev.to/pvsdev/how-pvs-studio-helps-enhance-quality-of-embedded-projects-2hk3</guid>
      <description>&lt;p&gt;Embedded development comes with a wide variety of custom configurations, compilers, and build systems, which can make standard analysis approaches difficult to apply. To address this challenge, PVS-Studio provides a dedicated mechanism designed specifically for such environments. Let's take a closer look at how it works and see how you can use it in real cases.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgo085zrvgbnh176in6ld.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgo085zrvgbnh176in6ld.png" alt="1399_PVS_embedded/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Features of embedded systems
&lt;/h2&gt;

&lt;p&gt;Embedded development in C and C++ powers an enormous range of devices—from electric kettles and toothbrushes to safety-critical systems such as medical equipment and aircraft control software. In these areas, the quality of software directly affects human safety. As a result, the code must be as reliable, predictable, and resilient to external factors as possible.&lt;/p&gt;

&lt;p&gt;The need for thorough code verification in embedded systems continues to grow for several reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Embedded systems interact directly with users and external data, so they must correctly handle all input.&lt;/li&gt;
&lt;li&gt;Memory is often severely limited, making careful resource management essential.&lt;/li&gt;
&lt;li&gt;The software must remain stable under all operating conditions and provide a high level of fault tolerance.&lt;/li&gt;
&lt;li&gt;Many other factors also contribute to these requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Embedded software targets a wide variety of platforms and use cases. Each project may rely on a different compiler, toolchain, or build system. The situation often becomes more challenging due to a "zoo" of custom-built scripts that are difficult to maintain.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com" rel="noopener noreferrer"&gt;PVS-Studio&lt;/a&gt; static analyzer offers several mechanisms specifically designed for embedded projects. They collect all the necessary information for analysis directly from the compilation process as it runs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Specialized analysis mechanisms
&lt;/h2&gt;

&lt;p&gt;All of the mechanisms described here follow the same general workflow. You start them before building your project, and they monitor every run of supported compilers throughout the build process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C and C++ compilers from the GNU Compiler Collection (gcc.exe, g++.exe) and compatible derivatives;&lt;/li&gt;
&lt;li&gt;Clang C and C++ compilers (clang.exe) and compatible derivatives;&lt;/li&gt;
&lt;li&gt;Borland C++;&lt;/li&gt;
&lt;li&gt;QCC;&lt;/li&gt;
&lt;li&gt;Keil MDK ARM Compiler 5/6;&lt;/li&gt;
&lt;li&gt;IAR C/C++ Compiler for ARM;&lt;/li&gt;
&lt;li&gt;Texas Instruments ARM Compiler;&lt;/li&gt;
&lt;li&gt;GNU Arm Embedded Toolchain;&lt;/li&gt;
&lt;li&gt;Texas Instruments Code Composer Studio, C6000-CGT, C2000-CGT (support is coming in October);&lt;/li&gt;
&lt;li&gt;GNU toolchain for RISC-V.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note.&lt;/strong&gt; If you'd like us to support a compiler that isn't currently supported, we'd be happy to evaluate adding it. You can send us a request to support a specific compiler or contact us about any issues you're experiencing via our &lt;a href="https://pvs-studio.com/en/about-feedback/" rel="noopener noreferrer"&gt;feedback form&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After monitoring finishes, the server generates intermediate files and then runs the static analyzer.&lt;/p&gt;

&lt;p&gt;Each mechanism targets a specific environment or use case. Let's look at them one by one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compilation monitoring CLMonitor.exe (for Windows)
&lt;/h3&gt;

&lt;p&gt;CLMonitor.exe is a monitoring server that tracks compiler runs. Start it before building your project. Once monitoring is enabled, the server intercepts every run of supported compilers.&lt;/p&gt;

&lt;p&gt;The compilation monitoring system follows a straightforward workflow, illustrated below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1r08ligqe8dei984rtnc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1r08ligqe8dei984rtnc.png" alt="1399_PVS_embedded/image2.png" width="799" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to start compilation monitoring&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To start the monitoring process, run the following command:&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="n"&gt;CLMonitor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="n"&gt;monitor&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CLMonitor.exe will run in the background and track all supported compilers. To stop the process, run one of the commands described below.&lt;/p&gt;

&lt;p&gt;You can also track only compiler runs that were started by a specific process identified by its PID. To do this, start &lt;code&gt;CLMonitor.exe&lt;/code&gt; in the trace mode with the &lt;code&gt;trace&lt;/code&gt; and &lt;code&gt;--parentProcessID&lt;/code&gt; (&lt;code&gt;-p&lt;/code&gt;) arguments.&lt;/p&gt;

&lt;p&gt;The command for running &lt;code&gt;CLMonitor.exe&lt;/code&gt; in this mode may look like this:&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="n"&gt;CLMonitor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="n"&gt;trace&lt;/span&gt; &lt;span class="err"&gt;–&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;parentProcessID&lt;/span&gt; &lt;span class="mi"&gt;10256&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want &lt;code&gt;CLMonitor.exe&lt;/code&gt; to track only the build started from the same console, you can run &lt;code&gt;CLMonitor.exe&lt;/code&gt; with the &lt;code&gt;--attach&lt;/code&gt; (&lt;code&gt;-a&lt;/code&gt;) argument:&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="n"&gt;CLMonitor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="n"&gt;monitor&lt;/span&gt; &lt;span class="err"&gt;–&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;attach&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Building the project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After starting compilation monitoring, build your project using the usual workflow. Once the project build is complete, stop compilation monitoring so that you can analyze the collected data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to stop compilation monitoring&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can run the analysis immediately after building the project using the following command:&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="n"&gt;CLMonitor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="n"&gt;analyze&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="n"&gt;D&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;ptest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;plog&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also pass additional parameters when starting the analysis:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-l&lt;/code&gt; is the path to the final analyzer report file;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-u&lt;/code&gt; is the path to the &lt;a href="https://pvs-studio.com/en/docs/manual/0032/" rel="noopener noreferrer"&gt;suppress file&lt;/a&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-c&lt;/code&gt; is the path to the &lt;a href="https://pvs-studio.com/en/docs/manual/6630/" rel="noopener noreferrer"&gt;&lt;code&gt;.pvsconfig&lt;/code&gt;&lt;/a&gt; analysis configuration file;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--intermodular&lt;/code&gt; enables the &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0851/" rel="noopener noreferrer"&gt;intermodular analysis&lt;/a&gt; mode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you encounter issues while building the project and need to stop compilation monitoring without analyzing the resulting files, you can do so using the following command:&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="n"&gt;CLMonitor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="n"&gt;abortTrace&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;CLMonitor.exe&lt;/code&gt; monitoring server enables saving captured compilation data in a separate dump file. This allows you to run the analysis without rebuilding the project.&lt;/p&gt;

&lt;p&gt;To save the dump file, use the following command:&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="n"&gt;CLMonitor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="n"&gt;saveDump&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="n"&gt;D&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;monitoring&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zip&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;-d&lt;/code&gt; is the path to the final dump file.&lt;/p&gt;

&lt;p&gt;To run the analysis using a saved dump file, use the following command:&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="n"&gt;CLMonitor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="n"&gt;analyzeFromDump&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;ptest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;plog&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;monitoring&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zip&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command supports all the flags described above that are used to start the analysis.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Wrap Compilers interception mode (for Windows)
&lt;/h3&gt;

&lt;p&gt;As we continue the discussion of embedded system specifics, it's worth mentioning another challenge involved in analyzing embedded projects. Such projects often consist of quickly compiling C source files, and &lt;code&gt;CLMonitor.exe&lt;/code&gt; may not have enough time to detect all source files.&lt;/p&gt;

&lt;p&gt;To ensure the interception of all compilation processes, the monitoring server can switch to a more aggressive mode using the &lt;a href="https://learn.microsoft.com/en-us/previous-versions/windows/desktop/xperf/image-file-execution-options" rel="noopener noreferrer"&gt;Image File Execution Options&lt;/a&gt; (IFEO) mechanism on Windows.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://pvs-studio.com/en/docs/manual/0031/#wrapcompilers" rel="noopener noreferrer"&gt;Wrap Compilers&lt;/a&gt; intercept mode runs a special handler before each compilation process begins. It sends the necessary information to the monitoring server and then continues running the compiler.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note.&lt;/strong&gt; This mode requires access to editing the path in the Windows Registry:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can grant the required access to the monitoring server by running it as administrator or by giving it write permissions to the registry branch above—or for the entire registry.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To enable the interception mode in the console version of the monitoring tool, pass the &lt;code&gt;--wrapCompilers&lt;/code&gt; (&lt;code&gt;-W&lt;/code&gt;) flag to the monitoring server with a list of compilers, for example:&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="n"&gt;CLMonitor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="n"&gt;trace&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;wrapCompilers&lt;/span&gt; &lt;span class="n"&gt;gcc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Compiler tracing (for Linux)
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;compile_commands.json&lt;/code&gt; file is used to analyze projects on the Linux system. You can find more information on the relevant &lt;a href="https://pvs-studio.com/en/docs/manual/6557/" rel="noopener noreferrer"&gt;documentation page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If generating the &lt;code&gt;compile_commands.json&lt;/code&gt; file is not possible, you can use the compilation trace mode. To do so, install the &lt;code&gt;strace&lt;/code&gt; utility of the 4.7 version or later. It enables the analyzer to collect essential information about project compilation during the build process.&lt;/p&gt;

&lt;p&gt;You can build the project and track its compilation process using the following command:&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="n"&gt;pvs&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;studio&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;analyzer&lt;/span&gt; &lt;span class="n"&gt;trace&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="n"&gt;build_command&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;strace_out&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;build_command&lt;/code&gt; is the command used to build the project;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-o&lt;/code&gt; is the path to the compilation trace file. If no flag is specified, a file named &lt;code&gt;strace_out&lt;/code&gt; is saved in the CWD.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a result of the tracing, a file named &lt;code&gt;strace_out&lt;/code&gt; will be created by default.&lt;/p&gt;

&lt;p&gt;Once you have obtained the &lt;code&gt;strace_out&lt;/code&gt; compilation trace file, you can run the analysis using the following command:&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="n"&gt;pvs&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;studio&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;analyzer&lt;/span&gt; &lt;span class="n"&gt;analyze&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;strace_out&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also pass additional parameters when starting the analysis:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-f&lt;/code&gt; is the path to the file containing the compilation trace results;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-l&lt;/code&gt; is the path to the final analyzer report file;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-u&lt;/code&gt; is the path to the &lt;a href="https://pvs-studio.com/en/docs/manual/0032/" rel="noopener noreferrer"&gt;suppress file&lt;/a&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-c&lt;/code&gt; is the path to the &lt;a href="https://pvs-studio.com/en/docs/manual/6630/" rel="noopener noreferrer"&gt;&lt;code&gt;.pvsconfig&lt;/code&gt;&lt;/a&gt; analysis configuration file;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--intermodular&lt;/code&gt; enables the &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0851/" rel="noopener noreferrer"&gt;intermodular analysis&lt;/a&gt; mode.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Visual Studio Code
&lt;/h3&gt;

&lt;p&gt;You can also use compilation monitoring with the &lt;a href="https://pvs-studio.com/en/docs/manual/6646/" rel="noopener noreferrer"&gt;PVS-Studio plugin&lt;/a&gt; for Visual Studio Code.&lt;/p&gt;

&lt;p&gt;To start compilation monitoring, use the &lt;strong&gt;PVS-Studio: Run compiler monitoring for C and C++&lt;/strong&gt; command in the Visual Studio Code Command Palette (&lt;code&gt;Ctrl + Shift + P&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;When monitoring starts, the plugin window containing the table will display an indicator showing that monitoring is running:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ficbosw4fe0p5ccfx45f5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ficbosw4fe0p5ccfx45f5.png" alt="1399_PVS_embedded/image3.png" width="800" height="160"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once compiler calls are intercepted, their number will be displayed in the monitoring indicator, and the start button will appear.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm05ld2p6e4ou5thyu2vs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm05ld2p6e4ou5thyu2vs.png" alt="1399_PVS_embedded/image4.png" width="396" height="42"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can start the project analysis by clicking the button or using the &lt;strong&gt;PVS-Studio: Stop monitoring and start analysis&lt;/strong&gt; command in the Visual Studio Code command palette.&lt;/p&gt;

&lt;p&gt;The first time you run monitoring on a project, the plugin will prompt you to edit the &lt;code&gt;./.PVS-Studio/CLMonitorAnalyzerConfig.jsonc&lt;/code&gt; configuration file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4zt5z48tprblerssu5wt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4zt5z48tprblerssu5wt.png" alt="1399_PVS_embedded/image5.png" width="548" height="165"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click &lt;strong&gt;Edit&lt;/strong&gt; to set the following parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the path to a monitoring dump file (by default, &lt;code&gt;./.PVS-Studio/lastMonitoring.zip&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;the path to the configuration file or directory containing &lt;code&gt;.pvsconfig&lt;/code&gt; files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Click &lt;strong&gt;Continue&lt;/strong&gt; to run the analysis using the default settings.&lt;/p&gt;

&lt;p&gt;If the source code files and the build configuration haven't been changed, you can use the dump file to run the analysis. To do this, select the &lt;strong&gt;PVS-Studio: Start analysis from compiler monitoring dump file&lt;/strong&gt; command from the Visual Studio Code command palette.&lt;/p&gt;

&lt;p&gt;In PVS-Studio plugin for Visual Studio Code, you can use the Wrap Compilers mode. To enable it, open the &lt;strong&gt;Monitoring (C and C++)&lt;/strong&gt; tab in the plugin settings and specify the names of the compiler executable files you want to monitor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffl1ikfmyqyszb1exw4ar.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffl1ikfmyqyszb1exw4ar.png" alt="1399_PVS_embedded/image6.png" width="798" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note.&lt;/strong&gt; To enable monitoring in this mode, restart Visual Studio Code with administrator privileges.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  A standard for improving reliability
&lt;/h2&gt;

&lt;p&gt;Fixing bugs in embedded systems after a device ships is both difficult and expensive. This may require product recalls, firmware updates, reshipping, or replacing devices entirely in the worst case. Such issues come at a high cost, both financially and in terms of reputation.&lt;/p&gt;

&lt;p&gt;To minimize risks, errors must be detected and fixed at the development stage. When there are many errors, it's much more effective to classify and track them using industry standards.&lt;/p&gt;

&lt;p&gt;For example, the IEC 61508-7 industry standard requires using a subset of the C and C++ languages, coding standards, and static code analyzers when designing systems with a high safety integrity level (SIL).&lt;/p&gt;

&lt;p&gt;The automotive industry relies on the ISO 26262, which establishes functional safety requirements for road vehicles. The standard also specifies requirements for verifying software modules and recommends using static code analysis as part of the verification process.&lt;/p&gt;

&lt;p&gt;MISRA C and MISRA C++ have become the industry standard for defining safe subsets of the C and C++ languages.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://pvs-studio.com/en/docs/manual/6966/" rel="noopener noreferrer"&gt;MISRA standards&lt;/a&gt; provide guidelines for developing safe and reliable C and C++ software in safety-critical fields, including automotive, aerospace, medical devices, and industrial automation. These are industries where the cost of failure is exceptionally high.&lt;/p&gt;

&lt;p&gt;MISRA improves software safety by preventing bugs and vulnerabilities and by restricting complex language features that can cause unexpected program behavior.&lt;/p&gt;

&lt;p&gt;Its authors have scrutinized international C and C++ standards and listed every possible way to make a mistake. The MISRA C and MISRA C++ standards provide guidelines to help reduce errors, improve code readability, and enhance maintainability.&lt;/p&gt;

&lt;p&gt;Manually enforcing code quality in large projects is both difficult and time-consuming. For this reason, MISRA strongly encourages the use of automated code quality tools.&lt;/p&gt;

&lt;p&gt;PVS-Studio supports code analysis to ensure compliance with multiple MISRA standards. At the time of writing, it covers the following Mandatory and Required rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MISRA C 2012 — 80%;&lt;/li&gt;
&lt;li&gt;MISRA C 2023 — 85%;&lt;/li&gt;
&lt;li&gt;MISRA C++ 2008 — 33%;&lt;/li&gt;
&lt;li&gt;MISRA C++ 2023 — 35%.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the PVS-Studio 7.41 release, we completed work on covering the MISRA C 2023 standard, achieving 85% coverage. We're not stopping there: This year, we've already begun expanding our support for the MISRA C++ 2023 standard.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For more information on the classification of warnings according to the MISRA C and MISRA C++ standards, see the relevant section of the &lt;a href="https://pvs-studio.com/en/pvs-studio/sast/misra/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When working with MISRA standards, the &lt;a href="https://pvs-studio.com/en/docs/manual/6966/#misra_compliance" rel="noopener noreferrer"&gt;MISRA Compliance&lt;/a&gt; report is particularly useful. It assesses whether a project is compliant with MISRA C and/or MISRA C++, considering all deviations and re-categorizations.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Learn more about the MISRA Compliance report in the relevant section of the &lt;a href="https://pvs-studio.com/en/docs/manual/6966/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Dangerous code fragments in embedded projects
&lt;/h2&gt;

&lt;p&gt;We've looked at several ways to analyze projects for embedded systems using PVS-Studio. Now let's take a look at some real errors the analyzer found in open-source embedded projects.&lt;/p&gt;

&lt;p&gt;We'll use several popular real-time operating systems (RTOSs) as examples. An RTOS is a specialized OS designed to ensure tasks are completed within strict timing constraints. These systems are widely used in microcontrollers, industrial equipment, electronic devices, and other embedded applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  RT-Thread
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/RT-Thread/rt-thread" rel="noopener noreferrer"&gt;RT-Thread&lt;/a&gt;, which originated in China in 2006, is developed by the RT-Thread Development Team. It's widely used in IoT devices, home appliances, and industrial equipment. It offers a well-developed ecosystem with a wide range of built-in components, coming close to being a full-fledged OS.&lt;/p&gt;

&lt;p&gt;The project was checked as of the &lt;a href="https://github.com/RT-Thread/rt-thread/tree/cfda3b3d1a9935dd14dcb55e43065ca143d68fcf" rel="noopener noreferrer"&gt;cfda3b3&lt;/a&gt; commit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fragment N1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warnings:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v1031/" rel="noopener noreferrer"&gt;V1031&lt;/a&gt; The 'memcmp' function is not declared. Passing data to or from this function can be affected. dhcp_server_raw.c 151&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v1031/" rel="noopener noreferrer"&gt;V1031&lt;/a&gt; The 'strchr' function is not declared. Passing data to or from this function can be affected. dhcp_server_raw.c 718&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v647/" rel="noopener noreferrer"&gt;V647&lt;/a&gt; The value of 'int' type is assigned to the pointer of 'char' type. Consider inspecting the assignment: 'p = strchr(str_tmp, '.')'. dhcp_server_raw.c 718&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;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdint.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;dhcp_client_node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="nf"&gt;dhcp_client_find_by_mac&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;dhcp_server&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dhcpserver&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;u8_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;chaddr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;u8_t&lt;/span&gt; &lt;span class="n"&gt;hlen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;dhcp_client_node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dhcpserver&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;node_list&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;node&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="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&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;memcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;chaddr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chaddr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hlen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;               &lt;span class="c1"&gt;// &amp;lt;=&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;node&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;return&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="c1"&gt;// ....&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;dhcpd_start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;netif_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;str_tmp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DHCPD_SERVER_IP&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;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;str_tmp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;ip4_addr_t&lt;/span&gt; &lt;span class="n"&gt;ip_start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ip_end&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strchr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str_tmp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'.'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;      &lt;span class="c1"&gt;// &amp;lt;=&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;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strchr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'.'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;      &lt;span class="c1"&gt;// &amp;lt;=&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;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strchr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'.'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two interesting warnings stem from the same mistake, which fundamentally changes the behavior of the entire source file. The analyzer reports that the &lt;code&gt;memcmp&lt;/code&gt; and &lt;code&gt;strchr&lt;/code&gt; functions aren't declared. A quick look at the included headers shows that the file is missing &lt;code&gt;&amp;lt;string.h&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This code still compiles in C because an undeclared function is assumed to return &lt;code&gt;int&lt;/code&gt; by default. As a result, the analyzer also reports a second issue: an &lt;code&gt;int&lt;/code&gt; value is assigned to a &lt;code&gt;char&lt;/code&gt; pointer.&lt;/p&gt;

&lt;p&gt;Such code can lead to incorrect program behavior, as demonstrated in the article "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0033/" rel="noopener noreferrer"&gt;A nice 64-bit error in C&lt;/a&gt;". The fix is straightforward: simply add &lt;code&gt;#include &amp;lt;string.h&amp;gt;&lt;/code&gt; at the beginning of the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fragment N2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v614/" rel="noopener noreferrer"&gt;V614&lt;/a&gt; Potentially uninitialized pointer 'GPIOx' used. HAL_GPIO.c 48&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;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;GPIOA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;GPIOB&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;GPIOC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;GPIOD&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="n"&gt;enum_GPIOx_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;HAL_GPIO_IRQHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enum_GPIOx_t&lt;/span&gt; &lt;span class="n"&gt;fe_GPIO&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;fu32_GPIO_Pin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;GPIO_TypeDef&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;GPIOx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// &amp;lt;=&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;fe_GPIO&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;GPIOA&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;GPIOB&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;GPIOx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GPIOAB&lt;/span&gt;&lt;span class="p"&gt;;&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;GPIOC&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;GPIOD&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;GPIOx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GPIOCD&lt;/span&gt;&lt;span class="p"&gt;;&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;default&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// &amp;lt;=&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;fe_GPIO&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;GPIOB&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;fe_GPIO&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;GPIOD&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fu32_GPIO_Pin&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;16&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;GPIOx&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;RIS&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;fu32_GPIO_Pin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;GPIOx&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;IC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fu32_GPIO_Pin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/* user can call your application process function here */&lt;/span&gt;
    &lt;span class="cm"&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 analyzer warns that an uninitialized pointer is being used. The &lt;code&gt;GPIOx&lt;/code&gt; variable is declared, but it's assigned a value in only four &lt;code&gt;switch&lt;/code&gt; branches. In the &lt;code&gt;default&lt;/code&gt; branch, it remains uninitialized, so using the pointer afterward results in undefined behavior.&lt;/p&gt;

&lt;p&gt;In C, the &lt;code&gt;enum&lt;/code&gt; parameter can receive any integer value. If this happens, execution enters the &lt;code&gt;default&lt;/code&gt; branch, and the pointer remains uninitialized.&lt;/p&gt;

&lt;p&gt;To fix the issue, we'll add appropriate handling to the &lt;code&gt;default&lt;/code&gt; branch. For example, return from the function when &lt;code&gt;fe_GPIO&lt;/code&gt; contains an unknown value:&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="nl"&gt;default:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fragment N3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v570/" rel="noopener noreferrer"&gt;V570&lt;/a&gt; The 'RTC_DateStruct-&amp;gt;RTC_WeekDay' variable is assigned to itself. hk32f0xx_rtc.c 986&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="nf"&gt;RTC_GetDate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;RTC_Format&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RTC_DateTypeDef&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;RTC_DateStruct&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;tmpreg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="cm"&gt;/* Check the input parameters format */&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;RTC_Format&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;RTC_Format_BIN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* Convert the structure parameters to Binary format */&lt;/span&gt;
    &lt;span class="n"&gt;RTC_DateStruct&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;RTC_Year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
                        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;RTC_Bcd2ToByte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RTC_DateStruct&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;RTC_Year&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;RTC_DateStruct&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;RTC_Month&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
                        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;RTC_Bcd2ToByte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RTC_DateStruct&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;RTC_Month&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;RTC_DateStruct&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;RTC_Date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
                        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;RTC_Bcd2ToByte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RTC_DateStruct&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;RTC_Date&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;RTC_DateStruct&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;RTC_WeekDay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
                        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;RTC_DateStruct&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;RTC_WeekDay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// &amp;lt;=&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;This error is quite interesting. I reformatted the code snippet because the original was too wide, which makes the issue immediately obvious here. Developers most likely copied the lines converting the value type from &lt;code&gt;BCD&lt;/code&gt; to &lt;code&gt;binary&lt;/code&gt;, but in the last line they forgot to call &lt;code&gt;RTC_Bcd2ToByte&lt;/code&gt; (this is a &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0260/" rel="noopener noreferrer"&gt;classic mistake&lt;/a&gt;). As a result, &lt;code&gt;RTC_WeekDay&lt;/code&gt; is assigned to itself, which is pointless.&lt;/p&gt;

&lt;p&gt;The fixed code:&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="n"&gt;RTC_DateStruct&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;RTC_WeekDay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
                    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;RTC_Bcd2ToByte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RTC_DateStruct&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;RTC_WeekDay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fragment N4&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v595/" rel="noopener noreferrer"&gt;V595&lt;/a&gt; The 'cond' pointer was utilized before it was verified against nullptr. Check lines: 346, 353. pthread_cond.c 346&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="n"&gt;rt_err_t&lt;/span&gt; &lt;span class="nf"&gt;_pthread_cond_timedwait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pthread_cond_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cond&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                 &lt;span class="n"&gt;pthread_mutex_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;mutex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                 &lt;span class="n"&gt;rt_int32_t&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;rt_err_t&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;RT_EOK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;rt_sem_t&lt;/span&gt; &lt;span class="n"&gt;sem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;rt_int32_t&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;sem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cond&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;sem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// &amp;lt;=&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;sem&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;RT_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;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;RT_ERROR&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;timeout&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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;cond&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;mutex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;RT_ERROR&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Developers used the &lt;code&gt;cond&lt;/code&gt; pointer before checking it for &lt;code&gt;NULL&lt;/code&gt;. Such errors are &lt;a href="https://pvs-studio.com/en/blog/examples/v595/" rel="noopener noreferrer"&gt;quite common&lt;/a&gt; in projects.&lt;/p&gt;

&lt;p&gt;Two options are possible here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The check is redundant because a valid object is always passed to the function input.&lt;/li&gt;
&lt;li&gt;The check is necessary, but the compiler might remove it during optimization because it recognizes that the pointer is used further up in the code. In this case, a dereference of a null pointer occurs, resulting in undefined behavior. &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0306/" rel="noopener noreferrer"&gt;This article&lt;/a&gt; provides a more detailed look at a similar case, where the address of an object is obtained relative to a null pointer, followed by dereferencing the pointer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To fix the error, we'll add a check at the beginning of the function. For example:&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="n"&gt;rt_err_t&lt;/span&gt; &lt;span class="nf"&gt;_pthread_cond_timedwait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pthread_cond_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cond&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                 &lt;span class="n"&gt;pthread_mutex_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;mutex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                 &lt;span class="n"&gt;rt_int32_t&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;rt_err_t&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;RT_EOK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;rt_sem_t&lt;/span&gt; &lt;span class="n"&gt;sem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;rt_int32_t&lt;/span&gt; &lt;span class="n"&gt;time&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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;cond&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;mutex&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="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;RT_ERROR&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;sem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cond&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;sem&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;sem&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;RT_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;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;RT_ERROR&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  FreeRTOS
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/freertos" rel="noopener noreferrer"&gt;FreeRTOS&lt;/a&gt; was created by Richard Barry in 2003. Since 2017, it has been developed under Amazon Web Services. Its small kernel footprint and excellent portability across dozens of hardware platforms have made it a popular choice for microcontroller-based devices.&lt;/p&gt;

&lt;p&gt;The project was checked as of the &lt;a href="https://github.com/FreeRTOS/FreeRTOS/tree/c73a397a418b1d5935dcc4233e651cf1773a57ca" rel="noopener noreferrer"&gt;c73a397&lt;/a&gt; commit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fragment N1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v557/" rel="noopener noreferrer"&gt;V557&lt;/a&gt; Array overrun is possible. The value of 'uxTimerID' index could reach 21. TimerDemo.c 1167&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;uint8_t&lt;/span&gt; &lt;span class="n"&gt;ucAutoReloadTimerCounters&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;configTIMER_QUEUE_LENGTH&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// ....&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;prvAutoReloadTimerCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;TimerHandle_t&lt;/span&gt; &lt;span class="n"&gt;pxExpiredTimer&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;uxTimerID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;uxTimerID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;pvTimerGetTimerID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;pxExpiredTimer&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;uxTimerID&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;configTIMER_QUEUE_LENGTH&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;ucAutoReloadTimerCounters&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;uxTimerID&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This error is particularly sneaky and often slips through code reviews. Few reviewers stop to check an index boundary when the code appears to work correctly. Tests are also unlikely to catch it, which makes the error even harder to find. However, accessing an array out of bounds results in undefined behavior.&lt;/p&gt;

&lt;p&gt;The issue stems from using a non-strict comparison &lt;code&gt;&amp;lt;=&lt;/code&gt; to set the bounds of the &lt;code&gt;uxTimerID&lt;/code&gt; index. As a result, &lt;code&gt;uxTimerID&lt;/code&gt; can take the &lt;code&gt;configTIMER_QUEUE_LENGTH + 1&lt;/code&gt; value, which is also the size of the array. Accessing an element at that index results in going outside the array bounds.&lt;/p&gt;

&lt;p&gt;The fixed code:&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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;uxTimerID&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;configTIMER_QUEUE_LENGTH&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;&lt;strong&gt;Fragment N2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v547/" rel="noopener noreferrer"&gt;V547&lt;/a&gt; Expression 'i + 1 &amp;gt; (4 + 32)' is always false. trcSnapshotRecorder.c 831&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;uint8_t&lt;/span&gt; &lt;span class="nf"&gt;writeInt8&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="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;TRACE_ASSERT&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="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="p"&gt;)&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"writeInt8: buffer == NULL"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;MAX_ARG_SIZE&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="mi"&gt;255&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;uint8_t&lt;/span&gt;&lt;span class="o"&gt;*&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="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&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;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;MAX_ARG_SIZE&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="mi"&gt;255&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="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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 error is that the second check, &lt;code&gt;if (i + 1 &amp;gt; MAX_ARG_SIZE)&lt;/code&gt;, is always false after the first check, &lt;code&gt;if (i &amp;gt;= MAX_ARG_SIZE)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once &lt;code&gt;i&lt;/code&gt; passes the first check, &lt;code&gt;i + 1&lt;/code&gt; can no longer exceed &lt;code&gt;MAX_ARG_SIZE&lt;/code&gt;. It's likely that one of the checks is redundant, or the second condition was intended to be different.&lt;/p&gt;

&lt;h3&gt;
  
  
  Zephyr
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/zephyrproject-rtos/zephyr" rel="noopener noreferrer"&gt;Zephyr&lt;/a&gt; launched in 2016 with Intel's support and is now being developed under the Linux Foundation. It targets IoT, embedded devices, and automotive systems, and features a modular architecture with a wide range of built-in subsystems.&lt;/p&gt;

&lt;p&gt;The project was checked as of the &lt;a href="https://github.com/zephyrproject-rtos/zephyr/tree/c6da46432a348b5a07e40e48692428eb01c968c3" rel="noopener noreferrer"&gt;c6da464&lt;/a&gt; commit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fragment N1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v547/" rel="noopener noreferrer"&gt;V547&lt;/a&gt; Expression 'conv-&amp;gt;pad0_value &amp;gt; 0' is always true. cbprintf_complete.c 1224&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;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;encode_float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/*....*/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&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;decexp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;precision&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;conv&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;pad0_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;decexp&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;conv&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;pad0_value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;precision&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;conv&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;pad0_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;precision&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;precision&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;conv&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;pad0_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;conv&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;pad_postdp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conv&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;pad0_value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&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 reports that &lt;code&gt;conv-&amp;gt;pad0_value &amp;gt; 0&lt;/code&gt; is always true. As a result, the &lt;code&gt;conv-&amp;gt;pad_postdp&lt;/code&gt; variable always receives the same value. Here's why the condition is always true:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;As soon as execution enters the first &lt;code&gt;if&lt;/code&gt; block, &lt;code&gt;conv-&amp;gt;pad0_value&lt;/code&gt; is assigned the &lt;code&gt;-decexp&lt;/code&gt; value. Since &lt;code&gt;decexp &amp;lt; 0&lt;/code&gt;, the result is always positive.&lt;/li&gt;
&lt;li&gt;If execution enters the nested &lt;code&gt;if&lt;/code&gt; block, the value remains positive because &lt;code&gt;precision &amp;gt; 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;As a result, &lt;code&gt;conv-&amp;gt;pad0_value&lt;/code&gt; is always greater than zero.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Most likely, the condition is redundant and can be removed. Alternatively, the initialization of &lt;code&gt;pad0_value&lt;/code&gt; needs to be moved outside the &lt;code&gt;if&lt;/code&gt; block. The final decision is up to the developers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fragment N2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v557/" rel="noopener noreferrer"&gt;V557&lt;/a&gt; Array overrun is possible. The value of 'keep_cnt ++' index could reach 16. cbprintf_packaged.c 1143&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;int&lt;/span&gt; &lt;span class="nf"&gt;cbprintf_package_convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/*....*/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="n"&gt;__ASSERT_NO_MSG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keep_cnt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;keep_str_pos&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;keep_cnt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;keep_str_pos&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;keep_str_pos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;keep_cnt&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arg_idx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;keep_str_pos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;keep_cnt&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arg_pos&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a particularly interesting case. The developers correctly checked the array bounds but overlooked how the post-increment operator affects the index. Here's what happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the &lt;code&gt;keep_cnt &amp;lt; sizeof(keep_str_pos)&lt;/code&gt; condition allows &lt;code&gt;keep_cnt&lt;/code&gt; to reach &lt;code&gt;N - 1&lt;/code&gt;, where &lt;code&gt;N&lt;/code&gt; is the array size;&lt;/li&gt;
&lt;li&gt;the first post-increment accesses the array at the &lt;code&gt;N - 1&lt;/code&gt; index and increments &lt;code&gt;keep_cnt&lt;/code&gt; to &lt;code&gt;N&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;the second post-increment accesses the array at the &lt;code&gt;N&lt;/code&gt; index and increments &lt;code&gt;keep_cnt&lt;/code&gt; to &lt;code&gt;N + 1&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Accessing outside the array bounds results in undefined behavior. We can fix the code by rewriting the condition as follows:&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keep_cnt&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;keep_str_pos&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fragment N3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v779/" rel="noopener noreferrer"&gt;V779&lt;/a&gt; Unreachable code detected. It is possible that an error is present. sched.c 345&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;#define z_except_reason(reason) do { \
    __EXCEPT_LOC();              \
    z_fatal_error(reason, NULL); \
  } while (false)
&lt;/span&gt;
&lt;span class="cp"&gt;#define k_panic()  z_except_reason(K_ERR_KERNEL_PANIC)
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;z_thread_halt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/*....*/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;_current&lt;/span&gt;&lt;span class="p"&gt;)&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;arch_is_in_isr&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;z_is_thread_essential&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;k_spin_unlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;_sched_spinlock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;k_panic&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;                                   &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
      &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;k_spin_lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;_sched_spinlock&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;         &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&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 detected unreachable code. The &lt;code&gt;k_panic&lt;/code&gt; macro calls &lt;code&gt;z_fatal_error&lt;/code&gt;, which terminates the system and never returns the control flow. As a result, the line that follows it is never executed.&lt;/p&gt;

&lt;p&gt;Unfortunately, it's hard to recommend the right solution without more information. It's possible that this line simply doesn't belong here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fragment N4&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v795/" rel="noopener noreferrer"&gt;V795&lt;/a&gt; Please note that the size of the 'time_t' type is not 64 bits. After year 2038, the program will work incorrectly. clock.c 47&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;timespec_from_ticks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;ticks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;timespec&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;elapsed_secs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ticks&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;CONFIG_SYS_CLOCK_TICKS_PER_SEC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;nremainder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ticks&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;CONFIG_SYS_CLOCK_TICKS_PER_SEC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;timespec&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_sec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;time_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;elapsed_secs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="cm"&gt;/* For ns 32 bit conversion can be used since its smaller than 1sec. */&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tv_nsec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;k_ticks_to_ns_floor32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nremainder&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 problems of the future are already knocking at the door. In just 12 years, on January 19, 2038, this project will run into the classic &lt;a href="https://en.wikipedia.org/wiki/Year_2038_problem" rel="noopener noreferrer"&gt;Year 2038 problem&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The root cause is that &lt;code&gt;time_t&lt;/code&gt; will no longer behave as expected. Its behavior will depend on the platform because the type stores the number of seconds since January 1, 1970. After the cutoff date, the value will overflow, causing incorrect time calculations.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to fix issues
&lt;/h2&gt;

&lt;p&gt;To keep dangerous code patterns out of your codebase, it's important to take a comprehensive approach to software quality. This may include code reviews, manual and automated testing, dynamic analysis, and other verification techniques.&lt;/p&gt;

&lt;p&gt;Static code analysis is one of the most effective and cost-efficient ways to catch issues early in the development process. &lt;a href="https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&amp;amp;utm_medium=devto&amp;amp;utm_campaign=article&amp;amp;utm_content=1399" rel="noopener noreferrer"&gt;Try PVS-Studio&lt;/a&gt; on your project for free and &lt;a href="https://pvs-studio.com/en/order/" rel="noopener noreferrer"&gt;explore pricing&lt;/a&gt; for the full version. &lt;/p&gt;

&lt;p&gt;If you're still looking for a reason to adopt static analysis, here are &lt;a href="https://pvs-studio.com/en/blog/posts/1046/" rel="noopener noreferrer"&gt;five reasons why it is important for business&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional links:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0863/" rel="noopener noreferrer"&gt;Why do you need the MISRA Compliance report and how to generate one in PVS-Studio?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0702/" rel="noopener noreferrer"&gt;What is MISRA and how to cook it&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/docs/manual/0036/" rel="noopener noreferrer"&gt;How to run PVS-Studio on Linux and macOS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/docs/manual/0031/" rel="noopener noreferrer"&gt;Build-system independent analysis (C and C++)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>iot</category>
      <category>programming</category>
      <category>software</category>
      <category>tooling</category>
    </item>
    <item>
      <title>Inside cloud-native Java: Exploring Quarkus</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Mon, 03 Aug 2026 14:12:58 +0000</pubDate>
      <link>https://dev.to/pvsdev/inside-cloud-native-java-exploring-quarkus-2454</link>
      <guid>https://dev.to/pvsdev/inside-cloud-native-java-exploring-quarkus-2454</guid>
      <description>&lt;p&gt;Java keeps finding itself on cloud nine these days, all because of cloud hosting. With more and more applications and services adopting cloud-native architectures, it's a good time to take a closer look at Quarkus, which is one of the leading frameworks for building high-performance cloud applications.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F66dprltjp1o2r4b0e814.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F66dprltjp1o2r4b0e814.png" alt="1398_Quarkus/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;We regularly &lt;a href="https://pvs-studio.com/en/blog/inspections/" rel="noopener noreferrer"&gt;check&lt;/a&gt; open-source projects with our PVS-Studio analyzer and publish articles about the errors we find. Articles like these give readers a better understanding of static analysis, highlight the most common errors, and showcase the nuances of developing in a particular language.&lt;/p&gt;

&lt;p&gt;Large-scale projects are especially interesting to look at, since they're constantly evolving. They have massive codebases, and their development pulls in a wide range of approaches and technologies. That variety carries over into the bugs themselves.&lt;/p&gt;

&lt;p&gt;This time we picked the popular project—&lt;a href="https://github.com/quarkusio/quarkus" rel="noopener noreferrer"&gt;Quarkus&lt;/a&gt;, a modern framework built for developing high-performance cloud applications in Java, using technologies that have become standard in enterprise development: microservices, DI containers, and more. &lt;/p&gt;

&lt;p&gt;Quarkus focuses on production performance by &lt;a href="https://quarkus.io/performance/" rel="noopener noreferrer"&gt;shifting much of the work to the build phase&lt;/a&gt;: including reading part of the application configuration, scanning the classpath for annotated classes, and constructing a model of the application. Quarkus apps also compile easily into a native image.&lt;/p&gt;

&lt;p&gt;This makes it clear we're dealing with a large, complex project, so we decided to see what errors we could find. We ran our analysis on this &lt;a href="https://github.com/quarkusio/quarkus/tree/19d6bc987d61c3d70995b557a93e840a1106bef0" rel="noopener noreferrer"&gt;commit&lt;/a&gt;. Here are the results.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to fix a Problem Child
&lt;/h2&gt;

&lt;p&gt;No, this isn't a lecture on parenting. I'll just remind you of some of the pitfalls hiding in Java's inheritance hierarchy. Here's the code we'll be looking at.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 1&lt;/strong&gt;&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;InterceptorInfo&lt;/span&gt; &lt;span class="n"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;BeanInfo&lt;/span&gt; 
                             &lt;span class="n"&gt;implements&lt;/span&gt; &lt;span class="n"&gt;Comparable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;InterceptorInfo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;AnnotationInstance&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bindings&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt; 
  &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;Override&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;toString&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="s"&gt;"INTERCEPTOR bean [bindings="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;bindings&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; 
           &lt;span class="s"&gt;", target="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;getTarget&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the &lt;code&gt;toString&lt;/code&gt; method uses the &lt;code&gt;bindings&lt;/code&gt; field of the &lt;code&gt;InterceptorInfo&lt;/code&gt; class. Now let's look at the constructor of the parent class &lt;code&gt;BeanInfo&lt;/code&gt;:&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="n"&gt;BeanInfo&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;identifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Hashes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sha1_base64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;identifier&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;identifier&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="o"&gt;+&lt;/span&gt; 
            &lt;span class="n"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;beanDeployment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toString&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;To finally put all the pieces of the puzzle together, here's the &lt;code&gt;InterceptorInfo&lt;/code&gt; class constructor:&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="n"&gt;InterceptorInfo&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="n"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;AnnotationInstance&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bindings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;super&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bindings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bindings&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;Let's trace through what's actually happening here. When &lt;code&gt;InterceptorInfo&lt;/code&gt; is initialized, the constructor of its parent class, &lt;code&gt;BeanInfo&lt;/code&gt;, gets called first. That constructor, in turn, calls the &lt;code&gt;toString&lt;/code&gt; method, which is overridden in the &lt;code&gt;InterceptorInfo&lt;/code&gt; class. At this point, the &lt;code&gt;toString&lt;/code&gt; method uses the &lt;code&gt;bindings&lt;/code&gt; field before it's been initialized. The resulting string ends up reflecting the object's state before initialization. &lt;/p&gt;

&lt;p&gt;We covered this issue in more detail, along with how to deal with it, in a separate &lt;a href="https://pvs-studio.com/en/blog/posts/java/1132/" rel="noopener noreferrer"&gt;article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6052/" rel="noopener noreferrer"&gt;V6052&lt;/a&gt; Calling overridden 'toString' method in 'BeanInfo' parent-class constructor may lead to use of uninitialized data. Inspect field: bindings. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/InterceptorInfo.java#L266" rel="noopener noreferrer"&gt;InterceptorInfo.java 265&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lost and found
&lt;/h2&gt;

&lt;p&gt;Forgotten, lost, or mixed-up code snippets tend to show up in every project sooner or later, and Quarkus isn't an exception.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 2&lt;/strong&gt;&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;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Path&lt;/span&gt; &lt;span class="n"&gt;projectDir&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;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ModelUtils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;persistModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;projectDir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"pom.xml"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;IllegalStateException&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;   &lt;span class="c1"&gt;// &amp;lt;= &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 class method is responsible for building the Maven project model and its dependencies. As the snippet shows, saving the model file can throw an exception, and that exception gets replaced by an &lt;code&gt;IllegalStateException&lt;/code&gt;. This isn't a great practice, because if something goes wrong, the exception thrown from the &lt;code&gt;build&lt;/code&gt; method won't carry any information about the actual error. No one will ever know what skeletons are hiding in that closet.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6118/" rel="noopener noreferrer"&gt;V6118&lt;/a&gt; The original exception object 'IOException' was swallowed. Cause of original exception could be lost. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/independent-projects/bootstrap/maven-resolver/src/test/java/io/quarkus/bootstrap/workspace/test/MvnProjectBuilder.java#L123" rel="noopener noreferrer"&gt;MvnProjectBuilder.java 122&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 3&lt;/strong&gt;&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="n"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Bar&lt;/span&gt; &lt;span class="n"&gt;bar1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Bar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="n"&gt;event1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fire&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bar1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getNames&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;     &lt;span class="c1"&gt;// &amp;lt;= &lt;/span&gt;
&lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bazinga"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getNames&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="n"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Bar&lt;/span&gt; &lt;span class="n"&gt;bar2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Bar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="n"&gt;event2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fire&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bar2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getNames&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;     &lt;span class="c1"&gt;// &amp;lt;= &lt;/span&gt;
&lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bazinga"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bar2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getNames&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an excerpt from tests that verify the event mechanism. Two instances of &lt;code&gt;Foo.Bar&lt;/code&gt; get created and tested here. It's easy to see that the second block of test code is copy-pasted from the first. The duplicated line &lt;code&gt;assertEquals(1, bar1.getNames().size())&lt;/code&gt;, still references &lt;code&gt;bar1&lt;/code&gt; instead of &lt;code&gt;bar2&lt;/code&gt;, so the check ends up testing the wrong object's state.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6072/" rel="noopener noreferrer"&gt;V6072&lt;/a&gt; Two similar code fragments were found. Perhaps, this is a typo and 'bar2' variable should be used instead of 'bar1'. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/integration-tests/injectmock/src/test/java/io/quarkus/it/mockbean/MockEventTest.java#L47" rel="noopener noreferrer"&gt;MockEventTest.java&lt;/a&gt; 40&lt;/p&gt;

&lt;p&gt;By the way, we've added the exact same &lt;a href="https://pvs-studio.com/en/docs/warnings/v7023/" rel="noopener noreferrer"&gt;rule&lt;/a&gt; to our new JavaScript and TypeScript analyzer. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 4&lt;/strong&gt;&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PathTreeBuilder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;excludes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;getIncludes&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;includes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;getExcludes&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;includes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// &amp;lt;= &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;As the class name suggests, &lt;code&gt;PathTreeBuilder&lt;/code&gt; creates a &lt;code&gt;PathTree&lt;/code&gt; instance, which is used to easily manage a project's dependencies as a tree structure. The builder has methods for populating the &lt;code&gt;includes&lt;/code&gt; and &lt;code&gt;excludes&lt;/code&gt; fields, but the &lt;code&gt;getExcludes&lt;/code&gt; method mistakenly returns the &lt;code&gt;includes&lt;/code&gt; field instead. &lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6091/" rel="noopener noreferrer"&gt;V6091&lt;/a&gt; Suspicious getter implementation. The 'excludes' field should probably be returned instead. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/independent-projects/bootstrap/app-model/src/main/java/io/quarkus/paths/PathTreeBuilder.java#L59" rel="noopener noreferrer"&gt;PathTreeBuilder.java&lt;/a&gt; 58&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 5&lt;/strong&gt;&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;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isIgnored&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DotName&lt;/span&gt; &lt;span class="n"&gt;classDotName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;className&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;classDotName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toString&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;className&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"java.util."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
      &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;className&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"java.lang."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;className&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"org.hibernate.engine.spi."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;className&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"jakarta.persistence."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;className&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"jakarta.persistence."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;// &amp;lt;=&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="nb"&gt;true&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="nb"&gt;false&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 method is used in the &lt;code&gt;JpaJandexScavenger&lt;/code&gt; class to filter the classes needed for the integrated Hibernate ORM to work. The class itself determines in advance which types it needs. As mentioned in the Intro, Quarkus aims to move part of its logic to build time, and &lt;code&gt;JpaJandexScavenger&lt;/code&gt; is a direct implementation of that idea.&lt;/p&gt;

&lt;p&gt;In the method body, you can see the &lt;code&gt;jakarta.persistence.&lt;/code&gt; package repeated in the logical condition:&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="n"&gt;className&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"jakarta.persistence."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
&lt;span class="n"&gt;className&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"jakarta.persistence."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Given what this method is for, it's possible that another restriction was meant to go here and got left out. This probably won't affect functionality, but the final size of the application could grow slightly, since more classes end up covered by metadata than necessary. Or maybe it's all simpler than that, and it's just an extra check that was left in by mistake.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6001/" rel="noopener noreferrer"&gt;V6001&lt;/a&gt; There are identical sub-expressions 'className.startsWith("jakarta.persistence.")' to the left and to the right of the '||' operator. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/JpaJandexScavenger.java#L622" rel="noopener noreferrer"&gt;JpaJandexScavenger.java 621&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When the math isn't mathing
&lt;/h2&gt;

&lt;p&gt;Numbers always have their own ways of going wrong. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 6&lt;/strong&gt;&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="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;ConsumeEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"address-4"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;CompletionStage&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;listenAddress4&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;i&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;CompletableFuture&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completedFuture&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;If the variable &lt;code&gt;i&lt;/code&gt; equals to &lt;code&gt;Integer.MAX_VALUE&lt;/code&gt;, adding one to it causes an overflow, and the result becomes &lt;code&gt;Integer.MIN_VALUE&lt;/code&gt;. We can prevent this by casting one of the operands to &lt;code&gt;long&lt;/code&gt;. Although there's a type cast in the example, it's applied to the result of the operation, so it doesn't actually prevent the overflow.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6117/" rel="noopener noreferrer"&gt;V6117&lt;/a&gt; Possible overflow. The expression will be evaluated before casting. Consider casting one of the operands instead. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/vertx/deployment/src/test/java/io/quarkus/vertx/CodecRegistrationTest.java#L195" rel="noopener noreferrer"&gt;CodecRegistrationTest.java 195&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 7&lt;/strong&gt;&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="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;Override&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SocketSettings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Builder&lt;/span&gt; &lt;span class="n"&gt;builder&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;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connectTimeout&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;isPresent&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connectTimeout&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connectTimeout&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;       &lt;span class="c1"&gt;// &amp;lt;= &lt;/span&gt;
                                       &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toMillis&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;TimeUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MILLISECONDS&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;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readTimeout&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;isPresent&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readTimeout&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readTimeout&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;        &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
                                    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toMillis&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;TimeUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MILLISECONDS&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;Here's another cast that can lead to an overflow. Its purpose is unclear, especially since both &lt;code&gt;connectTimeout&lt;/code&gt; and &lt;code&gt;readTimeout&lt;/code&gt; on the &lt;code&gt;builder&lt;/code&gt; variable take a &lt;code&gt;long&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The PVS-Studio analyzer issues the following warnings for these spots:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6106/" rel="noopener noreferrer"&gt;V6106&lt;/a&gt; Casting expression to int type before implicitly casting it to other type may be excessive or incorrect. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClients.java#L289" rel="noopener noreferrer"&gt;MongoClients.java 289&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6106/" rel="noopener noreferrer"&gt;V6106&lt;/a&gt; Casting expression to int type before implicitly casting it to other type may be excessive or incorrect. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoClients.java#L286" rel="noopener noreferrer"&gt;MongoClients.java 286&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 8&lt;/strong&gt;&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;takeLast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;list&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;n&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;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;IndexOutOfBoundsException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&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;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;   &lt;span class="c1"&gt;// &amp;lt;= &lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;list&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;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&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 method takes the last &lt;code&gt;n&lt;/code&gt; elements from &lt;code&gt;list&lt;/code&gt;. The check above implies we can't request fewer than one item, or more than the list actually contains. But the check also implies the list itself can't be empty. If we're taking at least one element from the list, the list has to contain at least one element already. So, the check right below, &lt;code&gt;list.isEmpty()&lt;/code&gt;, always evaluates to false. If the intent was to handle an empty list as a special case, this check won't catch it, and it needs to be reworked.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6007/" rel="noopener noreferrer"&gt;V6007&lt;/a&gt; Expression 'list.isEmpty()' is always false. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/qute/runtime/src/main/java/io/quarkus/qute/runtime/extensions/CollectionTemplateExtensions.java#L57" rel="noopener noreferrer"&gt;CollectionTemplateExtensions.java 57&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 9&lt;/strong&gt;&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errorRate&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ERROR"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errorRate&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;toPlainString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Prevent E notation&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one's worth a closer look. If we pass &lt;code&gt;0.1&lt;/code&gt; into the &lt;code&gt;BigDecimal&lt;/code&gt; constructor, what actually gets stored is &lt;code&gt;0.1000000000000000055511151231257827021181583404541015625&lt;/code&gt;. This "tail" is a natural feature of how real numbers are represented in a computer. If you're curious why this happens, here's a &lt;a href="https://en.wikipedia.org/wiki/IEEE_754" rel="noopener noreferrer"&gt;link&lt;/a&gt; with the theory behind it.&lt;/p&gt;

&lt;p&gt;Now let's see how to create the object without that tail showing up as a surprise. The standard library provides the &lt;a href="https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/math/BigDecimal.html#valueOf(double)" rel="noopener noreferrer"&gt;&lt;code&gt;BigDecimal#valueOf&lt;/code&gt;&lt;/a&gt; method for exactly this, and it implements some clever logic:&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;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;BigDecimal&lt;/span&gt; &lt;span class="nf"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;val&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="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FormattedFPDecimal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;valueForDoubleToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getSignificand&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;Roughly speaking, here's what happens: the method takes the binary representation of a real number and converts it into a decimal representation, one that's correctly rounded and as short as possible. For the number we're discussing, that string comes out to exactly 0.1. So the &lt;a href="https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/math/BigDecimal.html#valueOf(double)" rel="noopener noreferrer"&gt;&lt;code&gt;BigDecimal#valueOf&lt;/code&gt;&lt;/a&gt; method will preserve our exact value.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  But it wasn't always like that.
  &lt;br&gt;
This method first appeared in Java 5 and looked completely different:&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;BigDecimal&lt;/span&gt; &lt;span class="nf"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;val&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="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&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;A string was used instead of a decimal representation here, but the problem was solved the same way overall.&lt;/p&gt;

&lt;p&gt;By the way, the &lt;a href="https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/math/BigDecimal.html#valueOf(double)" rel="noopener noreferrer"&gt;&lt;code&gt;BigDecimal#valueOf&lt;/code&gt;&lt;/a&gt; method still refers to &lt;a href="https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Double.html#toString(double)" rel="noopener noreferrer"&gt;&lt;code&gt;Double#toString()&lt;/code&gt;&lt;/a&gt; in its Javadoc, even though it now uses different methods under the hood these days. The core of the algorithm hasn't changed though, and it's well documented in the Javadoc for the &lt;a href="https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Double.html#toString(double)" rel="noopener noreferrer"&gt;&lt;code&gt;Double#toString()&lt;/code&gt;&lt;/a&gt; method. &lt;/p&gt;

&lt;p&gt;Before Java 5, the &lt;code&gt;BigDecimal(String)&lt;/code&gt; constructor had already existed and was the recommended choice over &lt;code&gt;BigDecimal(double)&lt;/code&gt;.&lt;/p&gt;



&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Such nuances are particularly important in high-precision computing systems, where even small errors can lead to unexpected consequences.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6068/" rel="noopener noreferrer"&gt;V6068&lt;/a&gt; Constructor call can result in imprecise representation of the initialized value. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/redis-client/runtime/src/main/java/io/quarkus/redis/datasource/bloom/BfInsertArgs.java#L95" rel="noopener noreferrer"&gt;BfInsertArgs.java 95&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  New Year's frenzy
&lt;/h2&gt;

&lt;p&gt;If you suddenly find yourself in the past or the future after celebrating New Year's, don't panic, it's just a bug.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 10&lt;/strong&gt;&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WebSocketNextJsonRPCService&lt;/span&gt; &lt;span class="n"&gt;implements&lt;/span&gt; &lt;span class="n"&gt;ConnectionListener&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="n"&gt;DateTimeFormatter&lt;/span&gt; &lt;span class="n"&gt;FORMATTER&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
    &lt;span class="n"&gt;DateTimeFormatter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ofPattern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"YYYY-MM-dd HH:mm:ss"&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;At first glance, there's nothing wrong here at all, but take a closer look at the date format string: &lt;code&gt;YYYY-MM-dd&lt;/code&gt;. Still looks fine on the surface, but there's a catch.&lt;/p&gt;

&lt;p&gt;If the current date is, say, January 1, 2027, this format will display it as January 1, 2026. &lt;/p&gt;

&lt;p&gt;That's because &lt;code&gt;YYYY&lt;/code&gt; displays the year based on the week number, not the calendar date. There's a specific rule for it too: if most of the week falls in the previous year, that's the year that gets shown. In our example, January 1 falls on a Friday, so most of that week belongs to 2026.&lt;/p&gt;

&lt;p&gt;The reverse happens when only a small part of the week falls in the outgoing year. December 31, 2024, for instance, would show up as December 31, 2025, in this format, since most of that week belongs to 2025. A whole year gone, and nobody even noticed. &lt;/p&gt;

&lt;p&gt;This bug is unusual enough that we devoted a separate &lt;a href="https://pvs-studio.com/en/blog/posts/java/1185/" rel="noopener noreferrer"&gt;article&lt;/a&gt; to it. &lt;/p&gt;

&lt;p&gt;We can fix this by replacing &lt;code&gt;YYYY&lt;/code&gt; with &lt;code&gt;yyyy&lt;/code&gt;, which is exactly what PVS-Studio says:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6122/" rel="noopener noreferrer"&gt;V6122&lt;/a&gt; Usage of 'Y' (week year) pattern was detected: it was probably intended to use 'y' (year). &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/websockets-next/runtime-dev/src/main/java/io/quarkus/websockets/next/runtime/dev/ui/WebSocketNextJsonRPCService.java#L39" rel="noopener noreferrer"&gt;WebSocketNextJsonRPCService.java 39&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The broken windows theory
&lt;/h2&gt;

&lt;p&gt;We explained what this theory is and how it applies to development in the &lt;a href="https://pvs-studio.com/en/blog/posts/1237/#IDED1685F479" rel="noopener noreferrer"&gt;article&lt;/a&gt;. Now let's see once again how it plays out in practice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 11&lt;/strong&gt;&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="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;Override&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;getClaim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;claimName&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;claimName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;groups&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;getGroups&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;claimName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;groups&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;// &amp;lt;=&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;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;getAudience&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;claimName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exp&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getExpirationTime&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;claimName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iat&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getIssuedAtTime&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;claimName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;aud&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;getAudience&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getClaim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;claimName&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 PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6003/" rel="noopener noreferrer"&gt;V6003&lt;/a&gt; The use of 'if (A) {...} else if (A) {...}' pattern was detected. There is a probability of logical error presence. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/amazon-lambda-rest/runtime/src/main/java/io/quarkus/amazon/lambda/http/CognitoPrincipal.java#L38" rel="noopener noreferrer"&gt;CognitoPrincipal.java 38&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Here's what's happening: the second condition in the &lt;code&gt;if-else&lt;/code&gt; chain has a typo, checking &lt;code&gt;Claims.groups&lt;/code&gt; again, while still returning the result of the &lt;code&gt;getAudience&lt;/code&gt; method call. One might argue that it's no big deal, since the correct condition appears later in the chain, and the second check, &lt;code&gt;claimName.equals(Claims.groups)&lt;/code&gt;, will never be true at that point anyway, since it already failed the first condition. But that's not really the issue here. The warning is issued for the &lt;code&gt;amazon-lambda-rest&lt;/code&gt; package. Now let's take a look at another warning nearby, in the &lt;code&gt;amazon-lambda-http&lt;/code&gt; package:&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="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;Override&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;getClaim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;claimName&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;claimName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;groups&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;getGroups&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;claimName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;groups&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;// &amp;lt;=&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;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;getAudience&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;claimName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exp&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getExpirationTime&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;claimName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iat&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getIssuedAtTime&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;claimName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;aud&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;getAudience&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;getClaims&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;getClaims&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;claimName&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;Same exact code, same exact bug. The only difference is in the last line. That's how buggy code spreads through a project via simple copy-paste. &lt;/p&gt;

&lt;p&gt;PVS-Studio triggers in the second package: &lt;a href="https://pvs-studio.com/en/docs/warnings/v6003/" rel="noopener noreferrer"&gt;V6003&lt;/a&gt; The use of 'if (A) {...} else if (A) {...}' pattern was detected. There is a probability of logical error presence. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/amazon-lambda-http/runtime/src/main/java/io/quarkus/amazon/lambda/http/CognitoPrincipal.java#L41" rel="noopener noreferrer"&gt;CognitoPrincipal.java 41&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 12&lt;/strong&gt;&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="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;BuildStep&lt;/span&gt;
&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;Record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ExecutionTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RUNTIME_INIT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;VertxWebRouterBuildItem&lt;/span&gt; &lt;span class="nf"&gt;initializeRouter&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="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RouteBuildItem&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;redirectRoutes&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;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frameworkRouterCreated&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;redirectRoutes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;    &lt;span class="c1"&gt;// &amp;lt;= &lt;/span&gt;
      &lt;span class="n"&gt;recorder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setNonApplicationRedirectHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;nonApplicationRootPath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getNonApplicationRootPath&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="n"&gt;nonApplicationRootPath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getNormalizedHttpRootPath&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;redirectRoutes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;recorder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;addRoute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                           &lt;span class="n"&gt;httpRouteRouter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                           &lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getRouteFunction&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
                           &lt;span class="n"&gt;recorder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getNonApplicationRedirectHandler&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
                           &lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getType&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="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;VertxWebRouterBuildItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;httpRouteRouter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mainRouter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                     &lt;span class="n"&gt;frameworkRouter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;managementRouter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                     &lt;span class="n"&gt;mutinyRouter&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 PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6007/" rel="noopener noreferrer"&gt;V6007&lt;/a&gt; Expression 'redirectRoutes.size() &amp;gt; 0' is always false. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/VertxHttpProcessor.java#L392" rel="noopener noreferrer"&gt;VertxHttpProcessor.java 392&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, part of the logic never runs, since the &lt;code&gt;redirectRoutes&lt;/code&gt; collection never gets populated within the method and is never passed anywhere either. We decided to look at the project's history for this file, and we found a &lt;a href="https://github.com/quarkusio/quarkus/commit/e94ac2b8a2c973b81a23498b288fba4f6df7e76a#diff-f15233cea0f87118b4441038738a41197a55fb6e3e1f3e06efd1dc4b41fe0143" rel="noopener noreferrer"&gt;commit&lt;/a&gt; from 2021 with some curious lines that had been removed:&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;httpBuildTimeConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;redirectToNonApplicationRootPath&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; 
    &lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isRequiresLegacyRedirect&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="n"&gt;redirectRoutes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;route&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;So, what do broken windows have to do with any of this? Extra code left in one place can lead to the same kind of code appearing elsewhere in the project. Take that abandoned code, for example: it uses a method that never gets removed, because it's still needed right there. As a result, the abandoned code now touches two classes, and this chain could keep extending further. The end result is a bloated codebase that gets harder and harder to reason about.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't trust environment variables
&lt;/h2&gt;

&lt;p&gt;Here's the code the analyzer highlighted. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 13&lt;/strong&gt;&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="k"&gt;final&lt;/span&gt; &lt;span class="n"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;IS_CYGWIN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;OS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WINDOWS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isCurrent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"PWD"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;
        &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"PWD"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;startsWith&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code reads the &lt;code&gt;PWD&lt;/code&gt; environment variable, usually used to indicate the current working directory. It comes with its own quirks though: it might not be set at all, or it might be overridden from outside. . For consistent results, it's safer to use the JVM system property &lt;a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/System.html#user.dir" rel="noopener noreferrer"&gt;&lt;code&gt;user.dir&lt;/code&gt;&lt;/a&gt;, since it's guaranteed to be set when the application starts and can't be changed externally.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warnings for this code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6110/" rel="noopener noreferrer"&gt;V6110&lt;/a&gt; Using the 'PWD' environment variable could be unsafe or unreliable. Consider using trusted system property 'user.dir' instead. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/core/devmode-spi/src/main/java/io/quarkus/dev/console/TerminalUtils.java#L27" rel="noopener noreferrer"&gt;TerminalUtils.java 27&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6110/" rel="noopener noreferrer"&gt;V6110&lt;/a&gt; Using the 'PWD' environment variable could be unsafe or unreliable. Consider using trusted system property 'user.dir' instead. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/core/devmode-spi/src/main/java/io/quarkus/dev/console/TerminalUtils.java#L28" rel="noopener noreferrer"&gt;TerminalUtils.java 28&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The project also has several similar warnings for other values, which should likewise be retrieved through &lt;code&gt;System.getProperty&lt;/code&gt;: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6110/" rel="noopener noreferrer"&gt;V6110&lt;/a&gt; Using the 'HOME' environment variable could be unsafe or unreliable. Consider using trusted system property 'user.home' instead. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/tls-registry/cli/src/main/java/io/quarkus/tls/cli/Constants.java#L11" rel="noopener noreferrer"&gt;Constants.java 11&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6110/" rel="noopener noreferrer"&gt;V6110&lt;/a&gt; Using the 'USER' environment variable could be unsafe or unreliable. Consider using trusted system property 'user.name' instead. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/AnalyticsService.java#L231" rel="noopener noreferrer"&gt;AnalyticsService.java 231&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges of multithreaded programming
&lt;/h2&gt;

&lt;p&gt;Multithreaded code punishes even small mistakes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 14&lt;/strong&gt;&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;KubernetesDevUIProcessor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;volatile&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Manifest&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;manifests&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Manifest&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;getManifests&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="n"&gt;BootstrapException&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;manifests&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;synchronized&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Holder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;class&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;manifests&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;manifests&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;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;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;try&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CuratedApplication&lt;/span&gt; &lt;span class="n"&gt;bootstrap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;quarkusBootstrap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bootstrap&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;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;entrySet&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="n"&gt;manifests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                             &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Manifest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getKey&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; 
                             &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getValue&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;manifests&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;This is an example of an incorrect implementation of the double-checked locking pattern applied to the  &lt;code&gt;manifests&lt;/code&gt; field. If you want to know how this pattern works and why it's used, we cover that in the &lt;a href="https://pvs-studio.com/en/blog/posts/java/1128/#ID1BD1BA8916" rel="noopener noreferrer"&gt;article&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;When the thread enters the synchronization block, it initializes the &lt;code&gt;manifests&lt;/code&gt; field with an empty list and then proceeds to populate it. In other words, it was originally assumed that the field would become available once it had been created and populated with values. However, the publication occurs right after &lt;code&gt;manifests&lt;/code&gt; is initialized, so another thread might end up with a list that's been created but not yet populated. &lt;/p&gt;

&lt;p&gt;Fixing this is straightforward: just initialize the field with a fully built list from a local variable.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6082/" rel="noopener noreferrer"&gt;V6082&lt;/a&gt; Unsafe double-checked locking. Object was assigned to the field before it was initialized. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/devui/KubernetesDevUIProcessor.java#L66" rel="noopener noreferrer"&gt;KubernetesDevUIProcessor.java 66&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 15&lt;/strong&gt;&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;VertxUdpMetrics&lt;/span&gt; &lt;span class="n"&gt;implements&lt;/span&gt; &lt;span class="n"&gt;DatagramSocketMetrics&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;volatile&lt;/span&gt; &lt;span class="n"&gt;Tags&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;Override&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;listening&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;localName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SocketAddress&lt;/span&gt; &lt;span class="n"&gt;localAddress&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;tags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;and&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"address"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;NetworkMetrics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;localAddress&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;   &lt;span class="c1"&gt;// &amp;lt;= &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 &lt;code&gt;tags&lt;/code&gt; field has been marked with the keyword &lt;code&gt;volatile&lt;/code&gt;. Just to recap, here's what that means in Java: if one thread modifies a variable, another thread immediately sees that change. However, this works flawlessly for atomic operations. If the logic involves several actions, a completely different approach is required to avoid a &lt;a href="https://en.wikipedia.org/wiki/Race_condition" rel="noopener noreferrer"&gt;race condition&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To see what's happening here, suppose two threads call the &lt;code&gt;listening&lt;/code&gt; method at the same time. Both entered the method, both created a new instance using the &lt;code&gt;and&lt;/code&gt; method, and both attempted to store the new value in the &lt;code&gt;tags&lt;/code&gt; variable. &lt;/p&gt;

&lt;p&gt;These three operations don't add up to a single atomic operation, so the &lt;code&gt;volatile&lt;/code&gt; keyword won't help here. A different approach is needed instead, like making the method synchronized.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6074/" rel="noopener noreferrer"&gt;V6074&lt;/a&gt; Non-atomic modification of volatile variable. Inspect 'tags'. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/vertx/VertxUdpMetrics.java#L37" rel="noopener noreferrer"&gt;VertxUdpMetrics.java 37&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Attention, please!
&lt;/h2&gt;

&lt;p&gt;This is a case where carelessness led to buggy, unnecessary code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 16&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's break this code down step by step.&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TemplateHtmlBuilder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;HEADER_TEMPLATE_NO_STACK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;h1&amp;gt;%1$s&amp;lt;/h1&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"%2$s &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
           &lt;span class="s"&gt;"&amp;lt;div class=&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s"&gt;container content&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s"&gt;&amp;gt;&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="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;HTML_TEMPLATE_START_NO_STACK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
             &lt;span class="s"&gt;"&amp;lt;!doctype html&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
             &lt;span class="s"&gt;"&amp;lt;html lang=&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s"&gt;en&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s"&gt;&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
             &lt;span class="s"&gt;"&amp;lt;head&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
             &lt;span class="s"&gt;"    &amp;lt;title&amp;gt;%1$s%2$s&amp;lt;/title&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
             &lt;span class="s"&gt;"    &amp;lt;meta charset=&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s"&gt;&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
             &lt;span class="s"&gt;"&amp;lt;/head&amp;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;Here we see two HTML templates stored in these fields. Looking closely, we'll notice that both the first and second templates contain placeholders for two string variables. &lt;/p&gt;

&lt;p&gt;Now let's take a look at how they're used:&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;public&lt;/span&gt; &lt;span class="nf"&gt;TemplateHtmlBuilder&lt;/span&gt;&lt;span class="p"&gt;(....,&lt;/span&gt; 
        &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;details&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="n"&gt;result&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;StringBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HTML_TEMPLATE_START_NO_STACK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                        &lt;span class="n"&gt;escapeHtml&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                        &lt;span class="n"&gt;subTitle&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;subTitle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEmpty&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="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;" - "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; 
                        &lt;span class="n"&gt;escapeHtml&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subTitle&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;CSS&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;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HEADER_TEMPLATE_NO_STACK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                &lt;span class="n"&gt;escapeHtml&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                &lt;span class="n"&gt;escapeHtml&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;details&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
                &lt;span class="n"&gt;actionLinks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toString&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;When using the &lt;code&gt;HEADER_TEMPLATE_NO_STACK&lt;/code&gt; and &lt;code&gt;HTML_TEMPLATE_START_NO_STACK&lt;/code&gt; fields, the template gets populated with three arguments in both cases, even though the lines themselves only have two slots. This same method contains identical &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/core/runtime/src/main/java/io/quarkus/runtime/TemplateHtmlBuilder.java#L321" rel="noopener noreferrer"&gt;lines&lt;/a&gt; using the exact same arguments, but with &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/core/runtime/src/main/java/io/quarkus/runtime/TemplateHtmlBuilder.java#L134" rel="noopener noreferrer"&gt;templates&lt;/a&gt; built for three arguments. This looks like a typical copy-paste mistake, where the extra arguments never got removed. &lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6046/" rel="noopener noreferrer"&gt;V6046&lt;/a&gt; Incorrect format. A different number of format items is expected. Arguments not used: 3. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/core/runtime/src/main/java/io/quarkus/runtime/TemplateHtmlBuilder.java#L325" rel="noopener noreferrer"&gt;TemplateHtmlBuilder.java 325&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6046/" rel="noopener noreferrer"&gt;V6046&lt;/a&gt; Incorrect format. A different number of format items is expected. Arguments not used: 3. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/core/runtime/src/main/java/io/quarkus/runtime/TemplateHtmlBuilder.java#L328" rel="noopener noreferrer"&gt;TemplateHtmlBuilder.java 328&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 17&lt;/strong&gt;&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;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;boot&lt;/span&gt;&lt;span class="p"&gt;(....,&lt;/span&gt; &lt;span class="n"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;FunctionInitializedBuildItem&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hasFunctions&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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;hasFunctions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isPresent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;hasFunctions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;= &lt;/span&gt;
    &lt;span class="k"&gt;return&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 method takes a &lt;code&gt;hasFunctions&lt;/code&gt; parameter of type &lt;code&gt;Optional&lt;/code&gt;, which is used to perform a very strange check:&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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;hasFunctions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isPresent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;hasFunctions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a quick refresher on what &lt;code&gt;Optional::isPresent&lt;/code&gt; does:&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;public&lt;/span&gt; &lt;span class="n"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isPresent&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;value&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&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;In this specific case, only the call to &lt;code&gt;Optional::isPresent&lt;/code&gt; was needed, since &lt;code&gt;Optional::get&lt;/code&gt; throws an exception if the value is actually &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6007/" rel="noopener noreferrer"&gt;V6007&lt;/a&gt; Expression 'hasFunctions.get() == null' is always false. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/funqy/funqy-http/deployment/src/main/java/io/quarkus/funqy/deployment/bindings/http/FunqyHttpBuildStep.java#L77" rel="noopener noreferrer"&gt;FunqyHttpBuildStep.java 77&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 18&lt;/strong&gt;&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;typeInfo&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;typeInfo&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;   
    &lt;span class="n"&gt;typeInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;endsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SectionHelperFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HINT_METADATA&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;continue&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;This one's straightforward: the check for &lt;code&gt;typeInfo != null&lt;/code&gt; is unnecessary, since the opposite condition was already checked first. &lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6007/" rel="noopener noreferrer"&gt;V6007&lt;/a&gt; Expression 'typeInfo != null' is always true. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java#L920" rel="noopener noreferrer"&gt;QuteProcessor.java 920&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 19&lt;/strong&gt;&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;persistenceProviderResolver&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
     &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;persistenceProviderResolver&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;       &lt;span class="c1"&gt;// &amp;lt;= &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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;persistenceProviderResolver&lt;/span&gt; &lt;span class="n"&gt;instanceof&lt;/span&gt;
               &lt;span class="n"&gt;MultiplePersistenceProviderResolver&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;This warning is similar to the previous one, but here even the check for equality with &lt;code&gt;null&lt;/code&gt; is unnecessary. In fact, &lt;code&gt;instanceof&lt;/code&gt; returns &lt;code&gt;false&lt;/code&gt; if the object passed to it is &lt;code&gt;null&lt;/code&gt;. Since this checks that &lt;code&gt;persistenceProviderResolver&lt;/code&gt; is not of type &lt;code&gt;MultiplePersistenceProviderResolver&lt;/code&gt;, this bulky &lt;code&gt;if&lt;/code&gt; statement can be simplified to:&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="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;persistenceProviderResolver&lt;/span&gt; &lt;span class="n"&gt;instanceof&lt;/span&gt; &lt;span class="n"&gt;MultiplePersistenceProviderResolver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6007/" rel="noopener noreferrer"&gt;V6007&lt;/a&gt; Expression 'persistenceProviderResolver != null' is always true. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/PersistenceProviderSetup.java#L28" rel="noopener noreferrer"&gt;PersistenceProviderSetup.java 28&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 20&lt;/strong&gt;&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;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;names&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;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equalsIgnoreCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Transfer-Encoding"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;     &lt;span class="c1"&gt;// &amp;lt;= &lt;/span&gt;
    &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ignore transfer encoding, &lt;/span&gt;
            &lt;span class="c1"&gt;// chunked screws up message and response&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;getAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equalsIgnoreCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Transfer-Encoding"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
        &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"chunked"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;responseBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getMultiValueHeaders&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&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 &lt;code&gt;name&lt;/code&gt; variable gets checked for equality with the &lt;code&gt;Transfer-Encoding&lt;/code&gt; string before the inner loop and on every iteration of it. This unnecessary operation just bloats the code.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6007/" rel="noopener noreferrer"&gt;V6007&lt;/a&gt; Expression 'name.equalsIgnoreCase("Transfer-Encoding")' is always false. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/amazon-lambda-rest/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java#L104" rel="noopener noreferrer"&gt;LambdaHttpHandler.java 104&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snippet 21&lt;/strong&gt;&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;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
       &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;ResteasyReactiveDotNames&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OBJECT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentClazz&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; 
       &lt;span class="n"&gt;currentClazz&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;              &lt;span class="c1"&gt;// &amp;lt;=&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;And here's the cherry on top: &lt;code&gt;currentClazz&lt;/code&gt; gets dereferenced first, and the &lt;code&gt;null&lt;/code&gt; check comes only after.&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v6060/" rel="noopener noreferrer"&gt;V6060&lt;/a&gt; The 'currentClazz' reference was utilized before it was verified against null. &lt;a href="https://github.com/quarkusio/quarkus/blob/19d6bc987d61c3d70995b557a93e840a1106bef0/extensions/resteasy-reactive/rest/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveProcessor.java#L1125" rel="noopener noreferrer"&gt;ResteasyReactiveProcessor.java 1125&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;We've now gone through some interesting bugs and suspicious spots we managed to find in the Quarkus source code. We've checked other major projects too, and found plenty of interesting things there as well:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/java/1089/" rel="noopener noreferrer"&gt;Operation K. Looking for bugs in the IntelliJ IDEA code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/java/1247/" rel="noopener noreferrer"&gt;Searching in a search: let's check Elasticsearch&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/java/1344/" rel="noopener noreferrer"&gt;Exploring OpenAPI Generator via static analysis&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the way, in the Quarkus repository, like in many modern projects, you can find &lt;code&gt;.md&lt;/code&gt; files meant for AI agents. But no matter how powerful a tool AI is, it still makes sneaky mistakes sometimes, like the ones we already covered in the articles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1354/" rel="noopener noreferrer"&gt;Let's dig into some vibe code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1366/" rel="noopener noreferrer"&gt;Let's check vibe code that acts like optimized C++ but is actually a mess&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that wraps up this article. If you'd like to check your own project, we've got a free trial of the PVS-Studio analyzer for you, you can get it &lt;a href="https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&amp;amp;utm_medium=devto&amp;amp;utm_campaign=article&amp;amp;utm_content=1398" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Webinar: Let's make a programming language. Functions</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Mon, 03 Aug 2026 13:49:23 +0000</pubDate>
      <link>https://dev.to/pvsdev/webinar-lets-make-a-programming-language-functions-1ajn</link>
      <guid>https://dev.to/pvsdev/webinar-lets-make-a-programming-language-functions-1ajn</guid>
      <description>&lt;p&gt;We're continuing our webinar series on building your own programming language in C++!&lt;/p&gt;

&lt;p&gt;In the previous session, we introduced variables — they could be declared, reference one another, and were resolved through a global hash table. Now it's time to add one of the core building blocks of any language: functions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fq0nwfmh1hkk18z6yz5ir.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fq0nwfmh1hkk18z6yz5ir.png" alt=" " width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Implementing functions is really a story about scopes and name resolution. We'll explore how identifiers are looked up across different scope levels: the global scope, function scopes with their own variables, and nested local scopes inside function bodies. What happens when the same variable name exists in multiple scopes? How does the compiler determine which one to use?&lt;/p&gt;

&lt;p&gt;In this webinar, our speaker Yuri will walk through the implementation step by step, demonstrating how functions, scopes, and symbol lookup are built in code using C++. Along the way, you'll gain a deeper understanding of one of the fundamental mechanisms behind modern programming languages.&lt;/p&gt;

&lt;p&gt;Join us live to dive deeper into compiler design, ask your questions, and build a programming language from scratch together.&lt;/p&gt;

&lt;p&gt;The series is aimed at developers who want to go beyond just using languages and start understanding how they're built. Whether you've been with us from the beginning or are jumping in now, this is a great place to start.&lt;/p&gt;

&lt;p&gt;If you missed any of the previous sessions, don't worry. All registered participants get access to the recordings. You can also catch up on YouTube or the PVS-Studio website:&lt;/p&gt;

&lt;p&gt;First talk: &lt;a href="https://pvs-studio.com/en/blog/video/11585" rel="noopener noreferrer"&gt;&lt;strong&gt;Let's make a programming language. Intro&lt;/strong&gt;&lt;/a&gt;&lt;br&gt;
Second talk: &lt;a href="https://pvs-studio.com/en/blog/video/11644" rel="noopener noreferrer"&gt;&lt;strong&gt;Let's make a programming language. Grammars&lt;/strong&gt;&lt;/a&gt;&lt;br&gt;
Third talk: &lt;a href="https://pvs-studio.com/en/blog/video/11665" rel="noopener noreferrer"&gt;&lt;strong&gt;Let's make a programming language. Lexer&lt;/strong&gt;&lt;/a&gt;&lt;br&gt;
Fourth talk: &lt;a href="https://pvs-studio.com/en/blog/video/11709" rel="noopener noreferrer"&gt;&lt;strong&gt;Let's make a programming language. Parser&lt;/strong&gt;&lt;/a&gt;&lt;br&gt;
Fifth talk: &lt;a href="https://pvs-studio.com/en/blog/video/11729" rel="noopener noreferrer"&gt;&lt;strong&gt;Let's make a programming language. AST&lt;/strong&gt;&lt;/a&gt;&lt;br&gt;
Sixth talk: &lt;a href="https://www.youtube.com/watch?v=Sn8fVb_O90Y" rel="noopener noreferrer"&gt;&lt;strong&gt;Let's make a programming language. Variables&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Register to join the session. All registered participants will receive the recording of this webinar, along with recordings of all previous webinars in the series, after the webinar has concluded.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/webinar/32/?utm_source=devto&amp;amp;utm_medium=pvs&amp;amp;utm_campaign=webinar&amp;amp;utm_content=web7" rel="noopener noreferrer"&gt;&lt;strong&gt;Join us&lt;/strong&gt;&lt;/a&gt; on August 20, 1:00 PM UTC+1!&lt;/p&gt;

&lt;p&gt;P.S. Don't forget to check your inbox and confirm your registration!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>webinar</category>
    </item>
    <item>
      <title>Game++. Part 2.1: Types of optimization</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 31 Jul 2026 11:48:17 +0000</pubDate>
      <link>https://dev.to/pvsdev/game-part-21-types-of-optimization-4976</link>
      <guid>https://dev.to/pvsdev/game-part-21-types-of-optimization-4976</guid>
      <description>&lt;p&gt;This book offers insights into C++, including algorithms and practices in game development, explores strengths and weaknesses of the language, its established workflows, and hands-on solutions. C++ continues to dominate the game development industry today thanks to its combination of high performance, flexibility, and extensive low-level control capabilities.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjfc61cnec7pcbqkr2783.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjfc61cnec7pcbqkr2783.png" alt="1397_pt_5/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wikipedia defines optimization as the process of improving a system to increase its efficiency or reduce its resource consumption. In game development, those words hit harder, since every game runs on a tight budget. On one side of the scale sit CPU time, memory, and frame time; on the other, the player and the market, with their expectations around graphics, physics, AI, and overall experience. That scale almost never tips in the developer's favor. &lt;/p&gt;

&lt;p&gt;Optimization should always start with the big picture. Random small tweaks waste time—what matters is finding the real bottlenecks: where exactly performance drops, and which subsystems cost more than they give back. This is where profilers, performance counters, and tracing tools come into play. The process looks like this: locate a trouble spot, measure its impact, try an improvement, and measure again. If nothing improves, roll back and look for the next candidate.&lt;/p&gt;

&lt;p&gt;Here are the steps to follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Benchmark&lt;/strong&gt;—build a reproducible example of the problem scenario. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Measure&lt;/strong&gt;—establish a baseline for memory and resource consumption. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Detect&lt;/strong&gt;—find the part of the program that makes the poorest use of resources. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Solve&lt;/strong&gt;—change how it works to improve resource usage. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check&lt;/strong&gt;—confirm through measurement that the improvement actually happened. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repeat&lt;/strong&gt;—go back to the first step until you hit your performance goals. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There's a more vivid concept—&lt;em&gt;MEDAL&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;M&lt;/strong&gt;easure &amp;gt; &lt;strong&gt;E&lt;/strong&gt;xamine &amp;gt; &lt;strong&gt;D&lt;/strong&gt;evelop &amp;gt; &lt;strong&gt;A&lt;/strong&gt;ssess (Check results) &amp;gt; &lt;strong&gt;L&lt;/strong&gt;oop.&lt;/p&gt;

&lt;p&gt;An experienced developer treats optimization as a data-driven process, not a bag of tricks. Even so, it pays to keep proven techniques in your back pocket—for memory management, multithreading, rendering, math, and so on—and to pair them with personal experience and tooling to get a measurable result without wasting time. Hands-on experience is essential here: without practice or good mentors, learning to spot problem areas can be difficult. I've always been amazed by developers' ability to run demanding computations on relatively "weak" hardware, and by the tricks they use to keep applications running fast. This applies not only to game engines but also to databases, control systems, and the like. The gap becomes especially obvious when porting relatively recent games (PS3-generation and later) to handhelds like the Nintendo Switch and Steam Deck, or to mobile devices. In terms of raw performance, the Nintendo Switch hasn't moved far beyond the PlayStation 3, so attempts to port demanding titles built for at least the next console generation (PS4) run into significant and stubborn problems. Table 3.1 compares three generations of gaming hardware. It shows which solutions can be ported without major compromises, and what level of optimization developers need to focus on for each. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;PlayStation 3&lt;/th&gt;
&lt;th&gt;Nintendo Switch (portable)&lt;/th&gt;
&lt;th&gt;Steam Deck&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;CPU&lt;/td&gt;
&lt;td&gt;Cellx1 PPE + x6 SPE&lt;br&gt;~25 GFlops&lt;/td&gt;
&lt;td&gt;4× ARM Cortex-A57 1.02 GHz&lt;br&gt;~25 GFlops&lt;/td&gt;
&lt;td&gt;AMD Zen 2 2.4–3.5 GHz&lt;br&gt;(~448 GFlops)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPU&lt;/td&gt;
&lt;td&gt;NVIDIA (~GeForce 7800)&lt;br&gt;~192 GFlops&lt;/td&gt;
&lt;td&gt;NVIDIA Maxwell GM20B&lt;br&gt;~157 GFlops&lt;/td&gt;
&lt;td&gt;AMD RDNA 2&lt;br&gt;~1,6 TFlops&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RAM&lt;/td&gt;
&lt;td&gt;256 MB XDR&lt;br&gt;256 MB GDDR3&lt;br&gt;~25.6 GB/s (GPU)&lt;/td&gt;
&lt;td&gt;4 GB LPDDR4&lt;br&gt;~25.6 GB/s&lt;/td&gt;
&lt;td&gt;16 GB LPDDR5&lt;br&gt;~88 GB/s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;API&lt;/td&gt;
&lt;td&gt;Proprietary&lt;/td&gt;
&lt;td&gt;OpenGL ES 3.2 / Vulkan / NX&lt;/td&gt;
&lt;td&gt;OpenGL3+ / Vulkan&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Table 3.1. Comparison of gaming platform specifications&lt;/p&gt;

&lt;p&gt;From a CPU standpoint, the PS3 uses the Cell architecture with one main PPE core and six active SPE co-processors. In real game workloads, the performance of this configuration is generally estimated at around 20–30 GFLOPS, though fully leveraging Cell's potential is notoriously difficult. &lt;/p&gt;

&lt;p&gt;The Nintendo Switch has a 4-core ARM Cortex-A57 running at 1.02 GHz, but effectively it's a 3-core machine, since one core is always reserved for the system. It delivers roughly 20–25 GFLOPS under typical loads. Despite the architectural differences, both CPUs perform at a similar level. The GPU picture is a little different. The PS3's RSX is an adapted GeForce 7800 with peak performance around 192 GFLOPS and an outdated shader model. The Switch uses a GPU based on NVIDIA's Maxwell architecture (Tegra X1): in handheld mode it runs at reduced clocks, delivering about 157 GFLOPS, while docked performance can reach 393 GFLOPS. &lt;/p&gt;

&lt;p&gt;As for RAM, the Switch is powered by 4 GB of LPDDR4 offering about 25.6 GB/s of bandwidth, while the PS3 has 256 MB of XDR for the CPU and 256 MB of GDDR3 for the GPU, with roughly the same bandwidth. The Switch slightly wins on memory capacity, but not on bandwidth. In any case, it's the Steam Deck that sets today's baseline. Moving beyond that level, though, is no longer feasible, purely because of technical limitations. This comparison is really just a sketch. Once the Steam Deck 2 arrives, the Switch goes to the PS3 league, and suddenly the bar is raised for everyone. The optimization levels stay the same, with the same set of problems and solutions, and just backed by more resources. &lt;/p&gt;

&lt;p&gt;A game is demanding software, often with soft real-time requirements, whether you're targeting a PS3 or the latest Steam Deck. Acceptable performance is non-negotiable—otherwise the experience suffers and nobody buys the game. There's a small upside when porting to mobile platforms: the hardware tends to be fixed. On smartphones, though, you're staring into a jungle of CPUs, GPUs, and runtime environments. Consoles fare better here, since their hardware specs change only every few years. &lt;/p&gt;

&lt;p&gt;When porting a game, optimization can be split into several levels: architecture, algorithms, and code. &lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture-level optimizations (~50% gain)
&lt;/h2&gt;

&lt;p&gt;Architecture is about the bones of the system: how modules interact, how data sits in layers, the whole memory allocation dance (custom allocators vs. system malloc, object pooling vs. free-form allocation, dynamic vs. allocation-free), and the threading approach (single-threaded, multi-threaded, shared state, actor-based concurrency). A sub-optimal architecture means excessive memory consumption, sluggish execution, idle threads, resource contention, or expensive access patterns. &lt;/p&gt;

&lt;p&gt;Fixing architectural flaws requires large investments of time and people to refactor the game's core subsystems or engine, which often means rewriting fundamental components like the object management system, renderer, or memory system. Examples include moving from an inheritance-and-components model to an Entity Component System (ECS) architecture, replacing the object allocation mechanism, or reorganizing the command system. These architectural shifts deliver the most dramatic performance gains—smarter memory access can boost performance, and a well-tuned thread management model keeps all CPU cores busy, with idle time dropping sharply. &lt;/p&gt;

&lt;p&gt;However, late-stage architectural changes create a domino effect throughout the entire codebase, requiring months of fixes and expensive regression testing and makes such optimizations virtually impossible right before a game's release or when porting to new platforms. &lt;/p&gt;

&lt;h2&gt;
  
  
  Algorithm/data structure-level optimizations (~30%)
&lt;/h2&gt;

&lt;p&gt;Algorithms determine how efficiently the program runs. The right choice can literally save your frame rate: replacing a plain iteration over array elements (complexity &lt;code&gt;O(n)&lt;/code&gt;) with a binary search on a sorted array (&lt;code&gt;O(log n)&lt;/code&gt;) or a hash-table lookup (&lt;code&gt;O(1)&lt;/code&gt;) yields a massive speedup as data volume grows. In an array of a million elements, a linear search might cost a million operations, a binary search only about 20, and a hash table finds the element almost instantly. We rarely have arrays that large, but a handful of frequently called hot spots can still rack up that million accesses. &lt;/p&gt;

&lt;p&gt;But theoretical algorithmic complexity doesn't always match real-world performance, because of how modern hardware works. Processors use caches (&lt;code&gt;L1&lt;/code&gt;, &lt;code&gt;L2&lt;/code&gt;, &lt;code&gt;L3&lt;/code&gt;), and algorithms that access data sequentially get a huge boost from hardware prefetching. As a result, a simple linear scan over a tightly packed array can significantly outperform a hash-table lookup when the data fits in the &lt;code&gt;L1&lt;/code&gt; cache. Linked lists, theoretically efficient for insertions, end up slower than dynamic arrays in practice because of pointer-chasing. And since CPUs rely on branch prediction, predictable code runs faster—a straightforward algorithm with simple logic often leaves a complex, branch-heavy one in the dust. &lt;/p&gt;

&lt;p&gt;That's why developers often look beyond Big-O notation and weigh real-world conditions: data size, memory access patterns, branch predictability, and the architecture of the target hardware. &lt;/p&gt;

&lt;p&gt;Development includes profiling code on real data and writing benchmarks for the tricky sections. In real projects, a "naive" solution often beats the textbook approach: data locality works in its favor, and there's no heavy scaffolding to drag along. Here's a typical example:&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="c1"&gt;// The example of search optimization&lt;/span&gt;

&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;slow_contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;vec&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;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Call 'find' each time&lt;/span&gt;

  &lt;span class="k"&gt;return&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;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;fast_contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sorted_vec&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;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Simple optimization,&lt;/span&gt;
  &lt;span class="c1"&gt;// use binary search for sorted array&lt;/span&gt;

  &lt;span class="k"&gt;return&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;binary_search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sorted_vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
                            &lt;span class="n"&gt;sorted_vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;target&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;h2&gt;
  
  
  Source code-level optimizations (~10%)
&lt;/h2&gt;

&lt;p&gt;Local-level optimizations target individual blocks of code, functions, or bounded algorithmic scopes. They focus on fine-tuning a solution to raise performance and lower overhead. These small-scale optimizations usually touch only limited parts of the program and can be handled in a single code review: up to a hundred changes and maybe ten files at once. &lt;/p&gt;

&lt;p&gt;Key areas of focus include heapless logic (i.e. implementing algorithms and data structures on the stack rather than the heap), object pools, and specialized allocators. Loop unrolling improves instruction-level parallelism when the compiler can't do it automatically due to code complexity. Other techniques include inlining "hot" functions, introducing SIMD for vectorized computation, minding data locality, and following the access patterns. The optimization process often involves detailed profiling with tools like Intel VTune, PIX, Razor, Tracy, or a game engine's built-in profiler. The analysis covers inefficient instructions, cache miss rates, excessive virtual-function calls, hidden copies when passing parameters, and expensive object operations. In most cases, fixing these issues requires trade-offs between code readability and the solution you implement, so these optimizations apply only to code that eats up most of the frame time. &lt;/p&gt;

&lt;p&gt;Loop optimization is easily one of the most common tasks at this level. Developers regularly run into nested loops the compiler can't refine but that can be simplified or swapped out for smarter algorithms. For example, if an inner loop is searching through a collection, consider preparing the data structure in advance or using a hash table to avoid redundant passes. &lt;/p&gt;

&lt;p&gt;Sometimes simply moving loop invariants outside the loop or changing the iteration order is enough to reduce the load on the CPU:&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="nf"&gt;calculatePhysicsSlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;vel&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;frameStep&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;iterations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;positions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Invariant computations are repeated,&lt;/span&gt;
      &lt;span class="c1"&gt;// the compiler may not spot them in difficult cases&lt;/span&gt;

      &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;gravity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;world&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getGravity&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;damping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getDamping&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeStep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getDeltaTime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;frameStep&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="n"&gt;vel&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;gravity&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;timeStep&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;vel&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;damping&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;velocities&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;timeStep&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;--------------------------------------------------------------------------&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="nf"&gt;calculatePhysicsFast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;vel&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;frameStep&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Invariant computations were moved beyond loop boundaries&lt;/span&gt;

  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;gravity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;world&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getGravity&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;damping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getDamping&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeStep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getDeltaTime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;frameStep&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;gravityTimeStep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gravity&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;timeStep&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;iterations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;positions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;velocities&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;gravityTimeStep&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;velocities&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;damping&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;positions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;velocities&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;timeStep&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;Modern compilers with optimizations enabled can handle these transformations automatically, but once pointers, virtual functions, or convoluted logic enter the picture, you still have to step in to squeeze out maximum performance. Hoisting loop-invariant code is a fundamental optimization based on the loop-invariant code motion principle: you take expressions whose value never changes across iterations and move them out of the loop so they're computed once. Invariant candidates include operations on constants, accesses to unchanged variables, math expressions with fixed values, and calls to functions with deterministic results. &lt;/p&gt;

&lt;p&gt;For example, evaluating &lt;code&gt;deltaTime / iterations&lt;/code&gt;, &lt;code&gt;sin(angle)&lt;/code&gt; for a fixed angle, or &lt;code&gt;sqrt(maxDistance)&lt;/code&gt; inside a loop causes redundant operations on every iteration, whereas hoisting these into pre-computed variables reduces the operation count from &lt;code&gt;O(n)&lt;/code&gt; to &lt;code&gt;O(1)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another key step is rooting out linear searches in your code, especially those hiding inside loops. A linear search as &lt;code&gt;std::find&lt;/code&gt; or a manual array scan where works perfectly fine on tiny collections, but in a hot path or with a lot of data it quickly turns into a serious bottleneck. Replacing a linear search with a binary search, using an associative container, or pre-sorting the data and any of these can give you a performance boost.&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;struct&lt;/span&gt; &lt;span class="nc"&gt;GameObject&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;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isActive&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;GameObject&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;_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;_x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;_y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;_z&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;_active&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
             &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
               &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_x&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_y&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_z&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;isActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_active&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="c1"&gt;// The linear search by the name - O(n)&lt;/span&gt;
&lt;span class="n"&gt;GameObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;findObjectByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;find_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
                    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"const GameObject&amp;amp; obj"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;&amp;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;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;name&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;nullptr&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;Main optimization methods fall into three approaches: hash tables, binary search, and caching. Hash tables (&lt;code&gt;unordered_map&lt;/code&gt;) provide average &lt;code&gt;O(1)&lt;/code&gt; lookup complexity and are especially effective when access is frequent and keyed by unique identifiers. Pre-building indexes is the best practice: map an ID to an array position or index by a string name, and data access speeds up noticeably. Caching stores the results of expensive operations and returns them quickly on repeated requests. The main thing is to keep the data fresh and invalidate the cache correctly. &lt;/p&gt;

&lt;p&gt;A one-time investment in indexing and sorting quickly pays off when you're performing multiple searches. When choosing a data structure, the key is to pick one that fits your specific use case and to be ready to make trade-offs: faster access often requires more memory. &lt;/p&gt;

&lt;p&gt;Hash tables are good for frequent key-based lookups, binary search for stable, rarely changing data, and caching for heavy but repeated computations. All of this becomes a big deal inside a game loop, where you might be querying objects hundreds of times per frame. Here's what it looks like in code:&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="nf"&gt;addObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;GameObject&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;idToIndex&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Update indexes&lt;/span&gt;
  &lt;span class="n"&gt;nameToIndex&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Active objects cache invalidation&lt;/span&gt;
  &lt;span class="n"&gt;activeObjectsCacheValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Search by the name via the hash table - O(1)&lt;/span&gt;
&lt;span class="n"&gt;GameObject&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;findObjectByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;string&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nameToIndex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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;it&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nameToIndex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;nullptr&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;One key direction is minimizing function-call overhead in critical code paths. A function call generally involves creating a new stack frame, passing parameters, saving register state, and restoring it on return. When the iteration count is high enough, that overhead noticeably hurts performance. It becomes especially painful when functions do almost nothing—the actual work costs about as much as the call overhead itself.&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="c1"&gt;// Unoptimized code&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Vector3D&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;getX&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&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;x&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="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;magnitude&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&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;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;z&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="c1"&gt;// Slow version with multiple function calls&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;calculateDistance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Vector3D&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Vector3D&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;v2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getX&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;v2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getX&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getY&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;v2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getY&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;dz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getZ&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;v2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getZ&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dy&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dz&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dz&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;sum&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;Compiler developers are well aware of this and use a variety of optimization techniques to address it. Inlining lets the compiler embed the function body directly at the call site, eliminating the overhead. &lt;/p&gt;

&lt;p&gt;Hoisting loop-invariant computations (loop hoisting) prevents repeated execution of identical operations. Merging several simple functions into one reduces the number of jumps and improves data locality in the CPU cache. And yes, the compiler will most likely notice simple getter functions and optimize that fragment but it can't come up with a fundamentally better solution:&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;OptimizedVector3D&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;getX&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&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;x&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="c1"&gt;// Optimized function to evaluate distance&lt;/span&gt;
  &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;distanceTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;OptimizedVector3D&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;dz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;z&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;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dy&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dz&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dz&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;An alternative approach using &lt;code&gt;OptimizedVector3D&lt;/code&gt; inlines the getters, which lets the compiler embed their code directly where they're called. It also adds a new method, &lt;code&gt;distanceTo()&lt;/code&gt;, which performs all calculations locally without any extra calls. Keep a close eye on memory allocations that sit inside loops or hot functions; they can tank performance. Dynamic memory allocation ranks among the most expensive operations, and its cost multiplies when it shows up frequently in the program's hot paths. We'll dig deeper into how to optimize this kind of code in chapter "The Joy of Programming." &lt;/p&gt;

&lt;p&gt;Source-code and function-level optimizations affect performance differently depending on the hardware platform. On high-performance systems—modern consoles and desktops with multi-core CPUs, generous &lt;code&gt;L2&lt;/code&gt;/&lt;code&gt;L3&lt;/code&gt; caches, and fast memory—local code tweaks rarely net more than a 2–3% gain and barely move the needle overall. Human mistakes, of course, are another story. &lt;/p&gt;

&lt;p&gt;Such improvements rarely make sense for the business: the engineering time they eat up would be far better spent building new features or fixing critical bugs. &lt;/p&gt;

&lt;p&gt;The picture flips completely on portable hardware. There, developers deal with ARM chips clocked at 1–3 GHz (versus 4–5 GHz on desktop), &lt;code&gt;L2&lt;/code&gt; caches measured in kilobytes rather than megabytes, and strict power and thermal limits that leave little room to spare. Under those constraints, the same micro-optimizations can deliver double-digit percentage gains, smoother frame pacing, and longer battery life. &lt;/p&gt;

&lt;p&gt;Some code examples to make it more tangible:&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;long&lt;/span&gt; &lt;span class="nf"&gt;unpredictable_branches&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&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;val&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The data is random, and the branching is unpredictable&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;val&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;val&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;sum&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;--------------------------------------------------------------------------&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="c1"&gt;// Avoid branches through bitwise operations&lt;/span&gt;
&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;branchless_version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&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;val&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Use a mask instead of branching&lt;/span&gt;
    &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&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;sum&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;Tracking down and landing real micro-optimizations is an art unto itself—you can't do it well without a deep understanding of how the CPU and memory hierarchy actually work. Squeezing a few lines of code down to optimal can eat up days. You're profiling, chasing bottlenecks, staring at cache behavior, wading through assembly, and figuring out exactly how branch prediction works on that one particular architecture. &lt;/p&gt;

&lt;p&gt;The payoff from a micro-optimization depends heavily on context: memory access patterns, how your working set maps to the different cache levels, cache-line alignment, how often that code actually runs, and how it interacts with the rest of the system. Many developers treat this as a last resort, preferring changes at other architectural layers and in most cases that's justified. &lt;/p&gt;

&lt;h2&gt;
  
  
  Game replays (~?%)
&lt;/h2&gt;

&lt;p&gt;Game replays are one of the most powerful debugging tools out there, especially in complex systems with many interacting objects and non-linear logic. A replay is more than a video recording—it's a time-ordered sequence of input data (game commands, events, and states) that the game engine plays back. Its frame-level accuracy lets it reproduce game behavior regardless of the hardware or time of day, which makes it ideal for deterministic debugging. In a nutshell, it's time-travel debugging without the usual baggage: you don't touch the compiler, you don't freeze the entire VM state, and the runtime cost is next to nothing. &lt;/p&gt;

&lt;p&gt;Under the hood, a software replay comes down to capturing action points for every object on each game loop iteration: player commands, AI states, network messages, random-number generator updates, and so on. With the right architecture, a replay stays reproducible even months later. &lt;/p&gt;

&lt;p&gt;Such a mechanism lets QA or a developer reproduce a bug simply by "scrolling" the match to the right moment. Replays can also be adapted for automated tests, game-scenario simulation, and even AI training. In production, a replay is often more practical and powerful than classic debuggers or logs. &lt;/p&gt;

&lt;h2&gt;
  
  
  Component isolation (~5%)
&lt;/h2&gt;

&lt;p&gt;Modular component isolation comes down to a simple rule: separate interfaces from implementations, and lean on dependency injection. Well-designed modules interact strictly through defined API boundaries, hiding their internals and keeping subsystems loosely coupled. &lt;/p&gt;

&lt;p&gt;An audio system might expose a clean interface &lt;code&gt;PlaySound()&lt;/code&gt;, &lt;code&gt;SetVolume()&lt;/code&gt; while hiding whether it's backed by DirectSound, OpenAL, or a completely custom pipeline. That kind of isolation lets you swap implementations without touching any client code. It also smooths out porting and platform-specific work: each component can be tuned on its own, with no ripple effects through the rest of the system. The renderer can have separate backends for DirectX, Vulkan, Metal, or OpenGL, selected at startup, leaving the gameplay code completely untouched. The same goes for the input system: it can switch between keyboard/mouse, gamepad, touch, or VR controllers while the game code sees only a uniform interface. All of this enables platform-specific solutions without merge conflicts, lets developers test the performance of individual components in isolation, and makes it possible to change one part without threatening the stability of the whole system. &lt;/p&gt;

&lt;h2&gt;
  
  
  Specific low-level optimizations (&amp;lt;3%)
&lt;/h2&gt;

&lt;p&gt;Many developers fall into the trap of thinking low-level optimization calls for the heavy artillery—inline assembly, fancy algorithms, a full SIMD rewrite. It usually doesn't; in reality, many optimizations turn out to be fairly simple and intuitive. Clean code tends to be fast to compilers and optimizers like it that way. But that's not always the case. &lt;/p&gt;

&lt;h2&gt;
  
  
  Other (~10%)
&lt;/h2&gt;

&lt;p&gt;So what does this slice of development actually look like? Normally it means solid build pipelines: a build reaches QA within an hour of the commit and, if something's broken, lands back in the author's lap within two. A healthy setup includes good debuggers, replays, and modern game development tools like behavior trees (BT), visual scripts (VS), blueprints, and animation scripts (AS). These systems are notoriously hard to debug under a debugger, and the bugs they produce have a nasty habit of being unreproducible 99% of the time. Of course, a lot hinges on the skill of your QA team. These improvements may not directly affect the game's runtime performance, but they help identify and resolve issues faster. And when a developer builds a feature instead of fixing a bug, that's a direct win for the project. &lt;/p&gt;

&lt;h2&gt;
  
  
  Benchmarks
&lt;/h2&gt;

&lt;p&gt;Benchmarks are the cornerstone of any optimization approach. You can't improve what you can't measure. A good benchmark acts as a compass in the vast sea of optimization, giving you immediate feedback on the impact of every change. At its core, a benchmark is an execution scenario that reproduces a specific workload and serves as a reference point for quantitative comparison of different code versions and their performance. &lt;/p&gt;

&lt;p&gt;So, a solid benchmark should satisfy four critical requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reproducibility&lt;/strong&gt;, a fundamental property that gets overlooked surprisingly often. Repeated runs under the same configuration should produce statistically consistent results, although individual outliers can occur (even fairly large ones). Fixed input data, deterministic behavior for random values through a predefined seed, stable system conditions, and strong isolation from external influences such as network activity or background processes all contribute to reproducibility. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Speed test&lt;/strong&gt;, a practical requirement for maximizing development efficiency. A well-designed benchmark rarely takes more than a few dozen seconds to run, letting developers iterate on optimizations quickly without sitting around waiting. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Representativeness&lt;/strong&gt; or realistic behavior, achieved by modeling real gameplay scenarios with CPU, GPU, memory, and I/O usage patterns that closely match actual game behavior. A benchmark should reproduce real workloads such as physics simulations, rendering tasks, or AI-intensive calculations. Avoid purely synthetic tests that have little connection to the engine's real architecture. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bench sensitivity&lt;/strong&gt;, which ensures the benchmark can detect both performance improvements and regressions. Statistical analysis needs at least 10–20 runs to mean anything. A good benchmark shows a clear difference (typically 5–10%) after significant code changes while staying steady through cosmetic tweaks that don't affect performance. For example, you might notice the frame rate dropping in a particular game scene. Before starting any optimization work, set up a reproducible test case: pin down the exact spot where the problem shows up and make sure you can get to it quickly. This lets you take repeated measurements under identical conditions. In reality, though, that isn't always feasible, so developers often build a dedicated benchmark scene or test mode with strictly deterministic behavior. Each run then produces comparable results, making performance measurements far more trustworthy. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The whole point of benchmarking is to put a number on the comparison between the original and the optimized version of your code. Whenever possible, start with measurements so you can back up any claimed improvement with actual numbers rather than intuition. Despite its apparent simplicity, building good benchmarks hides plenty of pitfalls. One of the most common mistakes is measuring only hot runs: developers discard the first iterations to avoid warm-up effects or cache population, but forget that real games hit cold starts and cold data just as often as hot ones. &lt;/p&gt;

&lt;p&gt;Automating benchmarks through CI/CD catches performance regressions early, while graphing the results makes it much easier to spot long-term trends and correlations between different performance metrics. &lt;/p&gt;

&lt;h2&gt;
  
  
  Measurements
&lt;/h2&gt;

&lt;p&gt;The next step in optimization involves collecting and analyzing performance metrics to pinpoint bottlenecks. Every change should improve performance while consuming as little time and effort as possible—in other words, it should deliver a strong return on investment (ROI). Not every result will be a clear win, but even failed experiments help reveal the most promising direction for the next step. &lt;/p&gt;

&lt;p&gt;Proper diagnostics ensure that developers address an actual performance issue rather than a random piece of code that barely affects overall efficiency. Metrics, profiles, and logs are the main sources of information here, helping teams avoid blind changes and unnecessary rounds of optimization. &lt;/p&gt;

&lt;p&gt;The approach to optimization mirrors how engineers analyze any system: start with a high-level view, then gradually narrow the focus to specific cases. At the top level, the goal is to identify the primary performance constraint—whether it's the CPU, GPU, or memory. In most cases that actually need fixing, the bottleneck ultimately traces back to a particular module or code fragment, which narrows it down to the level of algorithms and data structures. To track down these issues, developers rely on profilers, system monitors, tracing tools, and engine performance metrics. These tools help identify hot spots—functions or routines that consume a significant share of CPU time, GPU resources, or memory. This is where optimization reaches the source-code level. &lt;/p&gt;

&lt;p&gt;When analyzing performance, start by evaluating CPU and GPU usage with general-purpose monitoring tools. If the GPU isn't under heavy load, a profiler can help measure how much time the CPU spends updating and processing game logic. Identifying issues at the high level narrows the scope of analysis and lets developers focus optimization efforts on a specific area. &lt;/p&gt;

&lt;p&gt;At the same time, it's important to remember that a performance issue rarely comes down to a single root cause. A game consists of countless micro-operations, and bottlenecks often emerge from the combined effect of several factors. At the instruction level, the issue may stem from a long-running call that leaves the processor sitting idle while it waits for data. At the GPU level, it might be an imbalance in how rendering commands are processed. &lt;/p&gt;

&lt;p&gt;These may look like two separate problems, but fixing one often affects the other. By looking across different levels of analysis, from low-level details to high-level system behavior, you can identify the component that's actually constraining performance the most and put your resources where they matter. Always start with the big picture, then gradually drill down into specific bottlenecks. Combine multiple tools and metrics to track down and address real issues rather than rushing to fix the first suspicious symptom you run into. Building a list of potential problem areas can help, as it often points the way toward the actual source of the issue. &lt;/p&gt;

&lt;p&gt;Finally, effective optimization has to factor in the cost of implementation. In some cases, removing a particular bottleneck may take substantial effort while delivering little gain. When that happens, it often makes more sense to focus elsewhere. The impact-to-cost ratio can serve as an optimization metric that helps teams avoid getting stuck obsessing over micro-optimizations. As a result, optimization stops being a collection of isolated tweaks and becomes a structured process that improves the efficiency of the whole system. &lt;/p&gt;

&lt;h2&gt;
  
  
  Changes and engineering intuition
&lt;/h2&gt;

&lt;p&gt;Once you've gathered enough data and identified the bottleneck limiting performance, set those results aside and analyze everything again from scratch, this time deliberately excluding that bottleneck. &lt;/p&gt;

&lt;p&gt;This technique is known as &lt;em&gt;hotspot avoidance&lt;/em&gt;. The idea is to focus first on how the other system components behave, rather than on the hotspot you've already found (in about 80% of cases, there won't be any other significant bottlenecks). This approach can reveal hidden bottlenecks that stay invisible when a dominant hotspot monopolizes the profiler. It also gives you a fuller picture of how the workload distributes across the components. &lt;/p&gt;

&lt;p&gt;The real value of this method becomes clearer once you've built up extensive experience optimizing different systems. By eliminating the main bottleneck, you can spot the points that, combined, can yield a significant performance gain, or that affect the hotspot itself in ways you wouldn't otherwise notice. The technique also simplifies the analysis of complex systems, where hidden dependencies often produce counterintuitive behavior (this is where so-called engineering intuition kicks in). &lt;/p&gt;

&lt;p&gt;At that point, you can start making changes. They may involve modifying algorithms, optimizing data structures, improving memory access patterns, or, in extreme cases, rewriting an entire subsystem. Even experienced optimization engineers rarely land on the best solution on the first try. Most end up testing several hypotheses and trying out multiple approaches before settling on a final implementation. This is where the insights from hotspot avoidance prove especially valuable. &lt;/p&gt;

&lt;p&gt;Once the changes are in place, measure performance again using benchmarks and profilers. Without measurement, there's no reliable way to tell whether fixing a bottleneck actually helped, or whether the real issue was sitting elsewhere. Systematic measurement not only helps identify the true causes of performance issues but also builds experience and sharpens what many engineers call engineering intuition—the ability to spot similar issues faster and more accurately on future projects. Some of my colleagues, who spent years working on game engines, could often pinpoint a problem just by examining logs and a profiler snapshot, without even looking at the source code. More often than not, their read on which subsystem or code fragment to modify turned out to be right. That's exactly the kind of intuition I mean. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Author: Sergei Kushnirenko&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sergei has over 20-year experience in coding and game development. He graduated from ITMO National Research University and began his career developing software for naval simulators, navigation systems, and network solutions. For the past fifteen years, Sergei has specialized in game development: at Electronic Arts, he worked on optimizing The Sims and SimCity BuildIt, and at Gaijin Entertainment, Sergei headed up the porting of games to the Nintendo Switch and Apple TV platforms. Sergei actively participates in open-source projects, including the ImSpinner library and the Pharaoh (1999) game restoration project.&lt;/p&gt;

&lt;h2&gt;
  
  
  All parts
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/1361/" rel="noopener noreferrer"&gt;Game++. Part 1: C++, game engines, and architectures&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/1375/" rel="noopener noreferrer"&gt;Game++. Part 1.2: C++, game engines, and architectures&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/1384/" rel="noopener noreferrer"&gt;Game++. Part 1.3: Game engine architectures&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/1390/" rel="noopener noreferrer"&gt;Game++. Part 1.4: Game engine architectures&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>cpp</category>
      <category>programming</category>
    </item>
    <item>
      <title>C++: Write, shorten, optimize</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Thu, 23 Jul 2026 14:01:55 +0000</pubDate>
      <link>https://dev.to/pvsdev/c-write-shorten-optimize-16il</link>
      <guid>https://dev.to/pvsdev/c-write-shorten-optimize-16il</guid>
      <description>&lt;p&gt;I stumble upon a C++ function that will be a really illustrative example of how refactoring can both shorten and optimize code. Today, we'll need to dust off our human brains in the era of vibe coding and recall what it's like to know what the good is and what the bad is—after all, someone has to do it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fau2ezvb9t42sjbbpg5yn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fau2ezvb9t42sjbbpg5yn.png" alt="1395_Write_Simplify_Optimize/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code snippet below from the vibe-coded project &lt;a href="https://github.com/NVlabs/vibetensor/tree/main" rel="noopener noreferrer"&gt;VibeTensor&lt;/a&gt;. I like exploring such projects as if I were a naturalist studying a new ecosystem—you can meet with my "field reports" &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1354/" rel="noopener noreferrer"&gt;here&lt;/a&gt; and &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1366/" rel="noopener noreferrer"&gt;there&lt;/a&gt;. So, I'm very curious to discover the genesis of new defects and flaws in code. Why? It helps us better understand and describe the challenges code can face in the GenAI era.&lt;/p&gt;

&lt;p&gt;One of my discoveries is code bloat that hurts readability and compilers' optimization. The following example showcases it in all its beauty.&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="n"&gt;TensorImpl&lt;/span&gt; &lt;span class="nf"&gt;make_contiguous_tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sizes&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;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&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="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;s&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;any_zero&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;It may seem that code size and performance are not so critical because they're used in tests.&lt;/p&gt;

&lt;p&gt;However, the code is duplicated across nine files, and ideally, that shouldn't happen at all, but if code is going to spread like this, it should at least stay compact.&lt;/p&gt;

&lt;p&gt;Also, we shouldn't forget that the generated C++ code must be optimized because the C++'s main purpose is to create highly efficient apps. In this case, we have to deal only with the slow test, but in another context, such patterns would significantly downgrade app performance and the negative impact would only grow as more flawed generated code accumulates over time.&lt;/p&gt;

&lt;p&gt;Slow C++ code goes against the idea of this programming language. That's why it's still a must-have to enhance the expertise in determining whether the code is well-written and how it can be refined, shortened, or optimized. Advanced knowledge can also help in regenerating code with better prompts.&lt;/p&gt;

&lt;p&gt;So, let's practice and do some refactoring. Let's start with this code fragment:&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;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sizes&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;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&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="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;s&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;any_zero&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;All the array elements are multiplied. If it encounters &lt;code&gt;0&lt;/code&gt;, the loop terminates to avoid unnecessarily processing the remaining array elements because it'll still set the result to zero.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;any_zero&lt;/code&gt; flag resets the variable that stores the product, but we can go easier:&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;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sizes&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;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;s&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;When we encounter &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;ne&lt;/code&gt; will be set to &lt;code&gt;0&lt;/code&gt; and the loop will end. We have another place for simplification: modern processors perform multiplication operations quickly, so we can do the multiplication first and then check it.&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;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;s&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;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code's functionality hasn't changed, but it's become shorter and, in my opinion, even easier to understand.&lt;/p&gt;

&lt;p&gt;If we already know that the array is small or that performance isn't critical (for example, because the function is called infrequently), we could simplify it even further:&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;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;s&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;Let's not think about that, since further actions make the loop be absorbed by another one.&lt;/p&gt;

&lt;p&gt;Now let's go back to the beginning of the function.&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="n"&gt;TensorImpl&lt;/span&gt; &lt;span class="nf"&gt;make_contiguous_tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&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 &lt;code&gt;static_cast&lt;/code&gt; makes code clunky. The first type casting is needed so that certain compilers or static analyzers don't flag odd arithmetic operations.&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="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's consider what would happen if we removed the &lt;code&gt;static_cast&lt;/code&gt; and the input array turned out to be empty:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the array is empty, then &lt;code&gt;nd = 0&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;If we subtract one from an unsigned zero, we get &lt;code&gt;SIZE_MAX&lt;/code&gt;, that is, a very large positive unsigned number.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;SIZE_MAX&lt;/code&gt; of the&lt;code&gt;size_t&lt;/code&gt; type is implicitly converted to &lt;code&gt;ptrdiff_t&lt;/code&gt; and stored in the &lt;code&gt;i&lt;/code&gt; variable. It turns out that &lt;code&gt;i = -1&lt;/code&gt; works as intended. But this is exactly where warnings may be issued. We initialize &lt;code&gt;ptrdiff_t&lt;/code&gt; with a number that is larger than the range of the largest number it can hold. Before C++20, the result was implementation-defined. Since C++20, the behavior is well-defined.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As a result, it'd be better to leave the first &lt;code&gt;static_cast&lt;/code&gt; as is. We can't say the same about the rest. They make no sense at all.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;vector&lt;/code&gt; class, the &lt;code&gt;[]&lt;/code&gt; &lt;a href="https://en.cppreference.com/cpp/container/vector/operator_at" rel="noopener noreferrer"&gt;operator&lt;/a&gt; takes an argument of the &lt;code&gt;size_type&lt;/code&gt; type (typically an alias of &lt;code&gt;size_t&lt;/code&gt;). The &lt;code&gt;i&lt;/code&gt; variable value will automatically be converted to &lt;code&gt;size_t&lt;/code&gt;, and there's nothing unsafe or suspicious about it. The explicit casts only add visual noise, so they're better removed.&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;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&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;You've taken the first step. But can we simplify this line even further? Let's see—I'd like to write something like this:&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;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;:&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;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;????&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;????&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&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;You still need the &lt;code&gt;i&lt;/code&gt; variable to iterate through the &lt;code&gt;strides&lt;/code&gt; array from the end, so we can't eliminate &lt;code&gt;i&lt;/code&gt; completely. But we can do something like that:&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;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;:&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;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&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;To be honest, I don't like that &lt;code&gt;--i&lt;/code&gt; has made the code more complicated and forces us to pause and realize why the variable is decremented in the beginning and then is used to access an array element.&lt;/p&gt;

&lt;p&gt;Okay, there is less code now, and it's easier to scan through it quickly, so half the work is done. But the code is still ultimately hard to read—it's neither clearer nor intrinsically harder.&lt;/p&gt;

&lt;p&gt;Let's take a look at the result.&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="n"&gt;TensorImpl&lt;/span&gt; &lt;span class="nf"&gt;make_contiguous_tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;:&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;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;s&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;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that there is less code, it becomes clear that the second loop is redundant. In the first loop, we iterate through all the elements. So why not multiply them right away?&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="n"&gt;TensorImpl&lt;/span&gt; &lt;span class="nf"&gt;make_contiguous_tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;:&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;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;sz&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;Oh, it's an absolute beauty. It's not a big deal that we multiply all the elements together, even though one of them might be zero. Microprocessors are multiplying rapidly these days. You could lose more by re-accessing all the elements in the second loop.&lt;/p&gt;

&lt;p&gt;What else is left? We don't need to initialize the &lt;code&gt;strides&lt;/code&gt; container with zeros. All of its elements will be overwritten anyway.&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="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// delete the second argument&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think that's all. We can also make one more small change: remove the &lt;code&gt;nd&lt;/code&gt; variable. It's not needed here or in the code that follows.&lt;/p&gt;

&lt;p&gt;As a result, we have:&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="n"&gt;TensorImpl&lt;/span&gt; &lt;span class="nf"&gt;make_contiguous_tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;:&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;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;sz&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;Hooray! We cut the code in half: from 20 lines down to just 9!&lt;/p&gt;

&lt;h2&gt;
  
  
  What's inside the assembly code?
&lt;/h2&gt;

&lt;p&gt;The function is shorter now, but does that also improve performance? Yes! Let's take a look at the main section of the assembly code for the original function version. &lt;/p&gt;

&lt;p&gt;Clang is used with the &lt;code&gt;-O2&lt;/code&gt; option:&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_7&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rbx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;r12&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r13&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;r12&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;adc&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rax&lt;/span&gt;
        &lt;span class="n"&gt;dec&lt;/span&gt;     &lt;span class="n"&gt;r12&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;r12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;ja&lt;/span&gt;      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_7&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;r12d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r13&lt;/span&gt;
        &lt;span class="n"&gt;je&lt;/span&gt;      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_9&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_4&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;test&lt;/span&gt;    &lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rax&lt;/span&gt;
        &lt;span class="n"&gt;je&lt;/span&gt;      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_5&lt;/span&gt;
        &lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;r12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rax&lt;/span&gt;
        &lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rbp&lt;/span&gt;
        &lt;span class="n"&gt;jne&lt;/span&gt;     &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_4&lt;/span&gt;
        &lt;span class="n"&gt;jmp&lt;/span&gt;     &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see a rather verbose assembly code with two loops. Now let's compare it with the assembly generated for the refactored version.&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_10&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rbp&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rdx&lt;/span&gt;
        &lt;span class="n"&gt;adc&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rcx&lt;/span&gt;
        &lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;r12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rdx&lt;/span&gt;
        &lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r13&lt;/span&gt;
        &lt;span class="n"&gt;jne&lt;/span&gt;     &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh, that's pure beauty. One loop. Compact and efficient. Wow!&lt;/p&gt;

&lt;p&gt;To those who aren't very familiar with the assembly language, it might seem strange that there isn't a conditional jump to implement this: &lt;code&gt;sz == 0 ? 1 : sz&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  Here's how it works
  &lt;br&gt;
The value of &lt;code&gt;acc&lt;/code&gt; is stored in the &lt;code&gt;RSI&lt;/code&gt; register. We'll need a copy of this value later, so it's stored in the &lt;code&gt;RCX&lt;/code&gt; register.&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;RBP&lt;/code&gt; register contains the address used to iterate through the elements of the &lt;code&gt;sizes&lt;/code&gt; array in reverse order. Here, we load the element into the &lt;code&gt;sz&lt;/code&gt; variable (&lt;code&gt;RDX&lt;/code&gt;) and shift the iterator by 8 bytes (the size of the &lt;code&gt;int64_t&lt;/code&gt; element) to the left.&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="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rbp&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;RSI&lt;/code&gt; is &lt;code&gt;acc&lt;/code&gt;. Let's put the register in the &lt;code&gt;strides&lt;/code&gt; array. &lt;code&gt;RAX&lt;/code&gt; holds the element address. The jump to the previous element will be executed below (see &lt;code&gt;add rax, -8&lt;/code&gt;).&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="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we've reached the cherry on top: &lt;code&gt;acc *= (sz == 0 ? 1 : sz)&lt;/code&gt; is executed and the RDX register(&lt;code&gt;sz&lt;/code&gt; variable) is compared with 1.&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="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;   &lt;span class="c1"&gt;// CF = is moved when sz - 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How does it work? &lt;/p&gt;

&lt;p&gt;1 is subtracted from &lt;code&gt;RDX&lt;/code&gt;. If a jump occurs, the carry flag (&lt;code&gt;CF&lt;/code&gt;) is set. This happens only when &lt;code&gt;CF = 1&lt;/code&gt; if &lt;code&gt;RDX&lt;/code&gt; is 0.&lt;/p&gt;

&lt;p&gt;This is &lt;code&gt;acc = sz&lt;/code&gt;. We overwrote the variable's value, but that's okay because we have a copy of it in &lt;code&gt;RCX&lt;/code&gt;.&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="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rdx&lt;/span&gt; &lt;span class="c1"&gt;// acc = sz&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;sz&lt;/code&gt; was 0, let's change it to 1. To do this, let's add the &lt;code&gt;CF&lt;/code&gt; carry flag. If &lt;code&gt;sz&lt;/code&gt; is not &lt;code&gt;0&lt;/code&gt;, then the carry flag is 0, and nothing will change. That's how the compiler avoided the conditional jump.&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="n"&gt;adc&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;   &lt;span class="c1"&gt;// acc = acc + 0 + CF&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All that's left is to perform two multiplications, decrement &lt;code&gt;RAX&lt;/code&gt;, and continue the loop.&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="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rcx&lt;/span&gt; &lt;span class="c1"&gt;// acc = acc * {ACC copy in RCS register}&lt;/span&gt;
&lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;r12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rdx&lt;/span&gt; &lt;span class="c1"&gt;// ne = ne * sz&lt;/span&gt;
&lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r13&lt;/span&gt; &lt;span class="c1"&gt;// Do we reach the beginning of strides?&lt;/span&gt;
&lt;span class="n"&gt;jne&lt;/span&gt;     &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_10&lt;/span&gt; &lt;span class="c1"&gt;// If no, we'll get goto LBB1_10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;br&gt;
&lt;p&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Another way
&lt;/h2&gt;

&lt;p&gt;Do we have another way? Yes, we have. I don't know whether I think about it or not. Unfortunately, after finishing my own refactoring, I made the mistake of immediately asking AI for alternative solutions.&lt;/p&gt;

&lt;p&gt;DeepSeek suggestions weren't any better, and some were even worse than the original. For example, it optimized one of the code implementations by reversing the array.&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="c1"&gt;// Wrap out strides&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;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But Claude Opus came up with an unusual implementation. It was noticed that we don't actually need two independent sequences of multiplications.&lt;/p&gt;

&lt;p&gt;If all elements of the input array are non-zero, then at the end of the loop, &lt;code&gt;acc == ne&lt;/code&gt;. And if even one element was zero, we can simply set &lt;code&gt;ne&lt;/code&gt; to zero at the end. We return to the &lt;code&gt;any_zero&lt;/code&gt; flag again, but in a smarter way. Using this idea, we can write the following code:&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="n"&gt;TensorImpl&lt;/span&gt; &lt;span class="nf"&gt;make_contiguous_tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&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;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;:&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;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;acc&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;&lt;/p&gt;
  Assembly code
  &lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_7&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r13&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;rdx&lt;/span&gt;
        &lt;span class="n"&gt;test&lt;/span&gt;    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;sete&lt;/span&gt;    &lt;span class="n"&gt;dil&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;adc&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rdx&lt;/span&gt;
        &lt;span class="n"&gt;or&lt;/span&gt;      &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dil&lt;/span&gt;
        &lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rbp&lt;/span&gt;
        &lt;span class="n"&gt;jne&lt;/span&gt;     &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_7&lt;/span&gt;
        &lt;span class="n"&gt;xor&lt;/span&gt;     &lt;span class="n"&gt;r13d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r13d&lt;/span&gt;
        &lt;span class="n"&gt;test&lt;/span&gt;    &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;cmove&lt;/span&gt;   &lt;span class="n"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;r14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Will code that performs half as many multiplications run faster? My prediction—not necessarily. The processor can perform two unrelated multiplications simultaneously on different pipelines. I'm willing to bet that my version and Claude's version will run at almost the same speed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance measurement
&lt;/h2&gt;

&lt;p&gt;Let's see how these implementations compare in reality. Designing a fair benchmark took a bit of effort. First, I had to ensure the input data couldn't overflow a 64-bit signed integer, since signed overflow is &lt;a href="https://pvs-studio.com/en/blog/terms/0066/" rel="noopener noreferrer"&gt;undefined behavior&lt;/a&gt; and would invalidate the measurements. Second, to make the contest fair, the test cases alternated between arrays with and without zero elements. Finally, the input arrays had varying lengths.&lt;/p&gt;

&lt;p&gt;I'm not claiming that the measurements are rigorous, but they represent a general idea. The speed of the first original algorithm is taken as the baseline. I used Clang with the &lt;code&gt;-O2&lt;/code&gt; and &lt;code&gt;-m64&lt;/code&gt; options.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The original version with two loops: &lt;strong&gt;1&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;My version with a single loop: &lt;strong&gt;0.88&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The Claude version with a single multiplication: &lt;strong&gt;0.92&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The refactoring improved performance by about 10% on average. I was actually hoping for a 20–30% speedup, but 10% is still a nice result—especially considering that it came from simplifying the code rather than making it more complicated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap-up
&lt;/h2&gt;

&lt;p&gt;Let's stop our little refactoring adventure here. Was it worth it? &lt;/p&gt;

&lt;p&gt;From the perspective of improving this particular piece of code, I'd say no. Tests are generated. It's not critical that code is longer and slower than it could be.&lt;/p&gt;

&lt;p&gt;It doesn't matter whether code is written by a human or generated by AI. One thing remains constant, someone must recognize what bad is and what good is. There must be experts who can spot the difference. Is the code good? Is it bad? Is the code fast? Is it secure?&lt;/p&gt;

&lt;p&gt;Some people can accept just the fact that the code works—and who cares what's inside it. In some cases—for example, for building a prototype—this is reasonable. However, those who aren't interested in architecture, optimization, or security most likely simply don't see or understand the whole picture of developing and maintaining large software products. So, I think we'll likely see a lot of drawbacks on this topic.&lt;/p&gt;

&lt;p&gt;AI is a powerful tool, but it's not a magic wand. When Claude suggested an alternative version of the algorithm to me, it was a good example of how AI can be used for research and to enhance human capabilities. But it can just as easily cause harm if used carelessly.&lt;/p&gt;

&lt;p&gt;If you want to continue improving your skills as a C++ specialist and create reliable, secure software, I suggest you check out these useful e-books and webinars on our website:&lt;/p&gt;

&lt;h2&gt;
  
  
  E-books &amp;amp; Webinars
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1215/" rel="noopener noreferrer"&gt;C++ programmer's guide to undefined behavior&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/book-giveaway/" rel="noopener noreferrer"&gt;Terrible tips for a C++ developer&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://youtube.com/playlist?list=PLGVoaOmC1PBw&amp;amp;si=fZ4RgpnxGZs5y1uh" rel="noopener noreferrer"&gt;Webinar series: Let's make a programming language&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>vibecoding</category>
    </item>
    <item>
      <title>Let's make a programming language. AST—Key points</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Wed, 22 Jul 2026 12:50:23 +0000</pubDate>
      <link>https://dev.to/pvsdev/lets-make-a-programming-language-ast-key-points-3ld8</link>
      <guid>https://dev.to/pvsdev/lets-make-a-programming-language-ast-key-points-3ld8</guid>
      <description>&lt;p&gt;In this part, we continue the development of a toy programming language by introducing the Abstract Syntax Tree (AST), the data structure that represents the hierarchical structure of source code after parsing.&lt;/p&gt;

&lt;h2&gt;
  
  
  About the speaker
&lt;/h2&gt;

&lt;p&gt;Yuri Minaev is an experienced C++ developer, architect at PVS-Studio, and a recognized voice in the C++ community who has spoken at CppCast, C++ on Sea, and CppCon. Over the course of ten sessions, he'll guide you through each stage of building your own programming language.&lt;/p&gt;

&lt;h2&gt;
  
  
  The foundation
&lt;/h2&gt;

&lt;p&gt;Each language construct corresponds to an AST node, while collections of nodes, such as function bodies or parameter lists, are represented by dedicated list nodes. Yuri explains the core node types, including literals, identifiers, unary and binary expressions, assignments, function calls, declarations, statements, and error nodes. &lt;/p&gt;

&lt;p&gt;Error nodes allow parsing to continue after syntax errors, enabling the compiler to collect multiple errors instead of stopping at the first one. Invalid nodes propagate their state upward so the entire program can be marked invalid before evaluation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's grow our tree!
&lt;/h2&gt;

&lt;p&gt;A central component of the implementation is the AST builder, which owns every created node and manages memory automatically. As the parser recognizes grammar rules, it instructs the builder to create nodes. Terminal nodes are stored in an internal state, while compound nodes consume previously created child nodes from that state and combine them into larger structures until the root node of the program is produced.&lt;/p&gt;

&lt;h2&gt;
  
  
  Traversing the Tree
&lt;/h2&gt;

&lt;p&gt;The second half of the talk focuses on traversing the AST with a visitor. The visitor performs a depth-first traversal using preview and visit functions. Preview executes when entering a node and can decide whether to traverse its children, while visit runs after returning from them. This design allows specialized visitors to process only the node types they need. &lt;/p&gt;

&lt;p&gt;As an example, Yuri demonstrates an AST printer that uses the visitor to produce a tree-like representation of the program structure, concluding with a preview of future topics covering variables, types, semantics, functions, and evaluation.&lt;/p&gt;

&lt;p&gt;Want more?&lt;/p&gt;

&lt;p&gt;If you want to watch other talks or see the whole episode, &lt;a href="https://www.youtube.com/watch?v=ZY7N6ALq2Bw" rel="noopener noreferrer"&gt;follow this link.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also sign up for our upcoming webinars, for example: &lt;a href="https://pvs-studio.com/en/webinar/31/" rel="noopener noreferrer"&gt;Let's make a programming language. Variables&lt;/a&gt;. We hope to see you there!&lt;/p&gt;

&lt;p&gt;If you'd like to learn more about PVS-Studio analyzer, check out our &lt;a href="https://pvs-studio.com/en/" rel="noopener noreferrer"&gt;website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;See ya!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>cpp</category>
      <category>webinar</category>
    </item>
    <item>
      <title>PVS-Studio brings Unreal Engine support to Team licenses</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Mon, 20 Jul 2026 12:58:32 +0000</pubDate>
      <link>https://dev.to/pvsdev/pvs-studio-brings-unreal-engine-support-to-team-licenses-58n</link>
      <guid>https://dev.to/pvsdev/pvs-studio-brings-unreal-engine-support-to-team-licenses-58n</guid>
      <description>&lt;p&gt;Good news for Unreal Engine developers: we've made PVS-Studio more accessible! You can now analyze UE projects in PVS-Studio with a Team license, not just an Enterprise license. We'll explain below why we changed our licensing model and what it means for small and mid-sized game studios.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkzeaf2yb0ocgg2muxbdo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkzeaf2yb0ocgg2muxbdo.png" alt="1394_ue_team/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is PVS-Studio?
&lt;/h2&gt;

&lt;p&gt;In case you haven't heard of us yet: PVS-Studio is a static analyzer for C, C++, C#, and Java code (+ JavaScript/TypeScript and Go in the upcoming August release). PVS-Studio is a tool that automatically checks your project's code for potential errors, security vulnerabilities, and optimization opportunities without executing the code itself.&lt;/p&gt;

&lt;p&gt;If you'd like to learn more about the analyzer's capabilities and use cases, check out our &lt;a href="https://pvs-studio.com/en/blog/posts/1358/" rel="noopener noreferrer"&gt;overview article&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  PVS-Studio is now more accessible to Unreal Engine developers
&lt;/h2&gt;

&lt;p&gt;Game projects continue to grow in both scale and complexity. A modern game can contain millions of code lines, hundreds of modules, and countless interconnected systems. Even experienced teams may miss bugs that slip past the compiler and code review.&lt;/p&gt;

&lt;p&gt;At the same time, a large part of the industry consists of indie and AA studios. Despite having smaller budgets and fewer developers, such teams tackle complex engineering challenges and care just as much about delivering high-quality products.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PVS-Studio 7.43, released in June 2026, introduced an important change: Unreal Engine project analysis is now available with the Team license instead of being limited to Enterprise. This &lt;a href="https://pvs-studio.com/en/order/" rel="noopener noreferrer"&gt;makes the tool more affordable&lt;/a&gt; for small and mid-sized game studios and lowers the barrier to adopting static analysis as part of their daily development workflow.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why did we make this change?
&lt;/h3&gt;

&lt;p&gt;Unreal Engine has long been the go-to option for teams of all sizes, from AAA studios and AA developers to indie teams and solo creators. For many of them, the Enterprise license was excessive, especially when working within a tight budget and carefully selecting development tools.&lt;/p&gt;

&lt;p&gt;According to the &lt;a href="https://gdconf.com/article/gdc-2026-state-of-the-game-industry-reveals-impact-of-layoffs-generative-ai-and-more/" rel="noopener noreferrer"&gt;2026 State of the Game Industry&lt;/a&gt; report, Unreal Engine is now the primary engine for the largest percentage of game developers, with approximately 42% of respondents using it as their main engine. It's particularly popular among AA studios, where the engine is used even more broadly than in the AAA segment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F03vxnwfvqm1trtt38z7o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F03vxnwfvqm1trtt38z7o.png" alt="1394_ue_team/image2.png" width="664" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 1—GDC 2026 State of the Game Industry Report: What game engine do you primarily use for game development?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Like larger studios, these teams face technical challenges such as complex game logic, ever-growing codebases, third-party integrations, platform-specific issues, and hard-to-reproduce bugs. The difference is that they usually have fewer resources available for extensive debugging, manual code reviews, and dedicated QA workflows.&lt;/p&gt;

&lt;p&gt;QA often takes a back seat while the team focuses on building gameplay features. As a result, bugs negatively impact the player experience and damage the game's reputation right at release. In smaller studios, developers often have to handle testing themselves because the team isn't big enough and the &lt;a href="https://globalstep.com/blog/game-qa-in-indie-vs-aaa-studios-whats-the-difference/" rel="noopener noreferrer"&gt;budget may not allow&lt;/a&gt; for hiring dedicated QA specialists.&lt;/p&gt;

&lt;p&gt;The Team license is designed with such teams in mind. By adding Unreal Engine support at this licensing tier, we've made static analysis more accessible where it can have the greatest impact: in teams where catching bugs early saves time, money, and effort while helping developers stay focused on creating great games.&lt;/p&gt;

&lt;h2&gt;
  
  
  How bugs affect development pace
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The cost of an error
&lt;/h3&gt;

&lt;p&gt;Players pay especially close attention to the quality of a game during the first few days after its launch. If bugs, crashes, or critical failures arise during that period, the team must immediately shift its focus to analyzing feedback, deploying hotfixes, and stabilizing the game. This means investing time and money on work that is rarely part of the intended project plan.&lt;/p&gt;

&lt;p&gt;High-profile releases such as Assassin's Creed Unity and &lt;a href="https://www.playstationlifestyle.net/2020/12/17/cdpr-founders-lose-1b-after-cyberpunk-2077-release/" rel="noopener noreferrer"&gt;Cyberpunk 2077&lt;/a&gt; demonstrate that technical issues at launch can lead to months of post-release fixes. Teams with limited resources may find it even more challenging to recover from a rocky launch.&lt;/p&gt;

&lt;p&gt;In reality, the benefits of catching bugs early extend beyond saving money. Understanding and fixing the issue is usually easier when the code is still fresh in the developer's mind. If the bug surfaces much later, the team has to reconstruct the original context, untangle dependencies, and stop working on other projects to investigate it.&lt;/p&gt;

&lt;p&gt;Static analysis naturally fits into everyday development workflows, including local development, builds, and CI. It helps developers spot suspicious code before fixes require lengthy investigations, repeated testing, or urgent patches.&lt;/p&gt;

&lt;p&gt;If you'd like to learn more, check out our article on &lt;a href="https://pvs-studio.com/en/blog/posts/1373/" rel="noopener noreferrer"&gt;how static analysis affects the cost of fixing errors throughout a project's lifecycle&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does this mean for a team?
&lt;/h3&gt;

&lt;p&gt;Based on &lt;a href="https://www.reddit.com/r/gamedev/comments/1fv2tmc/why_did_you_start_gamedev_i_know_so_many_people/" rel="noopener noreferrer"&gt;discussions within the community&lt;/a&gt;, it's clear that most people don't pursue a career in game development to spend their days shipping emergency patches or tracking down typos in legacy code. They'd rather focus on designing gameplay mechanics, building systems, creating content, and making their game better.&lt;/p&gt;

&lt;p&gt;The static analyzer doesn't replace developers or make decisions for the team. Its job is much simpler: it highlights potential issues in code and helping teams find bugs earlier with less effort.&lt;/p&gt;

&lt;p&gt;For the team, this means a more streamlined workflow with fewer interruptions and more time to focus on tasks that advance the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does PVS-Studio offer for Unreal Engine projects?
&lt;/h2&gt;

&lt;p&gt;Unreal Engine has its own architecture, type system, coding conventions, and internal mechanisms. So, the key to a thorough analysis is considering the engine specifics instead of relying solely on general C++ checks.&lt;/p&gt;

&lt;p&gt;We continue to expand Unreal Engine support in PVS-Studio, including in collaboration with Epic Games. It helps us better account for UE-specific behavior, improve our integrations, and respond more quickly to issues developers encounter when using the analyzer with real-world Unreal Engine projects.&lt;/p&gt;

&lt;p&gt;This translates into a smoother out-of-the-box experience for teams working with Unreal Engine:&lt;/p&gt;

&lt;h4&gt;
  
  
  Annotations for Unreal Engine code
&lt;/h4&gt;

&lt;p&gt;PVS-Studio includes built-in annotations for Unreal Engine. They help the analyzer interpret code more accurately, account for engine-specific behavior, and reduce false positives without requiring extensive manual configuration.&lt;/p&gt;

&lt;h4&gt;
  
  
  UE-specific diagnostic rules
&lt;/h4&gt;

&lt;p&gt;The analyzer also includes diagnostic rules specifically designed for Unreal Engine projects. They help detect issues stemming from UE-specific coding patterns, engine conventions, and the object lifecycle of the engine. Here are a couple of examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v1100/" rel="noopener noreferrer"&gt;V1100&lt;/a&gt;: Declaring a pointer to a type derived from 'UObject' in a class that is not derived from 'UObject' is dangerous.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v1102/" rel="noopener noreferrer"&gt;V1102&lt;/a&gt;: Violation of naming conventions may cause Unreal Header Tool to work incorrectly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Integration with the Unreal Engine toolchain
&lt;/h4&gt;

&lt;p&gt;Our goal is to support the workflows and tools that Unreal Engine developers already rely on. This way, introducing static analysis won't disrupt an established development process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run PVS-Studio in Unreal Build Tool&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Starting with Unreal Engine 4.17, you can run PVS-Studio directly through Unreal Build Tool. This makes it easy to integrate the analyzer into your existing build pipeline and use it as part of your regular development workflow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Support for multiple Unreal Engine versions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio supports both legacy and current Unreal Engine releases. This is especially important for teams that can't immediately migrate to the latest engine version and need to continue developing projects using their current version.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integration with Unreal Build Accelerator, Horde, and SN-DBS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For large projects and CI pipelines, analysis speed and seamless integration with existing build infrastructure are essential. PVS-Studio is compatible with &lt;a href="https://pvs-studio.com/en/docs/manual/0043/#uba" rel="noopener noreferrer"&gt;Unreal Build Accelerator&lt;/a&gt;, Horde, and &lt;a href="https://pvs-studio.com/en/docs/manual/0043/#sndbs" rel="noopener noreferrer"&gt;SN-DBS&lt;/a&gt;. This streamlines the integration of static analysis into existing distributed build and code check workflows.&lt;/p&gt;

&lt;h4&gt;
  
  
  A comprehensive set of diagnostic rules for everyday C++ development
&lt;/h4&gt;

&lt;p&gt;In addition to providing diagnostic rules specific to Unreal Engine, PVS-Studio detects a wide range of issues that can affect any C++ codebase. For example, it can identify:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;memory and resource management issues: memory leaks (&lt;a href="https://pvs-studio.com/en/docs/warnings/v773/" rel="noopener noreferrer"&gt;V773&lt;/a&gt;), use-after-free bugs (&lt;a href="https://pvs-studio.com/en/docs/warnings/v774/" rel="noopener noreferrer"&gt;V774&lt;/a&gt;), and double resource deallocation (&lt;a href="https://pvs-studio.com/en/docs/warnings/v586/" rel="noopener noreferrer"&gt;V586&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;uses of uninitialized data: in local variables, class members, and constructors (&lt;a href="https://pvs-studio.com/en/docs/warnings/v614/" rel="noopener noreferrer"&gt;V614&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v730/" rel="noopener noreferrer"&gt;V730&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v670/" rel="noopener noreferrer"&gt;V670&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;object lifetime and reference issues: dangling references, incorrect handling of temporary objects, &lt;code&gt;string_view&lt;/code&gt;, &lt;code&gt;lambda&lt;/code&gt;, and objects after &lt;code&gt;std::move&lt;/code&gt; (&lt;a href="https://pvs-studio.com/en/docs/warnings/v758/" rel="noopener noreferrer"&gt;V758&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1017/" rel="noopener noreferrer"&gt;V1017&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1047/" rel="noopener noreferrer"&gt;V1047&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1030/" rel="noopener noreferrer"&gt;V1030&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;logic errors in conditions and expressions: conditions that always evaluate to true or false, suspicious comparisons, and incorrect operators (&lt;a href="https://pvs-studio.com/en/docs/warnings/v547/" rel="noopener noreferrer"&gt;V547&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v517/" rel="noopener noreferrer"&gt;V517&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v709/" rel="noopener noreferrer"&gt;V709&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v564/" rel="noopener noreferrer"&gt;V564&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;copy-paste errors and other human mistakes: duplicated code fragments, variable name typos, and suspicious inconsistencies between similar checks (&lt;a href="https://pvs-studio.com/en/docs/warnings/v778/" rel="noopener noreferrer"&gt;V778&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v722/" rel="noopener noreferrer"&gt;V722&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1013/" rel="noopener noreferrer"&gt;V1013&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v524/" rel="noopener noreferrer"&gt;V524&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;integer overflows, unsafe conversions, and signedness issues (&lt;a href="https://pvs-studio.com/en/docs/warnings/v1028/" rel="noopener noreferrer"&gt;V1028&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1083/" rel="noopener noreferrer"&gt;V1083&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1112/" rel="noopener noreferrer"&gt;V1112&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1029/" rel="noopener noreferrer"&gt;V1029&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;issues with loops, indexing, and array bounds (&lt;a href="https://pvs-studio.com/en/docs/warnings/v557/" rel="noopener noreferrer"&gt;V557&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v621/" rel="noopener noreferrer"&gt;V621&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v776/" rel="noopener noreferrer"&gt;V776&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Fast fixes for issues and false positives
&lt;/h4&gt;

&lt;p&gt;Working effectively with Unreal Engine requires a stable and reliable tool. That's why we focus on both adding new features and refining existing integrations. We're constantly working to fix reported issues, improve compatibility, and reduce the number of false positives.&lt;/p&gt;

&lt;p&gt;Our collaboration with Epic Games plays an important role in this effort. It helps us respond more quickly to changes in the Unreal Engine ecosystem and adapt the analyzer to the real-world needs of developers.&lt;/p&gt;

&lt;p&gt;To learn more about our collaboration, check out the &lt;a href="https://www.unrealengine.com/blog/how-pvs-studio-team-improved-unreal-engines-code" rel="noopener noreferrer"&gt;article on the Epic Games website&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ready to give it a try?
&lt;/h2&gt;

&lt;p&gt;Starting with PVS-Studio 7.43, you can analyze Unreal Engine projects under the Team license.&lt;/p&gt;

&lt;p&gt;If you use Unreal Engine for development and have considered trying PVS-Studio, now's the perfect time to run the analyzer on your project. If you have any questions about integration or configuration, our support team will be happy to help you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&amp;amp;utm_medium=devto&amp;amp;utm_campaign=article&amp;amp;utm_content=1394" rel="noopener noreferrer"&gt;You can get a trial version here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Stay creative, and we'll be there to help protect your code from bugs, vulnerabilities, and typos with PVS-Studio.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>tooling</category>
      <category>news</category>
      <category>gamedev</category>
    </item>
    <item>
      <title>One mistake no one has ever made. Right?</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Thu, 16 Jul 2026 14:02:41 +0000</pubDate>
      <link>https://dev.to/pvsdev/one-mistake-no-one-has-ever-made-right-6kh</link>
      <guid>https://dev.to/pvsdev/one-mistake-no-one-has-ever-made-right-6kh</guid>
      <description>&lt;p&gt;As early as your freshman year of college—or even earlier—you might have realized that writing arr[len(arr)] is a bad idea. Surely, no one could get that wrong, right? We thought the same thing, until we checked 1,000 projects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5xyh3dm05v7mz9klzp6t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5xyh3dm05v7mz9klzp6t.png" alt="1393_len_error/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick explanation of the error, just in case
&lt;/h2&gt;

&lt;p&gt;Any Go developer knows that the built-in &lt;code&gt;len()&lt;/code&gt; function takes a value and returns its length based on the value's type.&lt;/p&gt;

&lt;p&gt;Let's check the &lt;a href="https://pkg.go.dev/builtin#len" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; and take a closer look at a function signature:&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="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The built-in &lt;code&gt;len&lt;/code&gt; function returns the length of &lt;code&gt;v&lt;/code&gt; based on the type of the passed value:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Array: the number of elements in &lt;code&gt;v&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Pointer to array: the number of elements in &lt;code&gt;*v&lt;/code&gt; (even if &lt;code&gt;v&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Slice, or map: the number of elements in &lt;code&gt;v&lt;/code&gt;; if &lt;code&gt;v&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt;, &lt;code&gt;len(v)&lt;/code&gt; is zero.&lt;/li&gt;
&lt;li&gt;String: the number of bytes in &lt;code&gt;v&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Channel: the number of elements queued (unread) in the channel buffer; if &lt;code&gt;v&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt;, &lt;code&gt;len(v)&lt;/code&gt; is zero.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Look at this simple example:&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="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;SimpleExample&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&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;Collection indices always start at 0. So, for a slice of the &lt;code&gt;n&lt;/code&gt; length, the valid indices range from 0 to &lt;code&gt;n – 1&lt;/code&gt;. Then &lt;code&gt;len(arr)&lt;/code&gt; returns &lt;code&gt;n&lt;/code&gt;. As a result, accessing &lt;code&gt;arr[len(arr)]&lt;/code&gt; always leads to a program panic.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's V8031, and how did we discover it?
&lt;/h2&gt;

&lt;p&gt;First, a quick note: in case you didn't know, we're developing PVS-Studio, a static analyzer for C, C++, C#, and Java code. The early access program for the new Go and JS/TS analyzers just ended. We're currently preparing them for the release, which is scheduled for August. Around the same time, the early access program for PVS-Studio Atlas—a team solution for managing code analysis results—will launch. &lt;a href="https://pvs-studio.com/en/subscribe/" rel="noopener noreferrer"&gt;Stay tuned&lt;/a&gt; for updates.&lt;/p&gt;

&lt;p&gt;Now back to V8031. It's a &lt;a href="https://pvs-studio.com/en/docs/warnings/v8031/" rel="noopener noreferrer"&gt;diagnostic rule&lt;/a&gt; for the Go analyzer. We didn't expect it to deliver such good results when we were working on it. We even assumed it might not find anything. We thought, "What if this is a made-up error that can be spotted in any review or test?" From our experience, though, we knew there could be dozens of such errors that are easy to spot but often overlooked due to exhaustion, carelessness, or a lack of effort.&lt;/p&gt;

&lt;p&gt;Implementing the diagnostic rule was easy: it's built entirely on &lt;a href="https://pvs-studio.com/en/blog/terms/0004/" rel="noopener noreferrer"&gt;AST&lt;/a&gt; and uses as little of our own code as possible beyond what Go's built-in analysis tools provide. Feel free to create your own diagnostic rules, by the way. You might want to start with our introductory article, "&lt;a href="https://pvs-studio.com/en/blog/posts/go/1329/" rel="noopener noreferrer"&gt;How to create your own Go static analyzer?&lt;/a&gt;" &lt;/p&gt;

&lt;p&gt;The next step is testing. In our team, it consists of three stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Unit testing;&lt;/li&gt;
&lt;li&gt;Running a diagnostic rule on our collection of GitHub projects;&lt;/li&gt;
&lt;li&gt;Running a diagnostic rule on the 1,000 most popular Go projects (selected by number of stars).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Right now, we'll focus on the third stage. We were surprised by the results as soon as we got them. Let's take a look at them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The results
&lt;/h2&gt;

&lt;p&gt;In our articles, we usually describe errors and explain possible ways to fix them. We won't do that here since all the code snippets would have the same description as the one at the beginning of the article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Project No. 1
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/bfenetworks/bfe" rel="noopener noreferrer"&gt;bfe&lt;/a&gt; is a modern load balancer used by Baidu.&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="n"&gt;type&lt;/span&gt; &lt;span class="n"&gt;BasicLitList&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;BasicLit&lt;/span&gt;
&lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;BasicLitList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;End&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pos&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;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)].&lt;/span&gt;&lt;span class="n"&gt;End&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;PVS-Studio warning: V8031 Using the call of the 'len' function as index will cause off-by-one error. Consider using 'len(b) - 1' instead. &lt;a href="https://github.com/bfenetworks/bfe/blob/d8d6dcb5c49e586f19b433acfee57fb57412ea7a/bfe_basic/condition/parser/ast.go#L138" rel="noopener noreferrer"&gt;ast.go&lt;/a&gt; 138&lt;/p&gt;

&lt;h3&gt;
  
  
  Project No. 2
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/MontFerret/ferret" rel="noopener noreferrer"&gt;Ferret&lt;/a&gt; is a programmable data extraction and automation tool for developers.&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="n"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Vector&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Vector&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;slice&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getCurrentSlice&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PVS-Studio warning: V8031 Using the call of the 'len' function as index will cause off-by-one error. Consider using 'len(slice) - 1' instead. &lt;a href="https://github.com/MontFerret/ferret/blob/13d1aa56e1aff334b741fc1b5de2841f763d1946/pkg/runtime/expressions/vector.go#L28" rel="noopener noreferrer"&gt;vector.go&lt;/a&gt; 28&lt;/p&gt;

&lt;h3&gt;
  
  
  Project No. 3
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/wader/fq/tree/61d5e42f478cd3800847baaf608ccd6c0277de26" rel="noopener noreferrer"&gt;fq&lt;/a&gt; is a tool, language, and set of decoders for working with binary and text formats.&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="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;blockFns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;uint64&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;D&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dc&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;decodeContext&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="nl"&gt;blockTypeInterfaceDescription:&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;D&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dc&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;decodeContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;typ&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FieldU16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"link_type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LinkTypeMap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FieldU16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"reserved"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FieldU32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"snap_len"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FieldArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"options"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;....)&lt;/span&gt;

  &lt;span class="n"&gt;dc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interfaceTypes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interfaceTypes&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;typ&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;PVS-Studio warning: V8031 Using the call of the 'len' function as index will cause off-by-one error. Consider using 'len(dc.interfaceTypes) - 1' instead. &lt;a href="https://github.com/wader/fq/blob/61d5e42f478cd3800847baaf608ccd6c0277de26/format/pcap/pcapng.go#L234" rel="noopener noreferrer"&gt;pcapng.go&lt;/a&gt; 234&lt;/p&gt;

&lt;h3&gt;
  
  
  Project No. 4
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/lxc/incus" rel="noopener noreferrer"&gt;Incus&lt;/a&gt; is a modern container management system and virtual machine manager.&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="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;qemuRawCfgOverride&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conf&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;cfg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Section&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;confOverride&lt;/span&gt; &lt;span class="n"&gt;string&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="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;sectionContent&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;comment:&lt;/span&gt; &lt;span class="n"&gt;sec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Comment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;entries:&lt;/span&gt; &lt;span class="n"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;indexedSection&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indexedSection&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;
  &lt;span class="n"&gt;orderedSections&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderedSections&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;section&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;name:&lt;/span&gt;    &lt;span class="n"&gt;sec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;content:&lt;/span&gt; &lt;span class="n"&gt;content&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;PVS-Studio warning: V8031 Using the call of the 'len' function as index will cause off-by-one error. Consider using 'len(indexedSection) - 1' instead. &lt;a href="https://github.com/lxc/incus/blob/46b29cb5f017f14bae9ba33d93def890890c1c1a/internal/server/instance/drivers/driver_qemu_config_override.go#L50" rel="noopener noreferrer"&gt;driver_qemu_config_override.go&lt;/a&gt; 50&lt;/p&gt;

&lt;p&gt;We spotted two such errors in this project:&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="n"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;heartbeatFixture&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Gateway&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&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="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gateways&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gateways&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gateway&lt;/span&gt;
  &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;states&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;gateway&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;
  &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;servers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;gateway&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gateway&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;V8031 Using the call of the 'len' function as index will cause off-by-one error. Consider using 'len(f.gateways) - 1' instead. &lt;a href="https://github.com/lxc/incus/blob/46b29cb5f017f14bae9ba33d93def890890c1c1a/internal/server/cluster/heartbeat_test.go#L253" rel="noopener noreferrer"&gt;heartbeat_test.go&lt;/a&gt; 253&lt;/p&gt;

&lt;p&gt;We've encountered some errors multiple times in other projects. The reason for this was that some Project 1 included some other Project 2 as a dependency. So, it's fair to say that the errors are piling up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's summarize
&lt;/h2&gt;

&lt;p&gt;We wanted to share these exciting results with you. Although unlikely, this error may lurk in your own project. Not long ago, we discussed another interesting error in the article &lt;a href="https://pvs-studio.com/en/blog/posts/go/1350/" rel="noopener noreferrer"&gt;Does the operator ^ represent exponentiation or exclusion?&lt;/a&gt;. And to ensure your project's code is clean, you can use &lt;a href="https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&amp;amp;utm_medium=devto&amp;amp;utm_campaign=article&amp;amp;utm_content=1393" rel="noopener noreferrer"&gt;PVS-Studio static analyzer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>History of JavaScript: Browser wars, ECMAScript, Node.js, TypeScript, and React</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Thu, 09 Jul 2026 12:44:06 +0000</pubDate>
      <link>https://dev.to/pvsdev/history-of-javascript-browser-wars-ecmascript-nodejs-typescript-and-react-3fbi</link>
      <guid>https://dev.to/pvsdev/history-of-javascript-browser-wars-ecmascript-nodejs-typescript-and-react-3fbi</guid>
      <description>&lt;p&gt;It only took ten days to develop the language that powers the web. This article tells the story of JavaScript and the tools that helped shape it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff6zpgxltcntrxdgvl8fh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff6zpgxltcntrxdgvl8fh.png" alt="1392_JavaScript_history/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1995. The birth of a legend
&lt;/h2&gt;

&lt;p&gt;The idea for JavaScript was born at Netscape. At the time, web pages consisted almost entirely of HTML, and Netscape wanted to make them more interactive.&lt;/p&gt;

&lt;p&gt;The first step in that direction was licensing Java for use in the Netscape browser. However, Java's complexity proved challenging for web designers.&lt;/p&gt;

&lt;p&gt;Brendan Eich was then tasked with creating a programming language that wasn't too complex and could be embedded directly into HTML pages.  Eventually, Marc Andreessen, co-founder of Netscape Communications, and Bill Joy, co-founder of Sun Microsystems, also contributed to the language development. To meet the deadline for the Netscape browser release, the companies agreed to collaborate on the language.&lt;/p&gt;

&lt;p&gt;During its development, the language changed its name several times. For example, the first version Eich created in just ten days was called Mocha. It was then renamed to LiveScript. The final name was chosen because the word Java was already popular and well-known. &lt;/p&gt;

&lt;p&gt;JavaScript was first announced shortly before the second beta release of Netscape Navigator. Meanwhile, Netscape announced that 28 leading IT companies planned to incorporate JavaScript into their future products.&lt;/p&gt;

&lt;p&gt;JavaScript 1.0 was released in 1996 alongside Netscape Navigator 2.&lt;/p&gt;

&lt;h2&gt;
  
  
  1997-1999. ECMAScript
&lt;/h2&gt;

&lt;p&gt;In 1996, Microsoft also released JScript as part of Internet Explorer 3, which was an open-source implementation of JavaScript for Windows. By the way, the name was changed to avoid negotiating trademark rights for Java with Sun Microsystems.&lt;/p&gt;

&lt;p&gt;To eliminate browser incompatibilities caused by different implementations, Netscape handed the JavaScript specification over to the ECMA international organization.&lt;/p&gt;

&lt;p&gt;So, the ECMA-262 specification was created. The language got the name ECMAScript because JavaScript was already trademarked. Around the same time, the &lt;a href="https://tc39.es/" rel="noopener noreferrer"&gt;TC39&lt;/a&gt; technical committee was formed, bringing together representatives from Netscape, Microsoft, Sun Microsystems, and other companies. It still exists today, continuing to define the evolution of ECMAScript.&lt;/p&gt;

&lt;p&gt;The ECMAScript 2 specification was released in September 1998. Although it didn't introduce any new features, it laid the groundwork for all future changes.&lt;/p&gt;

&lt;p&gt;ECMAScript 3 was released in 1999, introducing several new language capabilities, including regular expressions, exception handling, the &lt;code&gt;switch&lt;/code&gt;-&lt;code&gt;case&lt;/code&gt; statement, and the &lt;code&gt;do&lt;/code&gt;-&lt;code&gt;while&lt;/code&gt; loop variant.&lt;/p&gt;

&lt;h2&gt;
  
  
  2001. The end of the first browser war
&lt;/h2&gt;

&lt;p&gt;The first major rivalry among web browser companies began in 1995.&lt;/p&gt;

&lt;p&gt;At the time, Netscape Navigator was the most well-known browser because it was available for free non-commercial use. However, as part of the Windows 95 Plus! package, Microsoft released Internet Explorer 1.0.&lt;/p&gt;

&lt;p&gt;Just three months later, Internet Explorer 2.0 became available for free download. All Windows users, including commercial organizations, could use the browser for free, which was its key advantage over Netscape Navigator.&lt;/p&gt;

&lt;p&gt;From that point on, every Netscape Navigator and Internet Explorer update was an attempt to outdo the competition. In 1996, Internet Explorer began seriously challenging Netscape's browser when it became the first to offer commercial support for Cascading Style Sheets (CSS).&lt;/p&gt;

&lt;p&gt;The browser wars involved more than just the companies behind the browsers. Web designers at the time actively placed badges on their websites saying things like "Best viewed in Netscape" or "Best viewed in Internet Explorer." These badges usually linked to the corresponding browser download page and meant that the site had only been tested in one of them. In response, supporters of the open web launched the "Viewable with Any Browser" campaign.&lt;/p&gt;

&lt;p&gt;In the end, Microsoft won the battle, securing over 90% of the global market. This was primarily due to the widespread adoption of Windows, which came preinstalled with Internet Explorer as the default browser.&lt;/p&gt;

&lt;p&gt;Back in 1998, when defeat became inevitable, Netscape released the source code of its browser. This decision would later play a role in future browser wars. In 2003, AOL, which owned Netscape by that time, finally disbanded the development team.&lt;/p&gt;

&lt;p&gt;Meanwhile, Microsoft faced a major lawsuit from the U.S. Department of Justice for violating antitrust laws. The court ruled that the company held a monopoly, requiring it to allow computer manufacturers and users to remove Internet Explorer access icons and install different browsers without altering the Windows interface. &lt;/p&gt;

&lt;h3&gt;
  
  
  That same year: the first JSON message
&lt;/h3&gt;

&lt;p&gt;The first message in JSON format was sent in April 2001 from a computer in a garage in the San Francisco Bay Area.&lt;/p&gt;

&lt;p&gt;Douglas Crockford and Chip Morningstar, co-founders of the State Software technology consulting company, were already trying to build AJAX applications even before the term AJAX was first coined.&lt;/p&gt;

&lt;p&gt;The message looked like this:&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="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;domain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;fudco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"session"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="nl"&gt;text:&lt;/span&gt; &lt;span class="s"&gt;"Hello world"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only a small part of it resembles the JSON we know today. The message itself is an HTML document containing JavaScript code.&lt;/p&gt;

&lt;p&gt;The format was originally supposed to be called JavaScript Markup Language (JSML), but that acronym was already taken by Java Speech Markup Language, so it was renamed to the JavaScript Object Notation we know today.&lt;/p&gt;

&lt;p&gt;The authors realized they had developed a versatile format, but customers were reluctant to use an unproven technology without specifications.&lt;/p&gt;

&lt;p&gt;This prompted Crockford to purchase the &lt;a href="https://www.json.org/json-en.html" rel="noopener noreferrer"&gt;JSON.org&lt;/a&gt; domain in 2002, where he published the JSON grammar and a parser implementation.&lt;/p&gt;

&lt;h2&gt;
  
  
  2004. Google invents AJAX
&lt;/h2&gt;

&lt;p&gt;On April 1, 2004, Google launched Gmail, marking the first large-scale showcase of AJAX. The service loaded emails, updated the interface, and displayed search suggestions without reloading the page, which was a true breakthrough at the time. Paul Buchheit, project creator and one of Google's first employees, used XMLHttpRequest to make the browser experience feel like a desktop application.&lt;/p&gt;

&lt;p&gt;Interestingly, the term AJAX appeared a little later. In February 2005, Jesse James Garrett introduced it in his article "&lt;a href="https://designftw.mit.edu/lectures/apis/ajax_adaptive_path.pdf" rel="noopener noreferrer"&gt;Ajax: A New Approach to Web Applications&lt;/a&gt;."&lt;/p&gt;

&lt;p&gt;Asynchronous JavaScript and XML (AJAX) is an approach to web application development that enables websites to exchange data with a server in the background without fully reloading the page.&lt;/p&gt;

&lt;p&gt;AJAX marked the beginning of a new era in web applications and frameworks. It also kicked off Web 2.0, which transformed internet users from passive readers into active content creators.&lt;/p&gt;

&lt;h2&gt;
  
  
  2006. jQuery
&lt;/h2&gt;

&lt;p&gt;John Resig created &lt;a href="https://jquery.com/" rel="noopener noreferrer"&gt;jQuery&lt;/a&gt; at BarCamp NYC in January 2006. Its key sources of inspiration included Dean Edwards' CSSQuery library and other community projects from that time.&lt;/p&gt;

&lt;p&gt;The library was designed to streamline common tasks like DOM traversal, event handling, and AJAX.&lt;/p&gt;

&lt;p&gt;jQuery fixed many issues caused by browser incompatibilities, attracting developers with its well-documented and concise API.&lt;/p&gt;

&lt;p&gt;As of August 2022, 77% of the 10 million most popular websites used jQuery. On average, jQuery was used three to four times more often than any other JavaScript library, according to web analytics.&lt;/p&gt;

&lt;h2&gt;
  
  
  2007. Apple reinvents mobile web
&lt;/h2&gt;

&lt;p&gt;When the first iPhones hit the market in 2007, they didn't support Flash, which was the top technology for interactive web content at the time. Flash powered videos, animations, and browser games. Instead of actual content, users saw empty rectangles on websites opened in mobile Safari.&lt;/p&gt;

&lt;p&gt;Critics pointed out the obvious contradiction that without Flash, Apple's promotion of Safari as a "full-featured browser" was an exaggeration. Seizing the opportunity, Android added Flash support in version 2.2, presenting it as a key advantage over the iPhone.&lt;/p&gt;

&lt;p&gt;In April 2010, Steve Jobs published an open letter titled "Thoughts on Flash." He explained why Flash would never appear on Apple devices: a proprietary technology developed in the mouse-driven era was incompatible with touchscreen interfaces. Flash used too much power and wasn't compatible with hardware acceleration on mobile chips. According to Jobs, the future belonged to open-source standards: HTML5, CSS, and JavaScript.&lt;/p&gt;

&lt;p&gt;The market ruled in Apple's favor. The iPhone and iPad quickly gained market share, and web developers had to rewrite websites without Flash while also solving mobile adaptation issues. &lt;/p&gt;

&lt;p&gt;In November 2011, Adobe ceased development of Flash for mobile devices and announced the end of the desktop version in 2017. Flash officially ceased to exist on December 31, 2020.&lt;/p&gt;

&lt;p&gt;JavaScript was the biggest beneficiary of this story. Open-source standards filled the void left by Flash, with JS becoming their main driving force.&lt;/p&gt;

&lt;h2&gt;
  
  
  2008. Google Chrome
&lt;/h2&gt;

&lt;p&gt;For six years, Google CEO Eric Schmidt opposed the development of the company's own web browser. He had several reasons for doing so, but the main one was that he didn't want to drag the company into another browser war.&lt;/p&gt;

&lt;p&gt;However, the company's co-founders, Sergey Brin and Larry Page, hired several Mozilla Firefox developers and created a demo version of Chrome. Schmidt later admitted, "It was so good that it essentially forced me to change my mind."&lt;/p&gt;

&lt;p&gt;On September 2, 2008, Google released the first beta version of Chrome. Thanks to its new V8 JavaScript engine, the browser was built around performance from the start.&lt;/p&gt;

&lt;p&gt;Google's engine was revolutionary. Open source from day one, it influenced the development of other tools, most notably Node.js.&lt;/p&gt;

&lt;p&gt;The release of Google Chrome marked an important milestone in the second browser war.&lt;/p&gt;

&lt;h3&gt;
  
  
  That same year: ECMAScript 4 gets canceled
&lt;/h3&gt;

&lt;p&gt;Development of the new language version began in 2000 and continued through 2008; the creators envisioned it as a complete overhaul of JavaScript. However, it caused a real crisis within TC39.&lt;/p&gt;

&lt;p&gt;The committee split into two groups: supporters, including Adobe, Mozilla, Opera, and Google, and opponents, including Microsoft and Yahoo. The first group wanted large-scale changes, while the second considered ES4 too complex and incompatible with existing code.&lt;/p&gt;

&lt;p&gt;The situation got so bad that Microsoft effectively sabotaged the process by blocking the adoption of proposed changes.&lt;/p&gt;

&lt;p&gt;In the end, ECMAScript 4 was canceled. Instead, a more conservative ECMAScript 5 was released later, while some of the biggest changes returned with the release of ES6. Adobe developers adopted many of those ideas, which became part of ActionScript 3.&lt;/p&gt;

&lt;p&gt;Following this, TC39 introduced a staged process where each feature follows its own path from proposal to inclusion in the standard. This approach paved the way for the annual release of new ECMAScript versions in the future.&lt;/p&gt;

&lt;h2&gt;
  
  
  2009. Node.js
&lt;/h2&gt;

&lt;p&gt;The first version of an open-source, cross-platform JavaScript runtime environment outside of a browser was released in May 2009, approximately 13 years after Netscape introduced the LiveWire Pro Web server environment.&lt;/p&gt;

&lt;p&gt;Ryan Dahl, the creator of Node.js, criticized the Apache HTTP Server for its limited capacity to handle large numbers of simultaneous connections and the sequential programming approach dominant in the industry. &lt;/p&gt;

&lt;p&gt;Node.js overcame these challenges by combining Google's V8 engine, an event loop, and a low-level I/O API.&lt;/p&gt;

&lt;p&gt;Node.js was first presented on November 8, 2009, at JSConf. In early 2010, npm, a package manager for the Node.js environment that allows developers to publish and share packages, was introduced.&lt;/p&gt;

&lt;h3&gt;
  
  
  The same year: ECMAScript 5
&lt;/h3&gt;

&lt;p&gt;Released in 2009, ECMAScript 5 was the first major update to the language since ES3. It introduced many features that have become essential to JavaScript.&lt;/p&gt;

&lt;p&gt;This version added the &lt;code&gt;use strict&lt;/code&gt; directive. It helped identify errors that engines previously ignored. For example, creating a variable without first declaring it started throwing a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It also added many features for working with strings, arrays, objects, JSON, and dates.&lt;/p&gt;

&lt;p&gt;It's difficult to imagine modern code without features like &lt;code&gt;Array.forEach&lt;/code&gt;, &lt;code&gt;JSON.stringify&lt;/code&gt;, and &lt;code&gt;Date.now&lt;/code&gt;, all of which appeared in this version of the language.&lt;/p&gt;

&lt;h3&gt;
  
  
  That same year: Oracle acquires Sun Microsystems
&lt;/h3&gt;

&lt;p&gt;In April 2009, Oracle announced its acquisition of Sun Microsystems, with the deal closing in January 2010. Along with Sun, Oracle also acquired the JavaScript trademark.&lt;/p&gt;

&lt;p&gt;The acquisition of the trademark by the corporation resulted in several unusual situations. For example, the Rust for JavaScript Developers community received a letter demanding that they stop using the word "JavaScript" in their name. When renewing the trademark in 2019, Oracle submitted a screenshot of the Node.js website to the USPTO as proof of use, as if claiming ownership rights over any JavaScript projects.&lt;/p&gt;

&lt;p&gt;In 2022, this situation led Ryan Dahl, the creator of Node.js and Deno, to publicly ask Oracle to give up the trademark. In 2024, an open letter was released and signed by over 27,000 developers, including Brendan Eich, the creator of JavaScript.&lt;/p&gt;

&lt;p&gt;The Deno team filed a petition with the USPTO to cancel the trademark in November of the same year. The case is still ongoing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Read our &lt;a href="https://pvs-studio.com/en/blog/posts/java/1256/" rel="noopener noreferrer"&gt;article on the history of Java&lt;/a&gt; to learn more about how Sun Microsystems was acquired by Oracle.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2012. TypeScript
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt;, a language that added static typing to JavaScript, was released in October 2012. The project aimed to streamline the development and maintenance of large-scale JavaScript applications. Anders Hejlsberg, the creator of Turbo Pascal and C#, started developing it in 2010 at Microsoft.&lt;/p&gt;

&lt;p&gt;Over the years, TypeScript has influenced the entire ecosystem. &lt;/p&gt;

&lt;p&gt;Many TypeScript features appeared before they became part of the ECMAScript specification, including optional chaining (&lt;code&gt;?.&lt;/code&gt;), nullish coalescing (&lt;code&gt;??&lt;/code&gt;), and others. &lt;/p&gt;

&lt;p&gt;Also, strong typing, once considered unnecessary in the JavaScript world, has become the norm. Nowadays, many libraries, both large and small, ship with &lt;code&gt;.d.ts&lt;/code&gt; declaration files. &lt;/p&gt;

&lt;h2&gt;
  
  
  2013. React
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;React&lt;/a&gt; is a library for building UIs. It was first introduced at the JSConf US conference in May 2013.&lt;/p&gt;

&lt;p&gt;Development of React began at Facebook in 2010: Jordan Walke created an internal prototype called FaxJS for the news feed. By 2011, React was already in use within the company. The community response was skeptical, with people asking, "How can you mix HTML and JavaScript?"&lt;/p&gt;

&lt;p&gt;However, React changed the way developers build interfaces. The component-based model, Virtual DOM, and the one-way data flow have become the new industry standard.&lt;/p&gt;

&lt;p&gt;Vue.js, released in 2014, took direct inspiration from React's ideas. Starting with version 2, Angular also switched to a component-based architecture. These approaches have now become the standard.&lt;/p&gt;

&lt;p&gt;React Native was released in 2015, enabling developers to build mobile applications using React and paving the way for JavaScript in mobile development. &lt;/p&gt;

&lt;p&gt;The Hooks API, introduced in React in 2019, replaced class components and popularized a functional approach to writing code across the JavaScript ecosystem.&lt;/p&gt;

&lt;h3&gt;
  
  
  The same year: Electron
&lt;/h3&gt;

&lt;p&gt;The framework started as an internal GitHub tool for the Atom text editor, which entered public beta in April 2014. It combines Node.js and Chromium, enabling developers to reuse web code with minimal changes while running it in a controlled browser environment that provides native application privileges.&lt;/p&gt;

&lt;p&gt;The technology's potential was quickly recognized, prompting the renaming of Atom Shell to Electron in April 2015. Slack was among the first major adopters, transforming its web client into a full desktop application. Many other products soon followed.&lt;/p&gt;

&lt;p&gt;Electron revolutionized desktop application development by making it accessible to the web development community. In the past, building cross-platform applications required extensive knowledge of native technologies. Electron allowed developers to create them using only HTML, CSS, and JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  2014. Webpack
&lt;/h2&gt;

&lt;p&gt;In 2012, &lt;a href="https://webpack.js.org/" rel="noopener noreferrer"&gt;Webpack&lt;/a&gt; was released as an open-source JavaScript module bundler. It takes dependencies as input and builds a dependency graph, enabling developers to take a modular approach to web application development. This allowed them to import almost anything to client-side code and, over time, became the foundation of the build process for React, Angular, Vue, and many other frameworks. &lt;/p&gt;

&lt;p&gt;The foundation laid by Webpack later gave rise to modern build tools, including Rollup, Vite, and ESBuild.&lt;/p&gt;

&lt;h2&gt;
  
  
  2015. ECMAScript 6
&lt;/h2&gt;

&lt;p&gt;The new language standard, released in 2015, was the largest update in JavaScript's history. In many ways, JavaScript split into two eras: before and after ES6.&lt;/p&gt;

&lt;p&gt;The new version introduced &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; as replacements for &lt;code&gt;var&lt;/code&gt;, bringing block scope and eliminating many classic hoisting-related bugs. It also brought arrow functions, which made code more concise and solved common issues with the &lt;code&gt;this&lt;/code&gt; context. ES6 also added classes, modules, destructuring, and the &lt;code&gt;spread&lt;/code&gt; and &lt;code&gt;rest&lt;/code&gt; operators.&lt;/p&gt;

&lt;p&gt;ES6 introduced &lt;code&gt;Promise&lt;/code&gt; as JavaScript's native solution to the callback hell problem. ES8 later built on that foundation with &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Many practices that are now considered standard in modern JavaScript originated with ES6.&lt;/p&gt;

&lt;h3&gt;
  
  
  That same year: the end of the second browser war
&lt;/h3&gt;

&lt;p&gt;The second browser war lasted much longer than the first one, and it was on a completely different scale. While the first one was a showdown between Netscape and Microsoft, the second one was a battle of everyone against everyone else.&lt;/p&gt;

&lt;p&gt;By 2015, Internet Explorer still held around 80% of the market. Microsoft had grown complacent, causing the browser to stop evolving. Users noticed the stagnation and started switching to other options.&lt;/p&gt;

&lt;p&gt;Mozilla Firefox and Opera were the first to capitalize on the situation. Firefox launched in 2004 and immediately offered features Internet Explorer lacked: tabbed browsing, extensions, and better performance. The "ghost" of Netscape had returned for a rematch.&lt;/p&gt;

&lt;p&gt;Microsoft responded too late. After five years of stagnation, Internet Explorer 7 was released in October 2006. Microsoft even allowed users with pirated Windows copies to install the new version, but that didn't stop them from leaving. Internet Explorer continued to lose market share quickly.&lt;/p&gt;

&lt;p&gt;In 2008, Google Chrome entered the race and permanently shifted the balance of power. Its fast V8 engine, minimalist interface, and aggressive promotion through Google's services all contributed to its success. By the early 2010s, Chrome had overtaken every competitor.&lt;/p&gt;

&lt;p&gt;IE lingered on for a few more years until Microsoft released Edge in 2015. Internet Explorer was officially discontinued in 2022.&lt;/p&gt;

&lt;p&gt;Chrome has become the web's most popular browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  2016. Visual Studio Code
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;Visual Studio Code&lt;/a&gt;, a code editor created by Microsoft, was first introduced on April 29, 2015, at the Build conference.&lt;/p&gt;

&lt;p&gt;Microsoft developed it during a period of major change in the web development field, when many developers were switching to lightweight editors like Sublime Text and Atom. At the time, Microsoft was still closely associated with the heavyweight Visual Studio IDE. VS Code was built on Electron and has been open source and cross-platform since day one.&lt;/p&gt;

&lt;p&gt;The community initially reacted with caution. Many doubted Microsoft's ability to build a tool that would appeal to everyone.&lt;/p&gt;

&lt;p&gt;However, VS Code redefined what a code editor could be. Its powerful Extension API and built-in debugger combined with a lightweight user experience proved to be exactly what the market had been missing.&lt;/p&gt;

&lt;p&gt;Language Server Protocol was one of the important byproducts. The VS Code team proposed the standard for language integration, which quickly gained adoption across the industry in editors such as Vim, Emacs, and Eclipse. Stack Overflow's developer survey ranked VS Code as the most popular development tool in 2018, and it has held that position ever since.&lt;/p&gt;

&lt;p&gt;VS Code was especially important for the JavaScript ecosystem because it shipped with built-in support for JavaScript and TypeScript from the beginning.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;By the way, we're developing a Visual Studio Code extension that makes it possible to work with PVS-Studio directly from within the editor. You can learn more about it in the &lt;a href="https://pvs-studio.com/en/docs/manual/6646/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2017. WebAssembly
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://webassembly.org/" rel="noopener noreferrer"&gt;WebAssembly&lt;/a&gt; is a binary instruction format for a stack-based virtual machine that became a W3C web standard in 2019. It emerged after years of struggling against JavaScript's performance limits.&lt;/p&gt;

&lt;p&gt;Back in 2013, Mozilla introduced asm.js, a strict subset of JavaScript with explicit type annotations that browsers could compile ahead of time. &lt;a href="https://youtu.be/nB9Pm9Mr7xo?si=nrK9HNaQSPjCrDCC" rel="noopener noreferrer"&gt;A live demonstration of Unreal Engine 4 running directly in Firefox&lt;/a&gt; proved the concept was viable and made a strong impression. However, asm.js was cumbersome, and only one browser supported it.&lt;/p&gt;

&lt;p&gt;In 2015, Mozilla, Google, Microsoft, and Apple came together to develop a solution. They all faced the same challenge: JavaScript engines had reached an architectural limit that compiler optimizations alone couldn't overcome.&lt;/p&gt;

&lt;p&gt;The MVP was released in early 2017 and appeared in all four major browsers almost simultaneously, which was a level of coordinated rollout the web had never seen before. For compute-intensive workloads, WebAssembly achieved 60–80% of native performance.&lt;/p&gt;

&lt;p&gt;Its arrival opened up new possibilities that were previously impossible in web development. For example, Figma rebuilt its rendering engine using C++ and converted it to Wasm, enabling its browser-based editor to perform like a native application. Adobe Photoshop also made its way to the browser, while Unity and Unreal Engine got official Wasm export support.&lt;/p&gt;

&lt;p&gt;Although WebAssembly proved invaluable for workloads where JavaScript struggled, it never fully replaced JavaScript—that was never the goal. Instead, JavaScript became the glue that connects Wasm modules to the DOM and browser APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  2018. Deno
&lt;/h2&gt;

&lt;p&gt;In 2018, Ryan Dahl, the creator of Node.js, gave a talk at JSConf EU in Berlin titled "&lt;a href="https://youtu.be/M3BM9TB-8yA?si=usVIPTYqLuF0APHN" rel="noopener noreferrer"&gt;10 Things I Regret About Node.js&lt;/a&gt;." He outlined what he considered to be the biggest design flaws of Node.js, including its module system, unrestricted access to the network and file system, and so on.&lt;/p&gt;

&lt;p&gt;He concluded the talk by announcing &lt;a href="https://deno.com/" rel="noopener noreferrer"&gt;Deno&lt;/a&gt; (an anagram of Node—Ed.), a new JavaScript runtime that addresses those issues.&lt;/p&gt;

&lt;p&gt;For example, instead of granting unrestricted access to the file system and network, Deno introduced permission flags such as &lt;code&gt;--allow-write&lt;/code&gt; and &lt;code&gt;--allow-net&lt;/code&gt;. Rather than relying on an ever-growing &lt;code&gt;node_modules&lt;/code&gt; directory and &lt;code&gt;package.json&lt;/code&gt;, it adopted URL-based imports similar to the way external JavaScript files are included in an HTML page.&lt;/p&gt;

&lt;p&gt;The first stable version of Deno was released in 2020. In 2021, the project joined TC39. Although Deno differs from Node.js in many ways, its developers remain committed to the JavaScript standard.&lt;/p&gt;

&lt;p&gt;Deno 2.0, released in 2024, maintained the original's simplicity while adding compatibility with Node.js and npm.&lt;/p&gt;

&lt;h2&gt;
  
  
  2020. Vite
&lt;/h2&gt;

&lt;p&gt;In 2020, Evan You, the creator of Vue.js, made a surprisingly obvious point that had been overlooked by everyone: modern browsers already support native ES modules. This meant developers no longer had to bundle code during development. The browser could resolve the required modules on its own.&lt;/p&gt;

&lt;p&gt;This idea led to the creation of &lt;a href="https://vite.dev/" rel="noopener noreferrer"&gt;Vite&lt;/a&gt; (French for "fast" — Ed.). Unlike traditional tools, Vite's development server didn't waste time bundling the entire project at startup. Instead, it sent source files directly to the browser like ES modules do, while using esbuild, a Go-based bundler, to pre-bundle dependencies from &lt;code&gt;node_modules&lt;/code&gt;. As a result, the time required to initiate these large projects was reduced to mere seconds. When a single file changed, the browser no longer had to reload the entire project; it only needed to reload the modified module.&lt;/p&gt;

&lt;p&gt;The first release focused exclusively on Vue, but in February 2021 Vite 2.0 arrived as a completely redesigned, framework-agnostic tool. &lt;/p&gt;

&lt;p&gt;Vite played a major role in the decline of Create React App, the longstanding standard for building React projects.&lt;/p&gt;

&lt;p&gt;The same ecosystem also gave rise to Vitest, a test runner that uses the same configuration as the build system, allowing developers to avoid duplicating settings for Jest. Vite redefined expectations for development speed in the JavaScript ecosystem, pushing competing tools to match its performance.&lt;/p&gt;

&lt;p&gt;By the mid-2020s, Vite had become one of the world's most downloaded npm packages. In March 2026, Vite 8 was released with Rolldown as its sole bundler, replacing the previous esbuild + Rollup combination. Rolldown, written in Rust, enabled application builds that were dozens of times faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  2022. React on the server
&lt;/h2&gt;

&lt;p&gt;In December 2020, the React team published an RFC titled "&lt;a href="https://legacy.reactjs.org/blog/2020/12/21/data-fetching-with-react-server-components.html" rel="noopener noreferrer"&gt;Zero-Bundle-Size React Server Components&lt;/a&gt;." The idea was simple: what if some components never reached the browser at all? They would run on the server, issue the final output, and then disappear. No client-side re-rendering. No JavaScript added to the bundle.&lt;/p&gt;

&lt;p&gt;The proposal addressed a very real problem: large dependencies ended up in users' browsers simply because components imported them. Also, it required a lot of requests: we first send JavaScript, which then fetches the necessary data.&lt;/p&gt;

&lt;p&gt;For two years, React Server Components remained an experimental feature. They required a dedicated streaming server and worked only within a supporting framework rather than in React itself. Wider adoption began in October 2022 when the beta version of the App Router was introduced with Next.js 13. In May 2023, version 13.4 was declared stable. Server Actions came alongside it. These are functions marked with the &lt;code&gt;use server&lt;/code&gt; directive that are called from the client but executed on the server. &lt;/p&gt;

&lt;p&gt;From that point on, a single React project contained two distinct worlds, each with a different execution model. These worlds were separated by the &lt;code&gt;use client&lt;/code&gt; and &lt;code&gt;use server&lt;/code&gt; directives.&lt;/p&gt;

&lt;p&gt;The impact extended far beyond React. Solid.js, Qwik, and Svelte all embraced the idea of sending less JavaScript to the client. Qwik built its entire resumability model around this concept. Instead of rerunning all the necessary JavaScript to rebuild a page, the browser resumes execution from where the server left off.&lt;/p&gt;

&lt;p&gt;React 19, released in December 2024, officially added Server Actions to the public API. However, most developers still have to choose between the traditional approach and this newer model.&lt;/p&gt;

&lt;h2&gt;
  
  
  2026. PVS-Studio analyzer for JavaScript and TypeScript
&lt;/h2&gt;

&lt;p&gt;On April 6, 2026, &lt;a href="https://pvs-studio.com/en/" rel="noopener noreferrer"&gt;PVS-Studio&lt;/a&gt; launched an &lt;a href="https://pvs-studio.com/en/pvs-studio-eap/" rel="noopener noreferrer"&gt;Early Access Program (EAP)&lt;/a&gt; for its new analyzers. For many years, PVS-Studio has helped developers catch bugs and vulnerabilities in C, C++, C#, and Java projects. Now it's JavaScript's and TypeScript's turn.&lt;/p&gt;

&lt;p&gt;The initial release included 20 diagnostic rules, typo and logic error detection, a CLI version, and a plugin for WebStorm. TypeScript support and a VS Code extension followed in May.&lt;/p&gt;

&lt;p&gt;The EAP also introduced a new analyzer for Go.&lt;/p&gt;

&lt;p&gt;The new analyzers are scheduled for full release in August 2026, alongside PVS-Studio 8.00.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This is where our journey through the history of JavaScript comes to an end. It has been an eventful one, as JavaScript's story is deeply intertwined with the evolution of the web itself.&lt;/p&gt;

&lt;p&gt;This article reflects the author's perspective on the history of JavaScript and may not include every milestone you consider significant. If you think something important is missing, please add it in the comments! :)&lt;/p&gt;

&lt;p&gt;If you're interested in the history of other programming languages, check out our series of articles on the topic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1189/" rel="noopener noreferrer"&gt;History of C and C++. Part one: emergence and standardization of C language.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1224/" rel="noopener noreferrer"&gt;History of C and C++. Part two: standardization of C and C++, Qt, Clang, and Unreal Engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/csharp/1248/" rel="noopener noreferrer"&gt;History of C#: versions, .NET, Unity, Blazor, and MAUI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/java/1256/" rel="noopener noreferrer"&gt;History of Java: evolution, legal battles with Microsoft, Mars exploration, Spring, Gradle and Maven, IDEA and Eclipse&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See ya soon!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
