Skip to content

关于mqtt_subscribe的topic_filter指针问题 #104

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

Closed
youcanplay opened this issue Jul 5, 2024 · 3 comments
Closed

关于mqtt_subscribe的topic_filter指针问题 #104

youcanplay opened this issue Jul 5, 2024 · 3 comments

Comments

@youcanplay
Copy link

在阅读源码时,发现mqtt_subscribe()函数里的msg_handler->topic_filter指针指向的是形参的地址,而没有保存完整的字符串内容。
如果topic_filter传入的是一个局部变量指向的缓冲区,调用mqtt_subscribe是不是会有问题?

int mqtt_subscribe(mqtt_client_t* c, const char* topic_filter, mqtt_qos_t qos, message_handler_t handler)
{
//.......
    /* create a message and record it */
    msg_handler = mqtt_msg_handler_create(topic_filter, qos, handler);
    if (NULL == msg_handler) {
        rc = MQTT_MEM_NOT_ENOUGH_ERROR;
        goto exit;
    }
//.......

}


static message_handlers_t *mqtt_msg_handler_create(const char* topic_filter, mqtt_qos_t qos, message_handler_t handler)
{
    message_handlers_t *msg_handler = NULL;

    msg_handler = (message_handlers_t *) platform_memory_alloc(sizeof(message_handlers_t));
    if (NULL == msg_handler)
        return NULL;
    
    mqtt_list_init(&msg_handler->list);
    
    msg_handler->qos = qos;
    msg_handler->handler = handler;     /* register  callback handler */
    msg_handler->topic_filter = topic_filter;    //这里仅保存了传入的指针,而没有保存完整的字符串内容。

    return msg_handler;
}
@ajaybhargav
Copy link

是的,这是个问题。当我将mqtt主题名称声明为局部变量时遇到了问题。我认为在运行时需要将库修改为malloc MQTT主题名。

@ajaybhargav
Copy link

此问题已在pull请求 #97 中修复。花时间审查更改并提供反馈。

@youcanplay
Copy link
Author

好的,已审查。相关提交 12f787f

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

No branches or pull requests

2 participants