Skip to content

Prevent unnecessary string scanning. #16

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

Closed
wants to merge 2 commits into from
Closed

Prevent unnecessary string scanning. #16

wants to merge 2 commits into from

Conversation

EdSchouten
Copy link

The internal strcat_s() function already calls strlen() on the
source/destination strings to check for overflows. Instead of calling
into strcat() and scanning over the string again, we can simply call
memcpy() to copy the text over to the right place.

Apply the same optimization to strcpy_s(). memcpy() is almost always
faster than strcpy(), for the reason that testing against a counter is
faster than searching for a null byte in the input sequence.

This change allows the code to build with Nuxi CloudABI's C library[1].
This C library does not provide implementations for strcpy(), strcat()
and strncat(), as in addition to being potentially insecure, the
observation is that if you want to use these securely, you can almost
always use memcpy() instead.

[1] cloudlibc: https://github.com/NuxiNL/cloudlibc

The internal strcat_s() function already calls strlen() on the
source/destination strings to check for overflows. Instead of calling
into strcat() and scanning over the string again, we can simply call
memcpy() to copy the text over to the right place.

Apply the same optimization to strcpy_s(). memcpy() is almost always
faster than strcpy(), for the reason that testing against a counter is
faster than searching for a null byte in the input sequence.

This change allows the code to build with Nuxi CloudABI's C library[1].
This C library does not provide implementations for strcpy(), strcat()
and strncat(), as in addition to being potentially insecure, the
observation is that if you want to use these securely, you can almost
always use memcpy() instead.

[1] cloudlibc: https://github.com/NuxiNL/cloudlibc
@@ -207,9 +203,11 @@ namespace boost{ namespace re_detail{
const char *strSource
)
{
if(std::strlen(strSource) + std::strlen(strDestination) + 1 > sizeInBytes)
std::size_t lenSource = std::strlen(strSource) + 1;
std::size_t lenDestination = std::strlen(strDestination);
Copy link
Member

Choose a reason for hiding this comment

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

lenSource and lenDestination names are confusingly similar while one includes the terminating zero and the other doesn't.

Copy link
Author

Choose a reason for hiding this comment

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

There two ways we can resolve this. Both ways are fine by me. Be sure to let me know which one you prefer.

  • Rename lenSource to something like lenSourceWithNull, or
  • Don't add the + 1 and add it in both places where we need it.

My only concern with the second approach is that it makes the arithmetic that's going on less obvious. Right now it is quite obvious that this will never access the buffer out of bounds.

Copy link
Member

Choose a reason for hiding this comment

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

I think renaming would be better.

Copy link
Author

Choose a reason for hiding this comment

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

Done!

By renaming it to lenSourceWithNull, it is more obvious that the length
includes the trailing null byte.
@jeking3
Copy link
Contributor

jeking3 commented Jul 17, 2018

What happened to this PR? There was immediate engagement then it was dropped for 3 years?

This PR targets the wrong destination branch.
The author has since deleted his branch.
The code changes conflict with current master (maybe some of the changes were made already?)
It looks like two different fixes, one for accessing methods and one to optimize the copy.

Recommend splitting and resolving, or closing the PR.

jzmaddock pushed a commit that referenced this pull request Jul 18, 2018
@jzmaddock
Copy link
Collaborator

Manually merged to develop.

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.

4 participants