Bug Summary

File:tools/clang/lib/AST/CommentCommandTraits.cpp
Warning:line 90, column 14
Access to field 'Name' results in a dereference of a null pointer (loaded from variable 'Info')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CommentCommandTraits.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 -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-9/lib/clang/9.0.0 -D CLANG_VENDOR="Debian " -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-9~svn362543/tools/clang/include -I /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/include -I /build/llvm-toolchain-snapshot-9~svn362543/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/9.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-9/lib/clang/9.0.0/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-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-9~svn362543/build-llvm/tools/clang/lib/AST -fdebug-prefix-map=/build/llvm-toolchain-snapshot-9~svn362543=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2019-06-05-060531-1271-1 -x c++ /build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/CommentCommandTraits.cpp -faddrsig
1//===--- CommentCommandTraits.cpp - Comment command properties --*- 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#include "clang/AST/CommentCommandTraits.h"
10#include "llvm/ADT/STLExtras.h"
11
12namespace clang {
13namespace comments {
14
15#include "clang/AST/CommentCommandInfo.inc"
16
17CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator,
18 const CommentOptions &CommentOptions) :
19 NextID(llvm::array_lengthof(Commands)), Allocator(Allocator) {
20 registerCommentOptions(CommentOptions);
21}
22
23void CommandTraits::registerCommentOptions(
24 const CommentOptions &CommentOptions) {
25 for (CommentOptions::BlockCommandNamesTy::const_iterator
26 I = CommentOptions.BlockCommandNames.begin(),
27 E = CommentOptions.BlockCommandNames.end();
28 I != E; I++) {
29 registerBlockCommand(*I);
30 }
31}
32
33const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
34 if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
35 return Info;
36 return getRegisteredCommandInfo(Name);
37}
38
39const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
40 if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
41 return Info;
42 return getRegisteredCommandInfo(CommandID);
43}
44
45const CommandInfo *
46CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const {
47 // Single-character command impostures, such as \t or \n, should not go
48 // through the fixit logic.
49 if (Typo.size() <= 1)
50 return nullptr;
51
52 // The maximum edit distance we're prepared to accept.
53 const unsigned MaxEditDistance = 1;
54
55 unsigned BestEditDistance = MaxEditDistance;
56 SmallVector<const CommandInfo *, 2> BestCommand;
57
58 auto ConsiderCorrection = [&](const CommandInfo *Command) {
59 StringRef Name = Command->Name;
60
61 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
62 if (MinPossibleEditDistance <= BestEditDistance) {
63 unsigned EditDistance = Typo.edit_distance(Name, true, BestEditDistance);
64 if (EditDistance < BestEditDistance) {
65 BestEditDistance = EditDistance;
66 BestCommand.clear();
67 }
68 if (EditDistance == BestEditDistance)
69 BestCommand.push_back(Command);
70 }
71 };
72
73 for (const auto &Command : Commands)
74 ConsiderCorrection(&Command);
75
76 for (const auto *Command : RegisteredCommands)
77 if (!Command->IsUnknownCommand)
78 ConsiderCorrection(Command);
79
80 return BestCommand.size() == 1 ? BestCommand[0] : nullptr;
81}
82
83CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {
84 char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
85 memcpy(Name, CommandName.data(), CommandName.size());
86 Name[CommandName.size()] = '\0';
87
88 // Value-initialize (=zero-initialize in this case) a new CommandInfo.
89 CommandInfo *Info = new (Allocator) CommandInfo();
2
'Info' initialized to a null pointer value
90 Info->Name = Name;
3
Access to field 'Name' results in a dereference of a null pointer (loaded from variable 'Info')
91 // We only have a limited number of bits to encode command IDs in the
92 // CommandInfo structure, so the ID numbers can potentially wrap around.
93 assert((NextID < (1 << CommandInfo::NumCommandIDBits))(((NextID < (1 << CommandInfo::NumCommandIDBits)) &&
"Too many commands. We have limited bits for the command ID."
) ? static_cast<void> (0) : __assert_fail ("(NextID < (1 << CommandInfo::NumCommandIDBits)) && \"Too many commands. We have limited bits for the command ID.\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/CommentCommandTraits.cpp"
, 94, __PRETTY_FUNCTION__))
94 && "Too many commands. We have limited bits for the command ID.")(((NextID < (1 << CommandInfo::NumCommandIDBits)) &&
"Too many commands. We have limited bits for the command ID."
) ? static_cast<void> (0) : __assert_fail ("(NextID < (1 << CommandInfo::NumCommandIDBits)) && \"Too many commands. We have limited bits for the command ID.\""
, "/build/llvm-toolchain-snapshot-9~svn362543/tools/clang/lib/AST/CommentCommandTraits.cpp"
, 94, __PRETTY_FUNCTION__))
;
95 Info->ID = NextID++;
96
97 RegisteredCommands.push_back(Info);
98
99 return Info;
100}
101
102const CommandInfo *CommandTraits::registerUnknownCommand(
103 StringRef CommandName) {
104 CommandInfo *Info = createCommandInfoWithName(CommandName);
1
Calling 'CommandTraits::createCommandInfoWithName'
105 Info->IsUnknownCommand = true;
106 return Info;
107}
108
109const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) {
110 CommandInfo *Info = createCommandInfoWithName(CommandName);
111 Info->IsBlockCommand = true;
112 return Info;
113}
114
115const CommandInfo *CommandTraits::getBuiltinCommandInfo(
116 unsigned CommandID) {
117 if (CommandID < llvm::array_lengthof(Commands))
118 return &Commands[CommandID];
119 return nullptr;
120}
121
122const CommandInfo *CommandTraits::getRegisteredCommandInfo(
123 StringRef Name) const {
124 for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
125 if (RegisteredCommands[i]->Name == Name)
126 return RegisteredCommands[i];
127 }
128 return nullptr;
129}
130
131const CommandInfo *CommandTraits::getRegisteredCommandInfo(
132 unsigned CommandID) const {
133 return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
134}
135
136} // end namespace comments
137} // end namespace clang
138