diff --git a/README.md b/README.md index aeda8be..64f4c09 100644 --- a/README.md +++ b/README.md @@ -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. + diff --git a/examples/01_simple_struct.cpp b/examples/01_simple_struct.cpp index bafa32d..53520bd 100644 --- a/examples/01_simple_struct.cpp +++ b/examples/01_simple_struct.cpp @@ -14,7 +14,7 @@ const char json[] = R"json( struct VecMember { std::string key; - double value; + double value = 0.0; JS_OBJ(key, value); }; diff --git a/examples/03_optional_types.cpp b/examples/03_optional_types.cpp index 6f9d130..cbceef3 100644 --- a/examples/03_optional_types.cpp +++ b/examples/03_optional_types.cpp @@ -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) == 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 diff --git a/examples/13_diff_missing_data.cpp b/examples/13_diff_missing_data.cpp index bd7b70a..113f13d 100644 --- a/examples/13_diff_missing_data.cpp +++ b/examples/13_diff_missing_data.cpp @@ -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; } diff --git a/include/json_struct/json_struct.h b/include/json_struct/json_struct.h index 13e42ae..ab594f7 100644 --- a/include/json_struct/json_struct.h +++ b/include/json_struct/json_struct.h @@ -429,6 +429,7 @@ class RefCounter } RefCounter(const RefCounter &other) : callbackContainer(other.callbackContainer) + , index(other.index) { inc(); } @@ -3345,7 +3346,7 @@ struct MemberChecker using Super = decltype(Internal::template JsonStructBaseDummy::js_static_meta_super_info()); Error superError = StartSuperRecursion::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; } @@ -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) { @@ -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; } @@ -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; @@ -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) @@ -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++; @@ -8621,7 +8617,7 @@ struct TypeHandler }; template -struct ArrayVariableContent +struct ArrayVariableContent//-V730 { T data[COUNT]; size_t size = 0; diff --git a/include/json_struct/json_struct_diff.h b/include/json_struct/json_struct_diff.h index ec03642..f580e16 100644 --- a/include/json_struct/json_struct_diff.h +++ b/include/json_struct/json_struct_diff.h @@ -98,6 +98,7 @@ namespace Internal struct DiffTokens { DiffTokens() + : error(DiffError::NoError) {} explicit DiffTokens(const char* json, size_t size) @@ -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); @@ -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); diff --git a/tests/json-mias-mat.cpp b/tests/json-mias-mat.cpp index e0bd130..413eace 100644 --- a/tests/json-mias-mat.cpp +++ b/tests/json-mias-mat.cpp @@ -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)); }; diff --git a/tests/json-nullable-test.cpp b/tests/json-nullable-test.cpp index db31d89..9f66544 100644 --- a/tests/json-nullable-test.cpp +++ b/tests/json-nullable-test.cpp @@ -13,7 +13,7 @@ struct SmallStructWithoutNullable struct SmallStruct { - int a; + int a = 0; JS::Nullable b = 2.2f; JS_OBJECT(JS_MEMBER(a), JS_MEMBER(b)); @@ -21,7 +21,7 @@ struct SmallStruct struct SmallStructNullableChecked { - int a; + int a = 0; JS::NullableChecked b = 2.2f; JS_OBJECT(JS_MEMBER(a), JS_MEMBER(b)); diff --git a/tests/json-optional.cpp b/tests/json-optional.cpp index d157e84..9183a6b 100644 --- a/tests/json-optional.cpp +++ b/tests/json-optional.cpp @@ -8,7 +8,7 @@ namespace { struct SmallStructWithoutOptional { - int a; + int a = 0; float b = 2.2f; std::string d; @@ -18,7 +18,7 @@ struct SmallStructWithoutOptional #ifdef JS_STD_OPTIONAL struct SmallStructStd { - int a; + int a = 0; std::optional b = 2.2f; std::optional c; std::optional d; diff --git a/tests/json-struct-polymorphic-map.cpp b/tests/json-struct-polymorphic-map.cpp index 9b637bb..4a03f32 100644 --- a/tests/json-struct-polymorphic-map.cpp +++ b/tests/json-struct-polymorphic-map.cpp @@ -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 MoreValues; JS_OBJ(SimpleMember, MoreValues); }; @@ -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); diff --git a/tests/json-struct-test-new.cpp b/tests/json-struct-test-new.cpp index 16b7f22..c0803bc 100644 --- a/tests/json-struct-test-new.cpp +++ b/tests/json-struct-test-new.cpp @@ -58,7 +58,7 @@ static const char json_data1[] = "{\n" struct SubStruct { std::string SubString; - int SubNumber; + int SubNumber = 0; std::vector Array; JS::OptionalChecked optional_float; JS::OptionalChecked optional_double; @@ -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 * @@ -100,7 +100,8 @@ struct JsonData1 * json *} **/ - bool BooleanFalse; + bool BooleanFalse = false; + ; JS::Optional OptionalInt; /// Test structur comment SubStruct TestStruct; @@ -253,7 +254,7 @@ static const char error_in_sub[] = R"json( struct ErrorInSubChild { - int ffirst; + int ffirst = 0; JS_OBJ(ffirst); }; @@ -261,7 +262,7 @@ struct ErrorInSub { ErrorInSubChild first; std::string second; - int third; + int third = 0; JS::Optional not_assigned = 999; JS_OBJ(first, second, third); }; @@ -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 diff --git a/tests/json-struct-test.cpp b/tests/json-struct-test.cpp index d81868d..c637fd5 100644 --- a/tests/json-struct-test.cpp +++ b/tests/json-struct-test.cpp @@ -58,7 +58,7 @@ static const char json_data1[] = "{\n" struct SubStruct { std::string SubString; - int SubNumber; + int SubNumber = 0; std::vector Array; JS::OptionalChecked optional_float; JS::OptionalChecked optional_double; @@ -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 * @@ -102,7 +102,7 @@ struct JsonData1 * json *} **/ - bool BooleanFalse; + bool BooleanFalse = false; JS::Optional OptionalInt; /// Test structur comment SubStruct TestStruct; @@ -254,7 +254,7 @@ static const char error_in_sub[] = R"json({ struct ErrorInSubChild { - int ffirst; + int ffirst = 0; JS_OBJECT(JS_MEMBER(ffirst)); }; @@ -262,7 +262,7 @@ struct ErrorInSub { ErrorInSubChild first; std::string second; - int third; + int third = 0; JS::Optional not_assigned = 999; JS_OBJECT(JS_MEMBER(first), JS_MEMBER(second), JS_MEMBER(third)); }; diff --git a/tests/json-unordered-map.cpp b/tests/json-unordered-map.cpp index 423f573..a0e4be3 100644 --- a/tests/json-unordered-map.cpp +++ b/tests/json-unordered-map.cpp @@ -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()); diff --git a/tests/zero-value-test.cpp b/tests/zero-value-test.cpp index 1d582d0..906e7e4 100644 --- a/tests/zero-value-test.cpp +++ b/tests/zero-value-test.cpp @@ -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);