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 -Wstringop-truncation warning. #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

denravonska
Copy link
Contributor

This fixes a warning I encountered when cross compiling for Raspberry Pi. If term is longer than stagingBuffer it won't be 0-terminated.

Also, fun fact. strncpy always copies the specified bytes. If the source is shorter than that it pads the rest with 0. TIL.

If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written.

For the low end chips like AVR it could be worth adding a custom strncpy which guarantees termination without the overhead of filling the entire buffer.

char* tinygps_strncpy(char *dest, const char *src, size_t n) {
   size_t i;
   for (i = 0; i < n && src[i] != '\0'; i++)
      dest[i] = src[i];

   dest[i - 1] = 0;
   return dest;
}

@mikalhart
Copy link
Owner

Plan to fix in next release; thanks.

@denravonska
Copy link
Contributor Author

Don't use my particular strncpy though since it goes oob if the first character is 0 :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants