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

Adafruit_MQTT (API): the payload of publish method is read-only #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

flavio-fernandes
Copy link
Contributor

@flavio-fernandes flavio-fernandes commented Mar 4, 2018

The payload param provided in publish is a const. This change
fixes the API to ensure caller that this is the case for all
variations of the publish method.

This is a proposed fix to issue #115

This change simply adds const to the proper data parameter, to
ensure that the payload provided is not modified by the MQTT
implementation. At the core of this codepath, the method
memmove() is invoked and that is very okay since the src parameter is a const as well.

There are no limitations regarding this change. If caller passes in a
non-const payload, the compiler will gladly handle it as a const anyway.
So, this is 100% backwards compatible.

To test this change, simply call the publish method providing a
const pointer. Example:

...
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD);
Adafruit_MQTT_Publish service_pub = Adafruit_MQTT_Publish(&mqtt, "/topic/foo");

const uint8_t foo_payload = 0;
service_pub.publish(&foo_payload, 1 /*bLen*/));
...

@flavio-fernandes
Copy link
Contributor Author

@Fapiko @brentru can you review this too? It is kinda related to #169

Avoid memory corruption from happening when data payload provided
in Adafruit_MQTT::publishPacket is greater than MAXBUFFERSIZE.

In order to do that, a helper function is being added to calculate
how much space is available for the payload after subtracting what
is used as the header.

Pull request adafruit#166
Fixes adafruit#109
Fixes adafruit#122

Signed-off-by: Flavio Fernandes <[email protected]>
@flavio-fernandes flavio-fernandes force-pushed the publish_use_const.merge branch 2 times, most recently from 022ad0f to 28e0cf5 Compare August 29, 2020 20:57
The payload param provided in publish is a const. This change
fixes the API to ensure caller that this is the case for all
variations of the publish method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant