diff --git a/Source/core/Frame.h b/Source/core/Frame.h index 7f63ab432..8dddae503 100644 --- a/Source/core/Frame.h +++ b/Source/core/Frame.h @@ -465,11 +465,11 @@ namespace Core { _offset += _container->SetText(_offset, text); } - void NullTerminatedText(const string& text) + void NullTerminatedText(const string& text, const SIZE_CONTEXT maxLength = ~0) { ASSERT(_container != nullptr); - _offset += _container->SetNullTerminatedText(_offset, text); + _offset += _container->SetNullTerminatedText(_offset, text, maxLength); } private: @@ -647,16 +647,16 @@ namespace Core { return (SetBuffer(offset, static_cast(convertedText.length()), reinterpret_cast(convertedText.c_str()))); } - SIZE_CONTEXT SetNullTerminatedText(const SIZE_CONTEXT offset, const string& value) + SIZE_CONTEXT SetNullTerminatedText(const SIZE_CONTEXT offset, const string& value, const SIZE_CONTEXT maxLength) { - std::string convertedText(Core::ToString(value)); - SIZE_CONTEXT requiredLength(static_cast< SIZE_CONTEXT>(convertedText.length() + 1)); + std::string convertedText(Core::ToString(value).data(), (((maxLength != static_cast(~0)) && ((value.length() + 1) > maxLength)) ? (maxLength - 1) : value.length())); + SIZE_CONTEXT requiredLength(static_cast(convertedText.length() + 1)); if ((offset + requiredLength) >= _size) { Size(offset + requiredLength); } - ::memcpy(&(_data[offset]), convertedText.c_str(), convertedText.length() + 1); + ::memcpy(&(_data[offset]), convertedText.c_str(), requiredLength); return (requiredLength); } diff --git a/Source/core/MessageStore.cpp b/Source/core/MessageStore.cpp index 4eb1e860c..17549e9c3 100644 --- a/Source/core/MessageStore.cpp +++ b/Source/core/MessageStore.cpp @@ -199,7 +199,7 @@ namespace Core { ASSERT(bufferSize >= (length + extra)); if (bufferSize >= (length + extra)) { - Core::FrameType<0> frame(const_cast(buffer) + length, bufferSize - length, bufferSize - length); + Core::FrameType<0> frame(buffer + length, bufferSize - length, bufferSize - length); Core::FrameType<0>::Writer frameWriter(frame, 0); frameWriter.Number(_timeStamp); length += extra; @@ -261,7 +261,7 @@ namespace Core { ASSERT(bufferSize >= (length + extra)); if (bufferSize >= (length + extra)) { - Core::FrameType<0> frame(const_cast(buffer) + length, bufferSize - length, bufferSize - length); + Core::FrameType<0> frame(buffer + length, bufferSize - length, bufferSize - length); Core::FrameType<0>::Writer frameWriter(frame, 0); frameWriter.NullTerminatedText(_className); frameWriter.NullTerminatedText(_fileName); @@ -333,7 +333,7 @@ namespace Core { ASSERT(bufferSize >= (length + extra)); if (bufferSize >= (length + extra)) { - Core::FrameType<0> frame(const_cast(buffer) + length, bufferSize - length, bufferSize - length); + Core::FrameType<0> frame(buffer + length, bufferSize - length, bufferSize - length); Core::FrameType<0>::Writer frameWriter(frame, 0); frameWriter.NullTerminatedText(_callsign); length += extra; diff --git a/Source/extensions/warningreporting/WarningReportingUnit.cpp b/Source/extensions/warningreporting/WarningReportingUnit.cpp index 9e444d3cd..28cfda458 100644 --- a/Source/extensions/warningreporting/WarningReportingUnit.cpp +++ b/Source/extensions/warningreporting/WarningReportingUnit.cpp @@ -140,7 +140,7 @@ namespace WarningReporting { } void WarningReportingUnit::ReportWarningEvent(const char identifier[], const IWarningEvent& information) - { + { WPEFramework::Core::Messaging::Metadata metadata(WPEFramework::Core::Messaging::Metadata::type::REPORTING, information.Category(), WPEFramework::Core::Messaging::MODULE_REPORTING); WPEFramework::Core::Messaging::MessageInfo messageInfo(metadata, WPEFramework::Core::Time::Now().Ticks()); WPEFramework::Core::Messaging::IStore::WarningReporting report(messageInfo, identifier); diff --git a/Source/messaging/ConsoleStreamRedirect.h b/Source/messaging/ConsoleStreamRedirect.h index 568ec633f..5f078934c 100644 --- a/Source/messaging/ConsoleStreamRedirect.h +++ b/Source/messaging/ConsoleStreamRedirect.h @@ -22,8 +22,7 @@ namespace WPEFramework { if (OperationalStream::StandardOut::IsEnabled() == true) { Core::Messaging::MessageInfo messageInfo(OperationalStream::StandardOut::Metadata(), Core::Time::Now().Ticks()); Core::Messaging::IStore::OperationalStream operationalStream(messageInfo); - string text(buffer, length); - TextMessage data(text); + TextMessage data(length, buffer); MessageUnit::Instance().Push(operationalStream, &data); } } @@ -46,8 +45,7 @@ namespace WPEFramework { if (OperationalStream::StandardError::IsEnabled() == true) { Core::Messaging::MessageInfo messageInfo(OperationalStream::StandardError::Metadata(), Core::Time::Now().Ticks()); Core::Messaging::IStore::OperationalStream operationalStream(messageInfo); - string text(buffer, length); - TextMessage data(text); + TextMessage data(length, buffer); MessageUnit::Instance().Push(operationalStream, &data); } } diff --git a/Source/messaging/DirectOutput.cpp b/Source/messaging/DirectOutput.cpp index 0417263af..0cf89f514 100644 --- a/Source/messaging/DirectOutput.cpp +++ b/Source/messaging/DirectOutput.cpp @@ -31,17 +31,15 @@ namespace WPEFramework { ASSERT(message != nullptr); ASSERT(messageInfo.Type() != Core::Messaging::Metadata::type::INVALID); - string result = messageInfo.ToString(_abbreviate).c_str() + message->Data(); - #ifndef __WINDOWS__ if (_isSyslog == true) { //use longer messages for syslog - syslog(LOG_NOTICE, "%s\n", result.c_str()); + syslog(LOG_NOTICE, "%s%s\n", messageInfo.ToString(_abbreviate).c_str(), message->Data().c_str()); } else #endif { - std::cout << result << std::endl; + std::cout << messageInfo.ToString(_abbreviate).c_str() << message->Data() << std::endl; } } } // namespace Messaging diff --git a/Source/messaging/TextMessage.h b/Source/messaging/TextMessage.h index 7b859a358..bd2c08be0 100644 --- a/Source/messaging/TextMessage.h +++ b/Source/messaging/TextMessage.h @@ -32,28 +32,22 @@ namespace Messaging { : _text(text) { } + TextMessage(const uint16_t length, const TCHAR buffer[]) + : _text(buffer, length) + { + } TextMessage(const TextMessage&) = delete; TextMessage& operator=(const TextMessage&) = delete; uint16_t Serialize(uint8_t buffer[], const uint16_t bufferSize) const override { - uint16_t length = 0; Core::FrameType<0> frame(buffer, bufferSize, bufferSize); Core::FrameType<0>::Writer writer(frame, 0); - if (bufferSize < _text.size() + 1) { - string cutString(_text, 0, bufferSize - 1); - writer.NullTerminatedText(cutString); - length = bufferSize; - - } - else { - writer.NullTerminatedText(_text); - length = static_cast(_text.size() + 1); - } + writer.NullTerminatedText(_text, bufferSize); - return (length); + return (std::min(bufferSize, static_cast(_text.size() + 1))); } uint16_t Deserialize(const uint8_t buffer[], const uint16_t bufferSize) override