[PATCH] D45050: [clang-tidy] New checker for not null-terminated result caused by strlen or wcslen

Csaba Dabis via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 29 08:46:38 PDT 2018


Charusso created this revision.
Charusso added reviewers: alexfh, aaron.ballman, xazax.hun.
Charusso added a project: clang-tools-extra.
Herald added subscribers: cfe-commits, rnkovacs, mgorny.

New checker called bugprone-not-null-terminated-result. This check can be used to find function calls where `strlen` or `wcslen` are passed as an argument and cause a not null-terminated result. Usually the proper length is the source's own length + 1: `strlen(src) + 1` because the null-terminator need an extra space. Depending on the case of use, it may insert or remove the increase operation to ensure the result is null-terminated.

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

Example problematic code:

  void bad_memcpy(char *dest, const char *src) {
    memcpy(dest, src, strlen(src));
  }

After running the tool it would be the following if the target is C++11:

  void good_memcpy_cxx11(char *dest, const char *src) {
    strncpy_s(dest, src, strlen(src));
  }

or if the target is older, then it would be following:

  void good_memcpy_not_cxx11(char *dest, const char *src) {
    strncpy(dest, src, (strlen(src) + 1));
  }


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D45050

Files:
  clang-tidy/bugprone/BugproneTidyModule.cpp
  clang-tidy/bugprone/CMakeLists.txt
  clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
  clang-tidy/bugprone/NotNullTerminatedResultCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/bugprone-not-null-terminated-result.rst
  docs/clang-tidy/checks/list.rst
  test/clang-tidy/bugprone-not-null-terminated-result-strlen-before-cxx11.cpp
  test/clang-tidy/bugprone-not-null-terminated-result-strlen-cxx11.cpp
  test/clang-tidy/bugprone-not-null-terminated-result-wcslen-before-cxx11.cpp
  test/clang-tidy/bugprone-not-null-terminated-result-wcslen-cxx11.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45050.140255.patch
Type: text/x-patch
Size: 44118 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180329/3ad027e4/attachment-0001.bin>


More information about the cfe-commits mailing list