Skip to content

[SR-6361] Fix Data.withUnsafeMutableBytes() for slices with length < range.lowerBound #1314

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
merged 1 commit into from
Nov 14, 2017
Merged

[SR-6361] Fix Data.withUnsafeMutableBytes() for slices with length < range.lowerBound #1314

merged 1 commit into from
Nov 14, 2017

Conversation

sashabelonogov
Copy link
Contributor

@sashabelonogov sashabelonogov commented Nov 11, 2017

Solves SR-6361
This code snippet:

var data = Data(bytes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])[4..<6]
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) in
    ptr.advanced(by: 1).pointee = 0xFF
}

crashes with:
fatal error: UnsafeMutableRawBufferPointer with negative count

because count was not calculated correctly (_offset was not taken into account)

The existing tests were not covering this case because it works correctly for data slices when _length > range.lowerBound.

I created new tests for this exact situation but I can also update the existing ones by adding one more check to them.

Please, let me know what you think.

@spevans
Copy link
Contributor

spevans commented Nov 11, 2017

@swift-ci please test

@pingchen114
Copy link

Just curious, maybe Data.withUnsafeBytes() followed the same logic and should be fixed too?

("test_validateMutation_slice_mutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound", test_validateMutation_slice_mutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound),
("test_validateMutation_slice_customBacking_withUnsafeMutableBytes_lengthLessThanLowerBound", test_validateMutation_slice_customBacking_withUnsafeMutableBytes_lengthLessThanLowerBound),
("test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound",
test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound)
Copy link
Contributor

Choose a reason for hiding this comment

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

If you finish this line with a comma (like in like 511) then others won't need to change line 517 when they add more tests in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @alblue, I updated the commit

@alblue
Copy link
Contributor

alblue commented Nov 13, 2017

@phausler the tests pass on this; do you think the change is reasonable?

@sashabelonogov
Copy link
Contributor Author

@my0114 thanks for your feedback! You might be right, I will check it in a couple of hours to make sure.

@spevans
Copy link
Contributor

spevans commented Nov 13, 2017

@swift-ci please test

1 similar comment
@spevans
Copy link
Contributor

spevans commented Nov 13, 2017

@swift-ci please test

Copy link
Contributor

@phausler phausler left a comment

Choose a reason for hiding this comment

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

Good catch, can you please put up this as a PR on the overlay as well?

@sashabelonogov
Copy link
Contributor Author

thanks @phausler, sure, this is the PR: swiftlang/swift#12898

@sashabelonogov
Copy link
Contributor Author

@phausler I'm sorry for changing this PR after review but I tried to investigate the issue further and realized that simple count: Swift.min(range.count, _length))) is simpler and will do the job, so I updated both PRs.
I hope I understood everything correctly and it makes sense. Thanks.

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