Skip to content

Commit

Permalink
Merge pull request OpenDDS#4700 from mitza-oci/warnings
Browse files Browse the repository at this point in the history
Fixed warnings related to int-to-string conversions
  • Loading branch information
jrw972 authored Jun 20, 2024
2 parents ca84604 + 0b81444 commit db05e56
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 73 deletions.
4 changes: 2 additions & 2 deletions dds/DCPS/DataSampleHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,12 @@ OPENDDS_STRING to_string(const DataSampleHeader& value)
if (value.submessage_id_ != SUBMESSAGE_NONE) {
ret += to_string(SubMessageId(value.submessage_id_));
ret += " 0x";
ret += to_dds_string(unsigned(value.submessage_id_), true);
ret += to_dds_string(ACE_CDR::Octet(value.submessage_id_), true);
ret += "), ";
} else {
ret += to_string(MessageId(value.message_id_));
ret += " (0x";
ret += to_dds_string(unsigned(value.message_id_), true);
ret += to_dds_string(ACE_CDR::Octet(value.message_id_), true);
ret += "), ";
}

Expand Down
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
2 changes: 1 addition & 1 deletion dds/DCPS/GuidUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ OPENDDS_STRING
to_string(const EntityId_t& entityId)
{
return to_hex_dds_string(&entityId.entityKey[0], sizeof(EntityKey_t)) +
to_dds_string(unsigned(entityId.entityKind), true);
to_dds_string(entityId.entityKind, true);
}

OPENDDS_STRING
Expand Down
3 changes: 1 addition & 2 deletions dds/DCPS/RTPS/Sedp.h
Original file line number Diff line number Diff line change
Expand Up @@ -655,13 +655,12 @@ class Sedp : public virtual DCPS::RcEventHandler {
return OPENDDS_STRING("dds.builtin.TOS.") +
DCPS::to_hex_dds_string(&participant.guidPrefix[0], sizeof(DCPS::GuidPrefix_t)) +
DCPS::to_hex_dds_string(&participant.entityId.entityKey[0], sizeof(DCPS::EntityKey_t)) +
DCPS::to_dds_string(unsigned(participant.entityId.entityKind), true);
DCPS::to_dds_string(participant.entityId.entityKind, true);
}

EntityId_t counterpart_entity_id() const;
GUID_t make_counterpart_guid(const DCPS::GUID_t& remote_part) const;
bool associated_with_counterpart(const DCPS::GUID_t& remote_part) const;
bool pending_association_with_counterpart(const DCPS::GUID_t& remote_part) const;
bool associated_with_counterpart_if_not_pending(const DCPS::GUID_t& remote_part) const;

RcHandle<DCPS::BitSubscriber> get_builtin_subscriber_proxy() const;
Expand Down
75 changes: 33 additions & 42 deletions dds/DCPS/SafetyProfileStreams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,90 +9,74 @@

#include "Definitions.h"

#include <ace/OS_NS_stdio.h>

OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL

namespace OpenDDS {
namespace DCPS {

String to_dds_string(unsigned short to_convert)
String to_dds_string(ACE_CDR::Octet to_convert, bool as_hex)
{
const char* fmt = "%hu";
const int buff_size = 5 + 1; // note +1 for null terminator
static const int buff_size = 3 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%02x" : "%u", to_convert);
return String(buf);
}

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, as_hex ? "%04hx" : "%hu", to_convert);
return String(buf);
}

String to_dds_string(int to_convert)
{
const char* fmt = "%d";
const int buff_size = 20 + 1; // note +1 for null terminator
static const int buff_size = 20 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
ACE_OS::snprintf(&buf[0], buff_size, "%d", to_convert);
return String(buf);
}

String to_dds_string(unsigned int to_convert, bool as_hex)
{
const char* fmt;
if (as_hex) {
fmt = "%02x";
const int buff_size = 3; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
return String(buf);
} else {
fmt = "%u";
const int buff_size = 20 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
return String(buf);
}
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 ? "%08x" : "%u", to_convert);
return String(buf);
}

String to_dds_string(long to_convert)
{
const char* fmt = "%ld";
const int buff_size = 20 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
ACE_OS::snprintf(&buf[0], buff_size, "%ld", to_convert);
return String(buf);
}

String to_dds_string(long long to_convert)
{
const char* fmt = "%lld";
const int buff_size = 20 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
ACE_OS::snprintf(&buf[0], buff_size, "%lld", to_convert);
return String(buf);
}

String to_dds_string(unsigned long long to_convert, bool as_hex)
{
const char* fmt;
if (as_hex) {
fmt = "%0llx";
} else {
fmt = "%llu";
}
const int buff_size = 20 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
ACE_OS::snprintf(&buf[0], buff_size, as_hex ? "%016llx" : "%llu", to_convert);
return String(buf);
}

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

Expand All @@ -118,10 +102,17 @@ String to_dds_string(const unsigned char* array, size_t length)

OpenDDS_Dcps_Export String to_dds_string(double to_convert)
{
const char* fmt = "%g";
const int buff_size = 20 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
ACE_OS::snprintf(&buf[0], buff_size, "%g", to_convert);
return String(buf);
}

String to_dds_string(const void* to_convert)
{
const int buff_size = 20 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, "%p", to_convert);
return String(buf);
}

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

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

#ifdef OPENDDS_SAFETY_PROFILE
# include <cstdlib> // For strto*
Expand All @@ -21,15 +20,18 @@ OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
namespace OpenDDS {
namespace DCPS {

OpenDDS_Dcps_Export String to_dds_string(unsigned short to_convert);
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, 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);

// Pass-through for conditional compilation situtations, i.e., type may be an integer or string.
inline String to_dds_string(const String& to_convert)
{
Expand All @@ -50,17 +52,7 @@ OpenDDS_Dcps_Export String to_hex_dds_string(
const char* data, size_t size, char delim = '\0', size_t delim_every = 1);
//@}

/// Convert Pointer to String
template <typename T>
inline
String to_dds_string(const T* to_convert)
{
const char* fmt = "%p";
const int buff_size = 20 + 1; // note +1 for null terminator
char buf[buff_size];
ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert);
return String(buf);
}


/**
* Convert string s to value of integral type T.
Expand Down
4 changes: 2 additions & 2 deletions dds/DCPS/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ String EncapsulationHeader::to_string() const
case KIND_INVALID:
return "Invalid";
default:
return "Unknown: " + to_dds_string(static_cast<unsigned>(kind_), true);
return "Unknown: 0x" + to_dds_string(static_cast<unsigned>(kind_), true);
}
}

Expand Down Expand Up @@ -333,7 +333,7 @@ String Encoding::kind_to_string(Kind value)
case KIND_UNALIGNED_CDR:
return "Unaligned CDR";
default:
return "Unknown: " + to_dds_string(static_cast<unsigned>(value), true);
return "Unknown: 0x" + to_dds_string(static_cast<unsigned>(value), true);
}
}

Expand Down
6 changes: 3 additions & 3 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,8 +93,8 @@ bool set_security_error(DDS::Security::SecurityException& ex,
{
std::string full(message);
const size_t i = full.size();
full.resize(i + 25);
std::snprintf(&full[i], 25, " %.2x %.2x %.2x %.2x, %.2x %.2x %.2x %.2x",
full.resize(i + 26);
std::snprintf(&full[i], 26, " %.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
2 changes: 1 addition & 1 deletion tools/rtpsrelay/GuidAddrSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ void GuidAddrSet::remove(const OpenDDS::DCPS::GUID_t& guid,
relay_stats_reporter_.local_active_participants(guid_addr_set_map_.size(), now);

if (config_.log_activity()) {
ACE_DEBUG((LM_INFO, "(%P|%t) INFO: GuidAddrSet::remove_i "
ACE_DEBUG((LM_INFO, "(%P|%t) INFO: GuidAddrSet::remove "
"%C removed %C into session total=%B remote=%B deactivation=%B expire=%B admit=%B\n",
guid_to_string(guid).c_str(),
session_time.sec_str().c_str(),
Expand Down

0 comments on commit db05e56

Please sign in to comment.