<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Unicorn Developer</title>
    <description>The latest articles on Forem by Unicorn Developer (@unicorn_developer).</description>
    <link>https://forem.com/unicorn_developer</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F864708%2F4aaf8ad7-3996-44e2-b221-c92d66699c05.png</url>
      <title>Forem: Unicorn Developer</title>
      <link>https://forem.com/unicorn_developer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/unicorn_developer"/>
    <language>en</language>
    <item>
      <title>How to attract developers to your website with an IT events calendar</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Tue, 18 Apr 2023 15:01:11 +0000</pubDate>
      <link>https://forem.com/unicorn_developer/how-to-attract-developers-to-your-website-with-an-it-events-calendar-397j</link>
      <guid>https://forem.com/unicorn_developer/how-to-attract-developers-to-your-website-with-an-it-events-calendar-397j</guid>
      <description>&lt;p&gt;How to advertise in an environment that doesn't welcome advertising.&lt;/p&gt;

&lt;p&gt;Hi, this is the PVS-Studio team. We've been developing the code analyzer for C++, C#, and Java for 15 years, and now we have decided to launch an &lt;a href="https://pvs-studio.com/en/blog/events/"&gt;event calendar&lt;/a&gt; for IT professionals. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2E8HccYK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tec61knachosjwz7vnpx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2E8HccYK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tec61knachosjwz7vnpx.png" alt="Image description" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Context
&lt;/h2&gt;

&lt;p&gt;Before we begin, a bit of context: our company sells the static code analyzer. This is a complex specific product that developers use to automatically search for errors and potential vulnerabilities. Business saves money because it is several times cheaper to fix an error at the stage of writing code than at the code testing stage (and even more so after the release). &lt;/p&gt;

&lt;p&gt;PVS-Studio is used not only by thoroughbred IT companies: any company where one of the departments writes code subscribe to the license — gamedev, medical institutions, financial corporations. &lt;br&gt;
One of our eternal goals is to increase the number of programmers who visit our website and know the usefulness of the analyzer. &lt;/p&gt;

&lt;h2&gt;
  
  
  Idea
&lt;/h2&gt;

&lt;p&gt;Looking for new points of growth, we thought about how to attract more potential customers to the site, build up communication channels and distribute more product trials. A solution came from a problem that we ourselves encountered: among the bunch of websites, it is difficult to find a conference you want to speak at.&lt;/p&gt;

&lt;p&gt;There is no common website: you search for C++ events in one place, and for Java in another. We had to keep a list of sources that need to be checked regularly, otherwise we risked not being able to apply for a major conference. And if you're just a participant, you can miss the ticket discounts end up in a situation where tickets are sold out.&lt;/p&gt;

&lt;p&gt;We are sure that other developers face similar problems — that's why we have launched a common event calendar for developers. For users, this saves time and solves an urgent problem, for us it is an opportunity to highlight our main product.&lt;/p&gt;

&lt;h2&gt;
  
  
  Service
&lt;/h2&gt;

&lt;p&gt;The first version of the service was released in March 2023. The website now has about four hundred conferences. You can filter conferences by country, subject, and format of the event. For example, the calendar can show which C++ conferences will be held in Germany this year in a couple of clicks.&lt;/p&gt;

&lt;p&gt;Besides conferences for programmers, we add events to the website for IT managers, security specialists, game industry employees, and workers from other related fields. This is how we reach those who are not directly involved in coding, but may decide to buy PVS-Studio.&lt;/p&gt;

&lt;p&gt;In addition, we try to cover small meetups: we believe that small communities are no less important than large international conferences.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q8H9bTw1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/me1vzf8n1e9nj4b1lnfm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q8H9bTw1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/me1vzf8n1e9nj4b1lnfm.png" alt="Image description" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Team
&lt;/h2&gt;

&lt;p&gt;The main performer in the service is the event marketer. He spreads the news about the calendar on thematic sites, writes promo texts (like this one), orders advertising and arranges for advertising integrations. The web department draws unicorns and makes sure that nothing breaks on the website. One of the founders of the company follows the processes and vision.&lt;/p&gt;

&lt;p&gt;The team that works on the calendar is united by one idea: to attract programmers to the website with the help of marketing, and then retain them through the benefits the service creates.&lt;/p&gt;

&lt;h2&gt;
  
  
  Promotion
&lt;/h2&gt;

&lt;p&gt;It is easier and cheaper to advertise an aggregator than code analysis software. But the audience for these two products is very close. &lt;/p&gt;

&lt;p&gt;The promotion of the service is in its early stages. In addition to this text, we will post articles on thematic websites for developers, make text posts in social media, buy advertising in telegram channels and search engines. We also actively communicate with IT chat members — they are eager to use interesting innovations. But you have to be careful in these conversations: it's easy to get banned.&lt;/p&gt;

&lt;p&gt;We don't yet know how the programming community will welcome the service. Experienced developers often react extremely negatively to any advertising, so we will try to do delicate marketing. We hope that a free product with more or less understandable benefits will not be pelted with tomatoes. &lt;/p&gt;

&lt;h2&gt;
  
  
  Monetization
&lt;/h2&gt;

&lt;p&gt;The service does not sell ads. We do not receive money for the publication of conferences. We add to the general list only those events that we consider important. As mentioned above, the purpose of the calendar is to put our brand in the infofield, not to make money from contextual advertising. &lt;/p&gt;

&lt;p&gt;PVS-Studio has a B2B product and a fairly long sales cycle — several months. Therefore, here we primarily focus on the benefits that our calendar will bring to the user. Maybe one day this user will want to buy our analyzer, ask for a trial or tell their colleague about the program, the one who just codes in C++. &lt;/p&gt;

&lt;p&gt;We will see which part of the users who use the calendar will try out our code analyzer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Welcome: &lt;a href="https://pvs-studio.com/en/blog/events/"&gt;event calendar for developers&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>marketing</category>
      <category>webdev</category>
      <category>programming</category>
      <category>management</category>
    </item>
    <item>
      <title>Find your conference: calendar for developers</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 07 Apr 2023 07:44:00 +0000</pubDate>
      <link>https://forem.com/unicorn_developer/find-your-conference-calendar-for-developers-3hc9</link>
      <guid>https://forem.com/unicorn_developer/find-your-conference-calendar-for-developers-3hc9</guid>
      <description>&lt;p&gt;Finding a conference to your liking is a challenge. Many people are probably familiar with the situation: rummaging through a bunch of websites, stacking on the search engines and thematic communities. You have to bookmark a bunch of sources where announcements of events are published randomly. One website only posts JS conferences, while another only posts about Android development meetups. It's inconvenient. Therefore, the PVS-Studio team launched an event aggregator for developers. You can find it on our website. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_XpDYxuq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgsl5gr876icsgnan839.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_XpDYxuq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgsl5gr876icsgnan839.png" alt="Image description" width="880" height="515"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now all IT events are collected in one place. You no longer have to tediously search for a conference where you want to make a presentation or just be a participant. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/blog/events/"&gt;Welcome: Events for developers!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The events are sorted by subject, country, and the format. The aggregator will show you a catalog of all C++ conferences or Java meetings in Germany in a couple of clicks. You don't have to scroll through a long overall list. &lt;/p&gt;

&lt;p&gt;The card of each event contains information about the date, the venue, the format, and the event organizer. There are also tags that indicate the topic of the conference. All conferences are divided by the programming language and industry. Events on information security, mobile development, gamedev, and other topics have their own categories.&lt;/p&gt;

&lt;p&gt;The aggregator has now collected around 300 events. Each conference is checked before getting into the list. Only the most useful events reach the user. Once a week, the team searches for the newest events and adds them to the catalog. You can suggest your conference through the feedback form.&lt;/p&gt;

&lt;p&gt;In addition to large conferences known around the world, the aggregator can show small meetups. This is one of the key features of our service — we want to promote small communities of developers from different countries who might find it difficult to find an audience without third-party support. When you attend them, it's easy to meet really passionate professionals: these guys are boosting the IT industry with their enthusiasm. &lt;/p&gt;

&lt;p&gt;The service operates without advertising: PVS-Studio doesn't get paid for publishing conferences and only includes events that are considered important. &lt;/p&gt;

&lt;p&gt;The aggregator contains events for many countries, but the focus is on English-speaking conferences. We believe that developers from all over the world will use this service.&lt;/p&gt;

&lt;p&gt;The service is brand new — the team shared it to the public in March 2023. It would be cool if you could share your feedback :)&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>conferences</category>
    </item>
    <item>
      <title>Top 10 bugs found in C++ projects in 2022</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 27 Jan 2023 11:59:24 +0000</pubDate>
      <link>https://forem.com/unicorn_developer/top-10-bugs-found-in-c-projects-in-2022-1gl</link>
      <guid>https://forem.com/unicorn_developer/top-10-bugs-found-in-c-projects-in-2022-1gl</guid>
      <description>&lt;p&gt;New Year is coming! It means, according to tradition, it's time to recall 10 of the most interesting warnings that PVS-Studio found during 2022.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F4wD9-s9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vuvqfze9ergkt2g1amv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F4wD9-s9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vuvqfze9ergkt2g1amv.png" alt="Image description" width="780" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is worth noting that this year there were not as many articles about project checks as in past years. Articles on our &lt;a href="https://pvs-studio.com/en/blog/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;blog&lt;/a&gt; have become more varied, and &lt;a href="https://pvs-studio.com/en/blog/posts/?tag=guestarticle"&gt;translations&lt;/a&gt; of articles by guest authors have also appeared. However, I, as one of our blog's readers (and as the &lt;a href="https://pvs-studio.com/en/blog/posts/?author=vladislav-stolyarov"&gt;author of some articles&lt;/a&gt; from it), still made the top list of bugs found by the PVS-Studio analyzer that are described in our articles on project checks. Let me point out that this top is rather subjective — it has many bugs from articles written by me :). &lt;/p&gt;

&lt;p&gt;If you, dear readers, have your own vision of how this top should look, please, share it in the comments. So, let's get started, enjoy reading!&lt;/p&gt;

&lt;h2&gt;
  
  
  Tenth place: A classic typo
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v501/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V501&lt;/a&gt; [CWE-570] There are identical sub-expressions '(SrcTy.isPointer() &amp;amp;&amp;amp; DstTy.isScalar())' to the left and to the right of the '||' operator. CallLowering.cpp 1198&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;bool&lt;/span&gt; &lt;span class="nf"&gt;isCopyCompatibleType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LLT&lt;/span&gt; &lt;span class="n"&gt;SrcTy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LLT&lt;/span&gt; &lt;span class="n"&gt;DstTy&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;SrcTy&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;DstTy&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SrcTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getSizeInBits&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;DstTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getSizeInBits&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="n"&gt;SrcTy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SrcTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getScalarType&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;DstTy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DstTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getScalarType&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;SrcTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isPointer&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;DstTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isScalar&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;DstTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isScalar&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;SrcTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isPointer&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;We see a classic typo in 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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SrcTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isPointer&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;DstTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isScalar&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;DstTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isScalar&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;SrcTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isPointer&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the error:&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;SrcTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isPointer&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;DstTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isScalar&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;DstTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isScalar&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;SrcTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isPointer&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A developer simultaneously changed &lt;em&gt;Src&lt;/em&gt; to &lt;em&gt;Dst&lt;/em&gt; and &lt;em&gt;Pointer&lt;/em&gt; to &lt;em&gt;Scalar&lt;/em&gt; in the second line. As a result, it turns out that the same thing is checked twice! The code above is equivalent 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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SrcTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isPointer&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;DstTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isScalar&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;SrcTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isPointer&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;DstTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isScalar&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The correct 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;SrcTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isPointer&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;DstTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isScalar&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;SrcTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isScalar&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;DstTy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isPointer&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 entered the top of the article: "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1003/"&gt;Examples of errors that PVS-Studio found in LLVM 15.0&lt;/a&gt;."&lt;/p&gt;

&lt;h2&gt;
  
  
  Ninth place: Where would we be without array overrun
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v557/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V557&lt;/a&gt; Array overrun is possible. The 'j' index is pointing beyond array bound. OgreAnimationTrack.cpp 219&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="n"&gt;AnimationTrack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;_buildKeyFrameIndexMap&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="n"&gt;Real&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;keyFrameTimes&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;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="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;=&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;while&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;keyFrameTimes&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="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;mKeyFrameIndexMap&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="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ushort&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="k"&gt;while&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;mKeyFrames&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;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;mKeyFrames&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;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getTime&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;keyFrameTimes&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="c1"&gt;// &amp;lt;=&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;j&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;em&gt;j&lt;/em&gt; index that gives us access to the elements of the &lt;em&gt;keyFrameTimes&lt;/em&gt; container is incremented to a value equal to the container size. We can fix 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="k"&gt;while&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;keyFrameTimes&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="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;I took this error from the article: "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0927/"&gt;Checking the Ogre3D framework with the PVS-Studio static analyzer.&lt;/a&gt;"&lt;/p&gt;

&lt;h2&gt;
  
  
  Eighth place: A clear example of a null pointer dereference
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v522/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V522&lt;/a&gt; [CERT-EXP34-C] Dereferencing of the null pointer 'document' might take place. TextBlockCursor.cpp 332&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;auto&lt;/span&gt; &lt;span class="n"&gt;BlockCursor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;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;list&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TextBlock&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;::&lt;/span&gt;&lt;span class="n"&gt;iterator&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;document&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;nullptr&lt;/span&gt; 
            &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;blocks&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="n"&gt;document&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;blocks&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I remember that the first time I saw this piece of code, I couldn't help but facepalm. Let's figure out what's going on here. We see an explicit check of the &lt;em&gt;document&lt;/em&gt; pointer for &lt;em&gt;nullptr&lt;/em&gt; and its dereference in both branches of the ternary operator.&lt;/p&gt;

&lt;p&gt;The code is correct only if the developer aimed to crash the program.&lt;/p&gt;

&lt;p&gt;I took this error from the article: "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0914/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;MuditaOS: Will your alarm clock go off? Part I&lt;/a&gt;".&lt;/p&gt;

&lt;h2&gt;
  
  
  Seventh place: Learning to count to seven
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v557/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V557&lt;/a&gt; [CWE-787] Array overrun is possible. The 'dynamicStateCount ++' index is pointing beyond array bound. VltGraphics.cpp 157&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;VkPipeline&lt;/span&gt; &lt;span class="n"&gt;VltGraphicsPipeline&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;createPipeline&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="k"&gt;const&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&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;array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;VkDynamicState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dynamicStates&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;dynamicStateCount&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;dynamicStates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dynamicStateCount&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;VK_DYNAMIC_STATE_VIEWPORT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;dynamicStates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dynamicStateCount&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;VK_DYNAMIC_STATE_SCISSOR&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;useDynamicDepthBias&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;dynamicStates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dynamicStateCount&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;VK_DYNAMIC_STATE_DEPTH_BIAS&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;useDynamicDepthBounds&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;dynamicStates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dynamicStateCount&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;VK_DYNAMIC_STATE_DEPTH_BOUNDS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;dynamicStates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dynamicStateCount&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;VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;useDynamicBlendConstants&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;dynamicStates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dynamicStateCount&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;VK_DYNAMIC_STATE_BLEND_CONSTANTS&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;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;useDynamicStencilRef&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;dynamicStates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dynamicStateCount&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;VK_DYNAMIC_STATE_STENCIL_REFERENCE&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 warns that an overflow of the &lt;em&gt;dynamicStates&lt;/em&gt; array may occur. There are 4 checks in this code fragment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if (state.useDynamicDepthBias())&lt;/li&gt;
&lt;li&gt;if (state.useDynamicDepthBounds())&lt;/li&gt;
&lt;li&gt;if (state.useDynamicBlendConstants())&lt;/li&gt;
&lt;li&gt;if (state.useDynamicStencilRef())&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of these checks is a check of one of the independent flags. For example, the check of &lt;em&gt;if (state.useDynamicDepthBias())&lt;/em&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;bool&lt;/span&gt; &lt;span class="nf"&gt;useDynamicDepthBias&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;rs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depthBiasEnable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;VkBool32&lt;/span&gt; &lt;span class="nf"&gt;depthBiasEnable&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;VkBool32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_depthBiasEnable&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 turns out that all these 4 checks can be true at the same time. Then 7 lines of the* 'dynamicStates[dynamicStateCount++] =....'* kind will be executed. On the seventh such line, there will be a call to &lt;em&gt;dynamicStates[6]&lt;/em&gt;. It's an array index out of bounds.&lt;/p&gt;

&lt;p&gt;This bug is from the top of the article: &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0955/"&gt;"Checking the GPCS4 emulator: will we ever be able to play "Bloodborne" on PC?&lt;/a&gt;."&lt;/p&gt;

&lt;h2&gt;
  
  
  Sixth place: Throwing an exception from the noexcept function
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v509/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V509&lt;/a&gt; [CERT-DCL57-CPP] The noexcept function '=' calls function 'setName' which can potentially throw an exception. Consider wrapping it in a try..catch block. Device.cpp 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;struct&lt;/span&gt; &lt;span class="nc"&gt;Device&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;constexpr&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;NameBufferSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;240&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="n"&gt;setName&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;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;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="n"&gt;NameBufferSize&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="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;runtime_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Requested name is bigger than buffer &lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="s"&gt;                                  size"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;strcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;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;data&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;c_str&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="n"&gt;Devicei&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Devicei&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Devicei&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;noexcept&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;setName&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;name&lt;/span&gt;&lt;span class="p"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the analyzer detected that a function, marked as &lt;em&gt;noexcept&lt;/em&gt;, calls a function that throws an exception. If an exception arises from the nothrow function's body, the nothrow function calls &lt;em&gt;std::terminate&lt;/em&gt;, and the program crashes.&lt;/p&gt;

&lt;p&gt;It could make sense to wrap the &lt;em&gt;setName&lt;/em&gt; function in the &lt;em&gt;&lt;a href="https://en.cppreference.com/w/cpp/language/function-try-block"&gt;function-try-block&lt;/a&gt;&lt;/em&gt; and process the exceptional situation there — or one could use something else instead of generating the exception.&lt;/p&gt;

&lt;p&gt;I took this error from the article: "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0914/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;MuditaOS: Will your alarm clock go off? Part I&lt;/a&gt;".&lt;/p&gt;

&lt;h2&gt;
  
  
  Fifth place: Incorrect work with dynamic memory
&lt;/h2&gt;

