[c++] Fix some MSVC static analysis issues

* `isspace` takes ints, not chars, so promote.
* Instead of dealing with a potentially non-null terminated string when
  finding a field by ordinal in Simple JSON, attempt to parse the field
  ID and then compare the numeric values.
* Indicate that the `die` function never returns so that the analyzer
  can tell that we've checked for things like malloc returning null.
* The printf format string for unsigned ints is u, not d.
* Disable the code analysis warning C6326, "constant constant
  comparison". A lot of our template functions trigger this when they do
  things like: if (T == traits<U>::some_value) { ... } else { ... }
This commit is contained in:
Christopher Warrington 2017-08-17 17:17:45 -07:00 коммит произвёл Ted Stein
Родитель 5c6c8ea51e
Коммит 1127832811
5 изменённых файлов: 26 добавлений и 12 удалений

Просмотреть файл

@ -36,6 +36,12 @@ if (MSVC)
# https://msdn.microsoft.com/en-us/library/ms175759.aspx
add_definitions (-D_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1 -D_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1)
# Disable code analysis warnings about constant constant comparisons. A
# lot of our template functions trigger this when they do things like:
#
# if (T == traits<U>::some_value) { ... } else { ... }
add_compile_options(/wd6326)
# Enable standards-conformance mode for MSVC compilers that support this
# flag (Visual C++ 2017 and later).
#

Просмотреть файл

@ -535,10 +535,10 @@ protected:
for (int begin = 0; i < static_cast<int>(formated.length()); begin += 80, i = begin + 79 - indent)
{
while (i >= begin && !isspace(formated[i]))
while (i >= begin && !isspace(static_cast<int>(formated[i])))
--i;
while (i < static_cast<int>(formated.length()) && isspace(formated[i]))
while (i < static_cast<int>(formated.length()) && isspace(static_cast<int>(formated[i])))
++i;
formated.insert(i, begin + 80 - i, ' ');

Просмотреть файл

@ -4,6 +4,7 @@
#pragma once
#include "simple_json_reader.h"
#include "boost/lexical_cast.hpp"
namespace bond
{
@ -16,21 +17,28 @@ SimpleJsonReader<BufferT>::FindField(uint16_t id, const Metadata& metadata, Bond
if (it != MemberEnd())
{
char ids[6];
const char* name = detail::FieldName(metadata).c_str();
detail::JsonTypeMatching jsonType(type, type, is_enum);
#ifdef _MSC_VER
_itoa(id, ids, 10);
#else
sprintf(ids, "%u", id);
#endif
// Match member by type of value and either metadata name, or string reprentation of id
for (rapidjson::Value::ConstMemberIterator end = MemberEnd(); it != end; ++it)
{
if (jsonType.TypeMatch(it->value))
if (!strcmp(it->name.GetString(), name) || !strcmp(it->name.GetString(), ids))
{
if (strcmp(it->name.GetString(), name) == 0)
{
// metadata name match
return &it->value;
}
uint16_t parsedId;
if (boost::conversion::try_lexical_convert(it->name.GetString(), parsedId) && id == parsedId)
{
// string id match
return &it->value;
}
}
}
}
return NULL;

Просмотреть файл

@ -17,7 +17,7 @@
#include <bond/protocol/simple_json_writer.h>
#include <bond/stream/stdio_output_stream.h>
void die(const char* fmt, ...)
BOND_NORETURN void die(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);

Просмотреть файл

@ -146,7 +146,7 @@ public:
bond::Metadata metadata_copy = metadata;
char name[100];
sprintf(name, "%s%d", _name, id);
sprintf(name, "%s%u", _name, id);
UT_AssertIsTrue(metadata_copy.name == name);
UT_AssertIsTrue(metadata_copy.name == metadata_copy.attributes["field_name"]);