Re: [PATCH 2/4] gdbsupport: add array_view copy function

Simon Marchi via Gdb-patches
2021-11-16T21:56:36Z

On 2021-11-16 3:53 p.m., Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:
>
> Simon> An assertion was recently added to array_view::operator[] to ensure we
> Simon> don't do out of bounds accesses. However, when the array_view is copied
> Simon> to or from using memcpy, it bypasses this safety.
>
> Simon> To address this, add a `copy` free function that copies data from an
> Simon> array view to another. It ensures that the destination and source array
> Simon> views have the same size and element size, which prevents any kind of
> Simon> overflow in the source and in the destination.
>
> In C++20 I think we could use std::copy by having the array view
> iterators implement the contiguous_iterator concept. So, maybe a
> comment to this effect would be good.

Do you mean we could use std::copy to implement this copy function,
instead of using memcpy, and that would deal with the details of copying
in the most efficient manner?

If I read cppreference correctly [1], std::copy is available before
C++20, it's just that in C++20 it has been made constexpr. So I could
maybe give it a try, instead of using memcpy.

[1] https://en.cppreference.com/w/cpp/algorithm/copy

> Though maybe in C++20 we would want to switch from array_view to
> std::span.

Ack.

Simon