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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions include/boost/regex/v4/regex_workaround.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@
# include <locale>
#endif

#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{
using ::sprintf; using ::strcpy; using ::strcat; using ::strlen;
}
#endif

namespace boost{ namespace re_detail{
#ifdef BOOST_NO_STD_DISTANCE
template <class T>
Expand Down Expand Up @@ -83,6 +77,7 @@ namespace std{
using ::abs;
using ::memset;
using ::memcpy;
using ::strlen;
}

#endif
Expand Down Expand Up @@ -196,9 +191,10 @@ namespace boost{ namespace re_detail{
const char *strSource
)
{
if(std::strlen(strSource)+1 > sizeInBytes)
std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
if(lenSourceWithNull > sizeInBytes)
return 1;
std::strcpy(strDestination, strSource);
std::memcpy(strDestination, strSource, lenSourceWithNull);
return 0;
}
inline std::size_t strcat_s(
Expand All @@ -207,9 +203,11 @@ namespace boost{ namespace re_detail{
const char *strSource
)
{
if(std::strlen(strSource) + std::strlen(strDestination) + 1 > sizeInBytes)
std::size_t lenSourceWithNull = 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!

if(lenSourceWithNull + lenDestination > sizeInBytes)
return 1;
std::strcat(strDestination, strSource);
std::memcpy(strDestination + lenDestination, strSource,lenSourceWithNull);
return 0;
}

Expand Down