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

Issue #1874 Cast to unsigned char for isspace. #1888

Merged
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/utils/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ inline std::string LeftTrim(std::string str, char c)
// Starting from the left, trim all the space characters i.e. space, tabulation, etc.
inline std::string LeftTrim(std::string str)
{
const auto it = std::find_if(str.begin(), str.end(), [](char ch) { return !std::isspace(ch); });
const auto it = std::find_if(str.begin(), str.end(), [](char ch) { return !std::isspace(static_cast<unsigned char>(ch)); });
str.erase(str.begin(), it);
return str;
}
Expand All @@ -123,7 +123,7 @@ inline std::string RightTrim(std::string str, char c)
inline std::string RightTrim(std::string str)
{
const auto it =
std::find_if(str.rbegin(), str.rend(), [](char ch) { return !std::isspace(ch); });
std::find_if(str.rbegin(), str.rend(), [](char ch) { return !std::isspace(static_cast<unsigned char>(ch)); });
str.erase(it.base(), str.end());
return str;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/utils/StringUtils_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ OCIO_ADD_TEST(StringUtils, trim)
const std::string str = StringUtils::Trim(ref);
OCIO_CHECK_EQUAL(str, "lOwEr 1*& ctfG");
}

{
doug-walker marked this conversation as resolved.
Show resolved Hide resolved
// Test that no assert happens when the Trim argument is not an unsigned char (see issue #1874).
constexpr char ref2[]{ -1, -2, -3, '\0' };
const std::string str = StringUtils::Trim(ref2);
}
}

OCIO_ADD_TEST(StringUtils, split)
Expand Down