Skip to content

Fix off-by-one when initializing Data with discontiguous, underestimated Sequences #23244

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
Show file tree
Hide file tree
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
10 changes: 4 additions & 6 deletions stdlib/public/Darwin/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2084,9 +2084,8 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
// ... and append the rest byte-wise, buffering through an InlineData.
var buffer = InlineData()
while let element = iter.next() {
if buffer.count < buffer.capacity {
buffer.append(byte: element)
} else {
buffer.append(byte: element)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To confirm my understanding:

  • buffer has a fixed capacity
  • if it's at capacity after appending one element, you append the buffer to _representation and reset the buffer
  • the bug was, you were detecting one less-than capacity and not appending under those circumstances, so that element was dropped
  • you're sure it's not possible to ever append when already at capacity

Right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct on all accounts. The capacity is fixed to what we can stack-allocate and once we hit that capacity, we append what we had on the stack. The bug is that we threw away element previously in the case where we appended buffer; we now append it unconditionally with the knowledge that there is always space in the buffer before doing so.

if buffer.count == buffer.capacity {
buffer.withUnsafeBytes { _representation.append(contentsOf: $0) }
buffer.count = 0
}
Expand Down Expand Up @@ -2378,9 +2377,8 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
// ... and append the rest byte-wise, buffering through an InlineData.
var buffer = InlineData()
while let element = iter.next() {
if buffer.count < buffer.capacity {
buffer.append(byte: element)
} else {
buffer.append(byte: element)
if buffer.count == buffer.capacity {
buffer.withUnsafeBytes { _representation.append(contentsOf: $0) }
buffer.count = 0
}
Expand Down
14 changes: 10 additions & 4 deletions test/stdlib/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1260,10 +1260,13 @@ class TestData : TestDataSuper {

// If we append a sequence of elements larger than a single InlineData, the internal append here should buffer.
// We want to make sure that buffering in this way does not accidentally drop trailing elements on the floor.
d.append(contentsOf: 0x03...0x17)
d.append(contentsOf: 0x03...0x2F)
expectEqual(Data([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17]), d)
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F]), d)
}

// This test is like test_appendingNonContiguousSequence_exactCount but uses a sequence which reports 0 for its `.underestimatedCount`.
Expand All @@ -1282,10 +1285,13 @@ class TestData : TestDataSuper {

// If we append a sequence of elements larger than a single InlineData, the internal append here should buffer.
// We want to make sure that buffering in this way does not accidentally drop trailing elements on the floor.
d.append(contentsOf: (0x03...0x17).makeIterator()) // `.makeIterator()` produces a sequence whose `.underestimatedCount` is 0.
d.append(contentsOf: (0x03...0x2F).makeIterator()) // `.makeIterator()` produces a sequence whose `.underestimatedCount` is 0.
expectEqual(Data([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17]), d)
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F]), d)
}

func test_sequenceInitializers() {
Expand Down