CMP EMBEDDED.COM

Login | Register     Welcome Guest  
HOME DESIGN PRODUCTS COLUMNS E-LEARNING CONFERENCES CODE FORUMS/BLOGS NEWSLETTERS CONTACT FEATURES RSS RSS

Bug-killing standards for firmware coding



Embedded.com

Ten bug-killing rules
Here are some examples of coding rules you can follow to reduce or eliminate certain types of firmware bugs.


Rule #1:
Braces ({ }) shall always surround the blocks of code (also known as compound statements) following if, else, switch, while, do, and for keywords. Single statements and empty statements following these keywords shall also always be surrounded by braces.

Example (don't):

if (timer.done)
   // A single statement needs braces!
   timer.control = TIMER_RESTART;

Example (do):

while (!timer.done)
{
   // Even an empty statement should be
   // surrounded by braces.
}

Reasoning: Considerable risk is associated with the presence of empty statements and single statements that are not surrounded by braces. Code constructs of this type are often associated with bugs when nearby code is changed or commented out. This type of bug is entirely eliminated by the consistent use of braces.



Rule #2:
The const keyword shall be used whenever possible, including:

• To declare variables that should not be changed after initialization,

• To define call-by-reference function parameters that should not be modified (for example, char const * p_data),

• To define fields in structs and unions that cannot be modified (such as in a struct overlay for memory-mapped I/O peripheral registers), and

• As a strongly typed alternative to #define for numerical constants.

Reasoning: The upside of using const as much as possible is compiler-enforced protection from unintended writes to data that should be read-only.



Rule #3: The static keyword shall be used to declare all functions and variables that do not need to be visible outside of the module in which they are declared.

Reasoning: C's static keyword has several meanings. At the module-level, global variables and functions declared static are protected from inadvertent access from other modules. Heavy-handed use of static in this way thus decreases coupling and furthers encapsulation.



Rule #4:
The volatile keyword shall be used whenever appropriate, including:

• To declare a global variable accessible (by current use or scope) by any interrupt service routine,

• To declare a global variable accessible (by current use or scope) by two or more tasks, and

• To declare a pointer to a memory-mapped I/O peripheral register set (for example, timer_t volatile * const p_timer).1

Reasoning: Proper use of volatile eliminates a whole class of difficult-to-detect bugs by preventing the compiler from making optimizations that would eliminate requested reads or writes to variables or registers that may be changed at any time by a parallel-running entity.2



Rule #5:
Comments shall neither be nested nor used to disable a block of code, even temporarily. To temporarily disable a block of code, use the preprocessor's conditional compilation feature (for example, #if 0 ... #endif).

Example (don't):

/*
 a = a + 1;
 /* comment */
 b = b + 1;
*/

Reasoning: Nested comments and commented-out code both run the risk of allowing unexpected snippets of code to be compiled into the final executable.


View the full-size image



Rule #6:
Whenever the width, in bits or bytes, of an integer value matters in the program, a fixed-width data type shall be used in place of char, short, int, long, or long long. The signed and unsigned fixed-width integer types shall be as shown in Table 1.

Reasoning: The ISO C standard allows implementation-defined widths for char, short, int, long, and long long types, which leads to portability problems. Though the 1999 standard did not change this underlying issue, it did introduce the uniform type names shown in the table, which are defined in the new header file <stdint.h>. These are the names to use even if you have to create the typdefs by hand.



Rule #7:
None of the bit-wise operators (in other words, &, |, ~, ^, <<, and >>) shall be used to manipulate signed integer data.

Example (don't):

int8_t  signed_data = -4;
signed_data >>= 1;  // not necessarily -2 

Reasoning: The C standard does not specify the underlying format of signed data (for example, 2's complement) and leaves the effect of some bit-wise operators to be defined by the compiler author.

1 | 2 | 3 | 4 | 5

Rate this article: Low High
Current rating
  • .
Embedded.com Career Center
Looking for a new job?
SEARCH JOBS

Browse all jobs

SPONSOR
RECENT JOB POSTINGS



TECH PAPER
WEBINAR
WEBINAR
WEBINAR




 :