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

Example iothub_client_sample_mqtt crash (CA-251) #129

Open
Lisa999 opened this issue Oct 31, 2022 · 0 comments
Open

Example iothub_client_sample_mqtt crash (CA-251) #129

Lisa999 opened this issue Oct 31, 2022 · 0 comments

Comments

@Lisa999
Copy link

Lisa999 commented Oct 31, 2022

This code in file iothub_client_sample_mqtt.c will lead to a crash:

static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
{
    EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
    size_t id = eventInstance->messageTrackingId;

    if (result == IOTHUB_CLIENT_CONFIRMATION_OK) {
        (void)printf("Confirmation[%d] received for message tracking id = %d with result = %s\r\n", callbackCounter, (int)id, MU_ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
        /* Some device specific action code goes here... */
        callbackCounter++;
    }
    IoTHubMessage_Destroy(eventInstance->messageHandle);
}

The problem is the IoTHubMessage_Destroy call in the end when the function is called with result IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY . The callback is going to destroy the message also, leading to corrupted memory...

The correct code:

static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
{
    EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
    size_t id = eventInstance->messageTrackingId;

    if (result == IOTHUB_CLIENT_CONFIRMATION_OK) {
        (void)printf("Confirmation[%d] received for message tracking id = %d with result = %s\r\n", callbackCounter, (int)id, MU_ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
        /* Some device specific action code goes here... */
        callbackCounter++;
    }
    if (result != IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY)
        IoTHubMessage_Destroy(eventInstance->messageHandle);
}
@github-actions github-actions bot changed the title iothub_client_sample_mqtt iothubmessage_destroy crash iothub_client_sample_mqtt iothubmessage_destroy crash (CA-251) Oct 31, 2022
@Lisa999 Lisa999 changed the title iothub_client_sample_mqtt iothubmessage_destroy crash (CA-251) Example iothub_client_sample_mqtt iothubmessage_destroy crash (CA-251) Oct 31, 2022
@Lisa999 Lisa999 changed the title Example iothub_client_sample_mqtt iothubmessage_destroy crash (CA-251) Example iothub_client_sample_mqtt crash (CA-251) Oct 31, 2022
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

1 participant