swift-foundation icon indicating copy to clipboard operation
swift-foundation copied to clipboard

`base64EncodedData` appends unnecessary `NULL` bytes in certain rare cases. (6.2)

Open YOCKOW opened this issue 3 months ago • 1 comments

Describe the bug

Data's base64EncodedData appends NULL bytes to the last of its output when the following conditions are met:

  • Swift 6.2
  • OS is Ubuntu
  • The length of Data is 1993...1995.

To Reproduce

Execute the code below with Swift 6.2:

import Foundation

for length in 1992..<1997 {
  let encoded = Data(
    repeating: 0xFF, count: length
  ).base64EncodedData(
    options: .lineLength76Characters
  )
  print("Length: \(length) | Last byte is NULL?: \(encoded.last == 0x00 ? "YES" : "NO")")
}

// On Darwin:
//
// Length: 1992 | Last byte is NULL?: NO
// Length: 1993 | Last byte is NULL?: NO
// Length: 1994 | Last byte is NULL?: NO
// Length: 1995 | Last byte is NULL?: NO
// Length: 1996 | Last byte is NULL?: NO

// On Ubuntu:
// Length: 1992 | Last byte is NULL?: NO
// Length: 1993 | Last byte is NULL?: YES
// Length: 1994 | Last byte is NULL?: YES
// Length: 1995 | Last byte is NULL?: YES
// Length: 1996 | Last byte is NULL?: NO

Expected behavior

No unnecessary bytes are added to Base64 encoded data.

Configuration (please complete the following information):

  • Swift Version: 6.2
  • OS: Ubuntu 24.04

Additional context

This bug doesn't exist in Swift 6.3-dev. However, let me file this here because Swift 6.2.x is current "RELEASE" version.

YOCKOW avatar Oct 05 '25 07:10 YOCKOW

Was this fixed in https://github.com/swiftlang/swift-foundation/pull/1425? @fabianfett

parkera avatar Oct 14 '25 17:10 parkera