llvm-project/clang-tools-extra/clang-tidy
Tamás Zolnai 350da402ef [clang-tidy] new check: bugprone-signed-char-misuse
Summary:
This check searches for signed char -> integer conversions which might
indicate programming error, because of the misinterpretation of char
values. A signed char might store the non-ASCII characters as negative
values. The human programmer probably expects that after an integer
conversion the converted value matches with the character code
(a value from [0..255]), however, the actual value is in
[-128..127] interval.

See also:
STR34-C. Cast characters to unsigned char before converting to larger integer sizes
<https://wiki.sei.cmu.edu/confluence/display/c/STR34-C.+Cast+characters+to+unsigned+char+before+converting+to+larger+integer+sizes>

By now this check is limited to assignment / variable declarations.
If we would catch all signed char -> integer conversion, then it would
produce a lot of findings and also false positives. So I added only
this use case now, but this check can be extended with additional
use cases later.
The CERT documentation mentions another use case when the char is
used for array subscript. Next to that a third use case can be
the signed char - unsigned char comparison, which also a use case
where things happen unexpectedly because of conversion to integer.

Reviewers: alexfh, hokein, aaron.ballman

Reviewed By: aaron.ballman

Subscribers: sylvestre.ledru, whisperity, Eugene.Zelenko, mgorny, xazax.hun, cfe-commits

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D71174
2020-01-06 18:21:26 +01:00
..
abseil NFC: Fix trivial typos in comments 2020-01-04 10:28:41 -05:00
android Remove \brief commands from doxygen comments. 2019-08-22 11:32:57 +00:00
boost [clang-tidy] Switch checks to #include "ClangTidyCheck.h" 2019-03-25 12:38:26 +00:00
bugprone [clang-tidy] new check: bugprone-signed-char-misuse 2020-01-06 18:21:26 +01:00
cert [clang-tidy] Add cert-oop58-cpp check 2019-12-15 16:30:14 +01:00
cppcoreguidelines NFC: Fix trivial typos in comments 2020-01-04 10:28:41 -05:00
darwin [clang-tidy] Rename objc-avoid-spinlock check to darwin-avoid-spinlock 2019-10-01 21:18:40 +00:00
fuchsia Moved GlobList into a separate header file 2019-08-26 15:44:32 +00:00
google NFC: Fix trivial typos in comments 2020-01-04 10:28:41 -05:00
hicpp Add an option to hicpp-signed-bitwise for positive integer literals. 2019-10-30 14:11:29 -04:00
linuxkernel [clang-tidy] Add a module for the Linux kernel. 2019-07-25 22:32:50 +00:00
llvm [clang-tidy] Make llvm-header-guard work on llvm git monorepo 2019-09-26 09:56:37 +00:00
misc Fix a false positive in misc-redundant-expression check 2019-10-30 13:38:25 -04:00
modernize NFC: Fix trivial typos in comments 2020-01-04 10:28:41 -05:00
mpi [clang-tidy] Switch checks to #include "ClangTidyCheck.h" 2019-03-25 12:38:26 +00:00
objc [clang-tidy] Rename objc-avoid-spinlock check to darwin-avoid-spinlock 2019-10-01 21:18:40 +00:00
openmp [clang-tools-extra] Fix linking dylib for LLVMFrontendOpenMP 2019-12-18 22:33:23 +01:00
performance NFC: Fix trivial typos in comments 2020-01-04 10:28:41 -05:00
plugin clang-tidy: Don't repeat list of all checks in three places. 2019-09-27 12:56:14 +00:00
portability [clang-tidy] Switch checks to #include "ClangTidyCheck.h" 2019-03-25 12:38:26 +00:00
readability [APFloat] Fix compilation warnings 2020-01-06 11:30:40 +02:00
tool Use InitLLVM in clang-tidy 2019-11-28 13:50:35 +09:00
utils Fix MSVC "not all control paths return a value" warning. NFCI. 2020-01-06 10:20:20 +00:00
zircon [clang-tidy] Switch checks to #include "ClangTidyCheck.h" 2019-03-25 12:38:26 +00:00
CMakeLists.txt [clang-tools-extra] Fix overzealous linking of dylib to clangTidy 2019-10-15 13:05:38 +00:00
ClangTidy.cpp [clang-tidy] Use range-for for check registration. NFC 2019-11-26 16:34:23 +01:00
ClangTidy.h Remove \brief commands from doxygen comments. 2019-08-22 11:32:57 +00:00
ClangTidyCheck.cpp [clang-tidy] Separate the check-facing interface 2019-03-25 12:36:30 +00:00
ClangTidyCheck.h Remove \brief commands from doxygen comments. 2019-08-22 11:32:57 +00:00
ClangTidyDiagnosticConsumer.cpp Fix -Wswitch-coverage warning in clang-tidy after ak_addrspace introduction. 2019-12-13 12:57:48 -08:00
ClangTidyDiagnosticConsumer.h Fix naming style. NFC. 2019-12-12 17:00:57 +01:00
ClangTidyForceLinker.h clang-tidy: Don't repeat list of all checks in three places. 2019-09-27 12:56:14 +00:00
ClangTidyModule.cpp Return results by value from ClangTidyCheckFactories::createChecks 2019-09-26 13:55:01 +00:00
ClangTidyModule.h Return results by value from ClangTidyCheckFactories::createChecks 2019-09-26 13:55:01 +00:00
ClangTidyModuleRegistry.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
ClangTidyOptions.cpp Remove \brief commands from doxygen comments. 2019-08-22 11:32:57 +00:00
ClangTidyOptions.h Remove \brief commands from doxygen comments. 2019-08-22 11:32:57 +00:00
ClangTidyProfiling.cpp Rename F_{None,Text,Append} to OF_{None,Text,Append}. NFC 2019-08-05 05:43:48 +00:00
ClangTidyProfiling.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
ExpandModularHeadersPPCallbacks.cpp clang/Modules: Rename CompilerInstance::ModuleManager, NFC 2019-11-22 18:24:11 -08:00
ExpandModularHeadersPPCallbacks.h Use FileEntryRef for PPCallbacks::HasInclude 2019-08-27 17:32:42 +00:00
GlobList.cpp Refactor GlobList from an ad-hoc linked list to a vector 2019-08-27 10:56:13 +00:00
GlobList.h Refactor GlobList from an ad-hoc linked list to a vector 2019-08-27 10:56:13 +00:00
add_new_check.py Updated add_new_check.py to create checker tests in the new directory 2019-10-11 13:46:55 +00:00
rename_check.py [clang-tidy] Change the namespace for llvm checkers from 'llvm' to 'llvm_check' 2019-05-10 18:27:09 +00:00