llvm-project/clang-tools-extra/clang-tidy
Jonas Toth 78886233b3 [clang-tidy] New checker for not null-terminated result caused by strlen(), size() or equal length
New checker called bugprone-not-null-terminated-result. This check finds function calls where it is possible to cause a not null-terminated result. Usually the proper length of a string is strlen(src) + 1 or equal length of this expression, because the null terminator needs an extra space. Without the null terminator it can result in undefined behaviour when the string is read.

The following function calls are checked:
memcpy, wmemcpy, memcpy_s, wmemcpy_s, memchr, wmemchr, memmove, wmemmove, memmove_s, wmemmove_s, memset, wmemset, strerror_s, strncmp, wcsncmp, strxfrm, wcsxfrm

The following is a real-world example where the programmer forgot to increase the passed third argument, which is size_t length. That is why the length of the allocated memory is problematic too.

static char *StringCpy(const std::string &str) {
  char *result = reinterpret_cast<char *>(malloc(str.size()));
  memcpy(result, str.data(), str.size());
  return result;
}

After running the tool fix-it rewrites all the necessary code according to the given options. If it is necessary, the buffer size will be increased to hold the null terminator.

static char *StringCpy(const std::string &str) {
  char *result = reinterpret_cast<char *>(malloc(str.size() + 1));
  strcpy(result, str.data());
  return result;
}

Patch by Charusso.

Differential ID: https://reviews.llvm.org/D45050

llvm-svn: 344374
2018-10-12 17:22:36 +00:00
..
abseil [Clang-tidy] Alphabetical sort of files/checks. Add space after clang-tidy in source code headers. 2018-09-20 00:02:55 +00:00
android [Clang-tidy] Alphabetical sort of files/checks. Add space after clang-tidy in source code headers. 2018-09-20 00:02:55 +00:00
boost Port getLocStart -> getBeginLoc 2018-08-09 22:42:26 +00:00
bugprone [clang-tidy] New checker for not null-terminated result caused by strlen(), size() or equal length 2018-10-12 17:22:36 +00:00
cert Port getLocStart -> getBeginLoc 2018-08-09 22:42:26 +00:00
cppcoreguidelines [clang-tidy] NFC refactor lexer-utils to be usable without ASTContext 2018-10-05 14:15:19 +00:00
fuchsia Port getLocEnd -> getEndLoc 2018-08-09 22:43:02 +00:00
google Port getLocEnd -> getEndLoc 2018-08-09 22:43:02 +00:00
hicpp [Clang-tidy] Alphabetical sort of files/checks. Add space after clang-tidy in source code headers. 2018-09-20 00:02:55 +00:00
llvm Port getLocEnd -> getEndLoc 2018-08-09 22:43:02 +00:00
misc [Clang-tidy] Alphabetical sort of files/checks. Add space after clang-tidy in source code headers. 2018-09-20 00:02:55 +00:00
modernize [clang-tidy] Fix handling of parens around new expressions in make_<smartptr> checks. 2018-10-09 15:58:18 +00:00
mpi [clang-tidy] Add dependency to clangAnalysis after rC343160 2018-09-27 04:23:24 +00:00
objc [clang-tidy/ObjC] Update list of acronyms in PropertyDeclarationCheck 2018-09-07 22:02:38 +00:00
performance [clang-tidy] White List Option for performance-unnecessary-value-param, performance-unnecessary-copy-initialization and performance-for-range-copy 2018-10-12 13:05:21 +00:00
plugin [clang-tidy] Build it even without static analyzer 2018-10-01 20:24:22 +00:00
portability [clang-tidy] Add "portability" module and rename readability-simd-intrinsics to portability-simd-intrinsics 2018-03-07 16:57:42 +00:00
readability [clang-tidy] Added pointer types to clang-tidy readability-identifier-naming check. 2018-10-04 15:47:57 +00:00
tool [clang-tidy] Build it even without static analyzer 2018-10-01 20:24:22 +00:00
utils [clang-tidy] White List Option for performance-unnecessary-value-param, performance-unnecessary-copy-initialization and performance-for-range-copy 2018-10-12 13:05:21 +00:00
zircon [clang-tidy] Add Zircon module to clang-tidy 2018-03-14 23:47:50 +00:00
CMakeLists.txt [clang-tidy] Build it even without static analyzer 2018-10-01 20:24:22 +00:00
ClangTidy.cpp Lift VFS from clang to llvm (NFC) 2018-10-10 13:27:25 +00:00
ClangTidy.h Lift VFS from clang to llvm (NFC) 2018-10-10 13:27:25 +00:00
ClangTidyDiagnosticConsumer.cpp [clang-tidy] Store checks profiling info as JSON files 2018-06-06 15:07:51 +00:00
ClangTidyDiagnosticConsumer.h [clang-tidy] Store checks profiling info as JSON files 2018-06-06 15:07:51 +00:00
ClangTidyModule.cpp Change getChecksFilter() interface to hide implementation details. 2017-05-17 14:39:47 +00:00
ClangTidyModule.h [clang-tools-extra] Format sources with clang-format. NFC. 2016-11-08 07:50:19 +00:00
ClangTidyModuleRegistry.h Reapply r276973 "Adjust Registry interface to not require plugins to export a registry" 2016-08-05 11:01:08 +00:00
ClangTidyOptions.cpp Lift VFS from clang to llvm (NFC) 2018-10-10 13:27:25 +00:00
ClangTidyOptions.h Lift VFS from clang to llvm (NFC) 2018-10-10 13:27:25 +00:00
ClangTidyProfiling.cpp [clang-tidy] Store checks profiling info as JSON files 2018-06-06 15:07:51 +00:00
ClangTidyProfiling.h [clang-tidy] Store checks profiling info as JSON files 2018-06-06 15:07:51 +00:00
add_new_check.py [Clang-tidy] Alphabetical sort of files/checks. Add space after clang-tidy in source code headers. 2018-09-20 00:02:55 +00:00
rename_check.py [clang-tidy] Use :doc: for check links in Release Notes. 2018-03-21 17:06:13 +00:00