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.
The code uses
sprintf(..., "%s", ...);
to copy a string and then directly calculates the length of this string. This is just ridiculously inefficient:sprintf
returns the number of bytes anyway, so the return value could be used instead of usingstrlen
, but on a much more fundamental notesprintf
is way more powerful than required for such a usecase. It has to parse the format string which is both relatively slow and introduces flash overhead since it happens at runtime and therefore unneeded branches can't be omitted. Additionally it sometimes uses a more flexible but slower memory copy implementation. (Not that that would really matter for upto 32bytes).Here I used
memcpy
instead ofstrcpy
since the length has to be calculated anyway. The final NULL byte is not copied since it isn't sent anyway. With my compiler, this reduces the flash size of the firmware by more than 30% (from 51456 bytes to 35496 bytes). Like the original code, it does not check the length and assumes that the version fields never exceed 31 bytes.