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

Fix Serial::write_checked_line #49

Merged
merged 5 commits into from
Jul 11, 2024
Merged

Conversation

SeaTechRC
Copy link
Contributor

@SeaTechRC SeaTechRC commented Jun 26, 2024

Hello,

During a bit of digging I came across the fact, that the Serial::write_checked_line uses the util/uart.cpp check function as if it appends a checksum to the line.
This is a suggestion for a fix.

There are a few spots where potential buffer overflows remain unchecked.
I am unsure how this should be handled, which is why I included 2 TODOs.

Also: do note that this is a proposal and as of now is completely untested.

Create make_checked_line, reuse for echo function and fix Serial::write_checked_line.
Copy link
Collaborator

@falkoschindler falkoschindler left a comment

Choose a reason for hiding this comment

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

Thanks for this pull request, @SeaTechRC!

You are right: write_checked_line() should add a checksum before writing the buffer to UART. But it uses check(), which does the opposite. It remove a checksum and raises if the checksum doesn't match the string.

However, I think there's a problem with your solution. While check() can work on a buffer by simply stripping the final @xx and decrementing the length by 3, adding a checksum causes a potential buffer overflow (as you noticed). And even worse, I'm not 100% sure the message for write_checked_line() can't contain line breaks. Since we can't easily insert checksums in a buffer, I tend to use the same algorithm like echo, looping over the buffer and printing lines with checksums on the fly.

I'll try it out and see how it goes.

@falkoschindler falkoschindler added the bug Something isn't working label Jul 11, 2024
@falkoschindler falkoschindler added this to the 0.2.0 milestone Jul 11, 2024
Copy link
Collaborator

@falkoschindler falkoschindler left a comment

Choose a reason for hiding this comment

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

I think I found a pretty neat solution. By iterating over the message buffer and writing to UART line by line, we don't need to copy anything into additional buffers - apart from the checksum itself.

@JensOgorek Could you please have a brief look at the code for an additional pair of eyes? Thank you!

Copy link
Contributor

@JensOgorek JensOgorek left a comment

Choose a reason for hiding this comment

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

Looks fine to me

@falkoschindler falkoschindler merged commit 0700f56 into zauberzeug:main Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants