Skip to content

Commit

Permalink
Merge pull request #1042 from fabulous-dev/fix-combinemut-error
Browse files Browse the repository at this point in the history
When combining 2 arrays, array B should be reused but array A was passed instead
  • Loading branch information
TimLariviere committed May 22, 2023
2 parents 47ce174 + a0bbb97 commit 1662bfd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

_No unreleased changes_
### Fixed
- Fix an issue in `MutStackArray1.combineMut` that could result in a crash by @TimLariviere (https://github.com/fabulous-dev/Fabulous/pull/1042)

## [2.3.0] - 2023-03-17

Expand Down
24 changes: 24 additions & 0 deletions src/Fabulous.Tests/ArrayTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Fabulous.Tests

open System
open Fabulous.StackAllocatedCollections
open NUnit.Framework
open Fabulous

[<TestFixture>]
type ``Array tests``() =
[<Test>]
member _.``MutStackArray1.combineMut reuses array B if can fit all data``() =
let arrB = Array.zeroCreate 7

let a = MutStackArray1.Many((2us, Array.zeroCreate 4))
let b = MutStackArray1.Many((5us, arrB))
let c = MutStackArray1.combineMut(&a, b)
let cOpt = MutStackArray1.toArraySlice &c
let struct (usedC, arrC) = cOpt.Value

// We should have the same number of used items
Assert.AreEqual(7us, usedC)

// Reference should be equal to arrB since the array was reused
Assert.True(Object.ReferenceEquals(arrC, arrB))
1 change: 1 addition & 0 deletions src/Fabulous.Tests/Fabulous.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Compile Include="Generators.fs" />
<Compile Include="AttributesTests.fs" />
<Compile Include="ViewTests.fs" />
<Compile Include="ArrayTests.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" />
Expand Down
2 changes: 1 addition & 1 deletion src/Fabulous/Array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ module StackAllocatedCollections =
let arr = ArraySlice.shiftByMut &sliceB (uint16 usedA)

Array.blit arrA 0 arr 0 usedA
Many(newSize, arrA)
Many(newSize, arr)
else
// None of them can fit the result
// thus allocate a new one
Expand Down

0 comments on commit 1662bfd

Please sign in to comment.