Skip to content

Commit e6963e9

Browse files
penneleedoug-walker
andcommitted
Issue AcademySoftwareFoundation#1874 Cast to unsigned char for isspace. (AcademySoftwareFoundation#1888)
* Issue AcademySoftwareFoundation#1874 Cast to unsigned char for isspace. Signed-off-by: pylee <[email protected]> * Add unit test. Signed-off-by: pylee <[email protected]> * Add test comment as suggested in code review. Signed-off-by: pylee <[email protected]> --------- Signed-off-by: pylee <[email protected]> Signed-off-by: Doug Walker <[email protected]> Co-authored-by: Doug Walker <[email protected]> Signed-off-by: Doug Walker <[email protected]>
1 parent 5779ddf commit e6963e9

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/utils/StringUtils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ inline std::string LeftTrim(std::string str, char c)
106106
// Starting from the left, trim all the space characters i.e. space, tabulation, etc.
107107
inline std::string LeftTrim(std::string str)
108108
{
109-
const auto it = std::find_if(str.begin(), str.end(), [](char ch) { return !std::isspace(ch); });
109+
const auto it = std::find_if(str.begin(), str.end(), [](char ch) { return !std::isspace(static_cast<unsigned char>(ch)); });
110110
str.erase(str.begin(), it);
111111
return str;
112112
}
@@ -123,7 +123,7 @@ inline std::string RightTrim(std::string str, char c)
123123
inline std::string RightTrim(std::string str)
124124
{
125125
const auto it =
126-
std::find_if(str.rbegin(), str.rend(), [](char ch) { return !std::isspace(ch); });
126+
std::find_if(str.rbegin(), str.rend(), [](char ch) { return !std::isspace(static_cast<unsigned char>(ch)); });
127127
str.erase(it.base(), str.end());
128128
return str;
129129
}

tests/utils/StringUtils_tests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ OCIO_ADD_TEST(StringUtils, trim)
5050
const std::string str = StringUtils::Trim(ref);
5151
OCIO_CHECK_EQUAL(str, "lOwEr 1*& ctfG");
5252
}
53+
54+
{
55+
// Test that no assert happens when the Trim argument is not an unsigned char (see issue #1874).
56+
constexpr char ref2[]{ -1, -2, -3, '\0' };
57+
const std::string str = StringUtils::Trim(ref2);
58+
}
5359
}
5460

5561
OCIO_ADD_TEST(StringUtils, split)

0 commit comments

Comments
 (0)