From: Ted Kremenek Date: Fri, 18 Feb 2011 02:27:00 +0000 (+0000) Subject: Fix assertion failure on -Warray-bounds for 32-bit builds of Clang. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=25b3b849e3ad340b04442707038c86980035765b;p=clang Fix assertion failure on -Warray-bounds for 32-bit builds of Clang. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@125821 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 6b1013d82d..2cddac5f66 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -3109,11 +3109,14 @@ void Sema::CheckArrayAccess(const clang::ArraySubscriptExpr *E) { return; if (!index.isNegative()) { - const llvm::APInt &size = ArrayTy->getSize(); + llvm::APInt size = ArrayTy->getSize(); if (!size.isStrictlyPositive()) return; if (size.getBitWidth() > index.getBitWidth()) index = index.sext(size.getBitWidth()); + else if (size.getBitWidth() < index.getBitWidth()) + size = size.sext(index.getBitWidth()); + if (index.slt(size)) return; diff --git a/test/SemaCXX/array-bounds.cpp b/test/SemaCXX/array-bounds.cpp index 8c22865e79..0286c01d85 100644 --- a/test/SemaCXX/array-bounds.cpp +++ b/test/SemaCXX/array-bounds.cpp @@ -85,3 +85,9 @@ int test_no_warn_macro_unreachable() { ARR_IN_MACRO(1, arr, SIZE); // expected-warning{{array index of '10' indexes past the end of an array (that contains 10 elements)}} } +// This exhibited an assertion failure for a 32-bit build of Clang. +int test_pr9240() { + short array[100]; // expected-note {{array 'array' declared here}} + return array[(unsigned long long) 100]; // expected-warning {{array index of '100' indexes past the end of an array (that contains 100 elements)}} +} +