Skip to content

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

@YOCKOW

Description

@YOCKOW

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions