{"id":418670,"date":"2024-06-30T02:21:12","date_gmt":"2024-06-30T02:21:12","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=418670"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-30T02:21:12","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=418670","title":{"rendered":"<span>One Useful Comment<\/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\/bb4\/8ce\/afa\/bb48ceafa648b4239807b6382960cfd4.png\" alt=\"One Useful Comment\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/bb4\/8ce\/afa\/bb48ceafa648b4239807b6382960cfd4.png\"\/><\/p>\n<p>  <\/p>\n<p>Most influential programmers say that code must be self-documenting. They find comments useful only when working with something uncommon. Our team shares this opinion. Recently we came across a code snippet that perfectly proves it.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>We wrote out the following code while working with the article &#171;<a href=\"https:\/\/www.viva64.com\/en\/b\/0801\/\">Date Processing Attracts Bugs or 77 Defects in Qt 6<\/a>&#171;.<\/p>\n<p>  <\/p>\n<p>The PVS-Studio analyzer highlighted this code snippet and issued the warning: V575 [CWE-628] The &#8216;memcpy&#8217; function doesn&#8217;t copy the whole string. Use &#8216;strcpy \/ strcpy_s&#8217; function to preserve terminal null. qplaintestlogger.cpp 253. Actually, here it is:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">const char *msgFiller = msg[0] ? \" \" : \"\"; QTestCharBuffer testIdentifier; QTestPrivate::generateTestIdentifier(&amp;testIdentifier); QTest::qt_asprintf(&amp;messagePrefix, \"%s: %s%s%s%s\\n\",                    type, testIdentifier.data(), msgFiller, msg,                    failureLocation.data());  \/\/ In colored mode, printf above stripped our nonprintable control characters. \/\/ Put them back. memcpy(messagePrefix.data(), type, strlen(type));  outputMessage(messagePrefix.data());<\/code><\/pre>\n<p>  <\/p>\n<p>Note the call to the <em>memcpy<\/em> function. This code itself raises two questions at once:<\/p>\n<p>  <\/p>\n<ol>\n<li>Something is written to buffer, the contents of which were just generated with a printf-like function. Who does that?<\/li>\n<li>The terminal zero is not copied. Are you sure that&#8217;s not an error? The analyzer doesn&#8217;t like it.<\/li>\n<\/ol>\n<p>  <\/p>\n<p>Fortunately, the comment immediately makes it clear. Some non-printed characters must be restored.<\/p>\n<p>  <\/p>\n<p>Here&#8217;s a necessary and useful text. It&#8217;s a great comment explaining an unclear point in the code. It may be used as an example in how-to articles.<\/p>\n<p>  <\/p>\n<p>For comparison, take a look at another code snippet from the same file:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">char buf[1024];  if (result.setByMacro) {   qsnprintf(buf, sizeof(buf), \"%s%s%s%s%s%s\\n\", buf1, bufTag, fill,             buf2, buf2_, buf3); } else {   qsnprintf(buf, sizeof(buf), \"%s%s%s%s\\n\", buf1, bufTag, fill, buf2); }  memcpy(buf, bmtag, strlen(bmtag)); outputMessage(buf);<\/code><\/pre>\n<p>  <\/p>\n<p>The programmer forgot to leave a similar comment here. That&#8217;s why, everything&#8217;s changed. A new team member who has to maintain and modify this code may be a little confused. It&#8217;s not clear at all why this <em>memcpy<\/em> is used here. Moreover, it is not clear why the contents of a certain buffer <em>buf1<\/em> were printed at the beginning of the line and why the contents of the buffer <em>bmtag<\/em> are appended at the beginning of the string. So many questions, so few answers. Don&#8217;t write that way.<\/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\/548330\/\"> https:\/\/habr.com\/ru\/articles\/548330\/<\/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\/bb4\/8ce\/afa\/bb48ceafa648b4239807b6382960cfd4.png\" alt=\"One Useful Comment\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/bb4\/8ce\/afa\/bb48ceafa648b4239807b6382960cfd4.png\"\/><\/p>\n<p>  <\/p>\n<p>Most influential programmers say that code must be self-documenting. They find comments useful only when working with something uncommon. Our team shares this opinion. Recently we came across a code snippet that perfectly proves it.<\/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-418670","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/418670","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=418670"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/418670\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=418670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=418670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=418670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}