Skip to content
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-14496] Foundation.Data: base64EncodedString(options:) behaves differently between DarwinFoundation and SwiftFoundation. #3227

Closed
YOCKOW opened this issue Apr 16, 2021 · 3 comments

Comments

@YOCKOW
Copy link
Collaborator

YOCKOW commented Apr 16, 2021

Previous ID SR-14496
Radar None
Original Reporter @YOCKOW
Type Bug
Status Closed
Resolution Done
Environment

Darwin: macOS Big Sur 11.2.3
Linux: Ubuntu 20.04.2 LTS

Swift 5.3.2

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug, Linux
Assignee ogres (JIRA)
Priority Medium

md5: 213db9ebfe98e949c18c77e5d233fe12

Issue Description:

Execute the following Swift code.
The results with SwiftFoundation are different from ones with DarwinFoundation, as you can see the comments in it.

import Foundation

func oneLineBase64EncodedString(_ originalDataCount: Int) -> String {
  let data = Data(repeating: 0x00, count: originalDataCount)
  return data.base64EncodedString(options: .lineLength76Characters)
}

let encoded57bytes = oneLineBase64EncodedString(57)
print(encoded57bytes)
// On Darwin,
//   Prints "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
// On non-Darwin,
//   Prints "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\n"
print(encoded57bytes.last!.isNewline)
// On Darwin,
//   Prints "false"
// On non-Darwin,
//   Prints "true"

let encoded55bytes = oneLineBase64EncodedString(55)
print(encoded55bytes)
// On Darwin,
//   Prints "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="
// On non-Darwin,
//   Prints "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==\r\n"
print(encoded55bytes.last!.isNewline)
// On Darwin,
//   Prints "false"
// On non-Darwin,
//   Prints "true"

let encoded2bytes = oneLineBase64EncodedString(2)
print(encoded2bytes)
// -> Prints "AAA=" on any OS.
print(encoded2bytes.last!.isNewline)
// -> Prints "false" on any OS.
@YOCKOW
Copy link
Collaborator Author

YOCKOW commented Apr 16, 2021

It may be caused here:

buffer.baseAddress!.advanced(by: outputIndex).copyMemory(from: &outputBytes, byteCount: 4)
outputIndex += 4
if lineLength != 0 {
// Add required EOL markers.
currentLineCount += 4
assert(currentLineCount <= lineLength)
if currentLineCount == lineLength {
buffer[outputIndex] = separatorByte1
outputIndex += 1
if let byte2 = separatorByte2 {
buffer[outputIndex] = byte2
outputIndex += 1
}
currentLineCount = 0
}
}
bytesLeft = dataBuffer.count - inputIndex

@swift-ci
Copy link
Contributor

Comment by Sergo Beruashvili (JIRA)

Fixed within #2997

@YOCKOW
Copy link
Collaborator Author

YOCKOW commented Jun 11, 2021

ogres (JIRA User)
Thank you for fixing.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants