Skip to content

Commit

Permalink
Fix issues caught by MISRA 2023 rule 7.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Aug 20, 2024
1 parent 88f57ac commit 7a4e19d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <array>
#include <cstring>
#include <fstream>
#include <string>

#include "zserio/BitSizeOfCalculator.h"
#include "zserio/BitStreamWriter.h"
Expand Down Expand Up @@ -503,7 +504,7 @@ void BitStreamWriter::writeString(StringView data)
// we are not aligned to byte
for (size_t i = 0; i < len; ++i)
{
writeBits(static_cast<uint8_t>(data[i]), 8);
writeBits(static_cast<uint32_t>(::std::char_traits<char>::to_int_type(data[i])), 8);
}
}
else
Expand Down
44 changes: 25 additions & 19 deletions compiler/extensions/cpp/runtime/src/zserio/JsonEncoder.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <array>
#include <cmath>
#include <iomanip>
#include <string>

#include "zserio/JsonEncoder.h"

Expand Down Expand Up @@ -54,48 +55,53 @@ void JsonEncoder::encodeString(std::ostream& stream, StringView value)
(void)stream.put('"');
for (char character : value)
{
switch (character)
if (character == '\\' || character == '"')
{
case '\\':
case '"':
(void)stream.put('\\');
(void)stream.put(character);
break;
case '\b':
}
else if (character == '\b')
{
(void)stream.put('\\');
(void)stream.put('b');
break;
case '\f':
}
else if (character == '\f')
{
(void)stream.put('\\');
(void)stream.put('f');
break;
case '\n':
}
else if (character == '\n')
{
(void)stream.put('\\');
(void)stream.put('n');
break;
case '\r':
}
else if (character == '\r')
{
(void)stream.put('\\');
(void)stream.put('r');
break;
case '\t':
}
else if (character == '\t')
{
(void)stream.put('\\');
(void)stream.put('t');
break;
default:
if (static_cast<uint8_t>(character) <= 0x1F)
}
else
{
const unsigned int characterInt =
static_cast<unsigned int>(::std::char_traits<char>::to_int_type(character));
if (characterInt <= 0x1F)
{
(void)stream.put('\\');
(void)stream.put('u');
(void)stream.put('0');
(void)stream.put('0');
(void)stream.put(HEX[static_cast<uint8_t>(static_cast<uint8_t>(character) >> 4U) & 0xFU]);
(void)stream.put(HEX[static_cast<uint8_t>(character) & 0xFU]);
(void)stream.put(HEX[(characterInt >> 4U) & 0xFU]);
(void)stream.put(HEX[characterInt & 0xFU]);
}
else
{
(void)stream.put(character);
}
break;
}
}
(void)stream.put('"');
Expand Down
13 changes: 7 additions & 6 deletions compiler/extensions/cpp/runtime/src/zserio/StringConvertUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ template <typename T,
typename std::enable_if<std::is_unsigned<T>::value && !std::is_same<T, bool>::value, int>::type = 0>
const char* convertIntToString(std::array<char, 24>& buffer, T value, bool isNegative)
{
static const std::array<char, 201> DIGITS = {
static const std::array<char, 201> DIGITS_100_10 = {
"0001020304050607080910111213141516171819"
"2021222324252627282930313233343536373839"
"4041424344454647484950515253545556575859"
"6061626364656667686970717273747576777879"
"8081828384858687888990919293949596979899"};
static const std::array<char, 11> DIGITS_1 = {"0123456789"};

auto bufferEnd = buffer.end();
*(--bufferEnd) = '\0'; // always terminate with '\0'
Expand All @@ -39,19 +40,19 @@ const char* convertIntToString(std::array<char, 24>& buffer, T value, bool isNeg
{
const unsigned int index = static_cast<unsigned int>((value % 100) * 2);
value /= 100;
*(--bufferEnd) = DIGITS[index + 1];
*(--bufferEnd) = DIGITS[index];
*(--bufferEnd) = DIGITS_100_10[index + 1];
*(--bufferEnd) = DIGITS_100_10[index];
}

if (value < 10)
{
*(--bufferEnd) = static_cast<char>('0' + value);
*(--bufferEnd) = DIGITS_1[static_cast<unsigned int>(value)];
}
else
{
const unsigned int index = static_cast<unsigned int>(value * 2);
*(--bufferEnd) = DIGITS[index + 1];
*(--bufferEnd) = DIGITS[index];
*(--bufferEnd) = DIGITS_100_10[index + 1];
*(--bufferEnd) = DIGITS_100_10[index];
}

if (isNegative)
Expand Down

0 comments on commit 7a4e19d

Please sign in to comment.