Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cppguide.html
Original file line number Diff line number Diff line change
Expand Up @@ -6123,3 +6123,26 @@ <h3 id="Windows_Code">Windows Code</h3>
</div>
</body>
</html>

<h2 id="security‑coding‑best‑practice">Security Coding Best Practice Example</h2>
<p>Below is a secure C++ code example following Google C++ Style Guide, to avoid null‑pointer dereference and integer overflow vulnerabilities:</p>
<pre><code class="language‑cpp">#include &lt;stdexcept&gt;
#include &lt;climits&gt;

// Safe integer addition to prevent overflow
int SafeAdd(int a, int b) {
if ((b &gt; 0 && a &gt; INT_MAX - b) || (b &lt; 0 && a &lt; 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;
}
</code></pre>
<p><strong>Security Notes:</strong> Always validate input before operation; use defensive programming to avoid undefined behavior.</p>