Wrap Char in array to avoid pointer arithmetic (#3695)

This resolves the following finding reported by Coverity Static Analysis
v2023.6.1 on line 1964 of fmt/include/fmt/format.h:

  ptr_arith: Using &v as an array. This might corrupt or misinterpret
             adjacent memory locations.
This commit is contained in:
Carl Smedstad 2023-10-31 22:05:46 +01:00 committed by GitHub
parent 19276d7325
commit e0d3e346d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1958,11 +1958,12 @@ auto write_escaped_string(OutputIt out, basic_string_view<Char> str)
template <typename Char, typename OutputIt>
auto write_escaped_char(OutputIt out, Char v) -> OutputIt {
Char v_array[1] = {v};
*out++ = static_cast<Char>('\'');
if ((needs_escape(static_cast<uint32_t>(v)) && v != static_cast<Char>('"')) ||
v == static_cast<Char>('\'')) {
out = write_escaped_cp(
out, find_escape_result<Char>{&v, &v + 1, static_cast<uint32_t>(v)});
out, find_escape_result<Char>{v_array, v_array + 1, static_cast<uint32_t>(v)});
} else {
*out++ = v;
}