Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(span.h): span_memcpy is a safer memcpy when you know the span boundaries #4597

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading