aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaChecking.cpp
diff options
context:
space:
mode:
authoreahcmrh <chris.hamilton@ericsson.com>2021-06-16 23:35:19 +0200
committereahcmrh <chris.hamilton@ericsson.com>2021-06-17 16:16:59 +0200
commitfc6ec9b98cf96bca8f68cc65395f861919c06c2f (patch)
tree60b2426912fea6c728d3e61f5f0979f37ec4fa20 /clang/lib/Sema/SemaChecking.cpp
parentfa1de88f81e9c6db5255ca7c4d0fd25606c5a054 (diff)
downloadllvm-fc6ec9b98cf96bca8f68cc65395f861919c06c2f.zip
llvm-fc6ec9b98cf96bca8f68cc65395f861919c06c2f.tar.gz
llvm-fc6ec9b98cf96bca8f68cc65395f861919c06c2f.tar.bz2
[Sema] Fix for PR50741
Fixed crash when doing pointer math on a void pointer. Also, reworked test to use -verify rather than FileCheck. Reviewed By: erichkeane Differential Revision: https://reviews.llvm.org/D104424
Diffstat (limited to 'clang/lib/Sema/SemaChecking.cpp')
-rw-r--r--clang/lib/Sema/SemaChecking.cpp11
1 files changed, 8 insertions, 3 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index a9915cb..32b7861 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -14567,8 +14567,13 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
EffectiveType->getCanonicalTypeInternal()));
if (index.getBitWidth() < AddrBits)
index = index.zext(AddrBits);
- CharUnits ElemCharUnits = ASTC.getTypeSizeInChars(EffectiveType);
- llvm::APInt ElemBytes(index.getBitWidth(), ElemCharUnits.getQuantity());
+ Optional<CharUnits> ElemCharUnits =
+ ASTC.getTypeSizeInCharsIfKnown(EffectiveType);
+ // PR50741 - If EffectiveType has unknown size (e.g., if it's a void
+ // pointer) bounds-checking isn't meaningful.
+ if (!ElemCharUnits)
+ return;
+ llvm::APInt ElemBytes(index.getBitWidth(), ElemCharUnits->getQuantity());
// If index has more active bits than address space, we already know
// we have a bounds violation to warn about. Otherwise, compute
// address of (index + 1)th element, and warn about bounds violation
@@ -14599,7 +14604,7 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
DiagRuntimeBehavior(BaseExpr->getBeginLoc(), BaseExpr,
PDiag(DiagID)
<< toString(index, 10, true) << AddrBits
- << (unsigned)ASTC.toBits(ElemCharUnits)
+ << (unsigned)ASTC.toBits(*ElemCharUnits)
<< toString(ElemBytes, 10, false)
<< toString(MaxElems, 10, false)
<< (unsigned)MaxElems.getLimitedValue(~0U)