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

Catch exception when assiging String<> type with too long string #966

Merged
merged 5 commits into from
Feb 14, 2025

Conversation

marcemmers
Copy link
Contributor

Describe your changes

Fixes a crash when the MessageId string received is longer than 36 characters. Instead of crashing we now send an RpcFrameworkError.

Issue ticket number and link

Checklist before requesting a review

@Pietfried
Copy link
Contributor

I think we can still end up having issues here with the SecurityEventNotification with CiString<255>(message) when the ocpp message is > 255 characters.

@marcemmers
Copy link
Contributor Author

To be honest I did not check further, this was just an issue I found with some incoming message tests that we did.

I am not sure what you are referring to here? Isn't that CiString created by the application for the SecurityEventNotification? In that case there will be an error thrown that the application can/should catch right?

this->message_dispatcher->dispatch_call_error(
CallError(MessageId("-1"), "RpcFrameworkError", e.what(), json({})));
const auto& security_event = ocpp::security_events::INVALIDMESSAGES;
this->security->security_event_notification_req(CiString<50>(security_event), CiString<255>(message), true,
Copy link
Contributor

Choose a reason for hiding this comment

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

This can still throw if message > 255 characters or security_event > 50 characters.

It might be an option to truncate CiStrings by default inside the class in case the number of characters exceeds the specified limit instead of throwing the exception. I dont have a complete overview but I think it can happen at quite a few places

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added an argument that can optionally truncate if we want. I don't think truncating by default is an option because that might lead to strange behavior as well. We would silently accept incorrect messages from the BO for example.

Copy link
Contributor

@Pietfried Pietfried left a comment

Choose a reason for hiding this comment

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

Looks good, just added a couple of minor remarks

using std::runtime_error::runtime_error;
};

enum class StringToLarge {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit StringTooLarge


public:
/// \brief Creates a string from the given \p data
String(const std::string& data) : length(L) {
this->set(data);
String(const std::string& data, StringToLarge to_large = StringToLarge::Throw) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
String(const std::string& data, StringToLarge to_large = StringToLarge::Throw) {
explicit String(const std::string& data, StringToLarge to_large = StringToLarge::Throw) {

}

String(const char* data) : length(L) {
this->set(data);
String(const char* data, StringToLarge to_large = StringToLarge::Throw) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
String(const char* data, StringToLarge to_large = StringToLarge::Throw) {
explicit String(const char* data, StringToLarge to_large = StringToLarge::Throw) {

marcemmers and others added 5 commits February 14, 2025 11:40
Signed-off-by: Marc Emmers <m.emmers@alfen.com>
Signed-off-by: Marc Emmers <35759328+marcemmers@users.noreply.github.com>
Signed-off-by: Marc Emmers <35759328+marcemmers@users.noreply.github.com>
…ariable which can be done purely compile time

Signed-off-by: Marc Emmers <m.emmers@alfen.com>
Signed-off-by: Piet Gömpel <pietgoempel@gmail.com>
@Pietfried Pietfried force-pushed the me-fix-string-to-long-crash branch from 3060fa5 to 47351c5 Compare February 14, 2025 15:16
@Pietfried Pietfried merged commit 3283265 into main Feb 14, 2025
7 checks passed
@Pietfried Pietfried deleted the me-fix-string-to-long-crash branch February 14, 2025 15:56
hikinggrass pushed a commit that referenced this pull request Feb 17, 2025
* Catch exception when assiging String<> type with too long string
* Add choice between throw and truncating, also remove runtime length variable which can be done purely compile time
---------

Signed-off-by: Marc Emmers <m.emmers@alfen.com>
Signed-off-by: Marc Emmers <35759328+marcemmers@users.noreply.github.com>
Signed-off-by: Piet Gömpel <pietgoempel@gmail.com>
Co-authored-by: Piet Gömpel <pietgoempel@gmail.com>
maaikez pushed a commit that referenced this pull request Feb 17, 2025
* Catch exception when assiging String<> type with too long string
* Add choice between throw and truncating, also remove runtime length variable which can be done purely compile time
---------

Signed-off-by: Marc Emmers <m.emmers@alfen.com>
Signed-off-by: Marc Emmers <35759328+marcemmers@users.noreply.github.com>
Signed-off-by: Piet Gömpel <pietgoempel@gmail.com>
Co-authored-by: Piet Gömpel <pietgoempel@gmail.com>
Signed-off-by: Maaike Zijderveld, iolar <git.mail@iolar.nl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants