Secure Coding Practices in C++
These secure coding practices were picked from the list maintained by Carnegie Mellon University Software Engineering Institute:
-
DCL52-CPP: Never qualify a reference type with const or volatile
-
DCL57-CPP: Do not let exceptions escape from destructors or deallocation functions
-
DCL58-CPP. Do not modify the standard namespaces
-
DCL59-CPP. Do not define an unnamed namespace in a header file
-
DCL60-CPP. Obey the one-definition rule
-
DCL30-C. Declare objects with appropriate storage durations
-
DCL40-C. Do not create incompatible declarations of the same function or object
-
EXP50-CPP. Do not depend on the order of evaluation for side effects
-
EXP51-CPP. Do not delete an array through a pointer of the incorrect type
-
EXP52-CPP. Do not rely on side effects in unevaluated operands
-
EXP53-CPP. Do not read uninitialized memory
-
EXP54-CPP. Do not access an object outside of its lifetime
-
EXP55-CPP. Do not access a cv-qualified object through a cv-unqualified type
-
EXP57-CPP. Do not cast or delete pointers to incomplete classes
-
EXP61-CPP. A lambda object must not outlive any of its reference captured objects
-
EXP62-CPP. Do not access the bits of an object representation that are not part of the object's value representation
-
EXP34-C. Do not dereference null pointers
-
EXP35-C. Do not modify objects with temporary lifetime
-
EXP39-C. Do not access a variable through a pointer of an incompatible type
-
EXP42-C. Do not compare padding data
-
EXP45-C. Do not perform assignments in selection statements
-
INT50-CPP. Do not cast to an out-of-range enumeration value
-
INT30-C. Ensure that unsigned integer operations do not wrap
-
INT31-C. Ensure that integer conversions do not result in lost or misinterpreted data
-
INT32-C. Ensure that operations on signed integers do not result in overflow
-
INT33-C. Ensure that division and remainder operations do not result in divide-by-zero errors
-
INT34-C. Do not shift an expression by a negative number of bits or by greater than or equal to the number of bits that exist in the operand
-
CTR50-CPP. Guarantee that container indices and iterators are within the valid range
Skipped rest
-
ARR30-C. Do not form or use out-of-bounds pointers or array subscripts
-
STR50-CPP. Guarantee that storage for strings has sufficient space for character data and the null terminator
-
STR51-CPP. Do not attempt to create a std::string from a null pointer
-
STR52-CPP. Use valid references, pointers, and iterators to reference elements of a basic_string - simplify without begin and end
-
STR53-CPP. Range check element access
-
STR30-C. Do not attempt to modify string literals
-
STR31-C. Guarantee that storage for strings has sufficient space for character data and the null terminator
-
MEM50-CPP. Do not access freed memory
-
MEM51-CPP. Properly deallocate dynamically allocated resources
-
MEM52-CPP. Detect and handle memory allocation errors
-
MEM53-CPP. Explicitly construct and destruct objects when manually managing object lifetime
-
MEM31-C. Free dynamically allocated memory when no longer needed
-
MEM34-C. Only free memory allocated dynamically
-
FIO50-CPP. Do not alternately input and output from a file stream without an intervening positioning call
-
FIO51-CPP. Close files when they are no longer needed
-
FIO40-C. Reset strings on fgets() or fgetws() failure
-
FIO41-C. Do not call getc(), putc(), getwc(), or putwc() with a stream argument that has side effects
-
FIO42-C. Close files when they are no longer needed
-
ERR51-CPP. Handle all exceptions
-
ERR53-CPP. Do not reference base classes or class data members in a constructor or destructor function-try-block handler
-
ERR54-CPP. Catch handlers should order their parameter types from most derived to least derived
-
ERR55-CPP. Honor exception specifications
-
ERR57-CPP. Do not leak resources when handling exceptions
-
ERR58-CPP. Handle all exceptions thrown before main() begins executing
-
ERR60-CPP. Exception objects must be nothrow copy constructible
-
ERR61-CPP. Catch exceptions by lvalue reference
-
ERR62-CPP. Detect errors when converting a string to a number
-
ERR30-C. Set errno to zero before calling a library function known to set errno, and check errno only after the function returns a value indicating failure
-
OOP50-CPP. Do not invoke virtual functions from constructors or destructors\\
-
OOP51-CPP. Do not slice derived objects
-
OOP52-CPP. Do not delete a polymorphic object without a virtual destructor
-
OOP53-CPP. Write constructor member initializers in the canonical order
-
OOP54-CPP. Gracefully handle self-copy assignment
-
OOP55-CPP. Do not use pointer-to-member operators to access nonexistent members
-
OOP58-CPP. Copy operations must not mutate the source object
-
MSC51-CPP. Ensure your random number generator is properly seeded
-
MSC52-CPP. Value-returning functions must return a value from all exit paths
-
MSC37-C. Ensure that control never reaches the end of a non-void function
-
FLP30-C. Do not use floating-point variables as loop counters
-
ENV30-C. Do not modify the object referenced by the return value of certain functions
-
ENV32-C. All exit handlers must return normally
-
PRE31-C. Avoid side effects in arguments to unsafe macros