Fix invalid isspace call detected by PREfast

D:\vcpkg\toolsrc\include\catch2\catch.hpp(11285): warning C6330: 'char' passed as _Param_(1) when 'unsigned char' is required in call to 'isspace'.
D:\vcpkg\toolsrc\include\catch2\catch.hpp(11288): warning C6330: 'char' passed as _Param_(1) when 'unsigned char' is required in call to 'isspace'.

ISO/IEC 9899:2011:
"7.4 Character handling <ctype.h>"/1
[...] In all cases the argument is an int, the value of which shall be
representable as an unsigned char or shall equal the value of the macro
EOF. If the argument has any other value, the behavior is undefined.

This means if isspace was passed a character like ñ it could corrupt
memory without the static_cast to treat it as a positive value after
integral promotion (and C libraries commonly use the int index supplied
as a key into a table which result in out of bounds access if the
resulting int is negative).
This commit is contained in:
Billy Robert O'Neal III 2020-05-11 20:38:09 -07:00 committed by Martin Hořeňovský
parent 7dae3efad2
commit c24f7e5b34
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A

View File

@ -48,10 +48,10 @@ namespace Catch {
Capturer::Capturer( StringRef macroName, SourceLineInfo const& lineInfo, ResultWas::OfType resultType, StringRef names ) { Capturer::Capturer( StringRef macroName, SourceLineInfo const& lineInfo, ResultWas::OfType resultType, StringRef names ) {
auto trimmed = [&] (size_t start, size_t end) { auto trimmed = [&] (size_t start, size_t end) {
while (names[start] == ',' || isspace(names[start])) { while (names[start] == ',' || isspace(static_cast<unsigned char>(names[start]))) {
++start; ++start;
} }
while (names[end] == ',' || isspace(names[end])) { while (names[end] == ',' || isspace(static_cast<unsigned char>(names[end]))) {
--end; --end;
} }
return names.substr(start, end - start + 1); return names.substr(start, end - start + 1);