Skip to content

Commit

Permalink
Fixed issues found by PVS-Studio
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgen committed Jan 5, 2024
1 parent e4e16fd commit 36a6f51
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 50 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,9 @@ https://github.com/jorgen/json_struct/tree/master/examples

and have a look at the more complete unit tests at:
https://github.com/jorgen/json_struct/tree/master/tests


## SAST Tools

[PVS-Studio](https://pvs-studio.com/pvs-studio/?utm_source=website&utm_medium=github&utm_campaign=open_source) - static analyzer for C, C++, C#, and Java code.

2 changes: 1 addition & 1 deletion examples/01_simple_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const char json[] = R"json(
struct VecMember
{
std::string key;
double value;
double value = 0.0;

JS_OBJ(key, value);
};
Expand Down
4 changes: 2 additions & 2 deletions examples/03_optional_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const char json[] = R"json(
struct JsonData
{
std::string key;
int number;
bool boolean;
int number = 0;
bool boolean = false;
//sizeof(JS::Optional<double>) == sizeof(double) however if you want to see
//if it has been modified there is the JS::OptionalChecked. It has an "assigned" member
//allowing the user to inspect if the type has been assigned
Expand Down
9 changes: 6 additions & 3 deletions examples/13_diff_missing_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,13 @@ int main()
case JS::DiffType::MissingMembers:
{
const auto* missingMembers = diffTokens.getMissingMembers(diffToken);
for (const auto& m : *missingMembers)
if (missingMembers)
{
std::string missingStr = "\"" + std::string(m.name.data, m.name.size) + "\"";
fprintf(stdout, "Missing member: %s\n", missingStr.c_str());
for (const auto &m : *missingMembers)
{
std::string missingStr = "\"" + std::string(m.name.data, m.name.size) + "\"";
fprintf(stdout, "Missing member: %s\n", missingStr.c_str());
}
}
break;
}
Expand Down
20 changes: 8 additions & 12 deletions include/json_struct/json_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ class RefCounter
}
RefCounter(const RefCounter<T> &other)
: callbackContainer(other.callbackContainer)
, index(other.index)
{
inc();
}
Expand Down Expand Up @@ -3345,7 +3346,7 @@ struct MemberChecker<T, Members, PAGE, 0>
using Super = decltype(Internal::template JsonStructBaseDummy<T, T>::js_static_meta_super_info());
Error superError = StartSuperRecursion<T, PAGE + Members::size, Super::size>::verifyMembers(
assigned_members, track_missing_members, missing_members);
if (memberError != Error::NoError)
if (memberError != Error::NoError)//-V1051 memberError is correct, but we have to allways call supers verifyMembers first
return memberError;
return superError;
}
Expand Down Expand Up @@ -4382,13 +4383,13 @@ inline Error CallFunctionContext::callFunctions(T &container)
struct DefaultCallFunctionContext : public CallFunctionContext
{
DefaultCallFunctionContext(std::string &json_out)
: CallFunctionContext(p_context, s_context.serializer)
: CallFunctionContext(p_context, s_context.serializer)//-V1050 The super class only store the reference so we don't mind its not initialized
, s_context(json_out)
{
}

DefaultCallFunctionContext(const char *data, size_t size, std::string &json_out)
: CallFunctionContext(p_context, s_context.serializer)
: CallFunctionContext(p_context, s_context.serializer)//-V1050 The super class only store the reference so we don't mind its not initialized
, p_context(data, size)
, s_context(json_out)
{
Expand Down Expand Up @@ -4960,10 +4961,7 @@ inline void left_shift(uint64_t (&a)[2], int shift)
if (shift > int(sizeof(*a)) * 8)
{
auto shift_0 = (int(sizeof(uint64_t) * 8) - shift);
if (shift_0 > 0)
a[1] = a[0] >> shift_0;
else
a[1] = a[0] << -shift_0;
a[1] = a[0] << -shift_0;

a[0] = 0;
}
Expand Down Expand Up @@ -6715,7 +6713,7 @@ inline parse_string_error parseNumber(const char *number, size_t size, parsed_st
int desimal_position = -1;
bool increase_significand = true;

parsedString.negative = false;
parsedString.negative = 0;
parsedString.inf = 0;
parsedString.nan = 0;
parsedString.significand_digit_count = 0;
Expand All @@ -6730,7 +6728,7 @@ inline parse_string_error parseNumber(const char *number, size_t size, parsed_st
}
if (*current == '-')
{
parsedString.negative = true;
parsedString.negative = 1;
current++;
}
while (current < number_end)
Expand Down Expand Up @@ -6777,8 +6775,6 @@ inline parse_string_error parseNumber(const char *number, size_t size, parsed_st
{
if (desimal_position >= 0)
parsedString.exp = desimal_position - parsedString.significand_digit_count;
else
parsedString.exp = 0;
return parse_string_error::ok;
}
current++;
Expand Down Expand Up @@ -8621,7 +8617,7 @@ struct TypeHandler<Map>
};

template <typename T, size_t COUNT>
struct ArrayVariableContent
struct ArrayVariableContent//-V730
{
T data[COUNT];
size_t size = 0;
Expand Down
7 changes: 4 additions & 3 deletions include/json_struct/json_struct_diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ namespace Internal
struct DiffTokens
{
DiffTokens()
: error(DiffError::NoError)
{}

explicit DiffTokens(const char* json, size_t size)
Expand Down Expand Up @@ -462,7 +463,7 @@ namespace Internal
}
else
{
if (baseValue == diffValue)
if (baseValue == diffValue)//-V550
diff.set(diffPos, DiffType::NoDiff);
else
diff.set(diffPos, DiffType::ValueDiff);
Expand Down Expand Up @@ -739,9 +740,9 @@ namespace Internal
}
else if (base.tokens.data.size() && diff.tokens.data.empty())
{
if (diff.tokens.data[0].value_type == Type::ObjectStart)
if (base.tokens.data[0].value_type == Type::ObjectStart)
diff.addMissingMembers(0, base, 0);
else if (diff.tokens.data[0].value_type == Type::ArrayStart)
else if (base.tokens.data[0].value_type == Type::ArrayStart)
diff.addMissingArrayItems(0, base, 0);
else
setStateForEntireToken(diff, 0, DiffType::ErroneousRootItem);
Expand Down
2 changes: 1 addition & 1 deletion tests/json-mias-mat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct RecipeNameIdItem
}

std::string recipe_name;
int recipe_id;
int recipe_id = 0;

JS_OBJECT(JS_MEMBER(recipe_name), JS_MEMBER(recipe_id));
};
Expand Down
4 changes: 2 additions & 2 deletions tests/json-nullable-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ struct SmallStructWithoutNullable

struct SmallStruct
{
int a;
int a = 0;
JS::Nullable<float> b = 2.2f;

JS_OBJECT(JS_MEMBER(a), JS_MEMBER(b));
};

struct SmallStructNullableChecked
{
int a;
int a = 0;
JS::NullableChecked<float> b = 2.2f;

JS_OBJECT(JS_MEMBER(a), JS_MEMBER(b));
Expand Down
4 changes: 2 additions & 2 deletions tests/json-optional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace
{
struct SmallStructWithoutOptional
{
int a;
int a = 0;
float b = 2.2f;
std::string d;

Expand All @@ -18,7 +18,7 @@ struct SmallStructWithoutOptional
#ifdef JS_STD_OPTIONAL
struct SmallStructStd
{
int a;
int a = 0;
std::optional<float> b = 2.2f;
std::optional<std::string> c;
std::optional<std::string> d;
Expand Down
12 changes: 6 additions & 6 deletions tests/json-struct-polymorphic-map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ static const char json[] = R"json(

struct ComplexFields_t
{
int Hello;
int World;
int Hello = 0;
int World = 0;
JS_OBJ(Hello, World);
};

struct SubObject_t
{
bool SimpleMember;
bool SimpleMember = false;
std::vector<std::string> MoreValues;
JS_OBJ(SimpleMember, MoreValues);
};
Expand All @@ -67,10 +67,10 @@ struct Root_t
: ComplexFields2(true)
{
}
int Field1;
bool Field2;
int Field1 = 0;
bool Field2 = false;
std::string Field3;
int Field4;
int Field4 = 0;
ComplexFields_t ComplexFields;
ComplexFields2_t ComplexFields2;
JS_OBJ(Field1, Field2, Field3, Field4, ComplexFields, ComplexFields2);
Expand Down
15 changes: 8 additions & 7 deletions tests/json-struct-test-new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static const char json_data1[] = "{\n"
struct SubStruct
{
std::string SubString;
int SubNumber;
int SubNumber = 0;
std::vector<double> Array;
JS::OptionalChecked<float> optional_float;
JS::OptionalChecked<double> optional_double;
Expand Down Expand Up @@ -90,8 +90,8 @@ struct SubStruct3 : public SubStruct2
struct JsonData1
{
std::string StringNode;
double NumberNode;
bool BooleanTrue;
double NumberNode = 0.0;
bool BooleanTrue = false;
/*!
*very special comment for BooleanFalse
*
Expand All @@ -100,7 +100,8 @@ struct JsonData1
* json
*}
**/
bool BooleanFalse;
bool BooleanFalse = false;
;
JS::Optional<int> OptionalInt;
/// Test structur comment
SubStruct TestStruct;
Expand Down Expand Up @@ -253,15 +254,15 @@ static const char error_in_sub[] = R"json(

struct ErrorInSubChild
{
int ffirst;
int ffirst = 0;
JS_OBJ(ffirst);
};

struct ErrorInSub
{
ErrorInSubChild first;
std::string second;
int third;
int third = 0;
JS::Optional<int> not_assigned = 999;
JS_OBJ(first, second, third);
};
Expand Down Expand Up @@ -843,7 +844,7 @@ TEST_CASE("short_check_json_meta_outside", "json_struct")
auto error = context.parseTo(data);
REQUIRE(error == JS::Error::NoError);
REQUIRE(data.data == "this is some text");
REQUIRE(data.a == 44.5);
REQUIRE(data.a == 44.5);//-V550
}

struct FloatingPointAtTheEnd
Expand Down
12 changes: 6 additions & 6 deletions tests/json-struct-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static const char json_data1[] = "{\n"
struct SubStruct
{
std::string SubString;
int SubNumber;
int SubNumber = 0;
std::vector<double> Array;
JS::OptionalChecked<float> optional_float;
JS::OptionalChecked<double> optional_double;
Expand Down Expand Up @@ -92,8 +92,8 @@ struct SubStruct3 : public SubStruct2
struct JsonData1
{
std::string StringNode;
double NumberNode;
bool BooleanTrue;
double NumberNode = 0.0;
bool BooleanTrue = false;
/*!
*very special comment for BooleanFalse
*
Expand All @@ -102,7 +102,7 @@ struct JsonData1
* json
*}
**/
bool BooleanFalse;
bool BooleanFalse = false;
JS::Optional<int> OptionalInt;
/// Test structur comment
SubStruct TestStruct;
Expand Down Expand Up @@ -254,15 +254,15 @@ static const char error_in_sub[] = R"json({

struct ErrorInSubChild
{
int ffirst;
int ffirst = 0;
JS_OBJECT(JS_MEMBER(ffirst));
};

struct ErrorInSub
{
ErrorInSubChild first;
std::string second;
int third;
int third = 0;
JS::Optional<int> not_assigned = 999;
JS_OBJECT(JS_MEMBER(first), JS_MEMBER(second), JS_MEMBER(third));
};
Expand Down
2 changes: 1 addition & 1 deletion tests/json-unordered-map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TEST_CASE("unordered_map_complex_value", "json_struct")
JS::ParseContext parseContext(json);
REQUIRE(parseContext.parseTo(dataStruct) == JS::Error::NoError);

REQUIRE(dataStruct.unordered_map["bar"].front() == 2.0);
REQUIRE(dataStruct.unordered_map["bar"].front() == 2.0);//-V550
REQUIRE(dataStruct.map["sail"] == "boat");
REQUIRE(dataStruct.unordered_set.find(7) != dataStruct.unordered_set.end());
REQUIRE(dataStruct.set.find(1.6f) != dataStruct.set.end());
Expand Down
8 changes: 4 additions & 4 deletions tests/zero-value-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ void test_zero_value_parse(const char *const json)
pc.parseTo(zero);
REQUIRE(pc.error == JS::Error::NoError);

REQUIRE(zero.f_pos_zero == f_pos_zero);
REQUIRE(zero.f_neg_zero == f_neg_zero);
REQUIRE(zero.d_pos_zero == d_pos_zero);
REQUIRE(zero.d_neg_zero == d_neg_zero);
REQUIRE(zero.f_pos_zero == f_pos_zero);//-V550
REQUIRE(zero.f_neg_zero == f_neg_zero);//-V550
REQUIRE(zero.d_pos_zero == d_pos_zero);//-V550
REQUIRE(zero.d_neg_zero == d_neg_zero);//-V550

REQUIRE(memcmp(&f_pos_zero, &zero.f_pos_zero, sizeof(float)) == 0);
REQUIRE(memcmp(&f_neg_zero, &zero.f_neg_zero, sizeof(float)) == 0);
Expand Down

0 comments on commit 36a6f51

Please sign in to comment.