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

Conversation

itaiferber
Copy link
Contributor

What's in this pull request?
Resolves a serious off-by-one error which causes us to drop every 15th byte in a discontiguous, underestimated sequence past max(underestimatedCount, InlineData.bufferSize). (rdar://problem/48775516)

Our unit tests didn't previously catch this because the data they tested wasn't long enough.

@itaiferber itaiferber requested a review from phausler March 12, 2019 21:29
@itaiferber
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 7ce6f84

@airspeedswift
Copy link
Member

@swift-ci please clean test linux platform

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.

@tanner0101
Copy link
Contributor

tanner0101 commented Mar 12, 2019

Thanks a ton for this fix @itaiferber. I'm almost positive this is the issue causing test failures and crashes in http://github.com/vapor/vapor, https://github.com/vapor/url-encoded-form, and https://github.com/vapor/multipart. I would greatly appreciate if this fix made it into Swift 5.0.0 ❤️

Edit: Oh, and http://github.com/mordil/nio-redis. cc @Mordil

@itaiferber
Copy link
Contributor Author

@tanner0101 We're working on that right now. :) #23253

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 7ce6f84

@itaiferber
Copy link
Contributor Author

@swift-ci Please smoke test linux

@itaiferber
Copy link
Contributor Author

Build failures on Linux were unrelated (also, this version of Data is not used on Linux)

@phausler
Copy link
Contributor

We should also get this into SCLF too.

@itaiferber
Copy link
Contributor Author

@phausler Agreed — we have more leeway there so slightly less urgent, but still needs to be done ASAP

@itaiferber
Copy link
Contributor Author

Merging because smoke testing passed; full test is happening on #23253

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants