diff --git a/cppguide.html b/cppguide.html index c5c7b98d6..32e1962e1 100644 --- a/cppguide.html +++ b/cppguide.html @@ -6123,3 +6123,26 @@

Windows Code

+ +

Security Coding Best Practice Example

+

Below is a secure C++ code example following Google C++ Style Guide, to avoid null‑pointer dereference and integer overflow vulnerabilities:

+
#include <stdexcept>
+#include <climits>
+
+// Safe integer addition to prevent overflow
+int SafeAdd(int a, int b) {
+  if ((b > 0 && a > INT_MAX - b) || (b < 0 && a < INT_MIN - b)) {
+    throw std::overflow_error("Integer overflow prevented");
+  }
+  return a + b;
+}
+
+// Safe pointer access to avoid null‑pointer dereference
+int SafeAccess(int* ptr) {
+  if (ptr == nullptr) {
+    throw std::invalid_argument("Null pointer access blocked");
+  }
+  return *ptr;
+}
+
+

Security Notes: Always validate input before operation; use defensive programming to avoid undefined behavior.