&lt;p&gt;PVS-Studio issued two warnings at once for the code snippet presented below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v611/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V611&lt;/a&gt; [CERT-MEM51-CPP] The memory was allocated using 'new T[]' operator but was released using the 'delete' operator. Consider inspecting this code. It's probably better to use 'delete [] heightfieldData;'. PhysicsServerCommandProcessor.cpp 4741&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v773/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V773&lt;/a&gt; [CERT-MEM31-C, CERT-MEM51-CPP] The function was exited without releasing the 'worldImporter' pointer. A memory leak is possible. PhysicsServerCommandProcessor.cpp 4742
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;PhysicsServerCommandProcessor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;processCreateCollisionShapeCommand&lt;/span&gt;&lt;span class="p"&gt;(....)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;btMultiBodyWorldImporter&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;worldImporter&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;btMultiBodyWorldImporter&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;unsigned&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;heightfieldData&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;heightfieldData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;btScalar&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="n"&gt;heightfieldData&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's start with the &lt;a href="https://pvs-studio.com/en/docs/warnings/v773/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V773&lt;/a&gt; warning. The memory for the &lt;em&gt;worldImporter *pointer was allocated using the *new&lt;/em&gt; operator and was not released upon exiting the function.&lt;/p&gt;

&lt;p&gt;Let's move on to the &lt;a href="https://pvs-studio.com/en/docs/warnings/v611/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V611&lt;/a&gt;warning and the &lt;em&gt;heightfieldData&lt;/em&gt; buffer. Here, the developer cleared the dynamically allocated memory, but did so in the wrong way. Instead of calling &lt;em&gt;delete[]&lt;/em&gt; to release previously allocated memory with the &lt;em&gt;new[]&lt;/em&gt; operator, they called a simple &lt;em&gt;delete&lt;/em&gt;. According to the standard, such code will lead to undefined behavior — here is a &lt;a href="https://timsong-cpp.github.io/cppwp/n4861/expr.delete#2"&gt;link&lt;/a&gt; to the corresponding item.&lt;/p&gt;

&lt;p&gt;By the way, you can read about why arrays need to be deleted by delete[], and about how it all works in general in &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0973/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;article&lt;/a&gt; written by my colleague.&lt;/p&gt;

&lt;p&gt;Here is the fixed 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="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;PhysicsServerCommandProcessor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;processCreateCollisionShapeCommand&lt;/span&gt;&lt;span class="p"&gt;(....)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;btMultiBodyWorldImporter&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;worldImporter&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;btMultiBodyWorldImporter&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;unsigned&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;heightfieldData&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;heightfieldData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;btScalar&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;

  &lt;span class="k"&gt;delete&lt;/span&gt;   &lt;span class="n"&gt;worldImporter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;heightfieldData&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, it's possible to avoid problems with manual memory cleanup by writing more modern code. For example, automatically free up memory by using &lt;em&gt;&lt;a href="https://en.cppreference.com/w/cpp/memory/unique_ptr"&gt;std::unique_ptr&lt;/a&gt;&lt;/em&gt;. The code is shorter and more reliable. It will protect against unreleased memory errors in case of early exit from 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="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;PhysicsServerCommandProcessor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;processCreateCollisionShapeCommand&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;worldImporter&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;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;btMultiBodyWorldImporter&lt;/span&gt;&lt;span class="o"&gt;&amp;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;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;heightfieldData&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="n"&gt;heightfieldData&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;make_unique_for_overwrite&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[]&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;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;btScalar&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can find this error in the article: "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0957/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;In the world of anthropomorphic animals: PVS-Studio checked Overgrowth&lt;/a&gt;".&lt;/p&gt;

&lt;h2&gt;
  
  
  Fourth place: The power of Symbolic execution
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v560/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V560&lt;/a&gt; [CWE-570] A part of conditional expression is always false: DefaultCC == ToCC. SemaType.cpp 7856&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="n"&gt;Sema&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;adjustMemberFunctionCC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QualType&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;T&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;IsStatic&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;IsCtorOrDtor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                  &lt;span class="n"&gt;SourceLocation&lt;/span&gt; &lt;span class="n"&gt;Loc&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;CallingConv&lt;/span&gt; &lt;span class="n"&gt;CurCC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FT&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getCallConv&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;CallingConv&lt;/span&gt; &lt;span class="n"&gt;ToCC&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;getDefaultCallingConvention&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IsVariadic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;IsStatic&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;CurCC&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;ToCC&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="p"&gt;....&lt;/span&gt;
  &lt;span class="n"&gt;CallingConv&lt;/span&gt; &lt;span class="n"&gt;DefaultCC&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;getDefaultCallingConvention&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IsVariadic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IsStatic&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;CurCC&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;DefaultCC&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;DefaultCC&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;ToCC&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="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 figure out why the analyzer decided that the right part of the condition is always false. For convenience, let's remove all superfluous details and replace the names:&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="p"&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;B&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;C&lt;/span&gt; &lt;span class="o"&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;A&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;B&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 look at how this code works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;we have 3 variables A, B, C and values are unknown to us;&lt;/li&gt;
&lt;li&gt;but we know that if A == B, there is an exit from the function;&lt;/li&gt;
&lt;li&gt;therefore, if the function continues to execute, then A != B;&lt;/li&gt;
&lt;li&gt;if A != C, then, due to the &lt;a href="https://en.wikipedia.org/wiki/Short-circuit_evaluation"&gt;short-circuit evaluation&lt;/a&gt;, the right subexpression is not evaluated;&lt;/li&gt;
&lt;li&gt;if the right subexpression "C == B" is evaluated, then A == C;&lt;/li&gt;
&lt;li&gt;if A != B and A == C, then C cannot be equal to B.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This error entered the top of the article: "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1003/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;Examples of errors that PVS-Studio found in LLVM 15.0&lt;/a&gt;."&lt;/p&gt;

&lt;h2&gt;
  
  
  Third place: hmm, how we love std::optional
&lt;/h2&gt;

&lt;p&gt;A couple of analyzer warnings: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v571/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V571&lt;/a&gt; Recurring check. The 'if (activeInput)' condition was already verified in line 249. ServiceAudio.cpp 250&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v547/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V547&lt;/a&gt; Expression 'activeInput' is always true. ServiceAudio.cpp 250
&lt;/li&gt;
&lt;/ul&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;optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;AudioMux&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Input&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;AudioMux&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;GetActiveInput&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;Audio&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;handleSetVolume&lt;/span&gt;&lt;span class="p"&gt;(....)&lt;/span&gt; &lt;span class="o"&gt;-&amp;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;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;AudioResponseMessage&lt;/span&gt;&lt;span class="o"&gt;&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="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;activeInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;audioMux&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetActiveInput&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;activeInput&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;activeInput&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;retCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;activeInput&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;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;audio&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;SetOutputVolume&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clampedValue&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;&lt;em&gt;activeInput&lt;/em&gt; is an &lt;em&gt;std::optional&lt;/em&gt; entity from the pointer to &lt;em&gt;AudioMux::Input&lt;/em&gt;. The nested &lt;em&gt;if&lt;/em&gt; statement contains the &lt;em&gt;value *member function call&lt;/em&gt;.* The function is guaranteed to return the pointer and will not throw an exception. After, the result is dereferenced.&lt;/p&gt;

&lt;p&gt;However, the function may return either a valid — or a null pointer. The plan for the nested &lt;em&gt;if&lt;/em&gt; statement was probably to check this pointer. Hm, I also like wrapping pointers and boolean values in &lt;em&gt;std::optional&lt;/em&gt;! And then going through the same grief each time :).&lt;/p&gt;

&lt;p&gt;Let's see how we can fix 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="n"&gt;std&lt;/span&gt;&lt;span class="o"&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;AudioMux&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Input&lt;/span&gt; &lt;span class="o"&gt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;AudioMux&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;GetActiveInput&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;Audio&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;handleSetVolume&lt;/span&gt;&lt;span class="p"&gt;(....)&lt;/span&gt; &lt;span class="o"&gt;-&amp;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;unique_ptr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;AudioResponseMessage&lt;/span&gt;&lt;span class="o"&gt;&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="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;activeInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;audioMux&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetActiveInput&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;activeInput&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;activeInput&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;retCode&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;activeInput&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;audio&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;SetOutputVolume&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clampedValue&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;I took this error from the article: "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0914/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;MuditaOS: Will your alarm clock go off? Part I&lt;/a&gt;".&lt;/p&gt;

&lt;h2&gt;
  
  
  Second place: Incorrect use of the flag
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v547/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V547&lt;/a&gt; [CWE-570] Expression 'nOldFlag &amp;amp; VMPF_NOACCESS' is always false. PlatMemory.cpp 22&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 PAGE_NOACCESS           0x01
#define PAGE_READONLY           0x02
#define PAGE_READWRITE          0x04
#define PAGE_EXECUTE            0x10
#define PAGE_EXECUTE_READ       0x20
#define PAGE_EXECUTE_READWRITE  0x40
&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;VM_PROTECT_FLAG&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;VMPF_NOACCESS&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x00000000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;VMPF_CPU_READ&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x00000001&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;VMPF_CPU_WRITE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x00000002&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;VMPF_CPU_EXEC&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x00000004&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;VMPF_CPU_RW&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_READ&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_WRITE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;VMPF_CPU_RWX&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_READ&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_WRITE&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_EXEC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;GetProtectFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;VM_PROTECT_FLAG&lt;/span&gt; &lt;span class="n"&gt;nOldFlag&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;nNewFlag&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;do&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;nOldFlag&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;VMPF_NOACCESS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;nNewFlag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PAGE_NOACCESS&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nOldFlag&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_READ&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;nNewFlag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PAGE_READONLY&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;nOldFlag&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_WRITE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;nNewFlag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PAGE_READWRITE&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;nOldFlag&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_EXEC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;nNewFlag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PAGE_EXECUTE_READWRITE&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;while&lt;/span&gt; &lt;span class="p"&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;return&lt;/span&gt; &lt;span class="n"&gt;nNewFlag&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;em&gt;GetProtectFlag&lt;/em&gt; function converts a flag with file access permission from one format to another. However, the function does this incorrectly. The developer did not take into account that the value of &lt;em&gt;VMPF_NOACCESS&lt;/em&gt; is zero. Because of this, the &lt;em&gt;if (nOldFlag &amp;amp; VMPF_NOACCESS)&lt;/em&gt; condition is always false and the function will never return the &lt;em&gt;PAGE_NOACCESS&lt;/em&gt; value.&lt;/p&gt;

&lt;p&gt;In addition, the &lt;em&gt;GetProtectFlag&lt;/em&gt; function incorrectly converts not only the &lt;em&gt;VMPF_NOACCESS&lt;/em&gt; flag, but also other flags. For example, the &lt;em&gt;VMPF_CPU_EXEC&lt;/em&gt; flag will be converted to the &lt;em&gt;PAGE_EXECUTE_READWRITE&lt;/em&gt; flag.&lt;/p&gt;

&lt;p&gt;When a developer who wrote the article was thinking how to fix that issue, his first thought was 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="kr"&gt;inline&lt;/span&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="nf"&gt;GetProtectFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;VM_PROTECT_FLAG&lt;/span&gt; &lt;span class="n"&gt;nOldFlag&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;nNewFlag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PAGE_NOACCESS&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;nOldFlag&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_READ&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;nNewFlag&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="n"&gt;PAGE_READ&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;nOldFlag&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_WRITE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;nNewFlag&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="n"&gt;PAGE_WRITE&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;nOldFlag&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_EXEC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;nNewFlag&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="n"&gt;PAGE_EXECUTE&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;nNewFlag&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;However, in this case, this approach does not work. The thing is, &lt;em&gt;PAGE_NOACCESS&lt;/em&gt;, &lt;em&gt;PAGE_READONLY&lt;/em&gt;, and other flags are Windows flags and they have their own specifics. For example, there is no &lt;em&gt;PAGE_WRITE *flag among them. It is assumed that if there are write permissions, then at least there are also read permissions. For the same reasons, there is no *PAGE_EXECUTE_WRITE&lt;/em&gt; flag.&lt;/p&gt;

&lt;p&gt;In addition, the bitwise "OR" of two Windows flags does not result in a flag corresponding to the sum of the permissions: &lt;em&gt;PAGE_READONLY | PAGE_EXECUTE != PAGE_EXECUTE_READ&lt;/em&gt;. Therefore, you need to iterate through all possible flag combinations:&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="kr"&gt;inline&lt;/span&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="nf"&gt;GetProtectFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;VM_PROTECT_FLAG&lt;/span&gt; &lt;span class="n"&gt;nOldFlag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nOldFlag&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;VMPF_NOACCESS&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;PAGE_NOACCESS&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;VMPF_CPU_READ&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;PAGE_READONLY&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;VMPF_CPU_WRITE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;// same as ReadWrite&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_RW&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;PAGE_READWRITE&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;VMPF_CPU_EXEC&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;PAGE_EXECUTE&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;VMPF_CPU_READ&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_EXEC&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;PAGE_EXECUTE_READ&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_WRITE&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_EXEC&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;// same as ExecuteReadWrite&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;VMPF_CPU_RWX&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;PAGE_EXECUTE_READWRITE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;default:&lt;/span&gt;
      &lt;span class="n"&gt;LOG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"unknown PS4 flag"&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;PAGE_NOACCESS&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 bug is from the article: "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0955/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;Checking the GPCS4 emulator: will we ever be able to play "Bloodborne" on PC?&lt;/a&gt;."&lt;/p&gt;

&lt;h2&gt;
  
  
  First place: PVS-Studio prevents rash code changes!
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v530/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;V530&lt;/a&gt; [CWE-252]: The return value of function 'clamp' is required to be utilized. BLI_math_vector.hh 88&lt;/p&gt;

&lt;p&gt;So, once upon a time there was code that processed a vector of values. It prevented values from going beyond a certain range.&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 CLAMP(a, b, c) \
  { \
    if ((a) &amp;lt; (b)) { \
      (a) = (b); \
    } \
    else if ((a) &amp;gt; (c)) { \
      (a) = (c); \
    } \
  } \
  (void)0
&lt;/span&gt;
&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;
&lt;span class="nf"&gt;clamp&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;T&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="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bT&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;min_v&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;bT&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;max_v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;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;a&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;T&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;type_length&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;CLAMP&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;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;min_v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max_v&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;result&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;Everything was good. And then a developer decided to abandon the custom &lt;em&gt;CLAMP&lt;/em&gt; macro and use the standard &lt;em&gt;&lt;a href="https://en.cppreference.com/w/cpp/algorithm/clamp"&gt;std::clamp&lt;/a&gt;&lt;/em&gt; function. And the &lt;a href="https://github.com/blender/blender/commit/399168f3c13fadb41c9fbec8a1b5c56cb6609343"&gt;commit&lt;/a&gt; that supposed to make the code better 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="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&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;Size&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;vec_base&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="n"&gt;clamp&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;vec_base&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt;&lt;span class="o"&gt;&amp;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="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;min&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;T&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;vec_base&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&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;Size&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;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;clamp&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;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&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;result&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 seems like the developer was in a hurry. Do you see the error? Maybe yes, maybe no. Anyway, the developer who wrote the code, didn't notice that it was broken.&lt;/p&gt;

&lt;p&gt;The point being — the &lt;em&gt;&lt;a href="https://en.cppreference.com/w/cpp/algorithm/clamp"&gt;std::clamp&lt;/a&gt;&lt;/em&gt; function doesn't change the value of the element in the container:&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;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;
&lt;span class="n"&gt;clamp&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;T&lt;/span&gt;&lt;span class="o"&gt;&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="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;lo&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;T&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;hi&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;em&gt;CLAMP&lt;/em&gt; macro used to change the value, but the standard function did not. Now the code is broken and is waiting for someone to notice an error and look for its cause.&lt;/p&gt;

&lt;p&gt;Let me point out that we &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0799/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;regularly check&lt;/a&gt; several open-source projects. Sometimes it allows you to write quick articles and show interesting bugs in code just written.&lt;/p&gt;

&lt;p&gt;Although we already written many articles on this topic, I described the warning that seemed the most striking for me. By the way, if you haven't read these articles, then I strongly recommend you do!&lt;/p&gt;

&lt;p&gt;As the author of this compilation, I want to give the first place not only to this error, but to the whole series of articles on the topic. In fact, this is a great work of one well-known person (Andrey, hi!) who started a separate genre of articles on our blog that delighted us throughout the year 2022.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1001/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;How PVS-Studio prevents rash code changes, example N6&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0936/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;How PVS-Studio prevents rash code changes, example N5&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0924/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;How PVS-Studio prevents rash code changes, example N4&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0922/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;How PVS-Studio prevents rash code changes, example N3&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0910/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;How PVS-Studio prevents rash code changes, example N2&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0817/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;How PVS-Studio prevents rash code changes&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1004/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;0,1,2, Freddy came for Blender&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The same error entered the top of the article: "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0924/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;How PVS-Studio prevents rash code changes, example N4&lt;/a&gt;".&lt;/p&gt;

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

&lt;p&gt;This year was not as prolific in terms of articles on C++ project checks as the past ones. However, I hope that we were still able to delight you with a selection of interesting bugs. We have also written many articles in other genres this year. You can find them on our &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It has become a tradition to post such articles on New Year's Eve, so here are the articles with the top 10 bugs found in C and C++ projects of the past years: &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0483/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;2016&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0565/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;2017&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0619/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;2018&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0700/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;2019&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0784/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;2020&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0901/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1021"&gt;2021&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>opensource</category>
      <category>tooling</category>
    </item>
    <item>
      <title>Help the compiler, and the compiler will help you. Subtleties of working with nullable reference types in C#</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Tue, 24 Jan 2023 07:35:37 +0000</pubDate>
      <link>https://forem.com/unicorn_developer/help-the-compiler-and-the-compiler-will-help-you-subtleties-of-working-with-nullable-reference-types-in-c-el8</link>
      <guid>https://forem.com/unicorn_developer/help-the-compiler-and-the-compiler-will-help-you-subtleties-of-working-with-nullable-reference-types-in-c-el8</guid>
      <description>&lt;p&gt;Nullable reference types appeared in C# 3 years ago. By this time, they found their audience. But even those who work with this "beast" may not know all its capabilities. Let's figure out how to work with these types more efficiently.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mvyXOR7o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vprtu0qyttn3f984nug3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mvyXOR7o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vprtu0qyttn3f984nug3.png" alt="Image description" width="780" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Nullable reference types are designed to help create a better and safer application architecture. At the code writing stage, it is necessary to understand whether this or that reference variable can be &lt;em&gt;null&lt;/em&gt; or not, whether the method can return &lt;em&gt;null&lt;/em&gt;, and so on.&lt;/p&gt;

&lt;p&gt;It is safe to say that every developer has encountered NRE (&lt;em&gt;NullReferenceException&lt;/em&gt;). And the fact that this exception can be generated at the development stage is a good scenario, because you can fix the problem immediately. It is much worse when the user finds the problem when working with the product. Nullable reference types help protect against NRE.&lt;/p&gt;

&lt;p&gt;In this article I will talk about a number of non-obvious features related to nullable reference types. But it's worth starting with a brief description of these types.&lt;/p&gt;

&lt;h2&gt;
  
  
  A word about nullable reference
&lt;/h2&gt;

&lt;p&gt;In terms of program execution logic, a nullable reference type is no different from a reference type. The difference between them is only in the specific annotation that the first one has. The annotation allows the compiler to conclude whether a particular variable or expression can be &lt;em&gt;null&lt;/em&gt;. To use nullable reference types, you need to make sure that the nullable context is enabled for the project or file (I will describe later how to do this).&lt;/p&gt;

&lt;p&gt;To declare a nullable reference variable, add '?' at the end of the type name.&lt;/p&gt;

&lt;p&gt;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;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;str&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the variable &lt;em&gt;str&lt;/em&gt; can be &lt;em&gt;null&lt;/em&gt;, and the compiler will not issue a warning for this code. If you don't add '?' when declaring a variable and assigning it with &lt;em&gt;null&lt;/em&gt;, a warning will be issued.&lt;/p&gt;

&lt;p&gt;It is possible to suppress compiler warnings about possible writing &lt;em&gt;null&lt;/em&gt; to a reference variable that is not marked as nullable.&lt;/p&gt;

&lt;p&gt;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;object&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;GetPotentialNull&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;flag&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;flag&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="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;object&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="n"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetPotentialNull&lt;/span&gt;&lt;span class="p"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;obj&lt;/em&gt; variable will never be assigned with &lt;em&gt;null&lt;/em&gt;, but the compiler does not always understand this. You can suppress the warning 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="n"&gt;object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetPotentialNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;false&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the '!' operator, we "tell" the compiler that the method will definitely not return &lt;em&gt;null&lt;/em&gt;. Therefore, there will be no warnings for this code fragment.&lt;/p&gt;

&lt;p&gt;The functionality available when working with nullable reference types is not limited to declaring variables of that type (using '?') and suppressing warnings with '!'. Below I'll look at the most interesting features when working with nullable reference types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working with a nullable context
&lt;/h2&gt;

&lt;p&gt;There are a number of mechanisms for more flexible work with nullable reference types. Let's look at some of them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Working with attributes
&lt;/h3&gt;

&lt;p&gt;Attributes can be used to tell the compiler the null-state of various elements. Let's look at the most interesting ones. Check out the &lt;a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/nullable-analysis"&gt;documentation&lt;/a&gt; to find the full list of attributes.&lt;/p&gt;

&lt;p&gt;To make it easier, let's introduce the term — null-state. The null-state is information about whether a variable or expression can be &lt;em&gt;null&lt;/em&gt; at a given time.&lt;/p&gt;

&lt;h4&gt;
  
  
  AllowNull
&lt;/h4&gt;

&lt;p&gt;Let's look how the attribute work. Here is an 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="k"&gt;public&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="n"&gt;get&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;set&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;value&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s"&gt;"defaultName"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you write the &lt;em&gt;null&lt;/em&gt; value to the &lt;em&gt;Name&lt;/em&gt; property, the compiler will issue a warning: &lt;em&gt;Cannot convert null literal to non-nullable reference type&lt;/em&gt;. But you can see from the implementation of the property that it can be &lt;em&gt;null&lt;/em&gt;. In this case, the "defaultName" string is assigned to the &lt;em&gt;_name&lt;/em&gt; field.&lt;/p&gt;

&lt;p&gt;If you add '?' to the property type, the compiler will assume that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the set accessor can accept &lt;em&gt;null&lt;/em&gt; (this is correct);&lt;/li&gt;
&lt;li&gt;the get accessor can return &lt;em&gt;null&lt;/em&gt; (this is an error).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For correct implementation, it is worth adding the &lt;em&gt;AllowNull&lt;/em&gt; attribute to the property:&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;AllowNull&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;string&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, the compiler will assume that &lt;em&gt;Name&lt;/em&gt; may be assigned with &lt;em&gt;null&lt;/em&gt;, although the property's type is not marked as nullable. If you assign the value of this property to a variable that should never be &lt;em&gt;null&lt;/em&gt;, then there will be no warnings.&lt;/p&gt;

&lt;h4&gt;
  
  
  NotNullWhen
&lt;/h4&gt;

&lt;p&gt;Suppose we have a method that checks a variable for &lt;em&gt;null&lt;/em&gt;. Depending on the result of this check, the method returns a value of the &lt;em&gt;bool&lt;/em&gt; type. This method informs us about the null-state of the variable.&lt;/p&gt;

&lt;p&gt;Here's a synthetic code 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="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;CheckNotNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="o"&gt;?&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;obj&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;This method checks the &lt;em&gt;obj&lt;/em&gt; parameter for &lt;em&gt;null&lt;/em&gt; and returns a value of the &lt;em&gt;bool&lt;/em&gt; type depending on the check result.&lt;/p&gt;

&lt;p&gt;Let's use the result of this method in the 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="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;object&lt;/span&gt; &lt;span class="n"&gt;obj2&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;object&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;CheckNotNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obj1&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 compiler will issue a warning to code above: &lt;em&gt;Converting null literal or possibly null value to non-nullable type&lt;/em&gt;. But such a scenario is impossible, since the condition guarantees that &lt;em&gt;obj1&lt;/em&gt; is not &lt;em&gt;null&lt;/em&gt; in the then branch. The problem is that the compiler doesn't understand this, so we have to help it.&lt;/p&gt;

&lt;p&gt;Let's change the signature of the &lt;em&gt;CheckNotNull&lt;/em&gt; method by adding the &lt;em&gt;NotNullWhen&lt;/em&gt; attribute:&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;bool&lt;/span&gt; &lt;span class="n"&gt;CheckNotNull&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;NotNullWhen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This attribute takes a value of the &lt;em&gt;bool&lt;/em&gt; type as the first argument. With &lt;em&gt;NotNullWhen&lt;/em&gt;, we link the null-state of the argument with the return value of the method. In this case, we "tell" the compiler that if the method returns &lt;em&gt;true&lt;/em&gt;, the argument has a value other than &lt;em&gt;null&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;There is a peculiarity associated with this attribute.&lt;/p&gt;

&lt;p&gt;Here are some examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using the &lt;em&gt;out&lt;/em&gt; modifier&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="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;GetValidOrDefaultName&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;NotNullWhen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;validOrDefaultName&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="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="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;validOrDefaultName&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="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;else&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;validOrDefaultName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"defaultName"&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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the compiler will issue a warning: &lt;em&gt;Parameter 'validOrDefaultName' must have a non-null value when exiting with 'true'&lt;/em&gt;. It is quite reasonable, since '==' is used in the condition instead of the '!=' operator. In this implementation, the method returns &lt;em&gt;true&lt;/em&gt; when &lt;em&gt;validOrDefaultName&lt;/em&gt; is &lt;em&gt;null&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using the &lt;em&gt;ref&lt;/em&gt; modifier&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="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;SetDefaultIfNotValid&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;NotNullWhen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;string&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;if&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;null&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="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"defaultName"&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;We will also get a warning for this code fragment: &lt;em&gt;Parameter 'name' must have a non-null value when exiting with 'true'&lt;/em&gt;. Similarly to the previous example, the warning is reasonable. '==' is used instead of the '!=' operator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Without using a modifier&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="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;CheckingForNull&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;NotNullWhen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="n"&gt;string&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;if&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;null&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="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name is null"&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 situation here is similar to previous cases. If &lt;em&gt;name&lt;/em&gt; equals &lt;em&gt;null&lt;/em&gt;, the method returns &lt;em&gt;true&lt;/em&gt;. Following the logic of previous examples, a warning should also be issued here: &lt;em&gt;Parameter 'name' must have a non-null value when exiting with 'true'&lt;/em&gt;. However, there is no warning. It's hard to say what's caused this, but it looks strange.&lt;/p&gt;

&lt;h4&gt;
  
  
  NotNullIfNotNull
&lt;/h4&gt;

&lt;p&gt;This attribute allows you to establish a relationship between the argument and the return value of the method. If the argument is not &lt;em&gt;null&lt;/em&gt;, the return value is also not &lt;em&gt;null&lt;/em&gt;, and vice versa.&lt;/p&gt;

&lt;p&gt;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="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="o"&gt;?&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;obj&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;null&lt;/span&gt; &lt;span class="o"&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;Empty&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;em&gt;GetString&lt;/em&gt; method returns &lt;em&gt;null&lt;/em&gt; or an empty string, depending on the null-state of the argument.&lt;/p&gt;

&lt;p&gt;Usage of this method:&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;Foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="o"&gt;?&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="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&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;Empty&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;obj&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="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetString&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compiler's warning for this code: &lt;em&gt;Converting null literal or possibly null value to non-nullable type&lt;/em&gt;. In this case, the compiler is lying. Assignment is performed in the body of &lt;em&gt;if&lt;/em&gt;, the condition of which guarantees that &lt;em&gt;GetString&lt;/em&gt; will not return &lt;em&gt;null&lt;/em&gt;. To help the compiler, let's add the &lt;em&gt;NotNullIfNotNull&lt;/em&gt; attribute for the return value of the method:&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="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;NotNullIfNotNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"obj"&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;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;obj&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;Note.&lt;/strong&gt; Starting with C#11, you can get the parameter name using the &lt;em&gt;nameof&lt;/em&gt; expression*.* In this case, it would be &lt;em&gt;nameof(obj)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;NotNullIfNotNull&lt;/em&gt; attribute takes the value of the &lt;em&gt;string&lt;/em&gt; type as the first argument — the name of the parameter, based on which the null-state of the return value is set. Now the compiler has information about the relationship between &lt;em&gt;obj&lt;/em&gt; and the return value of the method: if &lt;em&gt;obj&lt;/em&gt; is not &lt;em&gt;null&lt;/em&gt;, the return value of the method will not be &lt;em&gt;null,&lt;/em&gt; and vice versa.&lt;/p&gt;

&lt;h4&gt;
  
  
  MemberNotNull
&lt;/h4&gt;

&lt;p&gt;Let's start with an 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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&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;string&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;public&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SetDefaultName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;SetDefaultName&lt;/span&gt;&lt;span class="p"&gt;()&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="s"&gt;"Bob"&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 compiler will issue a warning to this code fragment:* Non-nullable field '_name' must contain a non-null value when exiting constructor. Consider declaring the field as nullable*. However, the &lt;em&gt;SetDefaultName&lt;/em&gt; method is called in the constructor's body, which initializes the only field of the class. This means that the compiler's message is false. The &lt;em&gt;MemberNotNull&lt;/em&gt; attribute allows you to solve the 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="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;MemberNotNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nameof&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;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;SetDefaultName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This attribute takes an argument of the &lt;em&gt;string[]&lt;/em&gt; type with the &lt;em&gt;params&lt;/em&gt; keyword. The strings need to match the names of the members that are initialized in the method.&lt;/p&gt;

&lt;p&gt;Thus, we are indicating that the value of the* _name* field will not be &lt;em&gt;null&lt;/em&gt; after this method is called. Now the compiler can understand that the field is initialized in the constructor.&lt;/p&gt;

&lt;h4&gt;
  
  
  MemberNotNullWhen
&lt;/h4&gt;

&lt;p&gt;Let's look at the 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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;Regex&lt;/span&gt; &lt;span class="n"&gt;_nameReg&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;Regex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="s"&gt;"^I'm \w*"&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;string&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;public&lt;/span&gt; &lt;span class="n"&gt;Person&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="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;TryInitialize&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="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"invalid 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;private&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;TryInitialize&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="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;_nameReg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsMatch&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="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="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;else&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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;TryInitialize&lt;/em&gt; will initialize &lt;em&gt;_name&lt;/em&gt; if the argument's value matches some pattern. The method returns &lt;em&gt;true&lt;/em&gt; when the field has been initialized, otherwise it returns &lt;em&gt;false&lt;/em&gt;. Depending on the result of executing &lt;em&gt;TryInitialize&lt;/em&gt;, a value is assigned to the &lt;em&gt;_name&lt;/em&gt; field in the constructor. In this implementation, &lt;em&gt;_name&lt;/em&gt; &lt;strong&gt;cannot&lt;/strong&gt; be not initialized in the constructor. However, the compiler will issue a warning: &lt;em&gt;Non-nullable field '_name' must contain a non-null value when exiting constructor. Consider declaring the field as nullable&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To fix the situation, you need to add the &lt;em&gt;MemberNotNullWhen&lt;/em&gt; attribute:&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;MemberNotNullWhen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nameof&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;private&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;TryInitialize&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type of the first argument is* bool*, the second argument's type is &lt;em&gt;string[]&lt;/em&gt; (with the &lt;em&gt;params&lt;/em&gt; keyword). The attribute is used for methods with a return value of the* bool &lt;em&gt;type. The logic is simple: if the method returns a value that corresponds to the first argument of the attribute, the class members passed to *params&lt;/em&gt; will be considered initialized.&lt;/p&gt;

&lt;h4&gt;
  
  
  DoesNotReturn and DoesNotReturnIf
&lt;/h4&gt;

&lt;p&gt;It is not uncommon to have to create methods that throw out exceptions if something has not gone according to plan. Unfortunately, the compiler cannot always understand that program execution will be terminated after such a method is called.&lt;/p&gt;

&lt;p&gt;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="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ThrowException&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;Exception&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="n"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;str&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;str&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="n"&gt;ThrowException&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;notNullStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;str&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;For code above, the compiler will issue a warning: &lt;em&gt;Converting null literal or possibly null value to non-nullable type&lt;/em&gt;. However, if &lt;em&gt;str&lt;/em&gt; is &lt;em&gt;null&lt;/em&gt;, the execution of the method will not reach the code fragment with the assignment, as an exception will be thrown. Thus, at the time of assignment, the &lt;em&gt;str&lt;/em&gt; variable cannot be &lt;em&gt;null&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;DoesNotReturn&lt;/em&gt; attribute allows you to tell the compiler that after executing the method marked with the attribute, the execution of the calling method stops.&lt;/p&gt;

&lt;p&gt;Let's add the attribute for the &lt;em&gt;throwException&lt;/em&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="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;DoesNotReturn&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;ThrowException&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the compiler knows that after this method is called, control will not be returned to the calling method. Therefore, &lt;em&gt;null&lt;/em&gt; will never be written to &lt;em&gt;notNullStr&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;DoesNotReturnIf&lt;/em&gt; attribute works similarly to &lt;em&gt;DoesNotReturn&lt;/em&gt;, except for checking an additional condition.&lt;/p&gt;

&lt;p&gt;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="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ThrowException&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;DoesNotReturnIf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&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;flag&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;flag&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;Exception&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 compiler will assume that &lt;em&gt;throwException&lt;/em&gt; will not return control to the calling method if the &lt;em&gt;flag&lt;/em&gt; parameter is set to &lt;em&gt;true&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Specifying context at the project level
&lt;/h3&gt;

&lt;p&gt;To change the nullable context at the project level, you need to open the project properties and select the context in the "Build" section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bsn-qIAs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xa1xfuncr27gi0c4fly.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bsn-qIAs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xa1xfuncr27gi0c4fly.png" alt="Image description" width="702" height="525"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can set the nullable context in the project file (.csproj). You need to open this file and write the value to the &lt;em&gt;Nullable&lt;/em&gt; property:&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;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Project&lt;/span&gt; &lt;span class="n"&gt;Sdk&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Microsoft.NET.Sdk"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;PropertyGroup&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;OutputType&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;Exe&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;OutputType&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TargetFramework&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;net6&lt;/span&gt;&lt;span class="mf"&gt;.0&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;TargetFramework&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ImplicitUsings&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;enable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;ImplicitUsings&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Nullable&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;disable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;Nullable&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;             &lt;span class="c1"&gt;//&amp;lt;=&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;PropertyGroup&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;Project&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;It is likely that many people know that you can turn on or turn off the nullable context. However, there are two more context options.&lt;/p&gt;

&lt;h4&gt;
  
  
  Warnings
&lt;/h4&gt;

&lt;p&gt;Behavior in the nullable warning context:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the '?' sign does not affect the analysis in any way;&lt;/li&gt;
&lt;li&gt;from the point of view of the compiler, all values of the reference type can be &lt;em&gt;null&lt;/em&gt; by default;&lt;/li&gt;
&lt;li&gt;if you write the '?' sign, the compiler will issue a warning that it should not be used in this context;&lt;/li&gt;
&lt;li&gt;the compiler will issue a warning only for those parts of code where the null reference is dereferenced;&lt;/li&gt;
&lt;li&gt;you can indicate that the expression is not &lt;em&gt;null&lt;/em&gt; using the '!' operator.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This mode helps protect against exceptions of &lt;em&gt;NullReferenceException&lt;/em&gt; type. The mode informs about the dereference of null reference.&lt;/p&gt;

&lt;h4&gt;
  
  
  Annotations
&lt;/h4&gt;

&lt;p&gt;Behavior in the nullable annotation context:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;there are no warnings related to dereference of null references and errors when working with nullable reference;&lt;/li&gt;
&lt;li&gt;the compiler does not issue warnings when '?' and '!' are used.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This mode helps make a smooth entry into the use of nullable reference types in the project. It allows you to markup variables that can and cannot be &lt;em&gt;null&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Working with preprocessor directives
&lt;/h3&gt;

&lt;p&gt;Preprocessor directives are used at the file level with the .cs extension and allow you to change the states of the nullable context for fragments of code in this file. The way it works is similar to that described in the previous section. Each directive starts with '#'.&lt;/p&gt;

&lt;p&gt;Let's look at all possible directives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;#nullable disable – disables the nullable context;&lt;/li&gt;
&lt;li&gt;#nullable enable – enables nullable context;&lt;/li&gt;
&lt;li&gt;#nullable restore – restores the nullable context to its value at the project level;&lt;/li&gt;
&lt;li&gt;#nullable disable annotations – disables annotation context;&lt;/li&gt;
&lt;li&gt;#nullable enable annotations – enables annotation context;&lt;/li&gt;
&lt;li&gt;#nullable restore – restores the nullable context to its value at the project level;&lt;/li&gt;
&lt;li&gt;#nullable disable warnings – disables the warning context;&lt;/li&gt;
&lt;li&gt;#nullable enable warnings – enables the warning context;&lt;/li&gt;
&lt;li&gt;#nullable restore warnings – restores the warning context to its value at the project level.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In fact, the &lt;em&gt;enable&lt;/em&gt; value represents the enabled context of annotations and the context of warnings, and &lt;em&gt;disable&lt;/em&gt; – on the contrary, these same contexts are in the disabled state. So the '#nullable enable' directive would be equivalent to writing '#nullable enable annotations' and '#nullable enable warnings' together.&lt;/p&gt;

