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
[SR-4435] Memory corruption when using NSData.base64EncodedString #4536
Comments
Comment by Shmuel Kallner (JIRA) It seems that adding a byte of zero to the bytes in the NSData causes the memory corruption to go away. Doing so makes the number of bytes in the NSData a multiple of three. To me this sounds like an error calculating the size of the output buffer. |
This looks like an error in the test case: import Foundation Can you try this instead? import Foundation |
Comment by Quan (JIRA) I tried running both versions of the test on Mac and Linux Swift 3.1 and got the following results: Original test: Suggested fix: |
On second thought, NSData probably would have copied the bytes as soon as it was initialized, so maybe the original bytes array going away shouldn't be a problem. |
Comment by Ian Partridge (JIRA) This testcase now passes on Linux with the latest master branch of swift-corelibs-foundation. I am trying to check whether the 3.1 branch is fixed too, but the last 3.1 snapshot was March 27th, so it means building a toolchain from scratch. I'm trying to figure out how the CI builds the 3.1 branch. |
Comment by Ian Partridge (JIRA) I've verified that this bug is also fixed in the 3.1 branch of swift-corelibs-foundation. The testcase gives the correct output (same as macOS) and valgrind reports no errors. |
Comment by Ian Partridge (JIRA) We have also verified that the Data fix in the 3.1 branch fixes the issues we've had with Kitura WebSockets and Swift 3.1. I think this one is fixed! |
Comment by Quan (JIRA) 🙂 |
Closing this as done since we think it's resolved with the other Data fix on swift-3.1. |
Comment by Bridger Maxwell (JIRA) Is there a place to download a 3.1 snapshot with this fix? I was hoping to download from https://swift.org/download but I guess the 3.1 branch is no longer being built. Can the build server be nudged to upload another 3.1 build? |
Comment by Ian Partridge (JIRA) Swift 3.1.1 contains this fix. But I agree that it would be nice to run the 3.1 CI more frequently. |
Additional Detail from JIRA
md5: 890348ccbfdc98261dc31a7e76ba8816
Issue Description:
When one uses NSData.base64EncodedString to convert the contents of an NSData to a base 64 encoded String, memory gets corrupted. The same thing happens if one uses Data.base64EncodedString, as under the covers it invokes NSData.base64EncodedString. This memory corruption can lead to SIGSEGV's and malloc/free related problems.
The following simple program demonstrates the issue:
Compiling it simply with swiftc and running valgrind against it gets the following output:
The text was updated successfully, but these errors were encountered: