Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle of invalid datetime when format check fails #822

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.14)

project(ocpp
VERSION 0.17.2
VERSION 0.18.0
DESCRIPTION "A C++ implementation of the Open Charge Point Protocol"
LANGUAGES CXX
)
Expand Down
21 changes: 15 additions & 6 deletions include/ocpp/common/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ struct Message {
virtual std::string get_type() const = 0;
};

/// \brief Exception used when DateTime class is initialized by invalid timepoint string.
class TimePointParseException : public std::exception {
public:
explicit TimePointParseException(const std::string& timepoint_str) :
msg{"Timepoint string parsing failed. Could not convert: \"" + timepoint_str + "\" into DateTime."} {
}

const char* what() const noexcept override {
return msg.c_str();
}

private:
std::string msg;
};

/// \brief Contains a DateTime implementation that can parse and create RFC 3339 compatible strings
class DateTimeImpl {
private:
Expand Down Expand Up @@ -103,12 +118,6 @@ class DateTime : public DateTimeImpl {

/// \brief Creates a new DateTime object from the given \p timepoint_str
explicit DateTime(const std::string& timepoint_str);

/// \brief Assignment operator= that converts a given string \p s into a DateTime
DateTime& operator=(const std::string& s);

/// \brief Assignment operator= that converts a given char* \p c into a DateTime
DateTime& operator=(const char* c);
};

/// \brief Base exception for when a conversion from string to enum or vice versa fails
Expand Down
2 changes: 1 addition & 1 deletion lib/ocpp/common/database/database_handler_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ std::vector<DBTransactionMessage> DatabaseHandlerCommon::get_message_queue_messa

DBTransactionMessage control_message;
control_message.message_attempts = message_attempts;
control_message.timestamp = message_timestamp;
control_message.timestamp = ocpp::DateTime(message_timestamp);
control_message.message_type = message_type;
control_message.unique_id = unique_id;
control_message.json_message = json_message;
Expand Down
13 changes: 1 addition & 12 deletions lib/ocpp/common/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ DateTime::DateTime(std::chrono::time_point<date::utc_clock> timepoint) : DateTim
DateTime::DateTime(const std::string& timepoint_str) : DateTimeImpl(timepoint_str) {
}

DateTime& DateTime::operator=(const std::string& s) {
this->from_rfc3339(s);
return *this;
}

DateTime& DateTime::operator=(const char* c) {
this->from_rfc3339(std::string(c));
return *this;
}

DateTimeImpl::DateTimeImpl() {
this->timepoint = date::utc_clock::now();
}
Expand Down Expand Up @@ -53,8 +43,7 @@ void DateTimeImpl::from_rfc3339(const std::string& timepoint_str) {
in.seekg(0);
in >> date::parse("%FT%T", this->timepoint);
if (in.fail()) {
EVLOG_error << "Timepoint string parsing failed. Could not convert: \"" << timepoint_str
<< "\" into DateTime.";
throw TimePointParseException(timepoint_str);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm now you added a throw where the software did not crash in the past. Did you check everywhere this is used and provide a try / catch around it if needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the std::string constructor calls only when json is converted to the DateTime type or in database conversions. In other places the constructor is not (should not be) called. So then it should be caught either by TimePointParseException or std::exception, that is done. From my side and for 2.0.1, I don't see the missing places. @hikinggrass can approve as well for 1.6

}
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/ocpp/v16/charge_point_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,9 @@ void ChargePointImpl::message_callback(const std::string& message) {
EnhancedMessage<v16::MessageType> enhanced_message;
try {
enhanced_message = this->message_queue->receive(message);
} catch (const TimePointParseException& e) {
EVLOG_error << "Exception during handling of message: " << e.what();
this->send(CallError(enhanced_message.uniqueId, "FormationViolation", e.what(), json({})));
} catch (const json::exception& e) {
EVLOG_error << "JSON exception during reception of message: " << e.what();
this->send(CallError(MessageId("-1"), "GenericError", e.what(), json({})));
Expand Down
10 changes: 7 additions & 3 deletions lib/ocpp/v201/charge_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,10 @@ void ChargePoint::message_callback(const std::string& message) {
EVLOG_error << "EnumConversionException during handling of message: " << e.what();
auto call_error = CallError(enhanced_message.uniqueId, "FormationViolation", e.what(), json({}));
this->send(call_error);
} catch (const TimePointParseException& e) {
EVLOG_error << "Exception during handling of message: " << e.what();
auto call_error = CallError(enhanced_message.uniqueId, "FormationViolation", e.what(), json({}));
this->send(call_error);
} catch (json::exception& e) {
EVLOG_error << "JSON exception during handling of message: " << e.what();
if (json_message.is_array() and json_message.size() > MESSAGE_ID) {
Expand Down Expand Up @@ -1991,9 +1995,9 @@ void ChargePoint::security_event_notification_req(const CiString<50>& event_type

req.type = event_type;
if (timestamp.has_value()) {
req.timestamp = timestamp.value().to_rfc3339();
req.timestamp = timestamp.value();
} else {
req.timestamp = DateTime().to_rfc3339();
req.timestamp = DateTime();
}
req.techInfo = tech_info;
this->logging->security(json(req).dump());
Expand Down Expand Up @@ -2165,7 +2169,7 @@ void ChargePoint::status_notification_req(const int32_t evse_id, const int32_t c
StatusNotificationRequest req;
req.connectorId = connector_id;
req.evseId = evse_id;
req.timestamp = DateTime().to_rfc3339();
req.timestamp = DateTime();
req.connectorStatus = status;

ocpp::Call<StatusNotificationRequest> call(req, this->message_queue->createMessageId());
Expand Down
Loading