Skip to content

Assert that Copy's source and destination do not overlap #23594

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

Merged
merged 3 commits into from
Aug 19, 2025
Merged
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
34 changes: 28 additions & 6 deletions handy.h
Original file line number Diff line number Diff line change
Expand Up @@ -2850,14 +2850,36 @@ enum mem_log_type {
#define perl_assert_ptr(p) assert( ((void*)(p)) != 0 )


#define Move(s,d,n,t) (MEM_WRAP_CHECK_(n,t) perl_assert_ptr(d), perl_assert_ptr(s), (void)memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
#define Copy(s,d,n,t) (MEM_WRAP_CHECK_(n,t) perl_assert_ptr(d), perl_assert_ptr(s), (void)memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
#define Zero(d,n,t) (MEM_WRAP_CHECK_(n,t) perl_assert_ptr(d), (void)memzero((char*)(d), (n) * sizeof(t)))
#define Move(s,d,n,t) ((void)MoveD(s, d, n, t))
#define Copy(s,d,n,t) ((void)CopyD(s, d, n, t))
#define Zero(d,n,t) ((void)ZeroD(d, n, t))

/* Like above, but returns a pointer to 'd' */
#define MoveD(s,d,n,t) (MEM_WRAP_CHECK_(n,t) perl_assert_ptr(d), perl_assert_ptr(s), memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
#define CopyD(s,d,n,t) (MEM_WRAP_CHECK_(n,t) perl_assert_ptr(d), perl_assert_ptr(s), memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
#define ZeroD(d,n,t) (MEM_WRAP_CHECK_(n,t) perl_assert_ptr(d), memzero((char*)(d), (n) * sizeof(t)))
#define MoveD(s,d,n,t) \
( \
MEM_WRAP_CHECK_(n,t) \
perl_assert_ptr(d), \
perl_assert_ptr(s), \
memmove((char*)(d),(const char*)(s), (n) * sizeof(t)) \
)
#define CopyD(s,d,n,t) \
( \
MEM_WRAP_CHECK_(n,t) \
perl_assert_ptr(d), \
perl_assert_ptr(s), \
assert_( \
PTR2UV((char *)(d) + (n) * sizeof (t)) <= PTR2UV((const char *)(s)) \
|| \
PTR2UV((const char *)(s) + (n) * sizeof (t)) <= PTR2UV((char *)(d)) \
) \
memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)) \
)
#define ZeroD(d,n,t) \
( \
MEM_WRAP_CHECK_(n,t) \
perl_assert_ptr(d), \
memzero((char*)(d), (n) * sizeof(t)) \
)

#define NewCopy(s,d,n,t) STMT_START { \
Newx(d,n,t); \
Expand Down
Loading