Bug Summary

File:tools/clang/lib/AST/CommentCommandTraits.cpp
Warning:line 91, 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-eagerly-assume -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 -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-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-7~svn325118/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn325118/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.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-7~svn325118/build-llvm/tools/clang/lib/AST -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-02-14-150435-17243-1 -x c++ /build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/AST/CommentCommandTraits.cpp
1//===--- CommentCommandTraits.cpp - Comment command properties --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/AST/CommentCommandTraits.h"
11#include "llvm/ADT/STLExtras.h"
12
13namespace clang {
14namespace comments {
15
16#include "clang/AST/CommentCommandInfo.inc"
17
18CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator,
19 const CommentOptions &CommentOptions) :
20 NextID(llvm::array_lengthof(Commands)), Allocator(Allocator) {
21 registerCommentOptions(CommentOptions);
22}
23
24void CommandTraits::registerCommentOptions(
25 const CommentOptions &CommentOptions) {
26 for (CommentOptions::BlockCommandNamesTy::const_iterator
27 I = CommentOptions.BlockCommandNames.begin(),
28 E = CommentOptions.BlockCommandNames.end();
29 I != E; I++) {
30 registerBlockCommand(*I);
31 }
32}
33
34const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
35 if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
36 return Info;
37 return getRegisteredCommandInfo(Name);
38}
39
40const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
41 if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
42 return Info;
43 return getRegisteredCommandInfo(CommandID);
44}
45
46const CommandInfo *
47CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const {
48 // Single-character command impostures, such as \t or \n, should not go
49 // through the fixit logic.
50 if (Typo.size() <= 1)
51 return nullptr;
52
53 // The maximum edit distance we're prepared to accept.
54 const unsigned MaxEditDistance = 1;
55
56 unsigned BestEditDistance = MaxEditDistance;
57 SmallVector<const CommandInfo *, 2> BestCommand;
58
59 auto ConsiderCorrection = [&](const CommandInfo *Command) {
60 StringRef Name = Command->Name;
61
62 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
63 if (MinPossibleEditDistance <= BestEditDistance) {
64 unsigned EditDistance = Typo.edit_distance(Name, true, BestEditDistance);
65 if (EditDistance < BestEditDistance) {
66 BestEditDistance = EditDistance;
67 BestCommand.clear();
68 }
69 if (EditDistance == BestEditDistance)
70 BestCommand.push_back(Command);
71 }
72 };
73
74 for (const auto &Command : Commands)
75 ConsiderCorrection(&Command);
76
77 for (const auto *Command : RegisteredCommands)
78 if (!Command->IsUnknownCommand)
79 ConsiderCorrection(Command);
80
81 return BestCommand.size() == 1 ? BestCommand[0] : nullptr;
82}
83
84CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {
85 char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
86 memcpy(Name, CommandName.data(), CommandName.size());
87 Name[CommandName.size()] = '\0';
88
89 // Value-initialize (=zero-initialize in this case) a new CommandInfo.
90 CommandInfo *Info = new (Allocator) CommandInfo();
2
'Info' initialized to a null pointer value
91 Info->Name = Name;
3
Access to field 'Name' results in a dereference of a null pointer (loaded from variable 'Info')
92 // We only have a limited number of bits to encode command IDs in the
93 // CommandInfo structure, so the ID numbers can potentially wrap around.
94 assert((NextID < (1 << CommandInfo::NumCommandIDBits))(static_cast <bool> ((NextID < (1 << CommandInfo
::NumCommandIDBits)) && "Too many commands. We have limited bits for the command ID."
) ? void (0) : __assert_fail ("(NextID < (1 << CommandInfo::NumCommandIDBits)) && \"Too many commands. We have limited bits for the command ID.\""
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/AST/CommentCommandTraits.cpp"
, 95, __extension__ __PRETTY_FUNCTION__))
95 && "Too many commands. We have limited bits for the command ID.")(static_cast <bool> ((NextID < (1 << CommandInfo
::NumCommandIDBits)) && "Too many commands. We have limited bits for the command ID."
) ? void (0) : __assert_fail ("(NextID < (1 << CommandInfo::NumCommandIDBits)) && \"Too many commands. We have limited bits for the command ID.\""
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/AST/CommentCommandTraits.cpp"
, 95, __extension__ __PRETTY_FUNCTION__))
;
96 Info->ID = NextID++;
97
98 RegisteredCommands.push_back(Info);
99
100 return Info;
101}
102
103const CommandInfo *CommandTraits::registerUnknownCommand(
104 StringRef CommandName) {
105 CommandInfo *Info = createCommandInfoWithName(CommandName);
1
Calling 'CommandTraits::createCommandInfoWithName'
106 Info->IsUnknownCommand = true;
107 return Info;
108}
109
110const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) {
111 CommandInfo *Info = createCommandInfoWithName(CommandName);
112 Info->IsBlockCommand = true;
113 return Info;
114}
115
116const CommandInfo *CommandTraits::getBuiltinCommandInfo(
117 unsigned CommandID) {
118 if (CommandID < llvm::array_lengthof(Commands))
119 return &Commands[CommandID];
120 return nullptr;
121}
122
123const CommandInfo *CommandTraits::getRegisteredCommandInfo(
124 StringRef Name) const {
125 for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
126 if (RegisteredCommands[i]->Name == Name)
127 return RegisteredCommands[i];
128 }
129 return nullptr;
130}
131
132const CommandInfo *CommandTraits::getRegisteredCommandInfo(
133 unsigned CommandID) const {
134 return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
135}
136
137} // end namespace comments
138} // end namespace clang
139