{"id":5963,"date":"2020-08-12T15:48:36","date_gmt":"2020-08-12T15:48:36","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/compiler-explorer-pvs-studio-and-terrible-simple-bugs\/"},"modified":"2020-08-12T15:48:36","modified_gmt":"2020-08-12T15:48:36","slug":"compiler-explorer-pvs-studio-and-terrible-simple-bugs","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/compiler-explorer-pvs-studio-and-terrible-simple-bugs\/","title":{"rendered":"Compiler Explorer, PVS-Studio, and Terrible Simple Bugs"},"content":{"rendered":"<p>Do you know that PVS-Studio is integrated into the Compiler Explorer? If not, you should definitely read this guest post from Andrey Karpov including a promo code.&nbsp;<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><span lang=\"EN-US\">In this article, we&#8217;ll talk about Compiler Explorer (godbolt.org), its integration with the PVS-Studio static analyzer, and bugs. Whatever your case, you&#8217;ll surely find the information below helpful. If you haven&#8217;t tried Compiler Explorer yet, this post will help you get started with this interesting and useful online service for experimenting with code compilation. And if you have, you&#8217;ll get a few insights into some possible ways of using it. We&#8217;ll be talking about simple yet serious bugs and ways to fight them. Make yourself some tea or coffee and let&#8217;s get started. This article may inspire you to both ponder over some of the ideas I&#8217;ll share and play around with the tools mentioned.<\/span><\/p>\n<div class=\"WordSection1\">\n<p class=\"MsoNormal\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5959\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/CompilerExlorer.png\" alt=\"CompilerExlorer\" width=\"650\" height=\"306\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/CompilerExlorer.png 1920w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/CompilerExlorer-300x141.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/CompilerExlorer-1024x482.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/CompilerExlorer-768x361.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/CompilerExlorer-1536x722.png 1536w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<h1><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Compiler Explorer<\/span><\/h1>\n<p class=\"MsoNormal\"><span lang=\"RU\"><a href=\"https:\/\/godbolt.org\/\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Compiler Explorer<\/span><\/a><\/span><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\"> \u2013 is an interactive tool that lets you type code in one window and see the results of its compilation in another window. There you can explore warnings\/bugs and assembly code generated by the compiler, as well as view the results of running the program. The tool supports Ada, D, Fortran, Python, and other programming languages. But it is the support of C and C++ that we&#8217;re interested in right now.<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">If you&#8217;re interested, there&#8217;s a Compiler Explorer C++ channel in Slack. You can join it by getting an automatic invite.<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">One important and useful feature of Compiler Explorer is the ability to create a permanent link to a &#8220;snapshot&#8221; of your current work, with all typed code and positions of opened windows preserved. For instance, this <\/span><span lang=\"RU\"><a href=\"https:\/\/godbolt.org\/z\/TCx7-6\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">link<\/span><\/a><\/span><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\"> will take you to a page containing the program text, assembly code, and the output of the running program. These links can be conveniently shared or inserted into blog posts.<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\"><\/span><\/p>\n<p class=\"MsoNormal\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5960\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/Introduction.png\" alt=\"Introduction\" width=\"650\" height=\"306\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/Introduction.png 1920w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/Introduction-300x141.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/Introduction-1024x482.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/Introduction-768x361.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/Introduction-1536x722.png 1536w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<h1><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Compiler Explorer &amp; static analysis<\/span><\/h1>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">You may have already known all of that. But there&#8217;s more. Compiler Explorer is evolving and gradually turning into something bigger than just a regular online compiler. Its list of additional tools (&#8220;Add tool&#8221;) is growing, enabling developers not only to compile source code but also see the results of checking it with static analyzers such as clang-tidy and PVS-Studio.<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Of course, the tool should be used sensibly \u2013 do not expect the online service to be able to check a large project comprised of many files. A more natural approach, in that case, would be to use an installed version of the analyzer. Another thing you don&#8217;t want to do is to evaluate analyzers&#8217; abilities based on how they perform on synthetic examples in Compiler Explorer because such tests are very likely to be <\/span><span lang=\"RU\"><a href=\"https:\/\/www.viva64.com\/ru\/b\/0739\/\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">inadequate<\/span><\/a><\/span><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">.<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">In all other respects, Compiler Explorer is a very cool tool. You are free to play with analyzers as more tools become available. Check if and how a certain bug is reported by one analyzer or another. This may be promising from the perspective of both satisfying your curiosity and conducting research.<\/span><\/p>\n<h1><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Compiler Explorer &amp; education<\/span><\/h1>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">And there&#8217;s still more. If you are a teacher or a student, this section is for you. Compiler Explorer can be viewed as an environment where students can do simple lab assignments. Sure, this service has its limitations, but as far as one-file lab tasks are concerned, it&#8217;s a perfect tool.<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Just type the code, compile it, and watch the console output. Create a link to your work and share it with your teacher for assessment. You don&#8217;t need to install anything on your computer to do that kind of tasks. You can actually complete and share your assignment from any terminal, even your smartphone (though it might be less convenient). I&#8217;m sure students will appreciate this service, especially during the period of exams :).<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Add to this the ability to immediately check the code with static analyzers. Many bugs\/typos will be caught right off, while you will get an opportunity to teach yourself the good practice of using static analyzers.<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Here&#8217;s an example to show you how it&#8217;s done in practice. Suppose you are studying arrays and loops and need to write a matrix-transpose program \u2013 <\/span><span lang=\"RU\"><a href=\"https:\/\/godbolt.org\/z\/9j3AGA\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">here&#8217;s what you write<\/span><\/a><\/span><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">.<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5961\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/ProgramBug.png\" alt=\"ProgramBug\" width=\"650\" height=\"306\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/ProgramBug.png 1920w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/ProgramBug-300x141.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/ProgramBug-1024x482.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/ProgramBug-768x361.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/ProgramBug-1536x722.png 1536w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/span><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">As you can see, the program compiles and even starts. But it does something weird and won&#8217;t print the modified matrix.<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Good news is that you have PVS-Studio&#8217;s warning about a potential typo in the bottom right corner. Indeed, the variable that gets incremented in the inner loop is <span class=\"SpellE\">i<\/span>, not j. This typo leads to an infinite loop. <\/span><span lang=\"RU\"><a href=\"https:\/\/godbolt.org\/z\/tJJ6Lw\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">By fixing the code<\/span><\/a><\/span><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\"> we have the program work as expected.<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">&nbsp;<img decoding=\"async\" class=\" size-full wp-image-5962\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/ProgramBugFixed.png\" alt=\"ProgramBugFixed\" width=\"650 \" height=\"306\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/ProgramBugFixed.png 1920w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/ProgramBugFixed-300x141.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/ProgramBugFixed-1024x482.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/ProgramBugFixed-768x361.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/08\/ProgramBugFixed-1536x722.png 1536w\" sizes=\"(max-width: 1920px) 100vw, 1920px\" \/><\/span><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">If you are a teacher, take note of this scenario of doing and assessing lab assignments \u2013 it may come in handy. This practice will also help your students get started with the static code analysis methodology.<\/span><\/p>\n<p class=\"MsoNormal\"><b style=\"mso-bidi-font-weight: normal;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Note.<\/span><\/b><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\"> This approach won&#8217;t be as convenient with more complex lab tasks or term papers, of course. In that case, you can install PVS-Studio on your computer and use a free license. See the &#8220;<\/span><span lang=\"RU\"><a href=\"https:\/\/www.viva64.com\/en\/for-students\/?promo=mcpp\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Free PVS-Studio for Students and Teachers<\/span><\/a><\/span><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">&#8221; section for details.<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">In the next section, we&#8217;ll talk about bugs and find out why we need to use static code analyzers.<\/span><\/p>\n<h1><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">C++ developers underestimate simple bugs<\/span><\/h1>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">If you asked a hypothetical C++ programmer what bugs they thought were most common, they would most likely name null pointers, division by zero, undefined behaviour, array-index-out-of-bounds, and uninitialized variables. But in the real world, things are quite different. That list is rather a collection of error patterns that we all have learned about during our study of programming or from books. In practice, however, it&#8217;s quite other types of bugs that take a lot of effort and time to find and fix and that, for some reason, still don&#8217;t get much attention in discussions. Let&#8217;s take a closer look at this interesting subject.<\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">As was already said, our hypothetical programmer would most likely mention division-by-zero and uninitialized variables as the most pestering bugs. They do occur, of course, but the frequency of their occurrence is exaggerated, to say the least.<\/span><\/p>\n<p class=\"MsoNormal\"><a name=\"_Hlk47785160\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US; mso-bidi-font-style: italic;\">The nature of our work implies checking tons of open-source <\/span><\/a><a href=\"https:\/\/www.viva64.com\/en\/inspections\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">projects<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US; mso-bidi-font-style: italic;\">. And it&#8217;s my experience of examining projects that enable me to argue that bugs related to divisions by zero and accessing uninitialized memory are pretty rare<\/span><\/span><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US; mso-bidi-font-style: italic;\">First, there aren&#8217;t many division-related bugs because you don&#8217;t often use the division operation in the first place :). Second, everybody knows about these bugs and is usually careful to check the divisor value. Third, compilers are <\/span><\/span><a href=\"https:\/\/godbolt.org\/z\/25aQkM\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">getting better and better<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US; mso-bidi-font-style: italic;\"> at finding such bugs.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">It&#8217;s about the same thing with uninitialized variables. This pattern is well known, and developers generally write code that&#8217;s clean in that respect. Fortunately, most agree that the scope of a variable should be as small as possible and that initializing a variable along with its declaration is a good style. Besides, today&#8217;s compilers are smart and will likely <\/span><\/span><a href=\"https:\/\/godbolt.org\/z\/APd6Ac\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">warn<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\"> you about uninitialized variables.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">So it turns out those types of bugs aren&#8217;t to be found really often in real-life projects. Then why do they come up so frequently in discussions? Perhaps those were the bugs that programmers were running into as beginners. Accidental use of uninitialized variables in one&#8217;s very first programs is a pretty plausible scenario. And it&#8217;s the very first experience that usually leaves the most lasting mark :). An alternative explanation is that those concerns date back to the old books written in times when even less safe programming languages were in use and compilers were too bad at detecting potential defects.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Okay, but what about undefined behaviour, null pointers, and array-index-out-of-bounds? These bugs are very common indeed and generally hard to catch, which makes programmers&#8217; concerns justifiable. Just look how many bug patterns based on null pointers alone we have collected across open-source projects: <\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/examples\/v522\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">V522<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">, <\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/examples\/v595\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">V595<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">, <\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/examples\/v757\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">V757<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">, <\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/examples\/v769\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">V769<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">, <\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/examples\/v1004\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">V1004<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">, etc. Developers expect these issues to show up and they do. Let&#8217;s move on to the most interesting category: the bugs which nobody talks about but which are awfully rife in programs.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">I insist that there are entire classes of bugs that are the real bane but which don&#8217;t get proper attention. I&#8217;m talking about typos.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">You might say it&#8217;s not a big deal. You surely know about typos, don&#8217;t you? What&#8217;s so secret about them? No secret, but this problem is hugely underestimated. If you want proof, just look at how test sets for evaluating analyzers are composed. I already wrote about that in the article &#8220;<\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/b\/0471\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Why I Dislike Synthetic Tests<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">&#8220;. For example, Toyota ITC provides for all kinds of null-pointer bugs and even unnatural defects:<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\">void null_pointer_006 ()<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\">{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp; <\/span>int *p;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp; <\/span>p = (int *)(<span class=\"SpellE\">intptr_t<\/span>)rand();<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp; <\/span>*p = 1; \/*Tool should detect this line as error*\/<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>\/*ERROR:NULL pointer dereference*\/<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\">}<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">You don&#8217;t believe, do you, that a bug like that will ever happen in real code. But Toyota developers still make tests for such anomalies. At the same time, you won&#8217;t find tests for typos like this:<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\">static void <span class=\"SpellE\">gen_prov_ack<\/span>(&#8230;.)<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\">{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp; <\/span>&#8230;.<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp; <\/span>if (<span class=\"SpellE\">link.tx.cb<\/span> &amp;&amp; <span class=\"SpellE\">link.tx.cb<\/span>)<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp; <\/span>&#8230;.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\">}<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">This is an absolutely real bug \u2013 we <\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/b\/0721\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">found<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\"> it in the Zephyr operating system.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Developers don&#8217;t talk much about typos. This probably accounts for the problem of typos being less clearly definable than, say, that of null pointers. A null pointer is something you know and understand. If there&#8217;s a path in your program where a pointer becomes null and gets dereferenced, this can be easily explained in a book, and you know how to find such bugs with static analysis.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Now, what&#8217;s a typo? Hmm&#8230; Good question. This could be a misspelt variable name, messed up parentheses, incorrect array index, misused copy-paste, and even confusion between &amp; and &amp;&amp;. We have thousands of such bug patterns in our database. I&#8217;m not actually sure to what articles exactly to refer you to, but <\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/examples\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">this list here<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\"> contains all those infinite variations of typos.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">I guess it&#8217;s because typos can&#8217;t be clearly defined and diagnosed that they keep escaping the attention of book authors and analyzer developers. Ordinary programmers seem to ignore them too for their simplicity. They just won&#8217;t take a typo seriously. &#8220;It&#8217;s just a slip-up \u2013 it won&#8217;t happen again!&#8221; With this mindset, they keep coding only to make another mistake and then waste time hunting it down :). And while we are at it, here&#8217;s a link to the small post &#8220;<\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/b\/0116\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">The second myth &#8211; expert developers do not make silly mistakes<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">&#8221; :).<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Carelessness and lack of concentration leading to typos eventually lead to an incredible amount of time wasted hunting them. To back up that statement, I refer you to three articles of mine, each of which is dedicated to some particular class of typos:<\/span><\/span><\/p>\n<ol start=\"1\" type=\"1\" style=\"margin-top: 0cm;\">\n<li class=\"MsoNormal\" style=\"mso-list: l0 level1 lfo1;\"><span style=\"mso-bookmark: _Hlk47785160;\"><\/span><a href=\"https:\/\/www.viva64.com\/en\/b\/0713\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Zero, one, two, Freddy&#8217;s coming for you<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">;<\/span><\/span><\/li>\n<li class=\"MsoNormal\" style=\"mso-list: l0 level1 lfo1;\"><span style=\"mso-bookmark: _Hlk47785160;\"><\/span><a href=\"https:\/\/www.viva64.com\/en\/b\/0509\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">The Evil within the Comparison Functions<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">;<\/span><\/span><\/li>\n<li class=\"MsoNormal\" style=\"mso-list: l0 level1 lfo1;\"><span style=\"mso-bookmark: _Hlk47785160;\"><\/span><a href=\"https:\/\/www.viva64.com\/en\/b\/0260\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">The Last Line Effect<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">.<\/span><\/span><\/li>\n<\/ol>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Do read them and your life will never be the same again. Typos \u2013 typos are everywhere, and they are not that harmless to the development process as you might think.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">What are you to do with this newly acquired knowledge? Well, I can share at least four useful tips.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><b style=\"mso-bidi-font-weight: normal;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">First: knowledge is power!<\/span><\/b><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Now that you are aware of the threat, you are going to stay alert to typos from now on, which is already a big step forward. You&#8217;ll be more likely to notice them when reviewing the code of your teammates. Especially in the last lines :).<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><b style=\"mso-bidi-font-weight: normal;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Second: table-style formatting<\/span><\/b><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Bad formatting prevents many typos from being noticed when coding and reviewing the code. Just rewrite your code in columns or table-style and bugs will stick out. Here&#8217;s an example to illustrate that. This is a bug <\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/b\/0487\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">detected<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\"> in FreeBSD Kernel with the PVS-Studio analyzer:<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\">MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_PERS_MIN || reg == A_TP_PERS_MAX ||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_KEEP_IDLE || A_TP_KEEP_INTVL ||<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Now let&#8217;s format this code as follows:<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\">MPASS(reg == A_TP_RXT_MIN ||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_RXT_MAX ||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_PERS_MIN ||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_PERS_MAX ||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_KEEP_IDLE ||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>A_TP_KEEP_INTVL ||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_INIT_SRTT ||<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_FINWAIT2_TIMER);<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">The incorrect condition with the <i style=\"mso-bidi-font-style: normal;\">A_TP_KEEP_INTVL<\/i> constant<span style=\"mso-bidi-font-style: italic;\"> <\/span>stands out <span style=\"mso-bidi-font-style: italic;\">clearly <\/span>now.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">I&#8217;d arrange it even more neatly:<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\">MPASS(reg == A_TP_RXT_MIN<span style=\"mso-spacerun: yes;\">&nbsp;&nbsp; <\/span>||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_RXT_MAX<span style=\"mso-spacerun: yes;\">&nbsp;&nbsp; <\/span>||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_PERS_MIN<span style=\"mso-spacerun: yes;\">&nbsp; <\/span>||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_PERS_MAX<span style=\"mso-spacerun: yes;\">&nbsp; <\/span>||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_KEEP_IDLE ||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>A_TP_KEEP_INTVL<span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>||<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_INIT_SRTT ||<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>reg == A_TP_FINWAIT2_TIMER);<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Such formatting takes time though. So I borrowed another table style for code formatting that prescribes writing logical operators separating conditions at the beginning of a line:<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\">MPASS(<span style=\"mso-spacerun: yes;\">&nbsp;&nbsp; <\/span>reg == A_TP_RXT_MIN<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>|| reg == A_TP_RXT_MAX<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>|| reg == A_TP_PERS_MIN<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>|| reg == A_TP_PERS_MAX<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>|| reg == A_TP_KEEP_IDLE<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>|| A_TP_KEEP_INTVL<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0cm;\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>|| reg == A_TP_INIT_SRTT<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"font-family: 'Courier New'; color: black; mso-ansi-language: EN-US;\"><span style=\"mso-spacerun: yes;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>|| reg == A_TP_FINWAIT2_TIMER);<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">This style may feel somewhat unusual at first, but you&#8217;ll get used to it quickly and find it very useful. Our team actually uses it in the code of PVS-Studio. So I do recommend adopting it \u2013 you&#8217;ll see bugs pop up at the coding stage already.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">I elaborated on this approach to formatting in Chapter 13 [Table-style formatting] in my mini-book &#8220;<\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/b\/0391\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">The Ultimate Question of Programming, Refactoring, and Everything<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">&#8220;.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><b style=\"mso-bidi-font-weight: normal;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Third: table-driven methods are good<\/span><\/b><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">If you deal with a lot of similar code blocks, you will probably find it best to program an algorithm using table-driven methods. These are nicely explained by Steven McConnell in his book &#8220;Code Complete&#8221; (Chapter 18):<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><i style=\"mso-bidi-font-style: normal;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">A table-driven method is a scheme that allows you to look up information in a table rather than using logic statements (if and case) to figure it out. Virtually anything you can select with logic statements, you can select with tables instead. In simple cases, logic statements are easier and more direct. As the logic chain becomes more complex, tables become increasingly attractive.<\/span><\/i><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Actually, I strongly recommend reading this wonderful book in full. If you are scared off by its size, then please do read this Chapter 18 at least. And then take the trouble to write a function of your own using table-driven methods. Yes, this will take a bit more time but it&#8217;s going to pay off in the end. Adding new conditions will become easier and faster, while the risk of making a mistake much less.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><b style=\"mso-bidi-font-weight: normal;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Fourth: use code analyzers<\/span><\/b><\/span><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\"><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">A static code analyzer is a diligent and never-tiring assistant capable of finding typos at the earlier stages. The key here is to use it regularly. Rather than running it from time to time, integrate the analyzer into your development process. Should you meet a sceptic, refer them to this <\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/b\/0687\/\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">article<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">, where we counter the typical arguments against static analysis.<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">Thanks for reading. I hope I&#8217;ve managed to get you to look at software bugs and code review from a new perspective. Welcome to try our code analyzer <\/span><\/span><a href=\"https:\/\/www.viva64.com\/en\/pvs-studio-download\/?promo=mcpp\"><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">PVS-Studio<\/span><\/span><\/a><span style=\"mso-bookmark: _Hlk47785160;\"><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\">. It can become your biggest helper in typo search, allowing you to review code focusing on bugs in algorithms and high-level examination of the code rather than hunting out a misplaced parenthesis.<\/span><\/span><span lang=\"EN-US\" style=\"mso-ansi-language: EN-US;\"><\/span><\/p>\n<p><strong><i>Get a month free trial of the PVS-Studio analyzer using the <\/i><i>#mcpp <\/i><i>promo code.<\/i><\/strong><b><br \/> <\/b><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you know that PVS-Studio is integrated into the Compiler Explorer? If not, you should definitely read this guest post from Andrey Karpov including a promo code.&nbsp;<\/p>\n","protected":false},"author":21,"featured_media":5959,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[360],"tags":[],"class_list":["post-5963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5963"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5963\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5959"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}