From 454c8fe7bf33b401b88fd8d65286f32b97aee132 Mon Sep 17 00:00:00 2001 From: Angel Ezquerra Date: Thu, 21 Mar 2024 11:36:13 +0100 Subject: [PATCH] Fix issue #637 (crash when using `append` on an empty tensor) (#638) --- src/arraymancer/tensor/shapeshifting.nim | 3 ++- tests/tensor/test_shapeshifting.nim | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/arraymancer/tensor/shapeshifting.nim b/src/arraymancer/tensor/shapeshifting.nim index ea476370..b9c7978b 100644 --- a/src/arraymancer/tensor/shapeshifting.nim +++ b/src/arraymancer/tensor/shapeshifting.nim @@ -352,7 +352,8 @@ proc append*[T](t: Tensor[T], values: varargs[T]): Tensor[T] {.noinit.} = $t.rank & " (use `concat` for higher rank tensors)" let result_size = t.size + values.len result = newTensorUninit[T](result_size) - result[0 ..< t.size] = t + if t.size > 0: + result[0 ..< t.size] = t result[t.size ..< result.size] = values func squeeze*(t: AnyTensor): AnyTensor {.noinit.}= diff --git a/tests/tensor/test_shapeshifting.nim b/tests/tensor/test_shapeshifting.nim index 85c6b94f..1d7d1303 100644 --- a/tests/tensor/test_shapeshifting.nim +++ b/tests/tensor/test_shapeshifting.nim @@ -113,6 +113,10 @@ proc main() = check: a.append(b) == expected check: a.append(b.toTensor()) == expected + # Test fix for issue #637 (https://github.com/mratsim/Arraymancer/issues/637) + let c = newTensor[int](0) + check: c.append(1) == [1].toTensor + test "Squeeze": block: let a = toSeq(1..12).toTensor().reshape(3,1,2,1,1,2)