Skip to content

Commit

Permalink
feat(span.h): span_memcpy is a safer memcpy when you know the span bo…
Browse files Browse the repository at this point in the history
…undaries (#4597)

Signed-off-by: Larry Gritz <lg@larrygritz.com>
  • Loading branch information
lgritz authored Jan 26, 2025
1 parent d47da7b commit 8de907e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/iff.imageio/iffoutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,7 @@ IffOutput::compress_verbatim(const uint8_t*& in, uint8_t*& out, int size,
// copy
OIIO_DASSERT(out >= out_span.begin() && out < out_span.end());
*out++ = count - 1;
OIIO_DASSERT(out >= out_span.begin() && out + count <= out_span.end());
memcpy(out, in, count);
span_memcpy(out, in, size_t(count), out_span, in_span);

out += count;
in += count;
Expand Down
16 changes: 16 additions & 0 deletions src/include/OpenImageIO/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,22 @@ spancpy(span<T> dst, size_t dstoffset, cspan<T> src, size_t srcoffset = 0,



/// Perform a safe `memcpy(dst, src, n*sizeof(T))` but ensuring that the
/// memory accesses stay within the boundaries of spans `dst_span` and
/// `src_span`.
///
/// This is intended to be used as a memory-safe replacement for memcpy if
/// you know the spans representing safe areas.
template<typename T>
inline size_t
span_memcpy(T* dst, const T* src, size_t n, span<T> dst_span, cspan<T> src_span)
{
return spancpy(dst_span, dst - dst_span.begin(), src_span,
src - src_span.begin(), n);
}



/// Try to write `n` copies of `val` into `dst[offset...]`. Don't write
/// outside the span boundaries. Return the number of items actually written,
/// which should be `n` if the operation was fully successful, but may be less
Expand Down

0 comments on commit 8de907e

Please sign in to comment.