{"id":407589,"date":"2024-06-29T19:38:43","date_gmt":"2024-06-29T19:38:43","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=407589"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-29T19:38:43","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=407589","title":{"rendered":"<span>C# Programmer, It&#8217;s Time to Test Yourself and Find Error<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>The PVS-Studio analyzer is regularly updated with new diagnostic rules. Curiously enough, diagnostics often detect suspicious code fragments before the end of the work. For example, such a situation may happen while testing on open-source projects. So, let&#8217;s take a look at one of these interesting finding.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7bc\/79d\/3fc\/7bc79d3fcfbcb80537d039880f3009d9.png\" width=\"354\" height=\"305\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7bc\/79d\/3fc\/7bc79d3fcfbcb80537d039880f3009d9.png\"\/><figcaption><\/figcaption><\/figure>\n<p>As mentioned earlier, one of the stages of diagnostic rule testing is to check its operation on a real codebase. To that end, we have a set of selected open-source projects that we use for the analysis. The obvious advantage of this approach is the ability to see the diagnostic rule behavior in real conditions. There&#8217;s also a less obvious advantage. Sometimes you may find such an interesting case, so it would be a sin not to write an article about it. \ud83d\ude42<\/p>\n<p>Now, let&#8217;s take a look at the code from the Bouncy Castle C# project and find the error in it:<\/p>\n<pre><code>public static string ToString(object[] a) {   StringBuilder sb = new StringBuilder('[');   if (a.Length > 0)   {     sb.Append(a[0]);     for (int index = 1; index &lt; a.Length; ++index)     {       sb.Append(\", \").Append(a[index]);     }   }   sb.Append(']');   return sb.ToString(); } <\/code><\/pre>\n<p>For those who like to cheat and peek, I added a picture to keep you guessing.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/76e\/f87\/2ab\/76ef872ab21db182e53549db3bcfd96d.png\" width=\"580\" height=\"500\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/76e\/f87\/2ab\/76ef872ab21db182e53549db3bcfd96d.png\"\/><figcaption><\/figcaption><\/figure>\n<p>I&#8217;m sure some of you couldn&#8217;t see the error without using the IDE or the <em>StringBuilder<\/em> class documentation. The error happened when calling the constructor:<\/p>\n<pre><code>StringBuilder sb = new StringBuilder('['); <\/code><\/pre>\n<p>Actually, this is exactly what the PVS-Studio static analyzer warns us about: <a href=\"https:\/\/www.viva64.com\/en\/w\/v3165\/\">V3165<\/a> Character literal &#8216;[&#8216; is passed as an argument of the &#8216;Int32&#8217; type whereas similar overload with the string parameter exists. Perhaps, a string literal should be used instead. Arrays.cs 193.<\/p>\n<p>The programmer wanted to create an instance of the <em>StringBuilder<\/em> type, where the string begins with the &#8216;[&#8216; character. However, due to a typo, we&#8217;ll have an object without any characters with a capacity of 91 elements.<\/p>\n<p>This happened because the programmer used single quotes instead of double-quotes. That&#8217;s why the wrong constructor overload was called: <\/p>\n<pre><code>.... public StringBuilder(int capacity); public StringBuilder(string? value); .... <\/code><\/pre>\n<p>When the constructor is called, the &#8216;[&#8216; character literal will be implicitly cast to the corresponding value of the <em>int<\/em> type (91 in Unicode). Because of this, the constructor with the <em>int<\/em> type parameter setting the initial capacity will be called. Although, the programmer wanted to call the constructor which sets the string&#8217;s beginning.<\/p>\n<p>To fix the error, the developer has to replace the character literal with a string literal (i.e., use &#171;[&#187; instead of &#8216;[&#8216;). It will cause the correct constructor overload.<\/p>\n<p>We decided to go the extra mile and expanded the cases reviewed by diagnostic. As a result, in addition to character literals, some other expressions of the <em>char<\/em> type are considered now. We also check methods in the same way. <\/p>\n<p>The diagnostic described above was added in the PVS-Studio 7.11 release. You can <a href=\"https:\/\/www.viva64.com\/en\/pvs-studio-download\/\">download the analyzer latest version<\/a> yourself. You&#8217;ll see what the V3165 diagnostic can do, as well as other diagnostics for C, C++, C#, and Java.<\/p>\n<p>By the way, the users themselves often suggest some ideas of diagnostics to us. This time it has happened thanks to the user of <a href=\"https:\/\/habr.com\/en\/users\/krypt\/\">Krypt<\/a> from Habr. If you also have some ideas for diagnostic rules &#8212; <a href=\"https:\/\/www.viva64.com\/en\/about-feedback\/\">don&#8217;t hesitate to reach out to us<\/a>!<\/p>\n<p><strong>P.S.<\/strong> This error has already been fixed in the current project code base. Though, this doesn&#8217;t change the fact that it has existed in the code for some time, and that static analysis allows you to identify such problems and fix them at the earliest stages.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/540348\/\"> https:\/\/habr.com\/ru\/articles\/540348\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>The PVS-Studio analyzer is regularly updated with new diagnostic rules. Curiously enough, diagnostics often detect suspicious code fragments before the end of the work. For example, such a situation may happen while testing on open-source projects. So, let&#8217;s take a look at one of these interesting finding.<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>As mentioned earlier, one of the stages of diagnostic rule testing is to check its operation on a real codebase. To that end, we have a set of selected open-source projects that we use for the analysis. The obvious advantage of this approach is the ability to see the diagnostic rule behavior in real conditions. There&#8217;s also a less obvious advantage. Sometimes you may find such an interesting case, so it would be a sin not to write an article about it. \ud83d\ude42<\/p>\n<p>Now, let&#8217;s take a look at the code from the Bouncy Castle C# project and find the error in it:<\/p>\n<pre><code>public static string ToString(object[] a) {   StringBuilder sb = new StringBuilder('[');   if (a.Length > 0)   {     sb.Append(a[0]);     for (int index = 1; index &lt; a.Length; ++index)     {       sb.Append(\", \").Append(a[index]);     }   }   sb.Append(']');   return sb.ToString(); } <\/code><\/pre>\n<p>For those who like to cheat and peek, I added a picture to keep you guessing.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>I&#8217;m sure some of you couldn&#8217;t see the error without using the IDE or the <em>StringBuilder<\/em> class documentation. The error happened when calling the constructor:<\/p>\n<pre><code>StringBuilder sb = new StringBuilder('['); <\/code><\/pre>\n<p>Actually, this is exactly what the PVS-Studio static analyzer warns us about: <a href=\"https:\/\/www.viva64.com\/en\/w\/v3165\/\">V3165<\/a> Character literal &#8216;[&#8216; is passed as an argument of the &#8216;Int32&#8217; type whereas similar overload with the string parameter exists. Perhaps, a string literal should be used instead. Arrays.cs 193.<\/p>\n<p>The programmer wanted to create an instance of the <em>StringBuilder<\/em> type, where the string begins with the &#8216;[&#8216; character. However, due to a typo, we&#8217;ll have an object without any characters with a capacity of 91 elements.<\/p>\n<p>This happened because the programmer used single quotes instead of double-quotes. That&#8217;s why the wrong constructor overload was called: <\/p>\n<pre><code>.... public StringBuilder(int capacity); public StringBuilder(string? value); .... <\/code><\/pre>\n<p>When the constructor is called, the &#8216;[&#8216; character literal will be implicitly cast to the corresponding value of the <em>int<\/em> type (91 in Unicode). Because of this, the constructor with the <em>int<\/em> type parameter setting the initial capacity will be called. Although, the programmer wanted to call the constructor which sets the string&#8217;s beginning.<\/p>\n<p>To fix the error, the developer has to replace the character literal with a string literal (i.e., use &#171;[&#187; instead of &#8216;[&#8216;). It will cause the correct constructor overload.<\/p>\n<p>We decided to go the extra mile and expanded the cases reviewed by diagnostic. As a result, in addition to character literals, some other expressions of the <em>char<\/em> type are considered now. We also check methods in the same way. <\/p>\n<p>The diagnostic described above was added in the PVS-Studio 7.11 release. You can <a href=\"https:\/\/www.viva64.com\/en\/pvs-studio-download\/\">download the analyzer latest version<\/a> yourself. You&#8217;ll see what the V3165 diagnostic can do, as well as other diagnostics for C, C++, C#, and Java.<\/p>\n<p>By the way, the users themselves often suggest some ideas of diagnostics to us. This time it has happened thanks to the user of <a href=\"https:\/\/habr.com\/en\/users\/krypt\/\">Krypt<\/a> from Habr. If you also have some ideas for diagnostic rules &#8212; <a href=\"https:\/\/www.viva64.com\/en\/about-feedback\/\">don&#8217;t hesitate to reach out to us<\/a>!<\/p>\n<p><strong>P.S.<\/strong> This error has already been fixed in the current project code base. Though, this doesn&#8217;t change the fact that it has existed in the code for some time, and that static analysis allows you to identify such problems and fix them at the earliest stages.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/540348\/\"> https:\/\/habr.com\/ru\/articles\/540348\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-407589","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/407589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=407589"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/407589\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=407589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=407589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=407589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}