From bcd0cf245e9638a5f39c9340a28313dc6a3814c4 Mon Sep 17 00:00:00 2001 From: Martin Nowack Date: Thu, 18 Oct 2018 14:01:51 +0100 Subject: Avoid unsafe static downcasts --- lib/Expr/ArrayExprOptimizer.cpp | 20 +++++++++++++------- lib/Expr/ArrayExprRewriter.cpp | 12 +++++++----- 2 files changed, 20 insertions(+), 12 deletions(-) (limited to 'lib/Expr') diff --git a/lib/Expr/ArrayExprOptimizer.cpp b/lib/Expr/ArrayExprOptimizer.cpp index cadd9588..94bf2487 100644 --- a/lib/Expr/ArrayExprOptimizer.cpp +++ b/lib/Expr/ArrayExprOptimizer.cpp @@ -163,9 +163,10 @@ bool ExprOptimizer::computeIndexes(array2idx_ty &arrays, const ref &e, // skipping all those indexes that are not multiple of such value. // In fact, they will be rejected by the MulExpr interpreter since it // will not find any integer solution - Expr &e = *idxt_v.getMul(); - auto &ce = static_cast(e); - uint64_t mulVal = (*ce.getAPValue().getRawData()); + auto e = idxt_v.getMul(); + auto ce = dyn_cast(e); + assert(ce && "Not a constant expression"); + uint64_t mulVal = (*ce->getAPValue().getRawData()); // So far we try to limit this optimization, but we may try some more // aggressive conditions (i.e. mulVal > width) if (width == 1 && mulVal > 1) @@ -234,10 +235,12 @@ ref ExprOptimizer::getSelectOptExpr( "Expected concrete array, found symbolic array"); auto arrayConstValues = read->updates.root->constantValues; for (const UpdateNode *un = read->updates.head; un; un = un->next) { - auto *ce = static_cast(un->index.get()); + auto ce = dyn_cast(un->index); + assert(ce && "Not a constant expression"); uint64_t index = ce->getAPValue().getZExtValue(); assert(index < arrayConstValues.size()); - auto *arrayValue = static_cast(un->value.get()); + auto arrayValue = dyn_cast(un->value); + assert(arrayValue && "Not a constant expression"); arrayConstValues[index] = arrayValue; } std::vector arrayValues; @@ -307,13 +310,16 @@ ref ExprOptimizer::getSelectOptExpr( } } for (const UpdateNode *un = read->updates.head; un; un = un->next) { - auto *ce = static_cast(un->index.get()); + auto ce = dyn_cast(un->index); + assert(ce && "Not a constant expression"); uint64_t index = ce->getAPValue().getLimitedValue(); if (!isa(un->value)) { ba.set(index); } else { ba.unset(index); - auto *arrayValue = static_cast(un->value.get()); + auto arrayValue = + dyn_cast(un->value); + assert(arrayValue && "Not a constant expression"); arrayConstValues[index] = arrayValue; } } diff --git a/lib/Expr/ArrayExprRewriter.cpp b/lib/Expr/ArrayExprRewriter.cpp index 7bbadd61..8306e20a 100644 --- a/lib/Expr/ArrayExprRewriter.cpp +++ b/lib/Expr/ArrayExprRewriter.cpp @@ -48,9 +48,11 @@ ref ExprRewriter::rewrite(const ref &e, const array2idx_ty &arrays, // skipping all those indexes that are not multiple of such value. // In fact, they will be rejected by the MulExpr interpreter since it // will not find any integer solution - Expr &e = *idxt_v.getMul(); - auto &ce = static_cast(e); - llvm::APInt val = ce.getAPValue(); + auto e = idxt_v.getMul(); + auto ce = dyn_cast(e); + assert(ce && "Not a constant expression"); + + llvm::APInt val = ce->getAPValue(); uint64_t mulVal = val.getZExtValue(); // So far we try to limit this optimization, but we may try some more // aggressive conditions (i.e. mulVal > width) @@ -76,8 +78,8 @@ ref ExprRewriter::rewrite(const ref &e, const array2idx_ty &arrays, unsigned set = 0; BitArray ba(arr->size / width); for (auto &vals : opt_indexes) { - auto &ce = static_cast(*vals); - llvm::APInt v = ce.getAPValue(); + auto ce = dyn_cast(vals); + llvm::APInt v = ce->getAPValue(); ba.set(v.getZExtValue() / width); set++; } -- cgit 1.4.1