Skip to content

Commit 0331608

Browse files
Improve text on removing implicit casts
Co-authored-by: David A. Wheeler <[email protected]> Signed-off-by: Thomas Nyman <[email protected]>
1 parent 6cd4f38 commit 0331608

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

docs/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,14 @@ For brown-field projects (existing code) we recommend a staged rollout:
323323
3. Triage remaining warnings, prioritising high-risk ones (conversions that influences array indexing, object size calculations, or security-sensitive logic) and refactor them or make the casts explicit.
324324
4. Gradually tighten the flag: warning → error → part of CI.
325325

326-
##### Replacing implicit casts with explicit ones
326+
##### Removing implicit casts
327327

328-
Explicit casts are a temporary workaround, not a long-term solution. Each cast should eventually be:
328+
Where practical, implicit casts that cause data loss should be refactored so that they are unnecessary. For example, they can be:
329329

330-
- Eliminated by harmonizing the types, or
331-
- Justified with a comment, or
330+
- Eliminated by harmonizing the types
332331
- Rewritten to reflect the programmer’s intent clearly (e.g., clamping values instead of truncating bits).
333332

334-
Replacing implicit conversions with explicit C-style casts can introduce new bugs. For example, if an implicit cast from a 32-bit value to a 16-bit short is mistakenly replaced with a cast to an 8-bit char, this truncates more bits and misrepresents intent.
333+
If such a cast is necessary, convert it from an implicit cast to an explicit cast. Each such cast should be justified with a comment. A problem with replacing implicit conversions with explicit C-style casts is that they can introduce new bugs when used incorrectly. For example, if an implicit cast from a 32-bit value to a 16-bit short is mistakenly replaced with a cast to an 8-bit char, this truncates more bits and misrepresents intent.
335334

336335
##### Warning noise from third-party headers
337336

0 commit comments

Comments
 (0)