-
Couldn't load subscription status.
- Fork 119
fix: support zero-length clientId #330
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
Conversation
|
https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html#_Toc442180844 |
This is true of the actual MQTT connect message itself. The code/library creating that message is free to populate this client identifier anyway it likes while still being compliant. In several spots in this library we've equated a |
|
The other way to think of it: What's the value in erroring when a NULL client ID is provided to the library if it would be invalid to use anyway? This just exposes directly the MQTT specification. Why not use a default like a zero-length identifier and try to connect (aka be useful)? |
|
I have combined the two erroneous commits into a single commit. |
| status = MQTTBadParameter; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also add this below check since on line connectPacketSize += pConnectInfo->clientIdentifierLength + sizeof( uint16_t ); we are updating the length based only on the length provided by the user without checking whether the value itself is NULL. That will cause an issue.
@Ryan-CW-Code what do you think? I am open to combining the checks too. But I am currently out of office and can help once I am back.
| status = MQTTBadParameter; | |
| } | |
| status = MQTTBadParameter; | |
| } | |
| else if( ( pConnectInfo->clientIdentifierLength == 0U ) ^ ( pConnectInfo->pClientIdentifier == NULL ) ) | |
| { | |
| LogError( ( "Client ID length and value mismatch." ) ); | |
| status = MQTTBadParameter; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we pulled this check before the check on 1709 we could reduce that check too since the XOR implies that if the length is 0 that the value must be NULL.
Not sure I'm sold on this simplification as the code will no longer so obviously equate NULL to 0 length. I like this suggestion.
|
I agree with what @kstribrnAmzn said. I was thinking of equating NULL with client ID being 0 length. I have one minor suggestion though as I was looking through the code. Let me know if that is apt @kstribrnAmzn and @Ryan-CW-Code. Thanks, |
|
The newly added check means that when |
Yes - and vice versa.
On the surface this is true. If you look deeper into how we've chosen to handle strings in this library then they actually are equal. Let me explain.... A C-string is an array of characters which looks as follow the final character is the null terminator character. This special character is how the end of the string is signalled. This is why you would get To avoid this problem (and some others that come with it) our library requies you to pass in a string along with a length. This way, if there is no null terminator, there is no problem. If I was to pass the same string above with a length of 12 I'd get back Now the trick is how do you represent an empty string? With a C-String it's easy, it's just the null termination character
Please merge in @AniruddhaKanhere's suggestion to your current work. At this point we can merge your CR. Detecting the mismatch as he mentioned is important. |
|
Sorry, this differs somewhat from my initial expectations. In my understanding, when clientIdentifierLength = 0, passing "" should work correctly. From my understanding:
Therefore, I cannot agree with treating NULL and "" as equivalent in certain scenarios. They are fundamentally different in semantics:
For these reasons, I will temporarily close this PR. Please continue refining the implementation according to your coding practices and project requirements. Thanks for your patience and understanding. |
|
The way you have your PR written, when clientIdentifierLength = 0, passing "" DOES work correctly. @AniruddhaKanhere's suggestion would break things but this could be change to the following. Doing this will result in the library treating a Client ID which is empty string OR NULL the same. Which is aligns with our goal. |
|
Good, I used the code you recommended, and it meets our requirements and the coreMqtt specifications. |
|
Sorry, this PR can no longer be reopened. Please refer to the new PR。 |

Description
Test Steps
Checklist:
Related Issue
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.