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

API aws_iot_mqtt_subscribe() don't support to define topic name in stack memory #164

Closed
chunhuajiang opened this issue Apr 19, 2018 · 3 comments

Comments

@chunhuajiang
Copy link

Hi,
The API aws_iot_mqtt_subscribe() is used to subscribe some topic, and its 2nd parameter is the topic name, but I found that if I define topic name in stack, like this

void test(AWS_IoT_Client *client)
{
    char topic_name[128];
    IoT_Error_t rc = FAILURE;

    memset(topic_name, 0, sizeof(topic_name));
    strcpy(topic_name, "$aws/things/MyIoTButton/shadow/update/delta");
    rc = aws_iot_mqtt_subscribe(client, topic_name, strlen(topic_len), xxx, xxx);
    /// rc == SUCCESS, but xxx_cb will not be called when the topic is published at most time
}

Then, even thought its return code indicates SUCCESS, there is a great possibility that xxx_cb will not be called when the topic is published.

But If I define topic name like this,

void test(AWS_IoT_Client *client)
{
    aws_iot_mqtt_subscribe(client, "$aws/things/MyIoTButton/shadow/update/delta", strlen("$aws/things/MyIoTButton/shadow/update/delta"), xxx, xxx);
}

The xxx_cb will always be called when the topic is published.

I think its not reasonable,

@huguesBouvier
Copy link
Contributor

huguesBouvier commented Apr 19, 2018

Hi,

You are correct, this is an issue. Thank you very much for reporting it!
The problem comes from here:
in aws_iot_mqtt_client_subscribe.c, in _aws_iot_mqtt_internal_subscribe:
pClient->clientData.messageHandlers[indexOfFreeMessageHandler].topicName =
pTopicName;
It is passing a pointer fro stack to the message handler. That is what is causing the issue.
Regards,

@chunhuajiang
Copy link
Author

chunhuajiang commented Apr 20, 2018

yes, that's the root cause

@gordonwang0
Copy link
Contributor

Hi @tidyjiang8,

We added a note in the documentation about stack parameters needing to remain in scope during an API call, #175. A true fix to this problem (copying parameters) isn't planned at this time. Closing issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants