{"id":398120,"date":"2024-06-29T13:52:51","date_gmt":"2024-06-29T13:52:51","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=398120"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=398120","title":{"rendered":"<span>Static analysis protects your code from time bombs<\/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\/0cb\/693\/38c\/0cb69338c49a2c334736a6a7032848d5.png\" alt=\"0848_Timebomb\/image2.png\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/0cb\/693\/38c\/0cb69338c49a2c334736a6a7032848d5.png\"\/><br \/>  Static code analysis allows you to identify and eliminate many defects at an early stage. Moreover, it&#8217;s possible to detect dormant errors that don&#8217;t show themselves when they appear. They can cause many problems in the future and it requires many hours of debugging to detect them. Let&#8217;s look at an example of such a dormant error.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>To show the advantage of regular use of the PVS-Studio static analyzer, we regularly check the <a href=\"https:\/\/github.com\/blender\/blender\">Blender<\/a> project. My colleague wrote more about this idea <a href=\"https:\/\/pvs-studio.com\/en\/blog\/posts\/cpp\/0799\/\">here<\/a>.<\/p>\n<p>  <\/p>\n<p>Sometimes I keep an eye on the warnings generated for new or modified Blender code. New bugs appear regularly, but most of them are boring or minor. With patience of a fisherman I&#8217;m sitting here waiting for something interesting that is worth writing about. And today&#8217;s article is a case in point.<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">void UI_but_drag_set_asset(uiBut *but,                            const AssetHandle *asset,                            const char *path,                            int import_type,                            int icon,                            struct ImBuf *imb,                            float scale) {   ....   asset_drag->asset_handle = MEM_mallocN(sizeof(asset_drag->asset_handle),                                          \"wmDragAsset asset handle\");   *asset_drag->asset_handle = *asset;   .... }<\/code><\/pre>\n<p>  <\/p>\n<p>The code must allocate a buffer in memory sufficient to store a structure of the <em>AssetHandle<\/em> type. It is a programmer&#8217;s intent. But it allocates a buffer equal not to the size of the structure, but to the size of the pointer.<\/p>\n<p>  <\/p>\n<p>Here is the error:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">sizeof(asset_drag->asset_handle)<\/code><\/pre>\n<p>  <\/p>\n<p>The correct version:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">sizeof(*asset_drag->asset_handle)<\/code><\/pre>\n<p>  <\/p>\n<p>The analyzer detected this error and issued a warning: <a href=\"https:\/\/pvs-studio.com\/en\/docs\/warnings\/v568\/\">V568<\/a>: It&#8217;s odd that &#8216; sizeof()&#8217; operator evaluates the size of a pointer to a class, but not the size of the &#8216;asset_drag->asset_handle&#8217; class object. interface.c 6192<\/p>\n<p>  <\/p>\n<p>It&#8217;s simple. It&#8217;s a classic error pattern that we <a href=\"https:\/\/pvs-studio.com\/en\/blog\/examples\/v568\/\">encounter<\/a> in various projects. Something else is worth noting! This code works correctly now! The author who made it is lucky. Let&#8217;s look at what the <em>AssetHandle<\/em> structure is:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">typedef struct AssetHandle {   const struct FileDirEntry *file_data; } AssetHandle;<\/code><\/pre>\n<p>  <\/p>\n<p>The structure now has exactly one pointer. It turns out that the size of the structure is the same as the size of the pointer!<\/p>\n<p>  <\/p>\n<p>Look at a pretty time bomb right in front of us. This code will work safely and steadily for years. It will function fully until someone wants to add a new field to the structure.<\/p>\n<p>  <\/p>\n<p>At this point, the app crashes. And it&#8217;s not clear what and where exactly it crashed. Less memory allocates for the structure than is required. That&#8217;s great if a programmer is lucky enough to get an <a href=\"https:\/\/pvs-studio.com\/en\/blog\/terms\/0063\/\">Access Violation<\/a> after violating the buffer boundary. But, more likely, some memory will simply get corrupted. As a result, a developer may be doomed to torturously debug code for hours.<\/p>\n<p>  <\/p>\n<p>Use static code analyzer to significantly improve the quality and reliability of code. It&#8217;s useful both in the short and long term.<\/p>\n<p>  <\/p>\n<p>Static analysis can&#8217;t detect all errors. However, the benefits of its regular use are greater than the cost of reviewing a daily report with new warnings. Recently, in the article, a user of our analyzer concluded: you&#8217;d better run the analyzer than <a href=\"https:\/\/pvs-studio.com\/en\/blog\/posts\/cpp\/0847\/\">debug for three days<\/a>.<\/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\/571176\/\"> https:\/\/habr.com\/ru\/articles\/571176\/<\/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\/0cb\/693\/38c\/0cb69338c49a2c334736a6a7032848d5.png\" alt=\"0848_Timebomb\/image2.png\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/0cb\/693\/38c\/0cb69338c49a2c334736a6a7032848d5.png\"\/><br \/>  Static code analysis allows you to identify and eliminate many defects at an early stage. Moreover, it&#8217;s possible to detect dormant errors that don&#8217;t show themselves when they appear. They can cause many problems in the future and it requires many hours of debugging to detect them. Let&#8217;s look at an example of such a dormant error.<\/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-398120","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/398120","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=398120"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/398120\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=398120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=398120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}