Skip to content

Commit

Permalink
Fix code causing spurious Wstringop-overflow warning
Browse files Browse the repository at this point in the history
See #2989, #3054, and others
  • Loading branch information
codeinred authored and vitaut committed Mar 18, 2023
1 parent 9c5cd99 commit d9bc5f1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
14 changes: 3 additions & 11 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2206,20 +2206,12 @@ constexpr auto to_ascii(Char c) -> char {
return c <= 0xff ? static_cast<char>(c) : '\0';
}

FMT_CONSTEXPR inline auto code_point_length_impl(char c) -> int {
return "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0\0\0\2\2\2\2\3\3\4"
[static_cast<unsigned char>(c) >> 3];
}

// Returns the number of code units in a code point or 1 on error.
template <typename Char>
FMT_CONSTEXPR auto code_point_length(const Char* begin) -> int {
if (const_check(sizeof(Char) != 1)) return 1;
int len = code_point_length_impl(static_cast<char>(*begin));

// Compute the pointer to the next character early so that the next
// iteration can start working on the next character. Neither Clang
// nor GCC figure out this reordering on their own.
return len + !len;
auto c = static_cast<unsigned char>(*begin);
return static_cast<int>((0x3a55000000000000ull >> (2 * (c >> 3))) & 0x3) + 1;
}

// Return the result via the out param to workaround gcc bug 77539.
Expand Down
3 changes: 2 additions & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,8 @@ FMT_CONSTEXPR inline auto utf8_decode(const char* s, uint32_t* c, int* e)
constexpr const int shiftc[] = {0, 18, 12, 6, 0};
constexpr const int shifte[] = {0, 6, 4, 2, 0};

int len = code_point_length_impl(*s);
int len = "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0\0\0\2\2\2\2\3\3\4"
[static_cast<unsigned char>(*s) >> 3];
// Compute the pointer to the next character early so that the next
// iteration can start working on the next character. Neither Clang
// nor GCC figure out this reordering on their own.
Expand Down

0 comments on commit d9bc5f1

Please sign in to comment.