Skip to content

Error status discarded in MQTTClient_cycle() due to incorrect assignment #1687

Description

@sahusara

Describe the bug
In the MQTTClient_cycle() function, error status codes returned by Socket_getReadySocket() are lost due to incorrect assignment order. Line 2630 overwrites the result pointer with an uninitialized zero value, causing error conditions to be silently discarded and replaced with success (0).

To Reproduce
Steps to reproduce the behavior:

Build and link against the MQTT client library
Establish an MQTT connection under network conditions where Socket_getReadySocket() returns an error code (e.g., socket timeout, I/O error)
Observe that the error status is not properly propagated to the caller
The condition check at line 2637 (if (*sock > 0 && rc1 == 0)) executes with rc1=0 even when an error occurred
Expected behavior
The error code returned by Socket_getReadySocket() should be preserved in the rc pointer so that calling code can properly handle error conditions. When Socket_getReadySocket() returns an error status, that status should be visible to the caller for proper error handling and logging.

Root Cause
At line 2630, the assignment reads:
*rc = rc1;
However, rc1 is initialized to 0 at line 2618 and never updated. The actual result from Socket_getReadySocket() is written to *rc at line 2629. Line 2630 then immediately overwrites this result with the static rc1 value of 0, destroying the error information.

Correct Fix
Change line 2030 from:
*rc = rc1;
to:
rc1 = *rc;
This correctly captures the status code for the subsequent condition check at line 2610.

Environment

OS: Linux
Function: MQTTClient_cycle()
Additional context
This bug causes silent failure of error handling in the MQTT client polling loop. Error conditions from socket operations are masked and replaced with success status (0), potentially leading to:

Undetected network failures
Incorrect retry logic
Loss of diagnostic information in logs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions