Skip to content

Commit

Permalink
Wrap Char in array to avoid pointer arithmetic (#3695)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
carlsmedstad authored Oct 31, 2023
1 parent 19276d7 commit e0d3e34
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit e0d3e34

Please sign in to comment.