Catching string overflows#339
Merged
jgfoster merged 42 commits intoOpen-Acidification:mainfrom Dec 11, 2022
Merged
Conversation
je-foster
commented
Oct 23, 2022
je-foster
commented
Oct 23, 2022
je-foster
commented
Oct 23, 2022
je-foster
commented
Oct 24, 2022
jgfoster
requested changes
Dec 8, 2022
Member
jgfoster
left a comment
There was a problem hiding this comment.
Sorry this took so long to review. I have just a few small questions...
jgfoster
approved these changes
Dec 11, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
These changes aim to replace strcpy(), strncpy(), and dtostrf(), which can cause overflows.
A good replacement for the string copy functions would be strlcpy(), but it is not available in all C++ libraries. I can compile it and put it on the Arduino device, but when I upload my code to GitHub, the tests fail because its compiler doesn't recognize strlcpy().
For copying strings, I've added a "strscpy()" function to TC_util.cpp. If the destination is too small to fit the source plus a null terminator, then it will write a truncated, null-terminated string. It will also record in the day's log file that the string was truncated to avoid overflow. There's a similar strscpy_P() function for addresses in PROGMEM.
For printing floats, I've added a "floattostrf()" function to TC_util.cpp. It uses dtostrf() to print the float to a temporary buffer whose size is 10 bytes larger than the destination, and then it uses strscpy() to copy that buffer to the destination. Therefore there will be appropriate logging if the string is truncated. Eventually it might be better to replace "3552148" with "99999" instead of "35521", for example, but I think even truncation is preferable to the current (potential) behavior of overflowing the destination.
I hope this is a positive contribution; I'd be happy to hear feedback!