Skip to content

Commit

Permalink
Catch exception when assiging String<> type with too long string
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Emmers <m.emmers@alfen.com>
  • Loading branch information
marcemmers committed Jan 29, 2025
1 parent 1fa8fa0 commit 24905da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 7 additions & 3 deletions include/ocpp/common/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace ocpp {

class StringConversionException : public std::runtime_error {
using std::runtime_error::runtime_error;
};

/// \brief Contains a String impementation with a maximum length
template <size_t L> class String {
private:
Expand Down Expand Up @@ -43,11 +47,11 @@ template <size_t L> class String {
if (this->is_valid(data)) {
this->data = data;
} else {
throw std::runtime_error("String has invalid format");
throw StringConversionException("String has invalid format");
}
} else {
throw std::runtime_error("String length (" + std::to_string(data.length()) +
") exceeds permitted length (" + std::to_string(this->length) + ")");
throw StringConversionException("String length (" + std::to_string(data.length()) +
") exceeds permitted length (" + std::to_string(this->length) + ")");
}
}

Expand Down
9 changes: 9 additions & 0 deletions lib/ocpp/v201/charge_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,15 @@ void ChargePoint::message_callback(const std::string& message) {
this->security->security_event_notification_req(CiString<50>(security_event), CiString<255>(message), true,
utils::is_critical(security_event));
return;
} catch (const StringConversionException& e) {
this->logging->central_system("Unknown", message);
EVLOG_error << "JSON exception during reception of message: " << e.what();
this->message_dispatcher->dispatch_call_error(
CallError(MessageId("-1"), "RpcFrameworkError", e.what(), json({})));
const auto& security_event = ocpp::security_events::INVALIDMESSAGES;
this->security_event_notification_req(CiString<50>(security_event), CiString<255>(message), true,
utils::is_critical(security_event));
return;
} catch (const EnumConversionException& e) {
EVLOG_error << "EnumConversionException during handling of message: " << e.what();
auto call_error = CallError(MessageId("-1"), "FormationViolation", e.what(), json({}));
Expand Down

0 comments on commit 24905da

Please sign in to comment.