Skip to content

ByteBuffer uses UInt for Capacity, but Int internally #499

@helje5

Description

@helje5

Separated out of #486, ByteBuffer-core.swift has this:

private static func allocateAndPrepareRawMemory(bytes: Capacity, allocator: Allocator) -> UnsafeMutableRawPointer {
  let bytes = Int(bytes) /* Note(hh): this can fail on 32-bit (Int32 vs UInt32 Capacity)) */

where Capacity is

typealias Capacity = UInt32

This can fail on 32-bit platforms where Int cannot hold any UInt32 value (e.g. 0xDEADBEEF). Apparently the Int is used because of:

let sysMalloc: @convention(c) (Int) -> UnsafeMutableRawPointer? = malloc

TBD: What should be used here. Maybe size_t? That would be better, but theoretically it would still break ByteBuffer on platforms wheresize_t is less than UInt32, e.g. Int32 sounds at least possible for size_t.
Presumably Int is also often used in Swift itself (e.g. the capacity of a UnsafeBufferPointer is probably an Int?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions