Commit 1de25658 authored by Haocheng Zhang's avatar Haocheng Zhang Committed by Facebook Github Bot 7

fix bug for nullptr in qfind

Summary: Fix bug for passing null pointer to memchr function, which requires the first argument to never be null.

Reviewed By: luciang

Differential Revision: D3432130

fbshipit-source-id: 419924dd214d9f641d3d46335dae6abbe44ca751
parent bd6447c6
...@@ -1085,6 +1085,10 @@ size_t rfind(const Range<T>& haystack, ...@@ -1085,6 +1085,10 @@ size_t rfind(const Range<T>& haystack,
// specialization for StringPiece // specialization for StringPiece
template <> template <>
inline size_t qfind(const Range<const char*>& haystack, const char& needle) { inline size_t qfind(const Range<const char*>& haystack, const char& needle) {
// memchr expects a not-null pointer, early return if the range is empty.
if (haystack.empty()) {
return std::string::npos;
}
auto pos = static_cast<const char*>( auto pos = static_cast<const char*>(
::memchr(haystack.data(), needle, haystack.size())); ::memchr(haystack.data(), needle, haystack.size()));
return pos == nullptr ? std::string::npos : pos - haystack.data(); return pos == nullptr ? std::string::npos : pos - haystack.data();
...@@ -1092,6 +1096,10 @@ inline size_t qfind(const Range<const char*>& haystack, const char& needle) { ...@@ -1092,6 +1096,10 @@ inline size_t qfind(const Range<const char*>& haystack, const char& needle) {
template <> template <>
inline size_t rfind(const Range<const char*>& haystack, const char& needle) { inline size_t rfind(const Range<const char*>& haystack, const char& needle) {
// memchr expects a not-null pointer, early return if the range is empty.
if (haystack.empty()) {
return std::string::npos;
}
auto pos = static_cast<const char*>( auto pos = static_cast<const char*>(
::memrchr(haystack.data(), needle, haystack.size())); ::memrchr(haystack.data(), needle, haystack.size()));
return pos == nullptr ? std::string::npos : pos - haystack.data(); return pos == nullptr ? std::string::npos : pos - haystack.data();
...@@ -1101,6 +1109,10 @@ inline size_t rfind(const Range<const char*>& haystack, const char& needle) { ...@@ -1101,6 +1109,10 @@ inline size_t rfind(const Range<const char*>& haystack, const char& needle) {
template <> template <>
inline size_t qfind(const Range<const unsigned char*>& haystack, inline size_t qfind(const Range<const unsigned char*>& haystack,
const unsigned char& needle) { const unsigned char& needle) {
// memchr expects a not-null pointer, early return if the range is empty.
if (haystack.empty()) {
return std::string::npos;
}
auto pos = static_cast<const unsigned char*>( auto pos = static_cast<const unsigned char*>(
::memchr(haystack.data(), needle, haystack.size())); ::memchr(haystack.data(), needle, haystack.size()));
return pos == nullptr ? std::string::npos : pos - haystack.data(); return pos == nullptr ? std::string::npos : pos - haystack.data();
...@@ -1109,6 +1121,10 @@ inline size_t qfind(const Range<const unsigned char*>& haystack, ...@@ -1109,6 +1121,10 @@ inline size_t qfind(const Range<const unsigned char*>& haystack,
template <> template <>
inline size_t rfind(const Range<const unsigned char*>& haystack, inline size_t rfind(const Range<const unsigned char*>& haystack,
const unsigned char& needle) { const unsigned char& needle) {
// memchr expects a not-null pointer, early return if the range is empty.
if (haystack.empty()) {
return std::string::npos;
}
auto pos = static_cast<const unsigned char*>( auto pos = static_cast<const unsigned char*>(
::memrchr(haystack.data(), needle, haystack.size())); ::memrchr(haystack.data(), needle, haystack.size()));
return pos == nullptr ? std::string::npos : pos - haystack.data(); return pos == nullptr ? std::string::npos : pos - haystack.data();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment