{"id":382604,"date":"2024-06-29T04:22:57","date_gmt":"2024-06-29T04:22:57","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=382604"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=382604","title":{"rendered":"<span>How PVS-Studio prevents rash code changes, example N3<\/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-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/2af\/e59\/74f\/2afe5974f6edf8669077834c9e7edd02.png\" alt=\"Blender, PVS-Studio, Example 3\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/2af\/e59\/74f\/2afe5974f6edf8669077834c9e7edd02.png\"\/><br \/>  Let&#8217;s continue with a series of small notes illustrating the PVS-Studio&#8217;s ability to quickly find new errors in the code. If the analyzer is regularly used, of course :). Today we have another bug in the Blender project.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>I monitor the <a href=\"https:\/\/github.com\/blender\/blender\">Blender<\/a> project for fun. Every day I get a <a href=\"https:\/\/pvs-studio.com\/en\/pvs-studio\/\">PVS-Studio<\/a> report with warnings related to the new code. Sometimes an error catches my attention and I write a note about it. That&#8217;s what I&#8217;m doing right now :).<\/p>\n<p>  <\/p>\n<p>I won&#8217;t give you links to the previous articles, since they are of the same type. With these articles I want to show you that regular use of the static analyzer helps quickly find errors. The earlier the error is found, the lower the cost of fixing it.<\/p>\n<p>  <\/p>\n<p>This time my attention was caught by two PVS-Studio warnings. The analyzer was triggered by one code line:<\/p>\n<p>  <\/p>\n<ul>\n<li>[CWE-480] <a href=\"https:\/\/pvs-studio.com\/en\/docs\/warnings\/v616\/\">V616<\/a>: The &#8216;OB_MODE_OBJECT&#8217; named constant with the value of 0 is used in the bitwise operation. transform_snap_object.c <a href=\"https:\/\/github.com\/blender\/blender\/commit\/52be06301257a82a1b4a5746e91ff60daa637ded\">480<\/a><\/li>\n<li>[CWE-571] <a href=\"https:\/\/pvs-studio.com\/en\/docs\/warnings\/v560\/\">V560<\/a>: A part of conditional expression is always true: !(base->object->mode &amp; OB_MODE_OBJECT). transform_snap_object.c <a href=\"https:\/\/github.com\/blender\/blender\/commit\/52be06301257a82a1b4a5746e91ff60daa637ded\">480<\/a><\/li>\n<\/ul>\n<p>  <\/p>\n<p>This is OK. One code bug can be suspicious for several diagnostic rules. We have just the case here:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">if (is_object_active &amp;&amp; !(base->object->mode &amp; OB_MODE_OBJECT)) {<\/code><\/pre>\n<p>  <\/p>\n<p>If you&#8217;ve read the analyzer warnings, you already know what&#8217;s going on. However, if you look at the code without these warnings, it seems completely normal. This code line can go unnoticed during code review.<\/p>\n<p>  <\/p>\n<p>To understand that the code is incorrect, you need to look at how the named constant is declared in the <em>eObjectMode<\/em> enumeration:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">typedef enum eObjectMode {   OB_MODE_OBJECT = 0,   OB_MODE_EDIT = 1 &lt;&lt; 0,   OB_MODE_SCULPT = 1 &lt;&lt; 1,   OB_MODE_VERTEX_PAINT = 1 &lt;&lt; 2,   OB_MODE_WEIGHT_PAINT = 1 &lt;&lt; 3,   OB_MODE_TEXTURE_PAINT = 1 &lt;&lt; 4,   OB_MODE_PARTICLE_EDIT = 1 &lt;&lt; 5,   OB_MODE_POSE = 1 &lt;&lt; 6,   OB_MODE_EDIT_GPENCIL = 1 &lt;&lt; 7,   OB_MODE_PAINT_GPENCIL = 1 &lt;&lt; 8,   OB_MODE_SCULPT_GPENCIL = 1 &lt;&lt; 9,   OB_MODE_WEIGHT_GPENCIL = 1 &lt;&lt; 10,   OB_MODE_VERTEX_GPENCIL = 1 &lt;&lt; 11, } eObjectMode;<\/code><\/pre>\n<p>  <\/p>\n<p>The <em>OB_MODE_OBJECT<\/em> constant is zero! Let&#8217;s look at the condition once again:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">if (is_object_active &amp;&amp; !(base->object->mode &amp; OB_MODE_OBJECT)) {<\/code><\/pre>\n<p>  <\/p>\n<p>Thus, the result of the bitwise AND (&amp;) operation is always zero. The first analyzer&#8217;s message warns us about this. <\/p>\n<p>  <\/p>\n<p>If we apply the &#171;!&#187; operator to 0, we get the following expression:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">if (is_object_active &amp;&amp; true) {<\/code><\/pre>\n<p>  <\/p>\n<p>The second analyzer message tells us that the part of the expression is always true.<\/p>\n<p>  <\/p>\n<p>Most likely, the correct option would look like this:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">if (is_object_active &amp;&amp; base->object->mode != OB_MODE_OBJECT) {<\/code><\/pre>\n<p>  <\/p>\n<p>I&#8217;m not sure though, I don&#8217;t understand the Blender&#8217;s source code well. The analyzer&#8217;s task is to point out an error. It&#8217;s up to the developer to decide what to do with it.<\/p>\n<p>  <\/p>\n<p>Hope you enjoyed this note. Subscribe to my Twitter: <a href=\"https:\/\/twitter.com\/Code_Analysis\">@Code_Analysis<\/a>. <\/p>\n<p>  <\/p>\n<p><strong>Additional links:<\/strong><\/p>\n<p>  <\/p>\n<ol>\n<li><a href=\"https:\/\/pvs-studio.com\/en\/blog\/posts\/0614\/\">Ways to get a free PVS-Studio license<\/a>.<\/li>\n<li><a href=\"https:\/\/pvs-studio.com\/en\/blog\/posts\/0743\/\">How to introduce a static code analyzer in a legacy project and not to discourage the team<\/a>.<\/li>\n<li><a href=\"https:\/\/pvs-studio.com\/en\/blog\/posts\/cpp\/0873\/\">C++ tools evolution: static code analyzers<\/a>.<\/li>\n<\/ol>\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\/651937\/\"> https:\/\/habr.com\/ru\/articles\/651937\/<\/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-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/2af\/e59\/74f\/2afe5974f6edf8669077834c9e7edd02.png\" alt=\"Blender, PVS-Studio, Example 3\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/2af\/e59\/74f\/2afe5974f6edf8669077834c9e7edd02.png\"\/><br \/>  Let&#8217;s continue with a series of small notes illustrating the PVS-Studio&#8217;s ability to quickly find new errors in the code. If the analyzer is regularly used, of course :). Today we have another bug in the Blender project.<\/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-382604","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/382604","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=382604"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/382604\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=382604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=382604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=382604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}