Skip to content

Commit 6794e45

Browse files
committed
add wraps bypass for implicit-(un)signed-integer-truncation sanitizers
Signed-off-by: Justin Stitt <[email protected]>
1 parent 10ee328 commit 6794e45

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,8 @@ void ScalarExprEmitter::EmitIntegerTruncationCheck(Value *Src, QualType SrcType,
11081108
// If the comparison result is 'i1 false', then the truncation was lossy.
11091109

11101110
// Do we care about this type of truncation?
1111-
if (!CGF.SanOpts.has(Check.second.second))
1111+
if (!CGF.SanOpts.has(Check.second.second) ||
1112+
DstType.getTypePtr()->hasAttr(attr::Wraps))
11121113
return;
11131114

11141115
llvm::Constant *StaticArgs[] = {

clang/lib/Sema/SemaChecking.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16439,7 +16439,8 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
1643916439
S.Context, E, S.isConstantEvaluatedContext(), /*Approximate=*/true);
1644016440
IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target);
1644116441

16442-
if (LikelySourceRange.Width > TargetRange.Width) {
16442+
if (LikelySourceRange.Width > TargetRange.Width &&
16443+
!T.getTypePtr()->hasAttr(attr::Wraps)) {
1644316444
// If the source is a constant, use a default-on diagnostic.
1644416445
// TODO: this should happen for bitfield stores, too.
1644516446
Expr::EvalResult Result;
@@ -16487,7 +16488,7 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
1648716488

1648816489
if (TargetRange.Width == LikelySourceRange.Width &&
1648916490
!TargetRange.NonNegative && LikelySourceRange.NonNegative &&
16490-
Source->isSignedIntegerType()) {
16491+
Source->isSignedIntegerType() && !T.getTypePtr()->hasAttr(attr::Wraps)) {
1649116492
// Warn when doing a signed to signed conversion, warn if the positive
1649216493
// source value is exactly the width of the target type, which will
1649316494
// cause a negative value to be stored.

0 commit comments

Comments
 (0)