Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
sei_cert_c_coding_standard [2019/08/06 20:07] rpjdaysei_cert_c_coding_standard [2019/08/06 21:17] (current) – [3.5 DCL38-C. Use the correct syntax when declaring a flexible array member] rpjday
Line 3: Line 3:
   * [[https://wiki.sei.cmu.edu/confluence/display/c]]   * [[https://wiki.sei.cmu.edu/confluence/display/c]]
  
-=== = Preprocessor (PRE) =====+===== Preprocessor (PRE) =====
  
 ==== 2.1 PRE30-C. Do not create a universal character name through concatenation ==== ==== 2.1 PRE30-C. Do not create a universal character name through concatenation ====
  
-==== 2.2 PRE31-C. Avoid side effects in arguments to unsafe macros+==== 2.2 PRE31-C. Avoid side effects in arguments to unsafe macros ====
  
-==== 2.3 PRE32-C. Do not use preprocessor directives in invocations of function-like macros+An unsafe function-like macro is one whose expansion results in evaluating one of its parameters more than once or not at all. 
 + 
 +<code> 
 +#define ABS(x) (((x) < 0) ? -(x) : (x)) /* UNSAFE */ 
 +   
 +void func(int n) { 
 +  /* Validate that n is within the desired range */ 
 +  ++n; 
 +  int m = ABS(n); 
 +  
 +  /* ... */ 
 +
 +</code> 
 +==== 2.3 PRE32-C. Do not use preprocessor directives in invocations of function-like macros ==== 
 + 
 +The arguments to a macro must not include preprocessor directives, such as #define, #ifdef, and #include. 
 + 
 +<code> 
 +void func(const char *src) { 
 +  /* Validate the source string; calculate size */ 
 +  char *dest; 
 +  /* malloc() destination string */ 
 +  memcpy(dest, src, 
 +    #ifdef PLATFORM1 
 +      12 
 +    #else 
 +      24 
 +    #endif 
 +  ); 
 +  /* ... */ 
 +
 +</code> 
 + 
 +===== 3 Declarations and Initialization (DCL) ===== 
 + 
 +==== 3.1 DCL30-C. Declare objects with appropriate storage durations ==== 
 + 
 +==== 3.2 DCL31-C. Declare identifiers before using them ==== 
 + 
 +==== 3.3 DCL36-C. Do not declare an identifier with conflicting linkage classifications ==== 
 + 
 +==== 3.4 DCL37-C. Do not declare or define a reserved identifier ==== 
 + 
 +==== 3.5 DCL38-C. Use the correct syntax when declaring a flexible array member ==== 
 + 
 +  * The incomplete array type must be the last element within the structure. 
 +  * There cannot be an array of structures that contain a flexible array member. 
 +  * Structures that contain a flexible array member cannot be used as a member of another structure. 
 +  * The structure must contain at least one named member in addition to the flexible array member. 
 + 
 +<code> 
 +struct flexArrayStruct{ 
 +  int num; 
 +  int data[]; 
 +}; 
 +</code> 
 +==== 3.6 DCL39-C. Avoid information leakage when passing a structure across a trust boundary ==== 
 + 
 +==== 3.7 DCL40-C. Do not create incompatible declarations of the same function or object ==== 
 + 
 +==== 3.8 DCL41-C. Do not declare variables inside a switch statement before the first case label ====
  • sei_cert_c_coding_standard.1565122073.txt.gz
  • Last modified: 2019/08/06 20:07
  • by rpjday