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

MQTTProperties.subscriptionIdentifier accepts UInt, but gets serialised as Int #132

Open
jlsiewert opened this issue Jan 20, 2023 · 1 comment
Labels
breaking change Change that requires a new major version

Comments

@jlsiewert
Copy link

jlsiewert commented Jan 20, 2023

Describe the bug

When subscribing to a value, I tried to generate a random MQTTProperties.subscriptionIdentifier using the line

let subscriptionID = UInt(in: 0..<UInt.max)
let subscriptionProperties: MQTTProperties = [.subscriptionIdentifier(subscriptionID)]

However, this resulted in a runtime crash.

The subscription identifier is defined as MQTTProperties.subscriptionIdentifier(UInt) with an associated value of UInt.

However, when the header gets serialised, it gets written as an variableLengthInteger, where it gets converted to an Int. When setting MQTTProperties.subscriptionIdentifier to a high enough value, this might crash with the fatal error Not enough bits to represent the passed value.

To Reproduce
Subscribe to a topic with an MQTTProperties.subscriptionIdentifier with an associated value Int.max<value<UInt.max, which compiles fine but result in a runtime crash because MQTTSerializer.variableLengthIntegerPacketSize wraps the value in an Int.

Expected behavior
MQTTProperties.subscriptionIdentifier should either be declared as an Int or using an UInt should work without a crash.

Context (please complete the following information):

  • Platform macOS
  • Swift/Xcode Version: 5.7.2 / 14.2.0
  • Version 2.7.1

Additional context

The specification only defines the Subscription Identifier as Variable Byte Integer, so the associated value should probably be changed to an Int to prevent the crash.

@adam-fowler
Copy link
Collaborator

The subscription id is a value between 1 and 269,435,455. Which is 28 bits. See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901117

I'm not sure why I used a UInt when I should probably have used a UInt32 as I have elsewhere. I can add validation code in for the subscription id to ensure clients aren't sending an id that is too large.

@adam-fowler adam-fowler added the breaking change Change that requires a new major version label Apr 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Change that requires a new major version
Projects
None yet
Development

No branches or pull requests

2 participants