&lt;p&gt;You can use multiple directives in one file at once. This allows you to set a different nullable context for different code fragments. &lt;/p&gt;

&lt;p&gt;Let's look at an example of such usage (at the project level, nullable-context is disabled):&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="c1"&gt;// nullable-context is disabled in this code fragment &lt;/span&gt;
&lt;span class="cp"&gt;#nullable enable warnings
&lt;/span&gt;&lt;span class="p"&gt;....&lt;/span&gt; &lt;span class="c1"&gt;// the warning context is enabled in this code fragment&lt;/span&gt;
&lt;span class="cp"&gt;#nullable enable annotations
&lt;/span&gt;&lt;span class="p"&gt;....&lt;/span&gt; &lt;span class="c1"&gt;// the context of warnings and annotations is enabled &lt;/span&gt;
     &lt;span class="c1"&gt;// in this code fragment&lt;/span&gt;
&lt;span class="cp"&gt;#nullable disable annotations
&lt;/span&gt;&lt;span class="p"&gt;....&lt;/span&gt; &lt;span class="c1"&gt;// only the warning context is enabled in this code fragment&lt;/span&gt;
&lt;span class="cp"&gt;#nullable restore
&lt;/span&gt;&lt;span class="p"&gt;....&lt;/span&gt; &lt;span class="c1"&gt;// nullable-context is disabled in this code fragment  &lt;/span&gt;
     &lt;span class="c1"&gt;// (since the Nullable property – disable)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In conclusion, being able to use nullable reference types should be of great benefit to developers. These types allow you to make the application more secure and correct from the point of view of architecture.&lt;/p&gt;

&lt;p&gt;This mechanism is not without its drawbacks either. About drawbacks, and in general about nullable reference types, my colleagues told in articles: &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0631/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1017&amp;amp;utm_term=0631"&gt;one&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0764/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1017&amp;amp;utm_term=0764"&gt;two&lt;/a&gt;. The ability to add attributes makes sense largely because of the imperfection of the static analyzer. Therefore, it is necessary to add annotations to methods, fields, etc. manually, because the analyzer cannot understand some relationships. For example, the relationship between the return value of a method and the null state of a variable.&lt;/p&gt;

&lt;p&gt;A number of drawbacks are the result of insufficient in-depth analysis. Such analysis cannot be done on the fly. On the other hand, it is not required. nullable-context is a good help in the code-writing process. When part of the functionality is ready and it needs to be tested, we recommend using tools for deeper analysis – for example, PVS-Studio.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Top 10 bugs found in C# projects in 2022</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Mon, 16 Jan 2023 11:55:03 +0000</pubDate>
      <link>https://forem.com/unicorn_developer/top-10-bugs-found-in-c-projects-in-2022-3m59</link>
      <guid>https://forem.com/unicorn_developer/top-10-bugs-found-in-c-projects-in-2022-3m59</guid>
      <description>&lt;p&gt;In 2022, the PVS-Studio developers wrote lots of articles where they described bugs found in open-source projects. Now it's time to choose the most interesting ones.&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.amazonaws.com%2Fuploads%2Farticles%2Fzn9rsdf8sm0n91ud69ff.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.amazonaws.com%2Fuploads%2Farticles%2Fzn9rsdf8sm0n91ud69ff.png" alt="Image description" width="780" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How was this top created?
&lt;/h2&gt;

&lt;p&gt;I looked through all C# articles published this year and inspected all the bugs described there. To make this top more diverse, I used the following criteria:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one project — one error;&lt;/li&gt;
&lt;li&gt;errors should be different from each other.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's see what we've got.&lt;/p&gt;

&lt;h2&gt;
  
  
  10th place. Attempt to unsubscribe in Stride
&lt;/h2&gt;

&lt;p&gt;A classic analyzer warning takes the tenth place today. This error was found during the &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0994/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;Stride Game Engine project check&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;RemoveEditor&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;if&lt;/span&gt; &lt;span class="p"&gt;(....)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;multiEditor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OpenedAssets&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CollectionChanged&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="p"&gt;,&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;=&amp;gt;&lt;/span&gt; 
      &lt;span class="nf"&gt;MultiEditorOpenAssetsChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;multiEditor&lt;/span&gt;&lt;span class="p"&gt;,&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="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;a href="https://pvs-studio.com/en/docs/warnings/v3084/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;V3084&lt;/a&gt;. Anonymous function is used to unsubscribe from 'CollectionChanged' event. No handlers will be unsubscribed, as a separate delegate instance is created for each anonymous function declaration. AssetEditorsManager.cs 444&lt;/p&gt;

&lt;p&gt;Judging by the analyzer warning, the code doesn't unsubscribe from the event. Will it cause trouble? It can!&lt;/p&gt;

&lt;h2&gt;
  
  
  9th place. Ambiguous assignment in Bitwarden
&lt;/h2&gt;

&lt;p&gt;The ninth place goes to an error from the &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0947/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;Bitwarden project check&lt;/a&gt;. Let's look at it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&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;BillingInvoice&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;BillingInvoice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Invoice&lt;/span&gt; &lt;span class="n"&gt;inv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Amount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AmountDue&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;100M&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;Date&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Created&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Url&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HostedInvoiceUrl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;PdfUrl&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InvoicePdf&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Number&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Paid&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Paid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Amount&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Total&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;100M&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;public&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;Amount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&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;DateTime&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;Date&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Url&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;PdfUrl&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;Paid&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&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;a href="https://pvs-studio.com/en/docs/warnings/v3008/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;V3008&lt;/a&gt;. The 'Amount' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 148, 142. BillingInfo.cs 148&lt;/p&gt;

&lt;p&gt;At the time of writing the article, we could only guess what exactly the developers wanted. Either the first assignment is redundant, or vise versa — the second assignment is superfluous. However, after the article was published, we contacted the developers and &lt;a href="https://github.com/bitwarden/server/issues/2013" rel="noopener noreferrer"&gt;reported the issues&lt;/a&gt; we found. The developers fixed them — in this case, they just removed the first assignment.&lt;/p&gt;

&lt;h2&gt;
  
  
  8th place. An element from the void in Barotrauma
&lt;/h2&gt;

&lt;p&gt;The 8th place goes to the error we described in the &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0930/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;article about the Barotrauma check&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ParticlePrefab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;XElement&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ContentFile&lt;/span&gt; &lt;span class="n"&gt;file&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CollisionRadius&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;0.0f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="n"&gt;CollisionRadius&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Sprites&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; 
                                       &lt;span class="n"&gt;Sprites&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;SourceRect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Width&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2.0f&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;a href="https://pvs-studio.com/en/docs/warnings/v3106/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;V3106&lt;/a&gt; Possibly index is out of bound. The '0' index is pointing beyond 'Sprites' bound. ParticlePrefab.cs 303&lt;/p&gt;

&lt;p&gt;Accessing the first element of an empty collection is clearly something strange :). We can only hope that the exception thrown doesn't affect the gameplay.&lt;/p&gt;

&lt;h2&gt;
  
  
  7th place. Initializing fields in AvalonStudio
&lt;/h2&gt;

&lt;p&gt;The seventh place goes to well-hidden error we found when &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0966/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;checking AvalonStudio&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&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;ColorScheme&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="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ColorScheme&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s_colorSchemes&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;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ColorScheme&lt;/span&gt;&lt;span class="p"&gt;&amp;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="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ColorScheme&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s_colorSchemeIDs&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;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ColorScheme&lt;/span&gt;&lt;span class="p"&gt;&amp;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;readonly&lt;/span&gt; &lt;span class="n"&gt;ColorScheme&lt;/span&gt; &lt;span class="n"&gt;DefaultColorScheme&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; 
    &lt;span class="n"&gt;ColorScheme&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SolarizedLight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;ColorScheme&lt;/span&gt; &lt;span class="n"&gt;SolarizedLight&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;ColorScheme&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, seems like this time I won't show the analyzer warnings right away :) Just so you can feel what it's like to search for such issues without static analysis. This error is simple though, so I'm sure you'll handle it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's the matter here?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The analyzer issues the following warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v3070/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;V3070&lt;/a&gt;. Uninitialized variable 'SolarizedLight' is used when initializing the 'DefaultColorScheme' variable. ColorScheme.cs 32&lt;/p&gt;

&lt;p&gt;Indeed, there's something wrong with the field initialization.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  6th place. Confusion in .NET 7
&lt;/h2&gt;

&lt;p&gt;Yes, we found this error in the famous .NET 7 — here's an &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/1015/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;article about other errors we found there&lt;/a&gt;. Nobody's perfect :).&lt;/p&gt;

&lt;p&gt;First, let's look at the* XmlConfigurationElementTextContent* constructor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;XmlConfigurationElementTextContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;textContent&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;linePosition&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;lineNumber&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 let's see where it's used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&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;IDictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;?&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Read&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;case&lt;/span&gt; &lt;span class="n"&gt;XmlNodeType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EndElement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;....&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lineInfo&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;IXmlLineInfo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lineNumber&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lineInfo&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;LineNumber&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;linePosition&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lineInfo&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;LinePosition&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;TextContent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
      &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;XmlConfigurationElementTextContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                             &lt;span class="n"&gt;lineNumber&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                             &lt;span class="n"&gt;linePosition&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="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;XmlNodeType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;....&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lineInfo&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;IXmlLineInfo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lineNumber&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lineInfo&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;LineNumber&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;linePosition&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lineInfo&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;LinePosition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;XmlConfigurationElement&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;currentPath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Peek&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;TextContent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
      &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;XmlConfigurationElementTextContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reader&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="n"&gt;lineNumber&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                             &lt;span class="n"&gt;linePosition&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;Pay attention to the order of arguments and parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;arguments: ..., &lt;em&gt;lineNumber&lt;/em&gt;, &lt;em&gt;linePosition&lt;/em&gt;; &lt;/li&gt;
&lt;li&gt;parameters: ..., &lt;em&gt;linePosition&lt;/em&gt;, &lt;em&gt;lineNumber&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's exactly what the analyzer detected:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v3066/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;V3066&lt;/a&gt; Possible incorrect order of arguments passed to 'XmlConfigurationElementTextContent' constructor: 'lineNumber' and 'linePosition'. XmlStreamConfigurationProvider.cs 133&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v3066/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;V3066&lt;/a&gt; Possible incorrect order of arguments passed to 'XmlConfigurationElementTextContent' constructor: 'lineNumber' and 'linePosition'. XmlStreamConfigurationProvider.cs 148&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My teammate created an &lt;a href="https://github.com/dotnet/runtime/issues/78212" rel="noopener noreferrer"&gt;issue&lt;/a&gt; on GitHub: the developers fixed the order of arguments and added a test.&lt;/p&gt;

&lt;h2&gt;
  
  
  5th place. Orleans: don't rush with clearing
&lt;/h2&gt;

&lt;p&gt;The error from the &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0961/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;Orleans project check&lt;/a&gt; opens the second half of our top:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BatchOperation&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;readonly&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TableTransactionAction&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;batchOperation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;Flush&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;batchOperation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&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;try&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;....&lt;/span&gt;
        &lt;span class="n"&gt;batchOperation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Clear&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;keyIndex&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&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;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IsEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LogLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Trace&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="p"&gt;=&lt;/span&gt; &lt;span class="m"&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="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;batchOperation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&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="c1"&gt;// &amp;lt;=&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogTrace&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;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;....&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v3116/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;V3116&lt;/a&gt;. Consider inspecting the 'for' operator. It's possible that the loop will be executed incorrectly or won't be executed at all. AzureTableTransactionalStateStorage.cs 345&lt;/p&gt;

&lt;p&gt;Something tells me that the developers should have cleared &lt;em&gt;batchOperation&lt;/em&gt; a bit later. Judging by the &lt;a href="https://github.com/dotnet/orleans/pull/7839/files" rel="noopener noreferrer"&gt;fix&lt;/a&gt;, the Orleans developers agree with me.&lt;/p&gt;

&lt;h2&gt;
  
  
  4th place. Shadow features of Eto.Forms
&lt;/h2&gt;

&lt;p&gt;An error described in the &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0929/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;article about Eto.Forms&lt;/a&gt;, the GUI framework almost made it to the top 3 errors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;NSShadow&lt;/span&gt; &lt;span class="n"&gt;TextHighlightShadow&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;get&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;textHighlightShadow&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&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;textHighlightShadow&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;NSShadow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="n"&gt;textHighlightShadow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ShadowColor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NSColor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FromDeviceWhite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0F&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.5F&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;textHighlightShadow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ShadowOffset&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;CGSize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0F&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1.0F&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;textHighlightShadow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ShadowBlurRadius&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;2F&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;textHighlightShadow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;textShadow&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;value&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="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;NSShadow&lt;/span&gt; &lt;span class="n"&gt;TextShadow&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;get&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;textShadow&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&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;textShadow&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;NSShadow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="n"&gt;textShadow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ShadowColor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NSColor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FromDeviceWhite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1F&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.5F&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;textShadow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ShadowOffset&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;CGSize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0F&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1.0F&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;textShadow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ShadowBlurRadius&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0F&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;textShadow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;textShadow&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;value&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: &lt;a href="https://pvs-studio.com/en/docs/warnings/v3140/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;V3140&lt;/a&gt; Property accessors use different backing fields. Eto.Mac64 MacImageAndTextCell.cs 162&lt;/p&gt;

&lt;p&gt;Copy-paste says hello :).&lt;/p&gt;

&lt;p&gt;Perhaps, assigning a value to the &lt;em&gt;TextHighlightShadow&lt;/em&gt; property should somehow affect &lt;em&gt;textShadow&lt;/em&gt;. But even so, it's not clear why it doesn't affect the &lt;em&gt;textHighlightShadow&lt;/em&gt; field used in the get accessor.&lt;/p&gt;

&lt;h2&gt;
  
  
  3d place. Lost migration in Squidex
&lt;/h2&gt;

&lt;p&gt;The bronze goes to an error we described in the &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0926/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;article about checking the ASP.NET Core projects&lt;/a&gt;. This error was found in Squidex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IMigration&lt;/span&gt;&lt;span class="p"&gt;?&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;ResolveMigrators&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;version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;serviceProvider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;StopEventConsumers&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// Version 06: Convert Event store. Must always be executed first.&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;version&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;serviceProvider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ConvertEventStore&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Version 22: Integrate Domain Id.&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;version&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;22&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;serviceProvider&lt;/span&gt;
                   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;AddAppIdToEventStream&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Version 07: Introduces AppId for backups.&lt;/span&gt;
  &lt;span class="k"&gt;else&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;version&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;serviceProvider&lt;/span&gt;
                   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ConvertEventStoreAppId&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Version 05: Fixes the broken command architecture and requires a&lt;/span&gt;
  &lt;span class="c1"&gt;// rebuild of all snapshots.&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;version&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;serviceProvider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RebuildSnapshots&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Version 09: Grain indexes.&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;version&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;serviceProvider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ConvertOldSnapshotStores&lt;/span&gt;&lt;span class="p"&gt;&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;span class="c1"&gt;// Version 13: Json refactoring&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;version&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;13&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;serviceProvider&lt;/span&gt;
                   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ConvertRuleEventsJson&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;serviceProvider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;StartEventConsumers&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good luck finding the error in the code :).&lt;/p&gt;

&lt;p&gt;Or you can look at the shortened fragment and try to find it again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Version 22: Integrate Domain Id.&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;version&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;22&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;serviceProvider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;AddAppIdToEventStream&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Version 07: Introduces AppId for backups.&lt;/span&gt;
&lt;span class="k"&gt;else&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;version&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;serviceProvider&lt;/span&gt;
                 &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRequiredService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ConvertEventStoreAppId&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm sure it's way easier to search for it now. Oh, if only there were a button that would show only the code fragments related to errors... Wait, this button exists!&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.amazonaws.com%2Fuploads%2Farticles%2Fiwtmhu94fkckj53jtvoq.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.amazonaws.com%2Fuploads%2Farticles%2Fiwtmhu94fkckj53jtvoq.png" alt="Image description" width="723" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clicking on this miracle button helped discover this issue:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v3022/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;V3022&lt;/a&gt;. Expression 'version &amp;lt; 7' is always false. MigrationPath.cs 55&lt;/p&gt;

&lt;p&gt;Indeed, the &lt;em&gt;version &amp;lt; 7&lt;/em&gt; condition is checked only if &lt;em&gt;version &amp;gt;= 22&lt;/em&gt;. Strange, isn't it?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My IDE doesn't have that button&lt;/strong&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to get this button, download the extension &lt;a href="https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020&amp;amp;utm_term=link_try-free" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If your IDE is not supported, you can &lt;a href="https://pvs-studio.com/en/about-feedback/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;contact us&lt;/a&gt; and express your interest in the plugin with the button for it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2nd place. Funny shifts in Discord.NET
&lt;/h2&gt;

&lt;p&gt;This time the silver goes to an error we found when &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0974/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;checking the Discord.NET project&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;GuildFeature&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="n"&gt;None&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;AnimatedBanner&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;AnimatedIcon&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;Banner&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="n"&gt;TextInVoiceEnabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;ThreadsEnabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;33&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;ThreadsEnabledTesting&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="n"&gt;VIPRegions&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;WelcomeScreenEnabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;41&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;Just like the previous case, I'll &lt;del&gt;have a pleasure to&lt;/del&gt; let you find this error first. Or you can click on the miracle button:&lt;/p&gt;

&lt;p&gt;V3134. Shift by 32 bits is greater than the size of 'Int32' type of expression '1'. GuildFeature.cs 147&lt;/p&gt;

&lt;p&gt;The problem is, the type of shift operands here is &lt;em&gt;int&lt;/em&gt;, so, the result will have the same type. In this case, a value of the &lt;em&gt;1 &amp;lt;&amp;lt; 32&lt;/em&gt; type is 1, the value of &lt;em&gt;1 &amp;lt;&amp;lt; 33&lt;/em&gt; is the same as &lt;em&gt;1 &amp;lt;&amp;lt; 1&lt;/em&gt;, and so on.&lt;/p&gt;

&lt;p&gt;So, it leads to a weird situation: the &lt;em&gt;AnimatedBanner&lt;/em&gt; and &lt;em&gt;TextInVoiceEnabled&lt;/em&gt; elements are the same — just like the &lt;em&gt;AnimatedIcon-ThreadsEnabled&lt;/em&gt; pair, etc. Besides, the internal type of enumeration is &lt;em&gt;long,&lt;/em&gt; and now it becomes clear that the developer did not expect this behavior at all.&lt;/p&gt;



&lt;h2&gt;
  
  
  1st place. Vulnerability from BlogEngine.NET
&lt;/h2&gt;

&lt;p&gt;And we finally get to the most interesting bug described in 2022. Please welcome — the XXE vulnerability from BlogEngine.NET:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;XMLRPCRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;inputXml&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ParseRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// LogMetaWeblogCall(inputXml);&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadXmlRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputXml&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Loads Method Call &lt;/span&gt;
                                 &lt;span class="c1"&gt;// and Associated Variables&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="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;ParseRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&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;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InputStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&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;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InputStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Position&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&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;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InputStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Read&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="m"&gt;0&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;Length&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;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;LoadXmlRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;XmlDocument&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(!(&lt;/span&gt;&lt;span class="n"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;StartsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;?xml"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;StartsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;method"&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;xml&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IndexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;?xml"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadXml&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xml&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;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&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="nf"&gt;MetaWeblogException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"01"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                     &lt;span class="s"&gt;$"Invalid XMLRPC Request. (&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="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;Wait. What? What vulnerability?&lt;/p&gt;

&lt;p&gt;Even though I showed you only those methods that relate to the error, it's still hard to see a vulnerability to an XXE attack.&lt;/p&gt;

&lt;p&gt;Read &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0918/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;the article&lt;/a&gt; to find a detailed description of such vulnerabilities in general and this one in BlogEngine.NET in particular. Here, I'll briefly describe the most important parts.&lt;/p&gt;

&lt;p&gt;First, look at the &lt;em&gt;ParseRequest&lt;/em&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&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="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;ParseRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&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;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InputStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&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;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InputStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Position&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&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;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InputStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Read&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="m"&gt;0&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;Length&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;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;context.Request.InputStream&lt;/em&gt; contains some data sent by a user to an apllication. If the data is not checked, then &lt;em&gt;InputStream&lt;/em&gt; may contain anything. Such data is called potentially tainted (read more &lt;a href="https://pvs-studio.com/en/blog/terms/6496/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;ParseRequest&lt;/em&gt; method reads the request data into a buffer, converts it into a string and then returns it "as is". Where does the data go?&lt;/p&gt;

&lt;p&gt;The answer is here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;XMLRPCRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;inputXml&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ParseRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// LogMetaWeblogCall(inputXml);&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadXmlRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputXml&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Loads Method Call &lt;/span&gt;
                                 &lt;span class="c1"&gt;// and Associated Variables&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We see that the result of the &lt;em&gt;ParseRequest&lt;/em&gt; work is passed to the &lt;em&gt;LoadXmlRequest&lt;/em&gt; method. Once more, the data may contain anything, since only the user controls its contents. Now let's look at how the data is processed in the &lt;em&gt;LoadXmlRequest&lt;/em&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;LoadXmlRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;XmlDocument&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(!(&lt;/span&gt;&lt;span class="n"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;StartsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;?xml"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;StartsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;method"&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;xml&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IndexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;?xml"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadXml&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xml&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;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&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="nf"&gt;MetaWeblogException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"01"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                     &lt;span class="s"&gt;$"Invalid XMLRPC Request. (&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="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;That's where the vulnerability shows itself! The potentially tainted data is first passed to the &lt;em&gt;xml&lt;/em&gt; parameter and then — to the &lt;em&gt;LoadXml&lt;/em&gt; method of the &lt;em&gt;XmlDocument&lt;/em&gt; class.&lt;/p&gt;

&lt;p&gt;You may think: "Well, and? We have try-catch here, and even a check". The problem is, they won't save you from the real threat.&lt;/p&gt;

&lt;p&gt;If an attacker passes certain data to the request, then the contents of almost any file of a system, in which the request is processed, will be written to the &lt;em&gt;request&lt;/em&gt; variable. &lt;/p&gt;

&lt;p&gt;The article &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0918/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;describes&lt;/a&gt; how the contents may return to the user. It shows not just a "theoretical danger", but a completely working scenario for exploiting the vulnerability.&lt;/p&gt;

&lt;p&gt;How is it that processing custom &lt;em&gt;xml&lt;/em&gt; leads to reading files from the system? A brief explanation by the analyzer:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v5614/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;V5614&lt;/a&gt;. Potential XXE vulnerability inside method. Insecure XML parser is used to process potentially tainted data from the first argument: 'inputXml'. BlogEngine.Core XMLRPCRequest.cs 41&lt;/p&gt;

&lt;p&gt;Thus, PVS-Studio detects and points out 2 components of the XML External Entity vulnerability:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;unsafe (unsafely configured) XML parser;&lt;/li&gt;
&lt;li&gt;tainted data passed to this parser.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We have already mentioned the tainted data. However, it can be difficult to understand how the parser may be unsafe. In this example, the default configuration used is unsafe. However, in general, everything is more complicated. You can read more about this vulnerability and ways to protect from it in the article: "&lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0918/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020" rel="noopener noreferrer"&gt;Vulnerabilities due to XML files processing: XXE in C# applications in theory and in practice&lt;/a&gt;".&lt;/p&gt;

&lt;p&gt;The error in BlogEngine.NET is completely different from others discussed in this article — it is not just "strange code", but a real vulnerability. There is an &lt;a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-14485" rel="noopener noreferrer"&gt;entry&lt;/a&gt; for it in the CVE database.&lt;/p&gt;

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

&lt;p&gt;In 2022 we published many articles, thanks to which I managed to collect a diverse top. It also allows you to evaluate the variety of real errors that PVS-Studio can find. Some of them are the result of writing code in a rush, some are the result of not fully understanding the language features. Some of the errors hide until the analyzer finds them.&lt;/p&gt;

&lt;p&gt;As usual, I'll leave a &lt;a href="https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1020&amp;amp;utm_term=link_try-free" rel="noopener noreferrer"&gt;link&lt;/a&gt; to the page where you can download PVS-Studio and evaluate it on your project. That's all folks!&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Collecting the best C++ practices</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 13 Jan 2023 08:04:25 +0000</pubDate>
      <link>https://forem.com/unicorn_developer/collecting-the-best-c-practices-14g</link>
      <guid>https://forem.com/unicorn_developer/collecting-the-best-c-practices-14g</guid>
      <description>&lt;p&gt;Reading our articles, you may wonder: why are they always talking about bad things? This code contains bug; there are code smells here; it's an antipattern — don't code like that. So, would you help me to look on the bright side of C++ programming? Feel free to leave comments 💬&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jx-1xGf5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93ysizd5ku4ihw56p88d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jx-1xGf5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93ysizd5ku4ihw56p88d.png" alt="Image description" width="780" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recently I came across a discussion on Reddit "&lt;a href="https://www.reddit.com/r/cpp/comments/z3i2ae/good_repos_for_beginners_to_browse_that_follow/"&gt;Good repos for beginners to browse that follow best modern C++ practices (including testing, static analysis etc...)&lt;/a&gt;". The thread is not the longest one, but it's still quite interesting. Then, an idea popped into my head — why don't I post a survey on Habr.com? I did the same with terrible coding tips.&lt;/p&gt;

&lt;p&gt;By the way, the idea to write an article about &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0953/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=Best_Cpp_Practices&amp;amp;utm_term=0953"&gt;terrible coding tips&lt;/a&gt; is evolving. Soon I plan to publish a book with 60 anti-patterns in C++. &lt;a href="https://pvs-studio.com/en/subscribe/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=Best_Cpp_Practices&amp;amp;utm_term=subscribe"&gt;Subscribe&lt;/a&gt; to our monthly newsletter, if you don't want to miss the most interesting publications.&lt;/p&gt;

&lt;p&gt;So, the topic of terrible coding tips is covered :). It's time to talk about good practices!&lt;/p&gt;

&lt;p&gt;I go first. Then, I invite you to join me and write in the comments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what tools you recommend;&lt;/li&gt;
&lt;li&gt;what interesting and useful libraries you use;&lt;/li&gt;
&lt;li&gt;what projects are useful in learning C++;&lt;/li&gt;
&lt;li&gt;what practices, coding standards you can advise;&lt;/li&gt;
&lt;li&gt;and so on.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Starters
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://github.com/TheLartians/ModernCppStarter"&gt;ModernCppStarter&lt;/a&gt;. Kick-start your C++! A template for modern C++ projects using CMake, CI, code coverage, clang-format, reproducible dependency management and much more.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/cpp-best-practices/gui_starter_template"&gt;gui_starter_template&lt;/a&gt;. This is a C++ Best Practices GitHub template for getting up and running with C++ quickly.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Projects to learn C++
&lt;/h2&gt;

&lt;p&gt;Note to self. If I write an article on the collected information later, I need to check these and other proposed projects with PVS-Studio. &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0762/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=Best_Cpp_Practices&amp;amp;utm_term=0762"&gt;Libraries are different, you know...&lt;/a&gt; I don't want to recommend something potentially buggy as an example to follow.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://github.com/DiligentGraphics/DiligentEngine"&gt;Diligent Engine&lt;/a&gt;. A Modern Cross-Platform Low-Level 3D Graphics Library and Rendering Framework Tweet.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/nlohmann/json"&gt;JSON for Modern C++&lt;/a&gt;. Intuitive syntax. Trivial integration. Serious testing. Memory efficiency. Speed.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/SophistSolutions/Stroika"&gt;Stroika&lt;/a&gt; is a modern, portable, thread-savvy, C++ application framework. It makes writing high performance C++ applications easier by providing safe, flexible, modular building blocks.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/David-Haim/concurrencpp"&gt;concurrencpp&lt;/a&gt;. Modern concurrency for C++. Tasks, executors, timers and C++20 coroutines to rule them all.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/p-ranav/awesome-hpp"&gt;awesome-hpp&lt;/a&gt;. A curated list of awesome header-only C++ libraries.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Speeding up the build
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Here I invite you to read my colleague's article "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0549/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=Best_Cpp_Practices&amp;amp;utm_term=0549"&gt;Speeding up the build of C and C++ projects&lt;/a&gt;".&lt;/li&gt;
&lt;li&gt;There are various &lt;a href="https://stackoverflow.com/questions/373142/what-techniques-can-be-used-to-speed-up-c-compilation-times"&gt;old&lt;/a&gt; and &lt;a href="https://www.reddit.com/r/cpp/comments/hj66pd/c_is_too_slow_to_compile_can_you_share_all_your/"&gt;new&lt;/a&gt; discussions on this topic. I am excited to learn about new, interesting things on this topic from you. Thanks in advance.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Coding standards and style guides
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;a href="https://github.com/isocpp/CppCoreGuidelines"&gt;C++ Core Guidelines&lt;/a&gt; are a collaborative effort led by Bjarne Stroustrup, much like the C++ language itself. They are the result of many person-years of discussion and design across a number of organizations.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/cpp-best-practices/cppbestpractices"&gt;Collaborative Collection of C++ Best Practices&lt;/a&gt;. This online resource is part of Jason Turner's collection of C++ Best Practices resources. By the way, since I mentioned Jason, here's the link to his &lt;a href="https://www.youtube.com/channel/UCxHAlbZQNFU2LgEtiqd2Maw"&gt;C++ Weakly&lt;/a&gt; channel.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://google.github.io/styleguide/cppguide.html"&gt;Google C++ Style Guide&lt;/a&gt;. The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to use C++ language features productively.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Dynamic code analysis
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://github.com/google/sanitizers/wiki/AddressSanitizer"&gt;AddressSanitizer&lt;/a&gt; finds memory errors.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer"&gt;LeakSanitizer&lt;/a&gt; searches for memory leaks.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual"&gt;ThreadSanitizer&lt;/a&gt; detects data races and deadlocks.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/google/sanitizers/wiki/MemorySanitizer"&gt;MemorySanitizer&lt;/a&gt; looks for uninitialized memory.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html"&gt;HWASAN&lt;/a&gt;, or Hardware-assisted AddressSanitizer, a new variant of AddressSanitizer that uses less memory.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html"&gt;UBSan&lt;/a&gt; finds undefined behavior in a program.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Static code analyzers
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Here I will play dumb and mention only our PVS-Studio. Well, why not? It's a wonderful and &lt;a href="https://pvs-studio.com/en/blog/posts/0908/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=Best_Cpp_Practices&amp;amp;utm_term=0908"&gt;powerful static code analyzer&lt;/a&gt;. The tool helps find lots of errors and potential vulnerabilities even at the stage of writing C++ code.&lt;/li&gt;
&lt;li&gt;TODO. I'm also excited to hear about your successful experiences of using other static code analyzers.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Books and other references
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;I always recommend everyone to read Code Complete by Steve McConnell (ISBN 0-7356-1967-0).&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://rigtorp.se/cpp-best-practices/"&gt;C++ Best Practices (2019)&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0391/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=Best_Cpp_Practices&amp;amp;utm_term=0391"&gt;The Ultimate Question of Programming, Refactoring, and Everything&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://micro-os-plus.github.io/develop/sutter-101/"&gt;C++ Coding Standards: 101 Rules, Guidelines, and Best Practices&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt; Scott Meyers. Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd edition).&lt;/li&gt;
&lt;li&gt; Scott Meyers. Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14".&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Now, it's your turn 💥
&lt;/h2&gt;

&lt;p&gt;Please share everything that you consider useful to C++ programmers!&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>productivity</category>
      <category>cpp</category>
      <category>community</category>
    </item>
    <item>
      <title>How has LINQ performance enhanced in .NET 7?</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Wed, 11 Jan 2023 12:18:29 +0000</pubDate>
      <link>https://forem.com/unicorn_developer/how-has-linq-performance-enhanced-in-net-7-m74</link>
      <guid>https://forem.com/unicorn_developer/how-has-linq-performance-enhanced-in-net-7-m74</guid>
      <description>&lt;h1&gt;
  
  
  How has LINQ performance enhanced in .NET 7?
&lt;/h1&gt;

&lt;p&gt;New version of .NET enhanced the performance of the Min, Max, Average and Sum methods for arrays and lists. How much do you think their execution speed has increased? 2x or 5x? No, they got even faster. Let's see how that was achieved. &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.amazonaws.com%2Fuploads%2Farticles%2Fp1pvnpxlm4rh3hxsgw9o.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.amazonaws.com%2Fuploads%2Farticles%2Fp1pvnpxlm4rh3hxsgw9o.png" alt="Image description" width="780" height="548"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How has LINQ enhanced?
&lt;/h2&gt;

&lt;p&gt;LINQ (Language-Integrated Query) is a simple and convenient query language. It allows you to express complex operations in a simple way. Almost every .NET developer uses LINQ. However, this simplicity of use comes at a price of the execution speed and extra memory allocation. In most situations, it has no significant effect. However, in cases when performance is critical, these limitations may be pretty unpleasant. &lt;/p&gt;

&lt;p&gt;So, the recent update has enhanced the performance of the following methods: &lt;em&gt;Enumerable.Max&lt;/em&gt;, &lt;em&gt;Enumerable.Min&lt;/em&gt;, &lt;em&gt;Enumerable.Average&lt;/em&gt;, &lt;em&gt;Enumerable.Sum&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let's see how their performance increased using the following benchmark:&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;using&lt;/span&gt; &lt;span class="n"&gt;BenchmarkDotNet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Attributes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;BenchmarkDotNet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Running&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&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;Collections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Generic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&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;Linq&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;MemoryDiagnoser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;displayGenColumns&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;public&lt;/span&gt; &lt;span class="n"&gt;partial&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&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="n"&gt;Main&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;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;BenchmarkSwitcher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FromAssembly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Program&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Assembly&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Params&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&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;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&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;&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;GlobalSetup&lt;/span&gt;&lt;span class="p"&gt;]&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;Setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Range&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;Size&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;ToArray&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;Benchmark&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Benchmark&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Max&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Max&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Benchmark&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;Average&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Average&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Benchmark&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Sum&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&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;Benchmark results: &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Runtime&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;th&gt;Mean&lt;/th&gt;
&lt;th&gt;Ratio&lt;/th&gt;
&lt;th&gt;Allocated&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Min&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;75.491 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;32 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Min&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;7.749 ns&lt;/td&gt;
&lt;td&gt;0.10&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;71.128 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;32 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;6.493 ns&lt;/td&gt;
&lt;td&gt;0.09&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Average&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;68.963 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;32 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Average&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;7.315 ns&lt;/td&gt;
&lt;td&gt;0.11&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sum&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;69.509 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;32 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sum&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;9.058 ns&lt;/td&gt;
&lt;td&gt;0.13&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Min&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;61,567.392 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;32 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Min&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;2,967.947 ns&lt;/td&gt;
&lt;td&gt;0.05&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;56,106.592 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;32 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;2,948.302 ns&lt;/td&gt;
&lt;td&gt;0.05&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Average&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;52,803.907 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;32 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Average&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;2,967.810 ns&lt;/td&gt;
&lt;td&gt;0.06&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sum&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;52,732.121 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;32 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sum&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;5,897.220 ns&lt;/td&gt;
&lt;td&gt;0.11&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The results show that the execution time for finding the minimum element of an array has generally decreased. In 10 times for small arrays and 20 times for arrays containing 10,000 elements. Similarly, for other methods (except for finding the sum, the difference between the sizes of the collections did not affect the results much).&lt;/p&gt;

&lt;p&gt;It is also worth noting that in .NET 7 no additional memory is allocated when methods are called. &lt;/p&gt;

&lt;p&gt;Let's check how these methods work with &lt;em&gt;List&lt;/em&gt;. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Runtime&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;th&gt;Mean&lt;/th&gt;
&lt;th&gt;Ratio&lt;/th&gt;
&lt;th&gt;Allocated&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Min&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;122.554 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;40 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Min&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;8.995 ns&lt;/td&gt;
&lt;td&gt;0.07&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;115.135 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;40 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;9.171 ns&lt;/td&gt;
&lt;td&gt;0.08&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Average&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;110.825 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;40 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Average&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;8.163 ns&lt;/td&gt;
&lt;td&gt;0.07&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sum&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;113.812 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;40 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sum&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;13.197 ns&lt;/td&gt;
&lt;td&gt;0.12&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Min&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;91,529.841 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;40 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Min&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;2,941.226 ns&lt;/td&gt;
&lt;td&gt;0.03&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;84,565.787 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;40 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Max&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;2,957.451 ns&lt;/td&gt;
&lt;td&gt;0.03&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Average&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;81,205.103 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;40 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Average&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;2,959.882 ns&lt;/td&gt;
&lt;td&gt;0.04&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sum&lt;/td&gt;
&lt;td&gt;.NET 6.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;81,857.576 ns&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;40 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sum&lt;/td&gt;
&lt;td&gt;.NET 7.0&lt;/td&gt;
&lt;td&gt;10000&lt;/td&gt;
&lt;td&gt;5,783.370 ns&lt;/td&gt;
&lt;td&gt;0.07&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In .NET 6, all operations on arrays much faster than on lists. The same is true for small collections in .NET 7. As the number of elements increases, lists are equal to arrays in performance.&lt;/p&gt;

&lt;p&gt;According to the test results, lists' performance increased by 31 times.&lt;/p&gt;

&lt;h2&gt;
  
  
  But how could this be achieved?
&lt;/h2&gt;

&lt;p&gt;Let's take a closer look at the implementation of the &lt;em&gt;Min&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;That's how the &lt;em&gt;Min&lt;/em&gt; method is implemented in .NET 6:&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="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&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;&lt;/span&gt; &lt;span class="n"&gt;source&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;source&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;ThrowHelper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ThrowArgumentNullException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ExceptionArgument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;source&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;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IEnumerator&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;&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetEnumerator&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MoveNext&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;ThrowHelper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ThrowNoElementsException&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MoveNext&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&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;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;value&lt;/span&gt; &lt;span class="o"&gt;=&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="k"&gt;return&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method is quite simple. We get the &lt;em&gt;IEnumerable&lt;/em&gt; collection, take the collection element and use the &lt;em&gt;MoveNext&lt;/em&gt; method to get the next element. We compare them, save the one that is smaller, and repeat until the end of the collection. &lt;/p&gt;

&lt;p&gt;The new version of the &lt;em&gt;Min&lt;/em&gt; method is different:&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="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&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;&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;MinInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&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;em&gt;MinInteger&lt;/em&gt; method is applied to a collection of integers. Let's examine it in more details.&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;T&lt;/span&gt; &lt;span class="n"&gt;MinInteger&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="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&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;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;where&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IBinaryInteger&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="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;T&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;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TryGetSpan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="n"&gt;ReadOnlySpan&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;span&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;Vector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsHardwareAccelerated&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; 
        &lt;span class="n"&gt;span&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;&amp;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="n"&gt;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;Count&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&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 implementation&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;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt; &lt;span class="c1"&gt;//Implementation as in .NET 6&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Primarily, we try to get the object of the &lt;em&gt;ReadOnlySpan&lt;/em&gt; type from the provided collection. If we fail to get the &lt;em&gt;ReadOnlySpan&lt;/em&gt; collection representation, then the code branch (that matches the implementation of the &lt;em&gt;Min&lt;/em&gt; method in .NET 6) is executed. In our case, we can get &lt;em&gt;ReadOnlySpan&lt;/em&gt;, since we use arrays and lists.&lt;/p&gt;

&lt;p&gt;But what is &lt;em&gt;ReadOnlySpan&lt;/em&gt; actually? The &lt;em&gt;Span&lt;/em&gt; and &lt;em&gt;ReadOnlySpan&lt;/em&gt; types provide safe representation of a continuous managed and unmanaged memory area. The structure of the &lt;em&gt;Span&lt;/em&gt; type is defined as a &lt;em&gt;ref struct.&lt;/em&gt; This means that it can only be placed on the stack, which makes it possible to avoid allocating additional memory and improves data performance. &lt;/p&gt;

&lt;p&gt;The* Span* type has also undergone some changes in the new version of C#. Since C# 11 introduced the option to create reference fields in a &lt;em&gt;ref struct&lt;/em&gt;, the internal representation of &lt;em&gt;Span&lt;/em&gt; has changed. Previously, a special internal type — &lt;em&gt;ByReference&lt;/em&gt;, was used to store a reference to the beginning of the memory area, but there was no security check in it. Now &lt;em&gt;ref fields&lt;/em&gt; are used for this purpose. It provides a more secure memory operation. You can read more about other changes in C# 11 in the &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/1002/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1011&amp;amp;utm_term=1002" rel="noopener noreferrer"&gt;article&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;).&lt;/p&gt;

&lt;p&gt;But let's get back to the &lt;em&gt;Min&lt;/em&gt; method. When you get &lt;em&gt;ReadOnlySpan&lt;/em&gt;, the method tries to perform a vector search using the &lt;em&gt;Vector&lt;/em&gt; type. To do this, the following condition should be met:&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;Vector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsHardwareAccelerated&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;span&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;&amp;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="n"&gt;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;Count&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first part of the condition checks whether the Vector.IsHardwareAccelerated property returns true. Let's take a look at the implementation of this property.&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="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;IsHardwareAccelerated&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Intrinsic&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;[Intrinsic]&lt;/em&gt; attribute is applied to the getter. The attribute indicates that the value returned by &lt;em&gt;IsHardwareAccelerated&lt;/em&gt; can be replaced JIT. The property returns &lt;em&gt;true&lt;/em&gt; if hardware acceleration can be applied to operations on vectors through built-in JIT support, otherwise &lt;em&gt;false&lt;/em&gt; is returned. To enable hardware acceleration, you need to run the build for the x64 platform with the Release configuration or build the project for AnyCPU with the "Prefer 32-bit" setting disabled. &lt;/p&gt;

&lt;p&gt;To fulfill the second part of the condition, the size of &lt;em&gt;Span&lt;/em&gt; should be at least 2 times the size of the vector.&lt;/p&gt;

&lt;p&gt;How is the size of this vector calculated? &lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Vector&lt;/em&gt; data type is used in the new implementation of the &lt;em&gt;Min&lt;/em&gt; method to create a vector. This type is a wrapper for three other types: &lt;em&gt;Vector64&lt;/em&gt;, &lt;em&gt;Vector128&lt;/em&gt; and &lt;em&gt;Vector256&lt;/em&gt;. They contain vectorized data of the corresponding length. &lt;em&gt;Vector128&lt;/em&gt; can store 16 8-bit, 8 16-bit, 4 32-bit or 2 64-bit values. The type to be used is selected depending on whether it is supported by the processor or not. &lt;/p&gt;

&lt;p&gt;Thus, if the &lt;em&gt;Vector128&lt;/em&gt; type is used when executing the method, then the &lt;em&gt;Span&lt;/em&gt; type obtained from the array or list must contain 8 or more elements of the &lt;em&gt;int&lt;/em&gt; type.&lt;/p&gt;

&lt;p&gt;If all conditions are met, the method uses the advantages of the &lt;em&gt;ReadOnlySpan&lt;/em&gt; and &lt;em&gt;Vector&lt;/em&gt; types to optimize finding the minimum:&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;T&lt;/span&gt; &lt;span class="n"&gt;MinInteger&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="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&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;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;where&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IBinaryInteger&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="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;Vector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsHardwareAccelerated&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;span&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;&amp;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="n"&gt;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;Count&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&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;mins&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;Vector&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;span&lt;/span&gt;&lt;span class="p"&gt;);&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;Vector&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;mins&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;Min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mins&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;Vector&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;span&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;index&lt;/span&gt;&lt;span class="p"&gt;)));&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;Vector&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&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="n"&gt;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;Count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;span&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;);&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;mins&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;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;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;lt;&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="n"&gt;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;Count&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mins&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;&amp;lt;&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;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mins&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="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;Let's explore how the vectorized implementation of finding a minimum works. A vector is created from the first N &lt;em&gt;Span&lt;/em&gt; elements (N depends on the type of vector; for &lt;em&gt;Vector128&lt;/em&gt; it is 4 elements). This vector is compared with a vector of the following N elements in &lt;em&gt;Vector.Min&lt;/em&gt;. The resulting vector contains the minimum value of each pair of elements of the two given vectors. Then the next part of &lt;em&gt;Span&lt;/em&gt; is taken, and the search continues. You only need to find the minimum value among those stored in the resulting vector. The advantage of using a vector is that all operations on its elements occur simultaneously. &lt;/p&gt;

&lt;p&gt;An example of using &lt;em&gt;Span&lt;/em&gt; and vectorization to optimize the &lt;em&gt;Min&lt;/em&gt; method is shown above. But what about the other methods? You can meet similar features for the &lt;em&gt;Max&lt;/em&gt;, &lt;em&gt;Average&lt;/em&gt;, &lt;em&gt;Sum&lt;/em&gt; methods. The optimized versions of these methods are available for arrays and lists of &lt;em&gt;int&lt;/em&gt;, &lt;em&gt;long&lt;/em&gt;, &lt;em&gt;float&lt;/em&gt;, &lt;em&gt;double&lt;/em&gt; and &lt;em&gt;decimal&lt;/em&gt; types.&lt;/p&gt;

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

&lt;p&gt;Microsoft developers used &lt;em&gt;Span&lt;/em&gt; and hardware acceleration to work with vectors. As a result, the .NET community brought visible enhancement to LINQ performance. Now we can use advanced methods in cases where the speed of code execution, and allocated memory are critical. &lt;/p&gt;

&lt;p&gt;Actually, .NET 7 got a few other performance improvements. You can read about them in &lt;a href="https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/" rel="noopener noreferrer"&gt;.NET Blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>learning</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Christmas holidays with PVS-Studio</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 23 Dec 2022 13:03:00 +0000</pubDate>
      <link>https://forem.com/unicorn_developer/christmas-holidays-with-pvs-studio-k6e</link>
      <guid>https://forem.com/unicorn_developer/christmas-holidays-with-pvs-studio-k6e</guid>
      <description>&lt;p&gt;Christmas holidays are the most beloved and cherished ones all over the world. Gifts, decorations, family and friends all gathered together — these are the essentials of Christmas and New Year's buzz. Our team was also caught up in this. In the eve of the Christmas holidays, we have prepared various treats and gifts for you. Continue reading to find out which ones!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_0l0qiV1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6whhuwxfbklo63ea42qt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_0l0qiV1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6whhuwxfbklo63ea42qt.png" alt="Image description" width="780" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The quiz: Save Christmas from bugs
&lt;/h2&gt;

&lt;p&gt;Based on the feedback we got after releasing previous quizzes, we can safely say that our audience likes this format. That's why we decided to make a Christmas special for you. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9JypSrC---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sco2t2uzx2be6w6csd63.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9JypSrC---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sco2t2uzx2be6w6csd63.png" alt="Image description" width="780" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When there are Christmas lovers, there are also Christmas haters. And they'd love to spoil all these celebrations. In our case, these haters are bugs. We prepared 8 stories for you — each of them tells about bugs that plotted against well-known companies. Help us find bugs disguised in these cases, figure out what they did and what havoc they caused. Christmas mishaps, treacherous bugs, terrifying (and not) consequences, and, of course, gifts — they are waiting for you in our Christmas quiz. Well, are you ready? &lt;a href="https://pvs-studio.com/en/blog/quest/save_the_holidays/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1018&amp;amp;utm_term=quest_save_the_holidays"&gt;Let's save the holidays!&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The game: Endless coding
&lt;/h2&gt;

&lt;p&gt;Have you ever thought that your work day is like a video game? Deadlines, coffee breaks, bugs — you can find them all in &lt;a href="https://pvs-studio.com/en/blog/quest/endless/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1018&amp;amp;utm_term=quest_endless"&gt;"Endless coding"&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Avoid obstacles like technical debt, bugs, burning deadlines, etc. Catch buffs in bubbles — cups of coffee, donuts, pay raise and promotion, or help from PVS-Studio. Looks just like an ordinary work day, doesn't it?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nKGHWwEh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmi1f2ejgj5qfcbcj39j.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nKGHWwEh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmi1f2ejgj5qfcbcj39j.gif" alt="Image description" width="780" height="830"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take the highest place in the leaderboard and prove your team leader that you're not playing games here😊!&lt;/p&gt;

&lt;p&gt;Players who score the highest points will get a nice bonus in the end!&lt;/p&gt;

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

&lt;p&gt;Time doesn't wait for anyone — 2023 came in the blink of an eye. In order to keep up with the times, the PVS-Studio team decided to get to know our newcomers as well as have a chat with our regulars. We want to get better for our users and we want to know what matters the most for you. So, tell us about this — &lt;a href="https://pvs-studio.com/en/quiz/static_analyzer/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1018&amp;amp;utm_term=quiz_static_analyzer"&gt;take a survey&lt;/a&gt;. We appreciate any feedback.&lt;/p&gt;

&lt;p&gt;A gift will be waiting for you at the end of it. &lt;/p&gt;

&lt;p&gt;Thank you for being with us all these years. After all, we enhance and develop our tool for you.&lt;/p&gt;

&lt;p&gt;Happy holidays! See you in 2023! ✨ &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K0xi1U6t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l017eb4gq6hpyohbrljs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K0xi1U6t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l017eb4gq6hpyohbrljs.png" alt="Image description" width="780" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>challenge</category>
      <category>programming</category>
      <category>community</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>Save Christmas from Bug!</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Wed, 21 Dec 2022 08:02:17 +0000</pubDate>
      <link>https://forem.com/unicorn_developer/save-christmas-from-bug-3f8d</link>
      <guid>https://forem.com/unicorn_developer/save-christmas-from-bug-3f8d</guid>
      <description>&lt;p&gt;Anyone in Bug's shoes would turn green with envy and go mad. As a true introvert, he lives in the depths of the code and hides far away from developers, reviewers, testers, and everyone else 🐞 But these "everyone else" are preparing the biggest Christmas party ever 🎄🎅🎁 They make a fuss over the holidays, decorate everything they can decorate and make Bug so angry! He will not resist the urge of ruining their holidays. &lt;strong&gt;Bug decides to steal Christmas&lt;/strong&gt; 😱&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't let Bug ruin the holidays! &lt;a href="https://pvs-studio.com/go.php?url=1941"&gt;Take the quiz&lt;/a&gt; to save Christmas from the offended winged beast 🦸&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FtH6DPJj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omtl425b4yzecc4jyahw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FtH6DPJj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omtl425b4yzecc4jyahw.png" alt="Image description" width="218" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>community</category>
      <category>challenge</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>A software bug captured Apple and other huge companies</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Wed, 07 Dec 2022 14:59:05 +0000</pubDate>
      <link>https://forem.com/unicorn_developer/a-software-bug-captured-apple-and-other-huge-companies-459g</link>
      <guid>https://forem.com/unicorn_developer/a-software-bug-captured-apple-and-other-huge-companies-459g</guid>
      <description>&lt;p&gt;We collected some hot stories about programming errors for you to have a little fun and learn something new. Keep reading to find out how one programmer broke the Internet by deleting a tiny piece of code and see other not-so-obvious 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.amazonaws.com%2Fuploads%2Farticles%2Fcvef3vkmj1ki6nuw37to.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.amazonaws.com%2Fuploads%2Farticles%2Fcvef3vkmj1ki6nuw37to.png" alt="Image description" width="780" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;"Error" is something that can scare even experienced developers. Everyone faces them at some point. Today you'll see that everyone may screw up, and mistakes shouldn't frustrate you. We have already published related articles — &lt;a href="https://pvs-studio.com/en/blog/posts/0463/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1008&amp;amp;utm_term=0463" rel="noopener noreferrer"&gt;Stories about Christmas and New Year Bugs&lt;/a&gt; and others are listed in the end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Who wants to be a millionaire: the Chilean version
&lt;/h2&gt;

&lt;p&gt;This story is about the error that turned the Chilean man's life around. In May, 2022, a man in Chile was accidentally paid 165 million pesos (£150,000), 330 times his salary. This happened due a payroll blunder error.&lt;/p&gt;

&lt;p&gt;The worker reported about the error to the deputy manager. The manager asked the worker to go to his bank and initiate the return of the excess money. You could think the man got back the money, and the story was over. But it wasn't.&lt;/p&gt;

&lt;p&gt;Well, the man promised his employer to show up in the bank and get the amount refunded. However, a day later, he was off the radar. After two days, he appeared and promised to retrieve the amount soon — probably, he was still on the fence :). Diario Financiero reported that, the worker later sent a resignation letter via his attorney, and dropped out of the sight. &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.amazonaws.com%2Fuploads%2Farticles%2Fr7ccbdtvvclut3jdnw7l.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.amazonaws.com%2Fuploads%2Farticles%2Fr7ccbdtvvclut3jdnw7l.png" alt="Image description" width="486" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The company he was employed has now reached out to various agencies in attempt to get back the money and registered a complaint of misappropriating funds against the employee. Let's hope this story keeps going.&lt;/p&gt;

&lt;h2&gt;
  
  
  The £50 million electricity bill
&lt;/h2&gt;

&lt;p&gt;A couple from Lancashire were shocked when their monthly electricity bill had increased from £87 to £53,480,062. "Unfortunately, I don't have this kind of money in my account. It was a massive shock," Mr. Brotherton, a 63-year-old accountant, said.&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.amazonaws.com%2Fuploads%2Farticles%2F209wd3dsmnv9u65hslet.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.amazonaws.com%2Fuploads%2Farticles%2F209wd3dsmnv9u65hslet.png" alt="Image description" width="420" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nigel and Linda Brotherton had a new electricity meter installed, but an electrician didn't connect the wires correctly. This led to a wrong measure of the electricity usage. Due to this mistake, their power supplier calculated the bill and sent it to their bank account. &lt;/p&gt;

&lt;p&gt;"My wife was worried if they had tried to take the money it could have affected our credit rating," Mr. Brotherton said. After the recovering from initial shock, the couple contacted the power supplier, who apologized and promised to correct the wires.&lt;/p&gt;

&lt;h2&gt;
  
  
  iPhone's first glitches a month after release
&lt;/h2&gt;

&lt;p&gt;The accident took place a week after iPhone 14 Pro was released. Some owners have reported that their iPhones cause camera shaking when they use TikTok, Instagram, and Snapchat.&lt;/p&gt;

&lt;p&gt;Multiple users tweeted the videos of how it looked like — opening the camera in certain apps causes it to shake and produce audible grinding sounds. That's quite a creepy thing. However, if you use the built-in camera, nothing is wrong. &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.amazonaws.com%2Fuploads%2Farticles%2F50nmt63oeojdlfv84bqk.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.amazonaws.com%2Fuploads%2Farticles%2F50nmt63oeojdlfv84bqk.png" alt="Image description" width="593" height="809"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Apple reported that the problem was caused by the software issue and not by the third-party applications. Apple spokesperson Alex Kirschner stated in his email to The Verge, that the company is "aware of the issue and a fix will be released next week". However, Apple didn't explain what exactly had caused the error, and why some users encountered this problem and others did not. &lt;/p&gt;

&lt;p&gt;Apple introduced a new feature that could actually help you in car accidents — if your iPhone 14 detects a severe car crash, it can help you contact emergency services and notify your emergency contacts. That's impressive. But what if an accident takes place on rollercoasters? "You're raving, dude", — you think. Well, the iPhone 14 is sure it's possible.&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.amazonaws.com%2Fuploads%2Farticles%2F9p3bslpwtua1vcfqxf7f.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.amazonaws.com%2Fuploads%2Farticles%2F9p3bslpwtua1vcfqxf7f.png" alt="Image description" width="780" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The car accident detection feature has set off false alarms for emergency personnel near Cincinnati's Kings Island amusement park. The same crash detection technology is also featured on the Apple Watch 8. Officials say the fix is simple: Putting the iPhone 14 and the Apple Watch 8 on airplane mode before boarding a rollercoaster. So, here is the burning question — should we expect signs in the amusement park like "turn off your iPhones before you get on the rollercoaster?"&lt;/p&gt;

