Skip to content

Commit

Permalink
Updated to_dds_string int-to-string conversions for consistent format…
Browse files Browse the repository at this point in the history
…ting

Hex formats use leading zeroes.  Hex for the C++ long type is not supported
since it has different sizes on different platforms.
  • Loading branch information
mitza-oci committed Jun 19, 2024
1 parent 53fe621 commit a549ac4
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 18 deletions.
4 changes: 2 additions & 2 deletions dds/DCPS/GuidConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ GuidConverter::GuidConverter(const GUID_t& guid)
GuidConverter::~GuidConverter()
{}

long
unsigned int
GuidConverter::checksum() const
{
return ACE::crc32(reinterpret_cast<const void*>(&guid_), sizeof(guid_));
Expand Down Expand Up @@ -156,7 +156,7 @@ GuidConverter::operator OPENDDS_STRING() const
{
OPENDDS_STRING ret(to_string(guid_));
ret += "(";
ret += to_dds_string((unsigned long) checksum(), true);
ret += to_dds_string(checksum(), true);
ret += ")";
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion dds/DCPS/GuidConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class OpenDDS_Dcps_Export GuidConverter {
virtual ~GuidConverter();

/// Calculate the CRC32 checksum.
long checksum() const;
unsigned int checksum() const;

/// Extract the VendorId value.
long vendorId() const;
Expand Down
14 changes: 7 additions & 7 deletions dds/DCPS/SafetyProfileStreams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ String to_dds_string(ACE_CDR::Octet to_convert, bool as_hex)
{
static const int buff_size = 3 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%02x" : "%d", to_convert);
ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%02x" : "%u", to_convert);

Check failure on line 21 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u22_d0i0v1_sec_js0_FM-08

‘snprintf’ is not a member of ‘ACE_6_5_21::ACE_OS’

Check failure on line 21 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_clang12_i0w1_sec

no member named 'snprintf' in namespace 'ACE_OS'; did you mean simply 'snprintf'?

Check failure on line 21 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_i0_xer0_js0_j17

‘snprintf’ is not a member of ‘ACE_OS’

Check failure on line 21 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_w19_p1_stat_js0

'snprintf': is not a member of 'ACE_OS' [D:\a\OpenDDS\OpenDDS\OpenDDS\dds\DCPS\OpenDDS_Util.vcxproj]

Check failure on line 21 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_gcc8_i0_js0_j

‘snprintf’ is not a member of ‘ACE_OS’
return String(buf);
}

String to_dds_string(unsigned short to_convert)
String to_dds_string(unsigned short to_convert, bool as_hex)
{
static const int buff_size = 5 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, "%hu", to_convert);
ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%04hx" : "%hu", to_convert);

Check failure on line 29 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u22_d0i0v1_sec_js0_FM-08

‘snprintf’ is not a member of ‘ACE_6_5_21::ACE_OS’

Check failure on line 29 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_clang12_i0w1_sec

no member named 'snprintf' in namespace 'ACE_OS'; did you mean simply 'snprintf'?

Check failure on line 29 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_i0_xer0_js0_j17

‘snprintf’ is not a member of ‘ACE_OS’

Check failure on line 29 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_w19_p1_stat_js0

'snprintf': is not a member of 'ACE_OS' [D:\a\OpenDDS\OpenDDS\OpenDDS\dds\DCPS\OpenDDS_Util.vcxproj]

Check failure on line 29 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_gcc8_i0_js0_j

‘snprintf’ is not a member of ‘ACE_OS’
return String(buf);
}

Expand All @@ -42,7 +42,7 @@ String to_dds_string(unsigned int to_convert, bool as_hex)
{
static const int buff_size = 20 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%x" : "%u", to_convert);
ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%08x" : "%u", to_convert);

Check failure on line 45 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u22_d0i0v1_sec_js0_FM-08

‘snprintf’ is not a member of ‘ACE_6_5_21::ACE_OS’

Check failure on line 45 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_clang12_i0w1_sec

no member named 'snprintf' in namespace 'ACE_OS'; did you mean simply 'snprintf'?

Check failure on line 45 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_i0_xer0_js0_j17

‘snprintf’ is not a member of ‘ACE_OS’

Check failure on line 45 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_w19_p1_stat_js0

'snprintf': is not a member of 'ACE_OS' [D:\a\OpenDDS\OpenDDS\OpenDDS\dds\DCPS\OpenDDS_Util.vcxproj]

Check failure on line 45 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_gcc8_i0_js0_j

‘snprintf’ is not a member of ‘ACE_OS’
return String(buf);
}

Expand All @@ -66,15 +66,15 @@ String to_dds_string(unsigned long long to_convert, bool as_hex)
{
const int buff_size = 20 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%0llx" : "%llu", to_convert);
ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%016llx" : "%llu", to_convert);

Check failure on line 69 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u22_d0i0v1_sec_js0_FM-08

‘snprintf’ is not a member of ‘ACE_6_5_21::ACE_OS’

Check failure on line 69 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_clang12_i0w1_sec

no member named 'snprintf' in namespace 'ACE_OS'; did you mean simply 'snprintf'?

Check failure on line 69 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_i0_xer0_js0_j17

‘snprintf’ is not a member of ‘ACE_OS’

Check failure on line 69 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_w19_p1_stat_js0

'snprintf': is not a member of 'ACE_OS' [D:\a\OpenDDS\OpenDDS\OpenDDS\dds\DCPS\OpenDDS_Util.vcxproj]

Check failure on line 69 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_gcc8_i0_js0_j

‘snprintf’ is not a member of ‘ACE_OS’
return String(buf);
}

String to_dds_string(unsigned long to_convert, bool as_hex)
String to_dds_string(unsigned long to_convert)
{
const int buff_size = 20 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%08lx" : "%lu", to_convert);
ACE_OS::snprintf(&buf[0], buff_size, "%lu", to_convert);

Check failure on line 77 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u22_d0i0v1_sec_js0_FM-08

‘snprintf’ is not a member of ‘ACE_6_5_21::ACE_OS’

Check failure on line 77 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_clang12_i0w1_sec

no member named 'snprintf' in namespace 'ACE_OS'; did you mean simply 'snprintf'?

Check failure on line 77 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_i0_xer0_js0_j17

‘snprintf’ is not a member of ‘ACE_OS’

Check failure on line 77 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_w19_p1_stat_js0

'snprintf': is not a member of 'ACE_OS' [D:\a\OpenDDS\OpenDDS\OpenDDS\dds\DCPS\OpenDDS_Util.vcxproj]

Check failure on line 77 in dds/DCPS/SafetyProfileStreams.cpp

View workflow job for this annotation

GitHub Actions / build_u20_gcc8_i0_js0_j

‘snprintf’ is not a member of ‘ACE_OS’
return String(buf);
}

Expand Down
6 changes: 2 additions & 4 deletions dds/DCPS/SafetyProfileStreams.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "PoolAllocator.h"

#include <ace/CDR_Base.h>
#include <ace/INET_Addr.h>
#include <ace/OS_NS_stdio.h>

#ifdef OPENDDS_SAFETY_PROFILE
# include <cstdlib> // For strto*
Expand All @@ -23,13 +21,13 @@ namespace OpenDDS {
namespace DCPS {

OpenDDS_Dcps_Export String to_dds_string(ACE_CDR::Octet to_convert, bool as_hex = false);
OpenDDS_Dcps_Export String to_dds_string(unsigned short to_convert);
OpenDDS_Dcps_Export String to_dds_string(unsigned short to_convert, bool as_hex = false);
OpenDDS_Dcps_Export String to_dds_string(int to_convert);
OpenDDS_Dcps_Export String to_dds_string(unsigned int to_convert, bool as_hex = false);
OpenDDS_Dcps_Export String to_dds_string(long to_convert);
OpenDDS_Dcps_Export String to_dds_string(unsigned long to_convert);
OpenDDS_Dcps_Export String to_dds_string(long long to_convert);
OpenDDS_Dcps_Export String to_dds_string(unsigned long long to_convert, bool as_hex = false);
OpenDDS_Dcps_Export String to_dds_string(unsigned long to_convert, bool as_hex = false);
OpenDDS_Dcps_Export String to_dds_string(const unsigned char* array, size_t length);
OpenDDS_Dcps_Export String to_dds_string(double value);
OpenDDS_Dcps_Export String to_dds_string(const void* to_convert);
Expand Down
4 changes: 2 additions & 2 deletions dds/DCPS/security/CommonUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool set_security_error(DDS::Security::SecurityException& ex,
ex.minor_code = minor_code;
// OpenSSL errors are rendered in hex, matching "openssl errstr"
static const bool as_hex = true;
ex.message = (message_prefix + String(" OpenSSL error ") + to_dds_string(openssl_error, as_hex)).c_str();
ex.message = (message_prefix + String(" OpenSSL error ") + to_dds_string(static_cast<unsigned int>(openssl_error), as_hex)).c_str();
return false;
}

Expand All @@ -93,7 +93,7 @@ bool set_security_error(DDS::Security::SecurityException& ex,
{
std::string full(message);
const size_t i = full.size();
full.resize(i + 25);
full.resize(i + 26);
std::sprintf(&full[i], " %.2x %.2x %.2x %.2x, %.2x %.2x %.2x %.2x",
a1[0], a1[1], a1[2], a1[3], a2[0], a2[1], a2[2], a2[3]);
return set_security_error(ex, code, minor_code, full.c_str());
Expand Down
4 changes: 2 additions & 2 deletions tests/unit-tests/dds/DCPS/GuidConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ TEST(dds_DCPS_GuidConverter, validate_Checksum)
GUID_t guid0 = InitGUID(0);
GUID_t guid1 = InitGUID(1);

long crc0 = GuidConverter(guid0).checksum();
long crc1 = GuidConverter(guid1).checksum();
unsigned int crc0 = GuidConverter(guid0).checksum();
unsigned int crc1 = GuidConverter(guid1).checksum();

EXPECT_EQ(crc0, 0xf09df109);
EXPECT_EQ(crc1, 0xf1b7254f);
Expand Down
73 changes: 73 additions & 0 deletions tests/unit-tests/dds/DCPS/SafetyProfileStreams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,79 @@

using namespace OpenDDS::DCPS;

TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_octet)
{
const ACE_CDR::Octet nine = 9, ninetyNine = 99;
EXPECT_EQ(to_dds_string(nine), "9");
EXPECT_EQ(to_dds_string(nine, true), "09");
EXPECT_EQ(to_dds_string(ninetyNine), "99");
EXPECT_EQ(to_dds_string(ninetyNine, true), "63");
}

TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_ushort)
{
const unsigned short ninetyNine = 99, nineThousand = 9000, fiftyNineThousand = 59000;
EXPECT_EQ(to_dds_string(ninetyNine), "99");
EXPECT_EQ(to_dds_string(ninetyNine, true), "0063");
EXPECT_EQ(to_dds_string(nineThousand), "9000");
EXPECT_EQ(to_dds_string(nineThousand, true), "2328");
EXPECT_EQ(to_dds_string(fiftyNineThousand), "59000");
EXPECT_EQ(to_dds_string(fiftyNineThousand, true), "e678");
}

TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_int)
{
const int ninetyNine = 99, nineThousand = 9000, negativeNineThousand = -9000;
EXPECT_EQ(to_dds_string(ninetyNine), "99");
EXPECT_EQ(to_dds_string(nineThousand), "9000");
EXPECT_EQ(to_dds_string(negativeNineThousand), "-9000");
}

TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_uint)
{
const unsigned int ninetyNine = 99, nineThousand = 9000, fiftyNineThousand = 59000;
EXPECT_EQ(to_dds_string(ninetyNine), "99");
EXPECT_EQ(to_dds_string(ninetyNine, true), "00000063");
EXPECT_EQ(to_dds_string(nineThousand), "9000");
EXPECT_EQ(to_dds_string(nineThousand, true), "00002328");
EXPECT_EQ(to_dds_string(fiftyNineThousand), "59000");
EXPECT_EQ(to_dds_string(fiftyNineThousand, true), "0000e678");
}

TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_long)
{
const long ninetyNine = 99, nineThousand = 9000, negativeNineThousand = -9000;
EXPECT_EQ(to_dds_string(ninetyNine), "99");
EXPECT_EQ(to_dds_string(nineThousand), "9000");
EXPECT_EQ(to_dds_string(negativeNineThousand), "-9000");
}

TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_ulong)
{
const unsigned long ninetyNine = 99, nineThousand = 9000;
EXPECT_EQ(to_dds_string(ninetyNine), "99");
EXPECT_EQ(to_dds_string(nineThousand), "9000");
}

TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_longlong)
{
const long long ninetyNine = 99, nineThousand = 9000, negativeNineThousand = -9000;
EXPECT_EQ(to_dds_string(ninetyNine), "99");
EXPECT_EQ(to_dds_string(nineThousand), "9000");
EXPECT_EQ(to_dds_string(negativeNineThousand), "-9000");
}

TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_ulonglong)
{
const unsigned long long ninetyNine = 99, nineThousand = 9000, fiftyNineThousand = 59000;
EXPECT_EQ(to_dds_string(ninetyNine), "99");
EXPECT_EQ(to_dds_string(ninetyNine, true), "0000000000000063");
EXPECT_EQ(to_dds_string(nineThousand), "9000");
EXPECT_EQ(to_dds_string(nineThousand, true), "0000000000002328");
EXPECT_EQ(to_dds_string(fiftyNineThousand), "59000");
EXPECT_EQ(to_dds_string(fiftyNineThousand, true), "000000000000e678");
}

TEST(dds_DCPS_SafetyProfileStreams, to_dds_string_double)
{
EXPECT_EQ(to_dds_string(1.5), "1.5");
Expand Down

0 comments on commit a549ac4

Please sign in to comment.