Skip to content

Commit 9a4f52d

Browse files
committed
fix: StoreSlice must copy bits and refs
1 parent f6c3d3b commit 9a4f52d

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

TonLibDotNet.Tests/Cells/CellBuilderTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,5 +226,24 @@ public void WritesStringChunkedOk()
226226

227227
slice.EndRead();
228228
}
229+
230+
[Fact]
231+
public void StoreSliceStoresBitsAndRefs()
232+
{
233+
var original = new CellBuilder()
234+
.StoreUInt(11, 32)
235+
.StoreUInt(12, 32)
236+
.StoreRef(new CellBuilder().StoreBit(true))
237+
.StoreRef(new CellBuilder().StoreBit(false))
238+
.Build()
239+
.BeginRead();
240+
241+
Assert.Equal((uint)11, original.LoadUInt(32));
242+
Assert.True(original.LoadRef().BeginRead().LoadBit());
243+
244+
var slice = new CellBuilder().StoreSlice(original).Build().BeginRead();
245+
Assert.Equal((uint)12, slice.LoadUInt(32));
246+
Assert.False(slice.LoadRef().BeginRead().LoadBit());
247+
}
229248
}
230249
}

TonLibDotNet/Cells/CellBuilder.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,18 @@ public CellBuilder StoreBits(ReadOnlySpan<bool> values)
5858

5959
public CellBuilder StoreSlice(Slice value)
6060
{
61-
EnsureCanStore(value.Length);
62-
value.LoadBitsTo(data.AsSpan(Length, value.Length));
63-
Length += value.Length;
61+
var len = value.Length;
62+
EnsureCanStore(len);
63+
value.LoadBitsTo(data.AsSpan(Length, len));
64+
Length += len;
65+
66+
var rf = value.TryLoadRef();
67+
while (rf != null)
68+
{
69+
StoreRef(rf);
70+
rf = value.TryLoadRef();
71+
}
72+
6473
return this;
6574
}
6675

0 commit comments

Comments
 (0)