&lt;p&gt;Let's hope that there are only two glitches in the latest iPhone model and there would be nothing that could dishonor the company's impeccable reputation.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the Windows developer stole 2022 year
&lt;/h2&gt;

&lt;p&gt;On 2021 New Year's Eve, the Windows team decided to wish their Twitter followers a Happy New Year. The official Windows Developer account tweeted the following:&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.amazonaws.com%2Fuploads%2Farticles%2F5pwi4w2dyc2l8tm5tr0l.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.amazonaws.com%2Fuploads%2Farticles%2F5pwi4w2dyc2l8tm5tr0l.png" alt="Image description" width="595" height="616"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's C# console code. If you run this app when it's exactly midnight on New Year's Eve, you'll get a "Happy New Year!" message. But if you run it at any other time, it will show you that it's still 2021 — that's how this app should work. But it didn't deliver the expected result. &lt;/p&gt;

&lt;p&gt;There is a problem — you can get the New Year greeting at exactly midnight. I mean you will fail to get the greeting if it is one second into 2022 or one second before 2022. Moreover, if it's 2022 now, the app will tell you that it's still 2021 — no matter what the date or time is. It works only for midnight...&lt;/p&gt;

&lt;p&gt;The Windows developer realized the error and deleted the tweet. You can watch &lt;a href="https://youtu.be/Y195oMMLlqM" rel="noopener noreferrer"&gt;Scott Hanselman's overly-detailed analysis of the many problems with this code&lt;/a&gt;. If you're an early-career developer, you might find this interesting.&lt;/p&gt;

&lt;h2&gt;
  
  
  The "left-pad" havoc or how a programmer broke the Internet by deleting a tiny piece of code
&lt;/h2&gt;

&lt;p&gt;In March, 2016, Azer Koçulu, a programmer in Oakland, California broke the Internet around the world by deleting 11 lines of code. Koçulu published his code on &lt;a href="https://www.npmjs.com/about" rel="noopener noreferrer"&gt;npm&lt;/a&gt;, a popular tool among JavaScript developers that is used to find and install open-source software.&lt;/p&gt;

&lt;p&gt;Koçulu wrote the project and called it "kik". This project was intended to help programmers set up templates for their projects. By chance, the messaging app created in Ontario, Canada, shares the name with the Koçulu's project. On March 11, Koçulu received an email from Bob Stratton, a trademark agent of Kik. Bob Stratton asked Koçulu to change the project's name for legal reasons. Koçulu rejected his suggestion. &lt;a href="https://medium.com/@mproberts/a-discussion-about-the-breaking-of-the-internet-3d4d2a83aa4d" rel="noopener noreferrer"&gt;Here&lt;/a&gt; you can find the whole discussion (and find out why two parts failed to reach an agreement). After that, Koçulu intended to take down the packages he had registered on npm. "I think I have the right of deleting all my stuff from NPM" was the email, that ended up the dialogue between Koçulu and Bob Stratton.&lt;/p&gt;

&lt;p&gt;After a few days, JavaScript programmers around the world started receiving a strange error message when they tried to run their 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;Npm&lt;/span&gt; &lt;span class="n"&gt;ERR&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;pad&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;npm&lt;/span&gt; &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The point is, the code they were trying to run required a package called &lt;em&gt;left-pad&lt;/em&gt;, but the npm registry didn't have it. After that, developers were turned to the GitHub repository, where the left-pad was supported. The code that they found looks 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;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;leftpad&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;leftpad&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&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;ch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&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;str&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;i&lt;/span&gt; &lt;span class="o"&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="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;ch&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;ch&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;ch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&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;len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Return&lt;/span&gt; &lt;span class="n"&gt;str&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;There are only eleven code lines. This is a single-purpose function, simple enough for most developers to write themselves, but lots of npm packages relied on &lt;em&gt;left-pad&lt;/em&gt; to do it for them, which is how this tiny bit of code became so important, but unknown.&lt;/p&gt;

&lt;p&gt;Some of the largest, most widely used npm packages platforms have suddenly been broken. &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React&lt;/a&gt;, a JavaScript library for building user interfaces was one of the affected packages. And many smaller websites such as Quartz's own &lt;a href="https://qz.com/434425/atlas-the-new-home-for-charts-and-data/" rel="noopener noreferrer"&gt;Atlas&lt;/a&gt;. Its absence has been felt around the world — developers from Australia, Germany, the United States, and the Czech Republic commented on the left-pad page on GitHub. &lt;/p&gt;

&lt;p&gt;In Ontario, where the problem occurred, Kik's developers had left-pad problems as well. Two hours later, they added these 11 lines of code to the npm registry. This situation proved to be a valuable lesson for developers. It helped developers understand that wide interests are more important than the wishes of one author. &lt;/p&gt;

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

&lt;p&gt;I hope that this article set you thinking that mistakes are no big deal. If you are still afraid of programming errors, there are various tools to protect yourself. For example, our company — PVS-Studio — develops the static analyzer that can easily detect even ambiguous programming errors. By the way, we have collected various errors found in open-source projects as well. There are Chromium, Linux core, Tizen OS and even Windows Calculator projects listed, you may find it interesting to look at &lt;a href="https://pvs-studio.com/en/blog/inspections/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1008&amp;amp;utm_term=inspections" rel="noopener noreferrer"&gt;someone else's mistakes&lt;/a&gt; :). &lt;/p&gt;

&lt;p&gt;Let me end up with a phrase from The Simpsons episode, where Lenny says: "Everyone makes mistakes, that's why pencils have erasers".&lt;/p&gt;

&lt;p&gt;PS: didn't the errors seem funny enough to you? Then here is a collection of hilarious memes about software errors that people have encountered in their daily lives:&lt;/p&gt;

&lt;p&gt;1. How?&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.amazonaws.com%2Fuploads%2Farticles%2Fdhwfrh2ql9lspyqolhad.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.amazonaws.com%2Fuploads%2Farticles%2Fdhwfrh2ql9lspyqolhad.png" alt="Image description" width="700" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2. What the hell did I do&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.amazonaws.com%2Fuploads%2Farticles%2Ft4bmj948ywzbmu0tuedz.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.amazonaws.com%2Fuploads%2Farticles%2Ft4bmj948ywzbmu0tuedz.png" alt="Image description" width="700" height="1245"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3. Well, great...&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.amazonaws.com%2Fuploads%2Farticles%2Fx5o5tvv5o49ebg0z92uh.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.amazonaws.com%2Fuploads%2Farticles%2Fx5o5tvv5o49ebg0z92uh.png" alt="Image description" width="700" height="397"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4. Scuber Diver&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.amazonaws.com%2Fuploads%2Farticles%2F1d20ldof0lv2nbcbsaxt.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.amazonaws.com%2Fuploads%2Farticles%2F1d20ldof0lv2nbcbsaxt.png" alt="Image description" width="700" height="655"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5. This is getting ridiculous&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.amazonaws.com%2Fuploads%2Farticles%2F1h3mnbxdildei2hcau1r.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.amazonaws.com%2Fuploads%2Farticles%2F1h3mnbxdildei2hcau1r.png" alt="Image description" width="700" height="547"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;6. I don't really know what to say&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.amazonaws.com%2Fuploads%2Farticles%2Fjr1ik9evi3v4ksqh5d23.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.amazonaws.com%2Fuploads%2Farticles%2Fjr1ik9evi3v4ksqh5d23.png" alt="Image description" width="700" height="572"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;7. Homonyms are such a bliss to a Virtual Assistant&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.amazonaws.com%2Fuploads%2Farticles%2Fmp86yhumfkffgn4qf20i.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.amazonaws.com%2Fuploads%2Farticles%2Fmp86yhumfkffgn4qf20i.png" alt="Image description" width="700" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;8. Oh no, my PC ran away&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.amazonaws.com%2Fuploads%2Farticles%2Fa4nbrq838tezzmfwcq5k.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.amazonaws.com%2Fuploads%2Farticles%2Fa4nbrq838tezzmfwcq5k.png" alt="Image description" width="700" height="588"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The pictures were originally published on boredpanda.com.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/0439/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1008&amp;amp;utm_term=0439" rel="noopener noreferrer"&gt;Toyota: 81 514 issues in the code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/0438/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1008&amp;amp;utm_term=0438" rel="noopener noreferrer"&gt;Killer Bug. Therac-25: Quick-and-Dirty&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/0463/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1008&amp;amp;utm_term=0463" rel="noopener noreferrer"&gt;Stories about Christmas and New Year Bugs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/0462/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1008&amp;amp;utm_term=0462" rel="noopener noreferrer"&gt;The First Bug on Mars&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0426/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1008&amp;amp;utm_term=0426" rel="noopener noreferrer"&gt;A space error: 370.000.000 $ for an integer overflow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/0479/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1008&amp;amp;utm_term=0479" rel="noopener noreferrer"&gt;Bugs from the USSR&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Catastrophic backtracking: how can a regular expression cause a ReDoS vulnerability?</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Wed, 30 Nov 2022 14:47:54 +0000</pubDate>
      <link>https://forem.com/unicorn_developer/catastrophic-backtracking-how-can-a-regular-expression-cause-a-redos-vulnerability-aia</link>
      <guid>https://forem.com/unicorn_developer/catastrophic-backtracking-how-can-a-regular-expression-cause-a-redos-vulnerability-aia</guid>
      <description>&lt;p&gt;Regular expressions come in handy when you need to search for and replace text. However, in some cases, they may cause the system to slow down or even make vulnerable to ReDoS attacks. &lt;/p&gt;

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

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;ReDoS is a subtype of a &lt;a href="https://pvs-studio.com/en/blog/terms/6585/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1007" rel="noopener noreferrer"&gt;DoS attack&lt;/a&gt;. The aim of a ReDoS attack is to halt an application or cause it to slow down via an inefficient regex.&lt;/p&gt;

&lt;p&gt;ReDoS attacks can be divided into two types:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A string with a malicious pattern is passed to an application. Then this string is used as a regex, which leads to ReDoS.&lt;/li&gt;
&lt;li&gt;A string of a certain format is passed to an application. Then this string is evaluated by a vulnerable regex, which leads to ReDoS.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The main point of any ReDoS attack is using a vulnerable regular expression in an application. Passing a string of a certain format to a regex leads to its unreasonably long calculation.&lt;/p&gt;

&lt;p&gt;If a ReDoS attack is successful, then the regex calculation results in catastrophic backtracking. It is a consequence of the backtracking function in the Regex engine, which iterates through possible string matches until it finds the correct one. If there's no correct match, a regular expression won't stop until it iterates through all possible options. A complete iteration of all possible options leads to an unacceptably long calculation of a regex. This is called catastrophic backtracking. &lt;/p&gt;

&lt;p&gt;A regex is vulnerable to catastrophic backtracking if it contains at least one subexpression that may cause a large number of matching options.&lt;/p&gt;

&lt;h2&gt;
  
  
  Catastrophic backtracking: real examples
&lt;/h2&gt;

&lt;p&gt;Let's inspect several regular expressions for vulnerabilities.&lt;/p&gt;

&lt;p&gt;I wrote a small program — it displays a graph of how the calculation time of regex depends on the number of characters in the evaluated string. In next examples, I will use this program to show you the catastrophic backtracking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's look at a simple synthetic 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="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="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's compare the calculation time of the &lt;em&gt;(x+)+y&lt;/em&gt; expression in two cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The input to the regular expression accepts strings corresponding to the specified pattern one by one. At the same time the length of each subsequent string is more than the previous one by 1.&lt;/li&gt;
&lt;li&gt;The input to the regular expression accepts strings that do not match the pattern (there is no &lt;em&gt;y&lt;/em&gt; character at the end of the string). At the same time the length of each subsequent string is more than the previous one by 1.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The results of such an experiment are below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5jpbo7ovkimfoc4528mc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5jpbo7ovkimfoc4528mc.png" alt="Figure 1\. The execution time of a regular expression if a string matches the pattern \(*x\+\)\+y*\."&gt;&lt;/a&gt;&lt;br&gt;
Figure 1. The execution time of a regular expression if a string matches the pattern (&lt;em&gt;x+)+y&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcbvhdiagl6cw3ke6f3jm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcbvhdiagl6cw3ke6f3jm.png" alt="Figure 2\. The execution time of a regular expression if a string does not match the *\(x\+\)\+y* pattern \(the *y* character is missing at the end\)\."&gt;&lt;/a&gt;&lt;br&gt;
Figure 2. The execution time of a regular expression if a string does not match the &lt;em&gt;(x+)+y&lt;/em&gt; pattern (the &lt;em&gt;y&lt;/em&gt; character is missing at the end).&lt;/p&gt;

&lt;p&gt;As you can see, the strings from the first set are processed instantly. However, the processing of the second set increases exponentially! Why so?&lt;/p&gt;

&lt;p&gt;The thing is, in the first case a regular expression finds a match on the first try. When processing strings in the second case, everything becomes very complicated. The &lt;em&gt;x+&lt;/em&gt; template can match any number of &lt;em&gt;x&lt;/em&gt; characters. The &lt;em&gt;(x+)+&lt;/em&gt; template can fit a string that consists of one or more substrings corresponding with &lt;em&gt;x+&lt;/em&gt;. Because of this, there are many options for matching a string with a regular expression. Their number depends on the length of a substring consisting of &lt;em&gt;x&lt;/em&gt; characters. Every time the regular expression does not find the &lt;em&gt;y&lt;/em&gt; character, it starts checking the next option. Only after checking all of them, the regular expression gives the answer – no matches were found.&lt;/p&gt;

&lt;p&gt;The table below shows several possible matches of the &lt;em&gt;xxxx&lt;/em&gt; string with the &lt;em&gt;(x+)+y&lt;/em&gt; regex:&lt;/p&gt;

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

&lt;p&gt;Fortunately, not all regular expressions are vulnerable to catastrophic backtracking. A regex is vulnerable if it meets the following conditions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There are two subexpressions, and one of them includes another. Besides, one of the following quantifiers is applied to each of them: '*', '+', '*?', '+?', '{...}'. In the previous example, the &lt;em&gt;(x+)+&lt;/em&gt; subexpression includes &lt;em&gt;x+&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;There is a string that can be matched with both subexpressions. For example, the &lt;em&gt;xxxx&lt;/em&gt; string may fit both the &lt;em&gt;x+&lt;/em&gt; and &lt;em&gt;(x+)+&lt;/em&gt; templates.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Expressions of the &lt;em&gt;(\d?|....|[1-9])+&lt;/em&gt; type are a small exception. Here the &lt;em&gt;(\d?|....|[1-9])+&lt;/em&gt; expression includes subexpressions &lt;em&gt;\d?&lt;/em&gt; and &lt;em&gt;[1-9]&lt;/em&gt;. They are enumerated via the '|' operator. These subexpressions can also fit the same string, for example, &lt;em&gt;111&lt;/em&gt;. In this case, applying the '?' quantifier to one of the subexpressions also results in a vulnerability. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We found out that the &lt;em&gt;(x+)+y&lt;/em&gt; expression is vulnerable. Now let's change it a bit by adding a check for the presence of another character:&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;x&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="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have the &lt;em&gt;(x+z)+&lt;/em&gt; subexpression, and the &lt;em&gt;xz&lt;/em&gt; and &lt;em&gt;xxxxz&lt;/em&gt; strings can be matched to this expression. This subexpression includes the &lt;em&gt;x+&lt;/em&gt; subexpression, which can correspond to strings of &lt;em&gt;x&lt;/em&gt;, &lt;em&gt;xxxx&lt;/em&gt;, etc. As you can see, these subexpressions cannot be matched with the same values. Thus, the second condition is not met and there's no catastrophic backtracking.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7x7hbany5sjlce82qd0n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7x7hbany5sjlce82qd0n.png" alt="Figure 3\. Unsuccessful attempt to "&gt;&lt;/a&gt;&lt;br&gt;
Figure 3. Unsuccessful attempt to "break" a regex with a set of strings, each of them corresponds with either the &lt;em&gt;x+&lt;/em&gt; subexpression or the &lt;em&gt;(x+z)+&lt;/em&gt; subexpression.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now let's look at the next regex:&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;newDate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;-?&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;d&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="err"&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 regex has a task — search for a substring of the &lt;em&gt;newDate(12-09-2022)&lt;/em&gt; type. Can we call this regex safe? No. Besides correct strings, the regex will consider correct the &lt;em&gt;newDate(8-911-111-11-11)&lt;/em&gt; and even &lt;em&gt;newDate(1111111111111)&lt;/em&gt; strings. However, to understand the essence of the issue, such an expression will be enough for us.&lt;/p&gt;

&lt;p&gt;None of the above options will lead to catastrophic backtracking. However, it will happen if we process strings of the 'newDate(1111111111111' type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkqyngas51eg6ixn2219o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkqyngas51eg6ixn2219o.png" alt="Figure 4\. The execution time of the regex checking strings that do not match the pattern \(there is no closing parenthesis at the end of the string\)"&gt;&lt;/a&gt;&lt;br&gt;
Figure 4. The execution time of the regex checking strings that do not match the pattern (there is no closing parenthesis at the end of the string)&lt;/p&gt;

&lt;p&gt;We see catastrophic backtracking again. It happens because of the &lt;em&gt;(-?\d+)*&lt;/em&gt; subexpression, which includes the &lt;em&gt;\d+&lt;/em&gt; subexpression. The '*' or '+' quantifiers are applied to both subexpressions and the same string can be matched with each of them, for example, &lt;em&gt;111&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Let's compare these observations with the previously examined conditions of the regular expression with a vulnerability:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There are two subexpressions, and one of them includes another. One of the following quantifiers is applied to each of them: '*', '+', '*?', '+?', '{...}'. The &lt;em&gt;(-?\d+)*&lt;/em&gt;) subexpression includes &lt;em&gt;\d+&lt;/em&gt;;&lt;/li&gt;
&lt;li&gt;There is a string that can be matched with both subexpressions. For example, the &lt;em&gt;1111&lt;/em&gt; string may fit both the &lt;em&gt;\d+&lt;/em&gt; template and &lt;em&gt;(-?\d+)*&lt;/em&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By the way, the newDate\((-?\d+)*\) regex caused a &lt;a href="https://github.com/advisories/GHSA-9pq7-rcxv-47vq" rel="noopener noreferrer"&gt;vulnerability&lt;/a&gt; (CVE-2021-27293) in a real project – the RestSharp library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 4&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As a final example, let's look for vulnerability in a more complex regular expression:&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;A&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="o"&gt;:|&lt;/span&gt;&lt;span class="err"&gt;\\&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="err"&gt;\\&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="err"&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="o"&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;s&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="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The task of this expression is to find strings that represent a list of paths to files or directories. Each element of this list is separated from each other with a comma and a space character. A list item can be represented by a path corresponding to one of two types:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;full path, for example:* D:\catalog\subcatalog\file.txt*,&lt;/li&gt;
&lt;li&gt;relative path from the main folder, for example: &lt;em&gt;\main\catalog\file.exe&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thus, the string corresponding to the pattern may look like this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;D:\catalog, C:\catalog\file.cs, \main\file.txt, \main\, project\main.csproj&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A regular expression will evaluate such strings without any problems.&lt;/p&gt;

&lt;p&gt;The same goes for processing almost any incorrect string processing, for example:&lt;/p&gt;

&lt;p&gt;D:*\catalog\file.cs\catalog\file.cs\catalog\file.cs\catalog\file.cs\catalog\file.cs\catalog\file.cs\\\*&lt;/p&gt;

&lt;p&gt;However, the situation changes if we pass a string of the following type to a regex:&lt;/p&gt;

&lt;p&gt;*D:\main\main\main\main\main\main\main\main\main\main\main\main\main\main\main\\\*&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhwj69cvbk9zmlxbru6tn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhwj69cvbk9zmlxbru6tn.png" alt="Figure 5\. The running time of the regular expression when processing strings of the *D:\\main \.\.\.\\main\\\\\\* format\."&gt;&lt;/a&gt;&lt;br&gt;
[Figure 5. The running time of the regular expression when processing strings of the *D:\main ...\main\\\* format.&lt;/p&gt;

&lt;p&gt;Let's inspect the original regular expression (&lt;em&gt;^(([A-Z]:|\\main)(\\[^\\]+)*(,\s)?)+$&lt;/em&gt;) in more details. Note that subexpressions &lt;em&gt;([A-Z]:|\\main)&lt;/em&gt; and &lt;em&gt;(\\[^\\]+)*&lt;/em&gt; that follow each other can be matched with the same &lt;em&gt;\main&lt;/em&gt; string. Moreover, the following subexpression (&lt;em&gt;(,\s)?&lt;/em&gt;) can be ignored, because the '?' quantifier allows the absence of a match with this template.&lt;/p&gt;

&lt;p&gt;Thus, it is possible to simplify the original regular expression to check only one special case – strings of the &lt;em&gt;D:\main ...\main&lt;/em&gt; format:&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;A&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="o"&gt;:|&lt;/span&gt;&lt;span class="err"&gt;\\&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="err"&gt;\\&lt;/span&gt;&lt;span class="n"&gt;main&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="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The catastrophic backtracking vulnerability becomes clear when we look at the simplified version of this string.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There's a subexpression &lt;em&gt;(([A-Z]:|\\main)(\\main)*)+&lt;/em&gt; with the '+' quantifier. This subexpression includes &lt;em&gt;(\\main)*&lt;/em&gt; with the '*' quantifier.&lt;/li&gt;
&lt;li&gt;Both subexpressions: &lt;em&gt;(([A-Z]:|\\main)(\\main)*)+&lt;/em&gt; and &lt;em&gt;(\\main)*&lt;/em&gt; may fit the same string, for example, &lt;em&gt;\main\main\main&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Therefore, both conditions for a vulnerable expression are met.&lt;/p&gt;

&lt;p&gt;Let's highlight the main factors that cause catastrophic backtracking in the &lt;em&gt;^(([A-Z]:|\\main)(\\[^\\]+)*(,\s)?)+$&lt;/em&gt; regular expression:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The '+' quantifier is applied to the &lt;em&gt;(([A-Z]:|\\main)(\\[^\\]+)*(,\s)?)+&lt;/em&gt; subexpression;&lt;/li&gt;
&lt;li&gt;The '*' quantifier is applied to the &lt;em&gt;(\\[^\\]+)*&lt;/em&gt; subexpression;&lt;/li&gt;
&lt;li&gt;Subexpressions &lt;em&gt;([A-Z]:|\\main)&lt;/em&gt; and &lt;em&gt;(\\[^\\]+)*&lt;/em&gt; may fit the same &lt;em&gt;\main&lt;/em&gt; string;&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;(,\s)?&lt;/em&gt; subexpression can be omitted because of the '?' quantifier.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The absence of at least one of them would make the regular expression absolutely safe.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to protect from catastrophic backtracking?
&lt;/h2&gt;

&lt;p&gt;Let's look at the main ways to protect a regex from catastrophic backtracking. We'll use the &lt;em&gt;newDate\((-?\d+)*\)&lt;/em&gt; as an example. The code below is written in C#. However, the similar functionality probably exists in other programming languages that support regular expressions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 1.&lt;/strong&gt; Add a limit on execution time for processing a string by a regular expression. In .NET, we can do it by setting the &lt;em&gt;matchTimeout&lt;/em&gt; parameter when calling a static method or initializing the new &lt;em&gt;Regex&lt;/em&gt; object.&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;RegexOptions&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;RegexOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;None&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;TimeSpan&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TimeSpan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FromSeconds&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;Regex&lt;/span&gt; &lt;span class="n"&gt;pattern&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;Regex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="s"&gt;"newDate\((-?\d+)*\)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Regex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="s"&gt;"newDate\((-?\d+)*\)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F80pqes1f83rwit8gu9xf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F80pqes1f83rwit8gu9xf.png" alt="Figure 6\. The execution time of a regular expression is limited to one second"&gt;&lt;/a&gt;&lt;br&gt;
Figure 6. The execution time of a regular expression is limited to one second&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 2.&lt;/strong&gt; Use atomic groups &lt;em&gt;(?&amp;gt;...)&lt;/em&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;Regex&lt;/span&gt; &lt;span class="n"&gt;pattern&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;Regex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="s"&gt;"newDate\((-?\d+)*\)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For expressions marked as atomic groups, the backtracking function is disabled. Thus, of all possible matching options, an atomic group will always be matched with only one substring containing the maximum number of characters. &lt;/p&gt;

&lt;p&gt;Although atomic groups are a reliable way of protection against catastrophic backtracking, we recommend to use them carefully. In some cases, using atomic groups can decrease the accuracy of the regular expression's calculation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flpjk05ao65gcnpw2hija.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flpjk05ao65gcnpw2hija.png" alt="Figure 7\. Subexpressions marked as atomic groups are no longer vulnerable to catastrophic backtracking\."&gt;&lt;/a&gt;&lt;br&gt;
Figure 7. Subexpressions marked as atomic groups are no longer vulnerable to catastrophic backtracking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 3.&lt;/strong&gt; Rewrite a regex by replacing an unsafe subexpression with a safe equivalent. For example, to find a string of the &lt;em&gt;newDate(13-09-2022)&lt;/em&gt; type you can use &lt;em&gt;newDate\((\d{2}-\d{2}-\d{4})\)&lt;/em&gt; instead of &lt;em&gt;newDate\((-?\d+)*\)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The latter has two subexpressions: &lt;em&gt;(-?\d+)*&lt;/em&gt; and &lt;em&gt;\d+&lt;/em&gt;. The &lt;em&gt;\d+&lt;/em&gt; subexpression is included in &lt;em&gt;(-?\d+)*&lt;/em&gt;. The same substring can match both these subexpressions. The safe equivalent allows matching any substring with only one template due to the mandatory check of the '-' character between templates &lt;em&gt;\d{...}&lt;/em&gt;.&lt;/p&gt;

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

&lt;p&gt;Let's sum up:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Regular expressions may be vulnerable to a ReDoS attack, the aim of which is to halt or slow down  an application;&lt;/li&gt;
&lt;li&gt;The application slows down because of catastrophic backtracking. It occurs if there is a huge number of options for matching an input string with a regular expression and there are no correct options among them;&lt;/li&gt;
&lt;li&gt;A regex is vulnerable to catastrophic backtracking if it contains at least one vulnerable subexpression that may cause a large number of matching options.&lt;/li&gt;
&lt;li&gt;You can identify a vulnerability in a regular expression by inspecting it for the following conditions:

&lt;ul&gt;
&lt;li&gt;There are two subexpressions, and one of them includes another. One of the following quantifiers is applied to each of them: '*', '+', '*?', '+?', '{...}';&lt;/li&gt;
&lt;li&gt;There's a string that may fit both these subexpressions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And that's all for today :). I hope this article was interesting.&lt;/p&gt;

&lt;p&gt;Clean code and successful projects to you! See you in next articles!&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>security</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What 's new in .NET 7?</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 18 Nov 2022 11:17:33 +0000</pubDate>
      <link>https://forem.com/unicorn_developer/what-s-new-in-net-7-4boe</link>
      <guid>https://forem.com/unicorn_developer/what-s-new-in-net-7-4boe</guid>
      <description>&lt;p&gt;.NET 7 is now available, which means that we can enjoy all sorts of new features and enhancements. Let's talk about the most interesting improvements: C# 11, containers, performance, GC, and so on.&lt;/p&gt;

&lt;h2&gt;
  
  
  C# 11
&lt;/h2&gt;

&lt;p&gt;We have already posted an &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/1002/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1009&amp;amp;utm_term=1002"&gt;article&lt;/a&gt; and described the novelties in C# 11. In that article, we covered new features: generic math support, raw string literals, the &lt;em&gt;required&lt;/em&gt; modifier, the type parameters in attributes, and so on.&lt;/p&gt;

&lt;p&gt;By the way, we are already working on .NET 7 and C# 11 support — we plan to add it to PVS-Studio 7.22. The release is scheduled for early December, and &lt;a href="https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1009&amp;amp;utm_term=link_try-free"&gt;here&lt;/a&gt; is the link to download the latest version of the analyzer. If you can't wait to try the beta now, feel free to &lt;a href="https://pvs-studio.com/en/about-feedback/?utm_source=website&amp;amp;utm_medium=dev_to&amp;amp;utm_campaign=article&amp;amp;utm_content=1009&amp;amp;utm_term=about-feedback"&gt;contact us&lt;/a&gt; :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Native AOT
&lt;/h2&gt;

&lt;p&gt;AOT (ahead-of-time) – compilation of the application not into intermediate, but immediately into machine code. Native AOT uses the ahead-of-time compiler to compile IL into machine code while publishing a &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/deploying/#publish-self-contained"&gt;self-contained&lt;/a&gt; application. The dev team of .NET 7 moved native AOT from experimental status to mainstream development. The biggest advantages of native AOT apps are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;startup time;&lt;/li&gt;
&lt;li&gt;memory usage;&lt;/li&gt;
&lt;li&gt;access to restricted platforms (where JIT is not allowed).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Native AOT applications come with a few limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;no dynamic loading (for example, &lt;em&gt;Assembly.LoadFile&lt;/em&gt;);&lt;/li&gt;
&lt;li&gt;no runtime code generation (for example, &lt;em&gt;System.Reflection.Emit&lt;/em&gt;);&lt;/li&gt;
&lt;li&gt;no C++/CLI;&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  On Stack Replacement (OSR)
&lt;/h2&gt;

&lt;p&gt;In .NET, there is such a thing as tiered compilation. In simple terms, tiered compilation improves the startup time of an application. How? Initially, JIT generates poorly optimized machine code (tier-0) for methods because it simply takes less time. If the number of method calls exceeds a certain threshold, JIT generates more optimized code for this method (tier-1). This approach does not work, for example, with loops, because this can degrade the performance. By the way, at the moment there are only two levels.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#on-stack-replacement"&gt;OSR&lt;/a&gt; allows you to replace the machine code that is currently running with a new, more optimized one. Previously, this feature was only available between method calls. This approach allows you to apply tiered compilation to all methods. This results in quicker compilation with stable performance at the same time. According to Microsoft's tests, the technology helped to speed up the startup of high-load applications by 25%. OSR in .NET 7 is enabled by default for x64 and Arm64.&lt;/p&gt;

&lt;h2&gt;
  
  
  Central package management (CPM)
&lt;/h2&gt;

&lt;p&gt;Dependency management for multi-project solutions can be a challenge. In situations where you manage common dependencies for many different projects, you can use &lt;a href="https://learn.microsoft.com/en-us/nuget/consume-packages/Central-Package-Management"&gt;NuGet's central package management&lt;/a&gt;. To get started with central package management, create the &lt;em&gt;Directory.Packages.props&lt;/em&gt; file at the root of your solution. Because of CPM, the package version is only specified in &lt;em&gt;Directory.Packages.props&lt;/em&gt;, and projects only need to reference the package.&lt;/p&gt;

&lt;h2&gt;
  
  
  GC Regions
&lt;/h2&gt;

&lt;p&gt;GC Regions is a feature that has been under development for several years. If earlier it was necessary to have several large memory segments (for example, 1 GB), now GC supports many small segments (for example, 4 MB). GC regions provide more flexibility like repurposing regions of memory from one generation to another.&lt;/p&gt;

&lt;p&gt;In .NET 7, regions are used by default for 64-bit applications. You can find more details in the .NET GC architect 's &lt;a href="https://devblogs.microsoft.com/dotnet/put-a-dpad-on-that-gc/"&gt;article&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rate Limiting
&lt;/h2&gt;

&lt;p&gt;Rate limiting is the mechanism that limits the access to a resource. You can set a certain access limit, for example, to a database.&lt;/p&gt;

&lt;p&gt;To write a limiter in .NET 7, Microsoft added the NuGet &lt;em&gt;System.Threading.RateLimiting&lt;/em&gt; package. Most of the work will be done with the abstract &lt;em&gt;RateLimiter&lt;/em&gt; class. Here's one of the examples from Microsoft:&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;RateLimiter&lt;/span&gt; &lt;span class="n"&gt;limiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetLimiter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;RateLimitLease&lt;/span&gt; &lt;span class="n"&gt;lease&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;limiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Acquire&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;permitCount&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;lease&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsAcquired&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Do action that is protected by limiter&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Error handling or add retry logic&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 case, we are trying to get 1 permit using the &lt;em&gt;Acquire&lt;/em&gt; method. Next comes the check – whether permit has been acquired:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if the permit has been acquired, we can use the resource;&lt;/li&gt;
&lt;li&gt;if the permit was not acquired, this can be logged or handled as an error.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Built-in container support
&lt;/h2&gt;

&lt;p&gt;Now you can quickly and easily create containerized versions of your applications using the &lt;em&gt;dotnet publish&lt;/em&gt; command. &lt;/p&gt;

&lt;p&gt;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;dotnet&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;package&lt;/span&gt; &lt;span class="n"&gt;Microsoft&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NET&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Containers&lt;/span&gt;
&lt;span class="n"&gt;dotnet&lt;/span&gt; &lt;span class="n"&gt;publish&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt; &lt;span class="n"&gt;linux&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;arch&lt;/span&gt; &lt;span class="n"&gt;x64&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="n"&gt;Release&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;PublishProfile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;DefaultContainer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, we add a temporary link to the package to create a container and publish the project for linux x64. The result of executing the commands is an image that will be added to Docker. After that, you can launch the application using the container:&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;docker&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="o"&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;rm&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="mi"&gt;5010&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt; &lt;span class="n"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;awesome&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;container&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;1.0.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can read more about it &lt;a href="https://devblogs.microsoft.com/dotnet/announcing-builtin-container-support-for-the-dotnet-sdk/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance enhancements
&lt;/h2&gt;

&lt;p&gt;Performance in .NET is growing year by year. This release was no exception. It would take a separate article just to list all the improvements, so I will only tell you about the most interesting ones. &lt;/p&gt;

&lt;p&gt;If you want to know more about the performance improvements in .NET 7, you can read about them &lt;a href="https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reflection
&lt;/h3&gt;

&lt;p&gt;The reflection overhead is significantly reduced when a call is made more than once for the same item (whether it is a method, constructor or property). Performance has been increased by 3-4 times.&lt;/p&gt;

&lt;p&gt;You can read more about improvements of reflection &lt;a href="https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#reflection"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  LINQ
&lt;/h3&gt;

&lt;p&gt;In .NET 7, LINQ performance has been improved. For example, the efficiency of the &lt;em&gt;Min&lt;/em&gt; and &lt;em&gt;Max&lt;/em&gt; methods has been significantly improved when it comes to &lt;em&gt;int&lt;/em&gt; and &lt;em&gt;long&lt;/em&gt; arrays. This is achieved by vectorizing the processing, namely, using &lt;em&gt;Vector&lt;/em&gt;. The results are as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6__o7ual--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuilaxz3eihhxnyhzp6b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6__o7ual--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuilaxz3eihhxnyhzp6b.png" alt="Image description" width="780" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can read more about LINQ performance improvements in the &lt;a href="https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#linq"&gt;article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;By the way, it's also very cool that &lt;em&gt;System.Linq&lt;/em&gt; was expended with new &lt;em&gt;Order&lt;/em&gt; and &lt;em&gt;OrderDescending&lt;/em&gt; methods. Previously, when using &lt;em&gt;OrderBy/OrderByDescending&lt;/em&gt;, it was necessary to refer to its own 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="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;2&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="mi"&gt;3&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;sorted&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="n"&gt;OrderBy&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;=&amp;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;var&lt;/span&gt; &lt;span class="n"&gt;sortedDesc&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="n"&gt;OrderByDescending&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;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it is not necessary:&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;2&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="mi"&gt;3&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;sorted&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="n"&gt;Order&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;sortedDesc&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="n"&gt;OrderDescending&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Regular expressions
&lt;/h3&gt;

&lt;p&gt;At first, I wanted to briefly talk about improvements to regular expressions, but in the course of writing this article I realized that there were too many improvements. Therefore, let me just leave a link to a larger &lt;a href="https://devblogs.microsoft.com/dotnet/regular-expression-improvements-in-dotnet-7/"&gt;article&lt;/a&gt; dedicated to this topic :). It fully describes all the novelties and enhancements of regular expressions. I will also note here that Microsoft has not only improved performance, but also added various functional improvements.&lt;/p&gt;

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

&lt;p&gt;As you can see, there are many improvements in .NET 7. Not all of them are equally useful to all developers, but many technologies will continue to evolve in future .NET releases.&lt;/p&gt;

&lt;p&gt;I can single out the following features as the most interesting and useful ones for me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;of course, C#11;&lt;/li&gt;
&lt;li&gt;on stack replacement (OSR);&lt;/li&gt;
&lt;li&gt;Central package management (CPM);&lt;/li&gt;
&lt;li&gt;GC Regions. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The article, of course, did not list all the new features, but only the most interesting ones (in our opinion). You can find all the improvements &lt;a href="https://devblogs.microsoft.com/dotnet/announcing-dotnet-7/"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Have you already tried any of the new .NET features? Feel free to share them in the comments.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>news</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
