# SwiftNIO truncates to 32 bits?

**URL:** <https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583>\
**Category:** SwiftNIO\
**Tags:** swift-nio\
**Created:** [July 31, 2024, 12:45am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583 "2024-07-31T00:45:05Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [July 31, 2024, 12:45am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/1 "2024-07-31T00:45:05Z")

</div>

I have a little app that uploads to S3 via [Soto](https://github.com/soto-project/soto). I just tried to upload a 5 GB file and it failed, because down inside `ByteBuffer`, it casts an `Int` capacity value to `_Capacity`, which is typealiased to `UInt32`. This seems pretty bad. A related type, `_Index`, is also `UInt32`.

S3 itself is limited to 5 GB for a single PUT operation, but objects can be 5TB in size.

I can switch to multipart upload for this, but this still seems like a problem in NIO, no?

---

<div class="post-metadata">

**Author:** ![beaumont](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@beaumont](https://forums.swift.org/u/beaumont)\
**Post date:** [July 31, 2024, 7:42am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/2 "2024-07-31T07:42:16Z")

</div>

Wouldn't this only be a limitation if you wanted to _buffer_ more than `UInt32.max` bytes _in memory_? It shouldn't, in principle, limit you from uploading files of arbitrary size.

I'm not familiar with Soto or the specific Soto APIs you're using but maybe the problem is in there, or how you are calling it?

EDIT: I think you need to use the streaming APIs: [Soto - Streaming Payloads](https://soto.codes/user-guides/streaming-payloads.html)

---

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [July 31, 2024, 7:46am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/3 "2024-07-31T07:46:20Z")

</div>

I think they're loading the whole file into RAM via `ByteBuffer`, and Soto/S3 recommends using multipart upload for files larger than 100 MB, so I found a workaround.

But ByteBuffer at best is inconsistent, taking `Int` in some places (or `UnsafeRawBufferPointer`, which has an `Int` count), while taking `_Capacity` in most others.

I'm just not sure why it uses `UInt32` instead of `UInt` or `Int`.

---

<div class="post-metadata">

**Author:** ![beaumont](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@beaumont](https://forums.swift.org/u/beaumont)\
**Post date:** [July 31, 2024, 7:49am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/4 "2024-07-31T07:49:30Z")

</div>

> [@JetForMe](#):
>
> Soto/S3 recommends using multipart upload for files larger than 100 MB, so I found a workaround.

I think the link I posted above actually suggests using streaming APIs, without the need to move to multipart.

> Streaming Payloads
> 
> When uploading or downloading large blocks of data it is preferable if you can stream this data instead of holding it all in memory. Soto supplies methods for streaming raw data payloads of both requests to AWS and responses from AWS. The most common use of this would be when uploading or downloading large objects to S3.  
> — source: [Soto - Streaming Payloads](https://soto.codes/user-guides/streaming-payloads.html)

> [@JetForMe](#):
>
> I'm just not sure why it uses `UInt32` instead of `UInt` or `Int`.

I can't speak to the history of the type, but I don't think it's the limiting factor here.

---

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [July 31, 2024, 7:53am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/5 "2024-07-31T07:53:06Z")

</div>

> [@beaumont](#):
>
> I can't speak to the history of the type, but I don't think it's the limiting factor here.

Ignore my specific use case. I'm suggesting it's an inconsistency in ByteBuffer. There may well be times when one needs to allocate one larger than 31 bits, and as-is it crashes if you go over 2GB.

---

<div class="post-metadata">

**Author:** ![johannesweiss](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johannesweiss/32/38183_2.png) [@johannesweiss](https://forums.swift.org/u/johannesweiss)\
**Post date:** [July 31, 2024, 8:22am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/6 "2024-07-31T08:22:54Z")

</div>

ByteBuffer should be limited to 4GB, ie. 32 bits on all platforms, regardless of whether you use 32 or 64 bit machines. SwiftNIO is obviously still able to send and receive arbitrary amounts of data.

When uploading a file, you should be streaming it, otherwise you may suffer OOM kills anyway.

Any library that allows you to upload files should be doing this automatically. So if you're saying that Soto requires you to provide a single ByteBuffer holding all bytes, then I think you should file a Soto bug requesting streaming uploads.

If you need to hold more than 4GB in memory at once, then ByteBuffer is not the type you want. And in general if you're holding buffers if such enormous size you need to be a bit careful: any CoW copy would be disastrous (so maybe you want a specialised, immutable type without CoW?), it won't work on all platforms. It's also probably an orange flag for the design of a software if it requires single arbitrarily-sized memory buffers.

ByteBuffer also doesn't expose `_Capacity`, all APIs should be `Int`s (which is an issue on 32 bit platforms as they can't represent 4GB...)

---

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [July 31, 2024, 8:25am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/7 "2024-07-31T08:25:00Z")

</div>

Right, sorry, UInt32, but Int for the other types.

That ByteBuffer exposes Int, however, still strikes me as problematic, since it will crash if you exceed 32 bits.

---

<div class="post-metadata">

**Author:** ![johannesweiss](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johannesweiss/32/38183_2.png) [@johannesweiss](https://forums.swift.org/u/johannesweiss)\
**Post date:** [July 31, 2024, 8:27am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/8 "2024-07-31T08:27:29Z")

</div>

The "(almost) always use Int" mantra is inherited from Swift such that Swift users don't constantly need to convert between the various integer types.

It's the same with Array. If you pass `Array.reserveCapacity(-1)` then it'll also crash. And `Array.reserveCapacity(.max)` will (likely) crash too because you won't have the memory (and most allocators don't want to overcommit by that much).

* * *

FWIW, ByteBuffer is designed with network safety in mind. In general, you shouldn't just accept for example lengths from the network, but `buffer.readSlice(length: Int.max)` or `length: -1` or so will _not_ crash but instead yield a `nil`, just in case the user forgets to properly sanitise the values. All `read*` APIs share this.

The write side is different, NIO can't protect you there. If you accept arbitrary amounts of data and load them into memory, then your application is trivially attackable and there's not much that can be done automatically. (If you use `ByteToMessageDecoder` then that has built-in buffering limits that you can configure)

---

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [July 31, 2024, 8:30am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/9 "2024-07-31T08:30:29Z")

</div>

I too don't like that values that can never be negative are carried by signed integer variables, but I guess it simplifies somethings.

But `Array.reserveCapacity(2^33)` will likely succeed (on a real machine, anyway, if not an iOS device).

It’s one thing to crash because you've exhausted memory. It's another to crash because the underlying code assumes it will be called with values less than 2^32.

---

<div class="post-metadata">

**Author:** ![johannesweiss](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johannesweiss/32/38183_2.png) [@johannesweiss](https://forums.swift.org/u/johannesweiss)\
**Post date:** [July 31, 2024, 8:38am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/10 "2024-07-31T08:38:43Z")

</div>

I can understand your point of view but Swift made a different decision there, everything is `Int` based. SwiftNIO merely followed that.

The key reason is that constantly forcing users into converting integer types is very awkward and error-prone. Also subtracting unsigned integer types is extra dangerous because if they go under 0 it'll crash (underflow). But I think this isn't the right post to discuss Swift's API choices.

---

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [July 31, 2024, 8:40am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/11 "2024-07-31T08:40:42Z")

</div>

Again, I'm just questioning ByteBuffer's internal use of `UInt32` when its external API uses `Int` (which is almost guaranteed to be 64 bits).

---

<div class="post-metadata">

**Author:** ![johannesweiss](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johannesweiss/32/38183_2.png) [@johannesweiss](https://forums.swift.org/u/johannesweiss)\
**Post date:** [July 31, 2024, 9:06am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/12 "2024-07-31T09:06:00Z")

</div>

Oh I see, that is internal though (as in could be changed without API changes), right? The reason if uses 32 bits internally is to save bits.

It's quite important for a Swift type as prominent as ByteBuffer to fit inside an existential box (which is 3 words -- 24 bytes on 64bit which I'll now assume). ByteBuffer is (IIRC) 23 bytes wide which leaves a few spare bits such that it can be an enum payload and the composite would still fit into 3 words. Bute ByteBuffer holds a pointer (1 word), a reader index, a writer index, a slice begin and a slice end. Naively this would be 5 words (40 bytes) but ByteBuffer packs that in under 3 words. This can be a very significant performance win because it potentially saves an allocation each time ByteBuffer goes into an existential box.

But of course, it's a tradeoff. I think it's a good one. If we were to show that it's a bad tradeoff, this could be changed at any point in time without breaking the API at all.

And just to reiterate: if you have a program that relies on storing many gigabytes in a single allocation, then that's an orange, if not red flag. And even if you do need to store that much, it's quite easy to build a composite type that internally holds `[ByteBuffer]`, or you just use a specialised type for that (probably without CoW, likely `~Copyable`).

---

<div class="post-metadata">

**Author:** ![Honza\_Dvorsky](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/honza_dvorsky/32/29867_2.png) [@Honza\_Dvorsky](https://forums.swift.org/u/Honza_Dvorsky)\
**Post date:** [July 31, 2024, 9:14am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/13 "2024-07-31T09:14:14Z")

</div>

The reasons why the internals work this way make sense, I think part of the complaint here is that the limit was unexpected, undocumented, and resulted in a crash.

This should be documented by saying what the limit is today, and in the future it should never decrease (but can increase).

---

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [July 31, 2024, 9:17am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/14 "2024-07-31T09:17:50Z")

</div>

> [@Honza\_Dvorsky](#):
>
> The reasons why the internals work this way make sense, I think part of the complaint here is that the limit was unexpected, undocumented, and resulted in a crash.

Yes. I did look around for a comment explaining why it was limited to 32 bits, and didn't see one. Documenting that (and briefly explaining why, e.g. "for performance reasons," although a bit more might be nice) would help a lot.

And maybe some debug asserts to indicate what went wrong.

---

<div class="post-metadata">

**Author:** ![johannesweiss](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johannesweiss/32/38183_2.png) [@johannesweiss](https://forums.swift.org/u/johannesweiss)\
**Post date:** [July 31, 2024, 10:06am UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/15 "2024-07-31T10:06:03Z")

</div>

Yes, this should definitely be documented! Please send an issue or even better a PR

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [July 31, 2024, 3:51pm UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/16 "2024-07-31T15:51:18Z")

</div>

> [@johannesweiss](#):
>
> It's the same with Array. If you pass `Array.reserveCapacity(-1)` then it'll also crash.

Well, `array.reserveCapacity(-1)` doesn't crash, but it also doesn't do anything 😝

---

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [July 31, 2024, 10:00pm UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/17 "2024-07-31T22:00:09Z")

</div>

> [@johannesweiss](#):
>
> Please send an issue

It seems there’s one that closely relates, and implies documentation would be helpful: [ByteBuffer uses `UInt` for `Capacity`, but `Int` internally · Issue #499 · apple/swift-nio · GitHub](https://github.com/apple/swift-nio/issues/499)

If you like, I’ll write one more specific. I don’t trust myself to document it correctly, however, so I’ll refrain from writing a PR.

---

<div class="post-metadata">

**Author:** ![johannesweiss](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johannesweiss/32/38183_2.png) [@johannesweiss](https://forums.swift.org/u/johannesweiss)\
**Post date:** [August 1, 2024, 4:57pm UTC](https://forums.swift.org/t/swiftnio-truncates-to-32-bits/73583/18 "2024-08-01T16:57:31Z")

</div>

Yes, please send a request for documentation (or a PR adding docs) over.

The old bug you reference was different: If you had a 2GB `ByteBuffer` on 32-bit platforms and added a byte, then `ByteBuffer` would internally double its capacity to 4GB (which still fits into `UInt32` but not `Int` on 32bit platforms) and then cast that size to an `Int`. That would now crash in `ByteBuffer`-internal code. This has since been fixed.
