Skip to content

Commit

Permalink
[Core] : Treat warnings as errors
Browse files Browse the repository at this point in the history
  • Loading branch information
msieben committed Aug 17, 2023
1 parent 1165107 commit 44660f5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ string(REGEX REPLACE "\\-\\g$" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
string(REGEX REPLACE "-D_FORTIFY_SOURCE=[0-3]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REGEX REPLACE "-D_FORTIFY_SOURCE=[0-3]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Werror")

# make sure others can make use of the JSON creation tools as well!!!
configure_file( "${CMAKE_SOURCE_DIR}/cmake/project.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${NAMESPACE}.cmake"
Expand Down
65 changes: 34 additions & 31 deletions Source/core/JSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,9 @@ namespace Core {
// If this should be serialized/deserialized, it is indicated by a MinSize > 0)
uint16_t Serialize(char stream[], const uint16_t maxLength, uint32_t& offset) const override
{
ASSERT(maxLength > 0);
ASSERT( maxLength > 0
&& maxLength < std::numeric_limits<uint16_t>::max()
);

const int32_t available = maxLength
- (_set & QUOTED ? 2 : 0)
Expand All @@ -585,23 +587,24 @@ namespace Core {
- (BASETYPE == BASE_OCTAL ? 1 : 0)
;

int32_t loaded = 0;
uint16_t loaded = 0;

if (0 < available) {
if (_set & QUOTED) {
stream[loaded++] = '"';
}

if (_set & UNDEFINED) {
static_assert(sizeof(IElement::NullTag[0]) == sizeof(char));
static_assert(sizeof(IElement::NullTag[0]) == sizeof(char), "Mismatch sizes for underlying types not (yet) supported in copy");

const size_t count = sizeof(IElement::NullTag) - (IElement::NullTag[sizeof(IElement::NullTag) - 1] == '\0' ? 1 : 0);

if (count < available) {
offset = !(count < static_cast<size_t>(available));

if (!offset) {
memcpy(&stream[loaded], &IElement::NullTag[0], count);
loaded += static_cast<uint16_t>(count);
}

loaded += count;
} else {
if ((_set & NEGATIVE))
{
Expand All @@ -626,7 +629,7 @@ namespace Core {
}
}

offset = !(offset || loaded < available);
offset = !(!offset && loaded < available);

if (!offset) {
stream[loaded] = '\0';
Expand All @@ -649,9 +652,9 @@ namespace Core {
_set = 0;

while(!completed && loaded < maxLength && !(error.IsSet())) {
const char& c = stream[loaded++];
const char& ch = stream[loaded++];

switch (c) {
switch (ch) {
case 0x09 : // Tabulation
FALLTHROUGH
case 0x0A : // Line feed
Expand All @@ -672,7 +675,7 @@ namespace Core {
continue;
case '-' : // Signed value
if (!SIGNED) {
error = Error{"Character '" + std::string(1, c) + "' unsupported for UNSIGNED NumberType<>"};
error = Error{"Character '" + std::string(1, ch) + "' unsupported for UNSIGNED NumberType<>"};
_set = ERROR;
continue;
}
Expand All @@ -686,7 +689,7 @@ namespace Core {
|| suffix
) {
_set = ERROR;
error = Error{"Character '" + std::string(1, c) + "' at unsupported position for NumberType<>"};
error = Error{"Character '" + std::string(1, ch) + "' at unsupported position for NumberType<>"};
continue;
}

Expand All @@ -695,7 +698,7 @@ namespace Core {
case '"' : // Quoted character sequence
if (!(_set & QUOTED) && offset > 0) {// || suffix) {
_set = ERROR;
error = Error{"Character '" + std::string(1, c) + "' at unsupported position for NumberType<>"};
error = Error{"Character '" + std::string(1, ch) + "' at unsupported position for NumberType<>"};
continue;
}

Expand All @@ -714,9 +717,9 @@ namespace Core {
case 'l' : // JSON value null
ASSERT((offset - ((_set & QUOTED) ? 1 : 0)) < sizeof(NullTag));

if ((offset > 3 && !(_set & QUOTED)) || (offset > 4 && (_set & QUOTED)) || c != IElement::NullTag[offset - ((_set & QUOTED) ? 1 : 0)] || suffix) {
if ((offset > 3 && !(_set & QUOTED)) || (offset > 4 && (_set & QUOTED)) || ch != IElement::NullTag[offset - ((_set & QUOTED) ? 1 : 0)] || suffix) {
_set = ERROR;
error = Error{"Character '" + std::string(1, c) + "' at unsupported position for NumberType<>"};
error = Error{"Character '" + std::string(1, ch) + "' at unsupported position for NumberType<>"};
continue;
}

Expand All @@ -726,16 +729,16 @@ namespace Core {
break;
default : if (suffix || (_set & UNDEFINED)) {
_set = ERROR;
error = Error{"Character '" + std::string(1, c) + "' at unsupported position for NumberType<>"};
error = Error{"Character '" + std::string(1, ch) + "' at unsupported position for NumberType<>"};
continue;
}

// Define a set of rules without the use of regular expressions
switch(BASETYPE) {
case BASE_DECIMAL : // Decimal format rules
if (!(std::isdigit(c))) {
if (!(std::isdigit(ch))) {
_set = ERROR;
error = Error{"Invalid Character '" + std::string(1, c) + "' for NumberType<> with base decimal"};
error = Error{"Invalid Character '" + std::string(1, ch) + "' for NumberType<> with base decimal"};
continue;
}

Expand All @@ -745,12 +748,12 @@ namespace Core {
)
) {
_set = ERROR;
error = Error{"Character '" + std::string(1, c) + "' at unsupported position for NumberType<> with base decimal"};
error = Error{"Character '" + std::string(1, ch) + "' at unsupported position for NumberType<> with base decimal"};
continue;
}

// Convert
if (!AddDigitToValue(c)) {
if (!AddDigitToValue(ch)) {
_set = ERROR;
error = Error{"Data for NumberType<> with base decimal results in out-of-range"};
continue;
Expand All @@ -762,12 +765,12 @@ namespace Core {
if ( (offset == 0 && stream[loaded - 1] != '0' && !(_set & QUOTED) && !(_set & NEGATIVE))
|| (offset == 1 && stream[loaded - 1] != '0' && (((_set & QUOTED) && !(_set & NEGATIVE)) || (!(_set & QUOTED) && (_set & NEGATIVE))))
|| (offset == 2 && stream[loaded - 1] != '0' && (_set & QUOTED) && (_set & NEGATIVE))
|| (offset == 1 && std::toupper(c) != 'X' && !(_set & QUOTED) && !(_set & NEGATIVE))
|| (offset == 1 && std::toupper(ch) != 'X' && !(_set & QUOTED) && !(_set & NEGATIVE))
|| (offset == 2 && std::toupper(stream[loaded - 1]) != 'X' && (((_set & QUOTED) && !(_set & NEGATIVE)) || (!(_set & QUOTED) && (_set & NEGATIVE))))
|| (offset == 3 && std::toupper(stream[loaded - 1]) != 'X' && (_set & QUOTED) && (_set & NEGATIVE))
) {
_set = ERROR;
error = Error{"Character '" + std::string(1, c) + "' at unsupported position for NumberType<> with base hexadecimal"};
error = Error{"Character '" + std::string(1, ch) + "' at unsupported position for NumberType<> with base hexadecimal"};
continue;
}

Expand All @@ -778,14 +781,14 @@ namespace Core {
break;
}

if (!(std::isxdigit(c))) {
if (!(std::isxdigit(ch))) {
_set = ERROR;
error = Error{"Invalid Character '" + std::string(1, c) + "' for NumberType<> with base hexadecimal"};
error = Error{"Invalid Character '" + std::string(1, ch) + "' for NumberType<> with base hexadecimal"};
continue;
}

// Convert
if (!AddDigitToValue(c)) {
if (!AddDigitToValue(ch)) {
_set = ERROR;
error = Error{"Data for NumberType<> with base hexadecimal results in out-of-range"};
continue;
Expand All @@ -794,21 +797,21 @@ namespace Core {
_set |= HEXADECIMAL;
break;
case BASE_OCTAL : // Octal format rules
if (!(std::isdigit(c) && c <= '7')) {
error = Error{"Invalid Character '" + std::string(1, c) + "' for NumberType<> with base octal"};
if (!(std::isdigit(ch) && ch <= '7')) {
error = Error{"Invalid Character '" + std::string(1, ch) + "' for NumberType<> with base octal"};
continue;
}

if (( (offset == 0 && stream[loaded - 1] != '0') && !(_set & QUOTED) && !(_set & NEGATIVE)
if (( (offset == 0 && stream[loaded - 1] != '0' && !(_set & QUOTED) && !(_set & NEGATIVE))
|| (offset == 1 && stream[loaded - 1] != '0' && (((_set & QUOTED) && !(_set & NEGATIVE)) || (!(_set & QUOTED) && (_set & NEGATIVE))))
|| (offset == 2 && stream[loaded - 1] != '0' && (_set & QUOTED) && (_set & NEGATIVE))
|| (offset == 2 && stream[loaded - 1] == '0' && stream[loaded - 2] == '0') && !(_set & QUOTED) && !(_set & NEGATIVE)
|| (offset == 2 && stream[loaded - 1] == '0' && stream[loaded - 2] == '0' && !(_set & QUOTED) && !(_set & NEGATIVE))
|| (offset == 3 && stream[loaded - 1] == '0' && stream[loaded - 2] == '0' && (((_set & QUOTED) && !(_set & NEGATIVE)) || (!(_set & QUOTED) && (_set & NEGATIVE))))
|| (offset == 4 && stream[loaded - 1] == '0' && stream[loaded - 2] == '0' && (_set & QUOTED) && (_set & NEGATIVE))
)
) {
_set = ERROR;
error = Error{"Character '" + std::string(1, c) + "' at unsupported position for NumberType<> with base octal"};
error = Error{"Character '" + std::string(1, ch) + "' at unsupported position for NumberType<> with base octal"};
continue;
}

Expand All @@ -820,7 +823,7 @@ namespace Core {
}

// Convert
if (!AddDigitToValue(c)) {
if (!AddDigitToValue(ch)) {
_set = ERROR;
error = Error{"Data for NumberType<> with base octal results in out-of-range"};
continue;
Expand Down Expand Up @@ -1053,7 +1056,7 @@ namespace Core {

const uTYPE max = _set & NEGATIVE ? static_cast<uTYPE>(-(1 + std::numeric_limits<sTYPE>::min())) + 1 : static_cast<uTYPE>(std::numeric_limits<TYPE>::max());

number = _set & NEGATIVE ? static_cast<uTYPE>(-(1 + _value)) + 1 : static_cast<uTYPE>(_value);
number = _set & NEGATIVE ? static_cast<uTYPE>(-(1 + static_cast<sTYPE>(_value))) + 1 : static_cast<uTYPE>(_value);

result = number <= ((max - offset) / base);

Expand Down

0 comments on commit 44660f5

Please sign in to comment.