Skip to content

fix:fixed crash issues when the illegal topic is received #117

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

Merged
merged 5 commits into from
Aug 13, 2025

Conversation

maikebing
Copy link
Member

@maikebing maikebing requested a review from Copilot August 13, 2025 05:01
@maikebing maikebing merged commit efc44e6 into IoTSharp:master Aug 13, 2025
1 check failed
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes segmentation fault issues when receiving illegal MQTT topics by rewriting the topic matching logic and adding null pointer safety checks to list operations.

  • Complete rewrite of mqtt_topic_is_matched function with improved wildcard handling and input validation
  • Added null pointer checks to list deletion operations to prevent crashes
  • Removed unused variable assignment in MQTT deserialize function

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
mqttclient/mqttclient.c Rewrote topic matching algorithm and added safety checks to message handler destruction
mqtt/MQTTDeserializePublish.c Removed unused variable assignment in publish deserialization
common/mqtt_list.c Added null pointer validation and cleanup in list deletion function

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

mqtt_list_del(&msg_handler->list);
msg_handler->list.prev == NULL;
msg_handler->list.next == NULL;
Copy link
Preview

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line uses comparison operator '==' instead of assignment operator '='. Should be 'msg_handler->list.prev = NULL;'

Suggested change
msg_handler->list.next == NULL;
msg_handler->list.prev = NULL;
msg_handler->list.next = NULL;

Copilot uses AI. Check for mistakes.

mqtt_list_del(&msg_handler->list);
msg_handler->list.prev == NULL;
msg_handler->list.next == NULL;
Copy link
Preview

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line uses comparison operator '==' instead of assignment operator '='. Should be 'msg_handler->list.next = NULL;'

Suggested change
msg_handler->list.next == NULL;
msg_handler->list.prev = NULL;
msg_handler->list.next = NULL;

Copilot uses AI. Check for mistakes.

platform_memory_free(msg_handler);
msg_handler = NULL;
Copy link
Preview

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the local parameter 'msg_handler' to NULL has no effect outside this function since parameters are passed by value. This assignment is ineffective.

Suggested change
msg_handler = NULL;

Copilot uses AI. Check for mistakes.

nextpos = ++curn + 1;
const char* filter = topic_filter;
const char* topic = topic_name->lenstring.data;
int topic_len = topic_name == NULL || topic_name->lenstring.data == NULL || topic_name->lenstring.len <= 0 ? 0 : strnlen(topic, topic_name->lenstring.len);
Copy link
Preview

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable 'topic' is assigned from 'topic_name->lenstring.data' before checking if 'topic_name' is NULL. This could cause a segmentation fault if 'topic_name' is NULL.

Suggested change
int topic_len = topic_name == NULL || topic_name->lenstring.data == NULL || topic_name->lenstring.len <= 0 ? 0 : strnlen(topic, topic_name->lenstring.len);
const char* topic = NULL;
int topic_len = 0;
if (topic_name != NULL && topic_name->lenstring.data != NULL && topic_name->lenstring.len > 0) {
topic = topic_name->lenstring.data;
topic_len = strnlen(topic, topic_name->lenstring.len);
}

Copilot uses AI. Check for mistakes.

const char* filter = topic_filter;
const char* topic = topic_name->lenstring.data;
int topic_len = topic_name == NULL || topic_name->lenstring.data == NULL || topic_name->lenstring.len <= 0 ? 0 : strnlen(topic, topic_name->lenstring.len);
int filter_len = strlen(filter);
Copy link
Preview

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function doesn't validate that 'topic_filter' parameter is not NULL before calling strlen(). This could cause a segmentation fault if 'topic_filter' is NULL.

Copilot uses AI. Check for mistakes.

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.

2 participants