Bug Summary

File:clang/lib/AST/StmtCXX.cpp
Warning:line 69, column 10
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name StmtCXX.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/lib/AST -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/lib/AST -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include -D NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/lib/AST -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-09-04-040900-46481-1 -x c++ /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/lib/AST/StmtCXX.cpp
1//===--- StmtCXX.cpp - Classes for representing C++ statements ------------===//
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 file implements the subclesses of Stmt class declared in StmtCXX.h
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/StmtCXX.h"
14
15#include "clang/AST/ASTContext.h"
16
17using namespace clang;
18
19QualType CXXCatchStmt::getCaughtType() const {
20 if (ExceptionDecl)
21 return ExceptionDecl->getType();
22 return QualType();
23}
24
25CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
26 Stmt *tryBlock, ArrayRef<Stmt *> handlers) {
27 const size_t Size = totalSizeToAlloc<Stmt *>(handlers.size() + 1);
28 void *Mem = C.Allocate(Size, alignof(CXXTryStmt));
29 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
30}
31
32CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
33 unsigned numHandlers) {
34 const size_t Size = totalSizeToAlloc<Stmt *>(numHandlers + 1);
35 void *Mem = C.Allocate(Size, alignof(CXXTryStmt));
36 return new (Mem) CXXTryStmt(Empty, numHandlers);
37}
38
39CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
40 ArrayRef<Stmt *> handlers)
41 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
42 Stmt **Stmts = getStmts();
43 Stmts[0] = tryBlock;
44 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
45}
46
47CXXForRangeStmt::CXXForRangeStmt(Stmt *Init, DeclStmt *Range,
48 DeclStmt *BeginStmt, DeclStmt *EndStmt,
49 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
50 Stmt *Body, SourceLocation FL,
51 SourceLocation CAL, SourceLocation CL,
52 SourceLocation RPL)
53 : Stmt(CXXForRangeStmtClass), ForLoc(FL), CoawaitLoc(CAL), ColonLoc(CL),
54 RParenLoc(RPL) {
55 SubExprs[INIT] = Init;
56 SubExprs[RANGE] = Range;
57 SubExprs[BEGINSTMT] = BeginStmt;
58 SubExprs[ENDSTMT] = EndStmt;
59 SubExprs[COND] = Cond;
60 SubExprs[INC] = Inc;
61 SubExprs[LOOPVAR] = LoopVar;
62 SubExprs[BODY] = Body;
63}
64
65Expr *CXXForRangeStmt::getRangeInit() {
66 DeclStmt *RangeStmt = getRangeStmt();
67 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
2
Assuming null pointer is passed into cast
3
'RangeDecl' initialized to a null pointer value
68 assert(RangeDecl && "for-range should have a single var decl")(static_cast<void> (0));
69 return RangeDecl->getInit();
4
Called C++ object pointer is null
70}
71
72const Expr *CXXForRangeStmt::getRangeInit() const {
73 return const_cast<CXXForRangeStmt *>(this)->getRangeInit();
1
Calling 'CXXForRangeStmt::getRangeInit'
74}
75
76VarDecl *CXXForRangeStmt::getLoopVariable() {
77 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
78 assert(LV && "No loop variable in CXXForRangeStmt")(static_cast<void> (0));
79 return cast<VarDecl>(LV);
80}
81
82const VarDecl *CXXForRangeStmt::getLoopVariable() const {
83 return const_cast<CXXForRangeStmt *>(this)->getLoopVariable();
84}
85
86CoroutineBodyStmt *CoroutineBodyStmt::Create(
87 const ASTContext &C, CoroutineBodyStmt::CtorArgs const &Args) {
88 std::size_t Size = totalSizeToAlloc<Stmt *>(
89 CoroutineBodyStmt::FirstParamMove + Args.ParamMoves.size());
90
91 void *Mem = C.Allocate(Size, alignof(CoroutineBodyStmt));
92 return new (Mem) CoroutineBodyStmt(Args);
93}
94
95CoroutineBodyStmt *CoroutineBodyStmt::Create(const ASTContext &C, EmptyShell,
96 unsigned NumParams) {
97 std::size_t Size = totalSizeToAlloc<Stmt *>(
98 CoroutineBodyStmt::FirstParamMove + NumParams);
99
100 void *Mem = C.Allocate(Size, alignof(CoroutineBodyStmt));
101 auto *Result = new (Mem) CoroutineBodyStmt(CtorArgs());
102 Result->NumParams = NumParams;
103 auto *ParamBegin = Result->getStoredStmts() + SubStmt::FirstParamMove;
104 std::uninitialized_fill(ParamBegin, ParamBegin + NumParams,
105 static_cast<Stmt *>(nullptr));
106 return Result;
107}
108
109CoroutineBodyStmt::CoroutineBodyStmt(CoroutineBodyStmt::CtorArgs const &Args)
110 : Stmt(CoroutineBodyStmtClass), NumParams(Args.ParamMoves.size()) {
111 Stmt **SubStmts = getStoredStmts();
112 SubStmts[CoroutineBodyStmt::Body] = Args.Body;
113 SubStmts[CoroutineBodyStmt::Promise] = Args.Promise;
114 SubStmts[CoroutineBodyStmt::InitSuspend] = Args.InitialSuspend;
115 SubStmts[CoroutineBodyStmt::FinalSuspend] = Args.FinalSuspend;
116 SubStmts[CoroutineBodyStmt::OnException] = Args.OnException;
117 SubStmts[CoroutineBodyStmt::OnFallthrough] = Args.OnFallthrough;
118 SubStmts[CoroutineBodyStmt::Allocate] = Args.Allocate;
119 SubStmts[CoroutineBodyStmt::Deallocate] = Args.Deallocate;
120 SubStmts[CoroutineBodyStmt::ReturnValue] = Args.ReturnValue;
121 SubStmts[CoroutineBodyStmt::ResultDecl] = Args.ResultDecl;
122 SubStmts[CoroutineBodyStmt::ReturnStmt] = Args.ReturnStmt;
123 SubStmts[CoroutineBodyStmt::ReturnStmtOnAllocFailure] =
124 Args.ReturnStmtOnAllocFailure;
125 std::copy(Args.ParamMoves.begin(), Args.ParamMoves.end(),
126 const_cast<Stmt **>(getParamMoves().data()));
127}