clang 23.0.0git
UndefinedNewArraySizeChecker.cpp
Go to the documentation of this file.
1//===--- UndefinedNewArraySizeChecker.cpp -----------------------*- C++ -*--==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This defines UndefinedNewArraySizeChecker, a builtin check in ExprEngine
10// that checks if the size of the array in a new[] expression is undefined.
11//
12//===----------------------------------------------------------------------===//
13
20
21using namespace clang;
22using namespace ento;
23
24namespace {
25class UndefinedNewArraySizeChecker : public Checker<check::PreCall> {
26
27private:
28 BugType BT{this, "Undefined array element count in new[]",
30
31public:
32 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
33 void HandleUndefinedArrayElementCount(CheckerContext &C, SVal ArgVal,
34 const Expr *Init,
35 SourceRange Range) const;
36};
37} // namespace
38
39void UndefinedNewArraySizeChecker::checkPreCall(const CallEvent &Call,
40 CheckerContext &C) const {
41 if (const auto *AC = dyn_cast<CXXAllocatorCall>(&Call)) {
42 if (!AC->isArray())
43 return;
44
45 auto *SizeEx = *AC->getArraySizeExpr();
46 auto SizeVal = AC->getArraySizeVal();
47
48 if (SizeVal.isUndef())
49 HandleUndefinedArrayElementCount(C, SizeVal, SizeEx,
50 SizeEx->getSourceRange());
51 }
52}
53
54void UndefinedNewArraySizeChecker::HandleUndefinedArrayElementCount(
55 CheckerContext &C, SVal ArgVal, const Expr *Init, SourceRange Range) const {
56
57 if (ExplodedNode *N = C.generateErrorNode()) {
58 auto R = std::make_unique<PathSensitiveBugReport>(
59 BT, "Element count in new[] is a garbage value", N);
60 R->markInteresting(ArgVal);
61 R->addRange(Range);
63
64 C.emitReport(std::move(R));
65 }
66}
67
68void ento::registerUndefinedNewArraySizeChecker(CheckerManager &mgr) {
69 mgr.registerChecker<UndefinedNewArraySizeChecker>();
70}
71
72bool ento::shouldRegisterUndefinedNewArraySizeChecker(
73 const CheckerManager &mgr) {
74 return mgr.getLangOpts().CPlusPlus;
75}
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:152
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
const LangOptions & getLangOpts() const
Simple checker classes that implement one frontend (i.e.
Definition Checker.h:565
bool trackExpressionValue(const ExplodedNode *N, const Expr *E, PathSensitiveBugReport &R, TrackingOptions Opts={})
Attempts to add visitors to track expression value back to its point of origin.
The JSON file list parser is used to communicate input to InstallAPI.