The-Software-Experts |
|
![]() catch your bugs! |
Static Code Analysis
A static code analysis is commonly understood to be an automatic check by a tool. This is only a recent development. Only 10 years ago these tools
were not as good as they are today and they were not available in such a variety as today.
Static code analysis is simply part of what so far was called a static white-box test. This is a test which is performed on the source
code without executing it. Thus it is called "static".
The expression "white-box" stands for the kind of test which is concerned with the implementation of the code i.e. its
structure and details,
calculations and data operations. The overall functionality of the software is not in focus, but the details, quality and
safety of its programming.
A part of the test can be done automatically using analysis tools as e.g. Lint or the Safer-C toolset.
But this should not be all it has to be followed by a manual code inspection, which is an inspection or review of the source code by
specialists, according to defined rules. The output of the test is a
proper test report and respective inputs for a defect data collection for statistical and process improvement purposes. Types of Code CheckersNot all the code checkers are doing the same kind of analysis. Again here I will focus on the code analysis tools which are suitable for C and the microcontroller world. There are varoius checkers available. They can be grouped into 4 main categories. So far I did not find an official naming for the categories, so I made up my own. 1. Pitfalls and Traps Checkers
There is a commonly agreed area of C programming which is considered as dangerous programming. The outlines of this
kind of programming can be found in my course description
on safe programming in C. You will find similar information in Andrew Koenig's book "C Traps and Pitfalls" and a number
of other publications. Last but not least there is a so called MISRA standard which addresses the problem.
There are a number of checkers which try to find as much as possible of these language transgressions. One of the most
widely used is PC-Lint. However there are other similar ones, e.g. the Safer-C tool of Prof. Les Hatton or a tool called QA/C
made by a company with the same name, and finally a tool called Logiscope made by Telelogic.
2. Configurable Checkers
Although the simple traps and pitfalls checkers are configurable e.g. regarding the bit size of variables and the rules which have to
be checked or omitted, the term "configurable" means something else here. Configurable means that the tool offers an interface, i.e.
in most cases a script language, which can be used to write additional scripts to check additional proprietary rules. You may have
own rules and design guidelines which you want to enforce. E.g. this could be a naming convention. Imagine you have a coding rule
which says that you have to encode the data type of a variable in the variable name. By the way this is a very good style! An
encoding I have seen is e.g. to add a suffix to the variable name with a shortage of the data type. E.g. MyVariable_us would stand
for an "unsigned short". MyVariable_uc for an "unsigned char" and so on. A script can easily search all your variable names for
the existence of one of the defined suffixes (_uc, _us, etc. etc.). A script could even do further checks and search the source code to
see if I defined my variables correctly i.e. matching the suffixes. E.g. it would search for a variable definition "unsigned short MyVariable_us;"
in case it would find "unsigned char MyVariable_us;" it comes up with an error message warning me of a mismatch of the naming and
the definition. 3. Sophisticated CheckersDuring the recent years some promissing attempts have been made to dig with static checkers into the domain of so far dynamic checks. One of the tools in this field is Polyspace. These checkers attempt to find runtime problems before the SW is executed. Imagine a code line like "unsigned short = unsigned short * unsigned short". For runtime and other resource reasons the programmer choose to use an unsigned short as variable for the result of the multiplication. Now this is potentially very dangerous. Depending on the values which have to be multiplied the calcualtion will overflow or not. Sopisticated checkers will attempt to find out and calculate the worst case inputs into such an operation. Depending on these worst case inputs they will issue a warning or not. The sophisticated checkers are usually very expensive. However not the price is the problem. These tools will usually come with a threefold result for critical code lines. They will give you a "green" in case everything is fine. They will give you a "red" in case they detected a real problem, and they will give you a "yellow" which basically means "I don't know you have to check by hand". Personally I would be grateful to use such a tool if they leave me with 10% or 20% of yellows. Then I have a big benefit. But if I get 80% to 90% yellow, I can not see the benefit. However, there is a future for these tools and as time goes on I am sure the benefit will rise. 4. Supporting ToolsThere are also some supporting tools like SNIFF+ or CodeSurfer. They were made for re-engineerig of software and better project overview. If you are confronted with an unknown source code for review or maintenance these tools make it much easier to dig into it and understand foreign code. Instead of only focusing on individual C expressions they visualize the logic and structure of a program. You will get call graphs and data flow charts of of these little helpers. They can not be considered as true automatic checkers but since I said in the introduction to this page that automatic checks are only part of the bigger subject of static white-box tests, the supporting tools would be the bridge between automatic checks and manual checks, supplementing them with tool support. Things to observe when you do automatic code checks
|
|