-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Closed
Description
Describe the bug
Hello, the latest devel of Catch2 raises a warning on g++ 12.
conversion to ‘std::size_t’ {aka ‘long unsigned int’} from ‘long int’ may change the sign of the result
This warning was introduced a few days ago by PR #2540
File catch_tostring.hpp line 46:
inline std::size_t catch_strnlen(const char *str, std::size_t n) {
auto ret = std::char_traits<char>::find(str, n, '\0');
if (ret != nullptr) {
return ret - str; // <-- Here !
}
return n;
}Expected behavior
Catch2 builds without warnings.
Reproduction steps
Build Catch2 with g++ .
Platform information:
g++ version 12.1.0 (Ubuntu 12.1.0-2ubuntu1~22.04)
Possible fixes
- Change the return type of
catch_strnlentostd::ptrdiff_t. This would prevent to returnnwithout a cast tostd::ptrdiff_t. - Explicitly cast the result to
std::size_t.
return static_cast<std::size_t>(ret - str);This should be relatively safe as str should never be greater than ret. In my opinion this is the way to go.
@rkaminsk what do you think ?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels