eclipse / tahu

Eclipse Tahu addresses the existence of legacy SCADA/DCS/ICS protocols and infrastructures and provides a much-needed definition of how best to apply MQTT into these existing industrial operational environments.
https://eclipse.org/tahu
Eclipse Public License 2.0
216 stars 123 forks source link

C library possible memory leak on failure in add_property_to_set() #360

Closed ian-abbott closed 4 months ago

ian-abbott commented 4 months ago

In this code:

    void *key_allocation_result = realloc(propertyset->keys, key_allocation_size);
    void *value_allocation_result = realloc(propertyset->values, value_allocation_size);
    //DEBUG_PRINT("key=%p value=%p\n", key_allocation_result, value_allocation_result);
    if ((key_allocation_result == NULL) || (value_allocation_result == NULL)) {
        fprintf(stderr, "realloc failed in add_metric_to_payload\n");
        return -1;
    }

It is possible that one of the two calls to realloc() succeeded, resulting in a memory leak. Also, the one that succeeded will leave a now invalid pointer behind in propertyset->keys or propertyset->values, which will result in undefined behavior when the property set is cleaned up by pb_release().