目录

C++ Coding Specifications

Basic Requirements

[h2]Variables

[Rule] Assign initial values to pointer variables, resource descriptors, and Boolean variables.

[Rule] Assign a new value to pointers, resource handles and descriptors immediately after free

[Rule] Assign initial values to class member variables in the constructor.

[Rule] Do not perform sizeof operations on pointer variables.

[Recommendation] Use const.

[Recommendation] If the access to a global variable involves multiple threads, multi-thread race conditions may occur.

[Recommendation] Ensure that local variables in a function take up less than 16 KB of space.

[h2]Functions

[Rule] When an array is used as a function parameter, its length must also be used as a function parameter.

[Rule] Define pointer parameters that do not modify content as const.

[Recommendation] Exercise caution when using non-reentrant functions.

[Recommendation] Check whether the parameter is null when a string or pointer is used as a function parameter

[h2]Loop

[Rule] Set terminating conditions for loops.

[h2]Classes

[Rule] If a constructor allocates resources that need to be manually released, a destructor must be provided.

[Rule] Do not perform any operations that may fail on a constructor.

[Rule] Do not create a thread in the construction function.

[Rule] Do not perform the "delete this" operation.

[Recommendation] Avoid defining public members.

String/Array Operations

[Rule] Ensure that the storage space is sufficient.

[Rule] Ensure that strings to be stored end with \0.

[Rule] Ensure that external data to be used as an array index is within the array scope

[Rule] Check the validity of external data used as the length of a replication memory operation function

[Rule] Ensure that the format parameter is not externally controllable when a formatted function is called

[Rule] When the formatting function is called, the type and number of parameters in the format must be the same as the actual parameter type and number.

Integers

[Rule] Strictly check integer operations to ensure no overflow, wrap, or division by zero.

[Rule] Evaluate integer expressions in a larger size before comparing or assigning to that size

[Rule] Do not use bitwise operators for signed integers.

[Rule] Do not convert between integer and pointer. In a 64-bit system, this may cause high-order bit truncation. Use safe conversion types such as intptr_t.

[Rule] Verify the number of cycles if it is controlled by external data

[Rule] Do not perform logical or bitwise operations on pointers.

Memory

[Rule] Verify the requested memory size before requesting memory

[Rule] Check whether memory allocation is successful

[Rule] Do not reference uninitialized memory.

[Rule] Assign a new value immediately after memory deallocation.