From ec17029ab985765c1b23fab673a29cb558c4f151 Mon Sep 17 00:00:00 2001 From: Minh Vu Date: Fri, 12 Jun 2026 19:15:47 +0200 Subject: [PATCH] Validate copy offsets before pointer math Signed-off-by: Minh Vu --- warp/_src/context.py | 6 ++++++ warp/tests/test_copy.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/warp/_src/context.py b/warp/_src/context.py index a2ce99aa6e..d11c6b3563 100644 --- a/warp/_src/context.py +++ b/warp/_src/context.py @@ -11156,6 +11156,12 @@ def copy( if not warp._src.types.is_array(src) or not warp._src.types.is_array(dest): raise RuntimeError("Copy source and destination must be arrays") + if src_offset < 0: + raise RuntimeError(f"Source offset must be non-negative, got {src_offset}") + + if dest_offset < 0: + raise RuntimeError(f"Destination offset must be non-negative, got {dest_offset}") + # backwards compatibility, if count is zero then copy entire src array if count <= 0: count = src.size diff --git a/warp/tests/test_copy.py b/warp/tests/test_copy.py index f173b8fffe..8beca5a58e 100644 --- a/warp/tests/test_copy.py +++ b/warp/tests/test_copy.py @@ -264,6 +264,17 @@ def test_copy_adjoint(test, device): assert_np_equal(state_in.grad.numpy(), np.array([1.0, 1.0, 1.0]).astype(np.float32)) +def test_copy_negative_offsets(test, device): + src = wp.array([1, 2, 3, 4], dtype=wp.int32, device=device) + dest = wp.zeros_like(src) + + with test.assertRaisesRegex(RuntimeError, "Source offset must be non-negative"): + wp.copy(dest, src, src_offset=-1) + + with test.assertRaisesRegex(RuntimeError, "Destination offset must be non-negative"): + wp.copy(dest, src, dest_offset=-1) + + devices = get_test_devices() @@ -301,6 +312,7 @@ class TestCopy(unittest.TestCase): ) add_function_test(TestCopy, "test_copy_adjoint", test_copy_adjoint, devices=devices) +add_function_test(TestCopy, "test_copy_negative_offsets", test_copy_negative_offsets, devices=devices) if __name__ == "__main__": unittest.main(verbosity=2)