Azure / azure-iot-sdk-c

A C99 SDK for connecting devices to Microsoft Azure IoT services
https://azure.github.io/azure-iot-sdk-c
Other
587 stars 739 forks source link

Potential issue in samples/solutions/remote_monitoring_client/remote_monitoring.c: Return Value Not Checked from Function Call #1719

Closed monocle-ai closed 3 years ago

monocle-ai commented 3 years ago

In this codebase, you often check the return value of the implicated function when calling it, but in this instance, it appears that you didn’t. Using a consistent return value checking and/or error handling approach can improve code robustness and readability.

1 instance of this defect were found in the following locations:


Instance 1 File : samples/solutions/remote_monitoring_client/remote_monitoring.c Enclosing Function : send_message Function : strftime https://github.com/Azure/azure-iot-sdk-c/blob/0ca4bbb7c416dd347398fe5897cc38193b6f03fe/samples/solutions/remote_monitoring_client/remote_monitoring.c#L344 Code extract:

#pragma warning(pop)
#endif
    char timebuff[50];
    strftime(timebuff, 50, "%Y-%m-%dT%H:%M:%SZ", timeinfo); <------ HERE
    (void)Map_AddOrUpdate(propMap, "$$CreationTimeUtc", timebuff);

How can I fix it? Correct reference usage found in iothub_client/samples/pnp/pnp_temperature_controller/pnp_thermostat_component.c at line 85. https://github.com/Azure/azure-iot-sdk-c/blob/0ca4bbb7c416dd347398fe5897cc38193b6f03fe/iothub_client/samples/pnp/pnp_temperature_controller/pnp_thermostat_component.c#L85 Code extract:

    time(&currentTime);
    currentTimeTm = gmtime(&currentTime);

    if (strftime(utcTimeBuffer, utcTimeBufferSize, g_ISO8601Format, currentTimeTm) == 0) <------ HERE
    {
        LogError("snprintf on UTC time failed");
ewertons commented 3 years ago

Hi @monocle-ai ,

as we have in our disclaimer in our samples, the returns are omitted intentionally.

https://github.com/Azure/azure-iot-sdk-c/blob/0ca4bbb7c416dd347398fe5897cc38193b6f03fe/samples/solutions/remote_monitoring_client/remote_monitoring.c#L10

// CAVEAT: This sample is to demonstrate azure IoT client concepts only and is not a guide design principles or style
// Checking of return codes and error values are omitted for brevity.  Please practice sound engineering practices 
// when writing production code.

So this won't be a change we can take.

Thanks, Azure SDK Team.

az-iot-builder-01 commented 3 years ago

@monocle-ai, thank you for your contribution to our open-sourced project! Please help us improve by filling out this 2-minute customer satisfaction survey