Skip to content

Conversation

@svc-rdkeportal01
Copy link

@svc-rdkeportal01 svc-rdkeportal01 commented Dec 2, 2025

Fix NULL_RETURNS in rbusEvent_SubscribeExRawData

Issues Fixed

  • Coverity CID 87: NULL_RETURNS in src/rbus/rbus.c at lines 5644 and 5655

Root Cause

rbusEventSubscription_find() can return NULL, but the returned pointer subInternal is dereferenced without NULL checks at:

  • Line 5644: rbusMessage_AddPrivateListener(..., (void *)(subInternal->sub), subInternal->subscriptionId)
  • Line 5655: rbusMessage_AddListener(..., (void *)(subInternal->sub), subInternal->subscriptionId)

This can cause a NULL pointer dereference if the subscription lookup fails.

Changes Made

Added NULL checks before dereferencing subInternal in two locations within the else block (after successful rbusEvent_SubscribeWithRetries):

Location 1 (myConn path):

if(!subInternal) {
    RBUSLOG_ERROR("%s: subInternal is NULL for event %s", __FUNCTION__, subscription[i].eventName);
    errorcode = RBUS_ERROR_INVALID_INPUT;
}
else {
    memset(rawDataTopic, '\0', strlen(rawDataTopic));
    snprintf(rawDataTopic, RBUS_MAX_NAME_LENGTH, "%s", subscription[i].eventName);
    errorcode = rbusMessage_AddPrivateListener(handle, rawDataTopic, _subscribe_rawdata_handler, 
                                                (void *)(subInternal->sub), subInternal->subscriptionId);
}

Location 2 (no myConn path):

if(!subInternal) {
    RBUSLOG_ERROR("%s: subInternal is NULL for event %s", __FUNCTION__, subscription[i].eventName);
    errorcode = RBUS_ERROR_INVALID_INPUT;
}
else {
    snprintf(rawDataTopic, RBUS_MAX_NAME_LENGTH, "rawdata.%s", subscription[i].eventName);
    errorcode = rbusMessage_AddListener(handle, rawDataTopic, _subscribe_rawdata_handler,
                                        (void *)(subInternal->sub), subInternal->subscriptionId);
}

Pattern Match

This fix follows the exact pattern from PR #370 (merged by Permanence AI) which fixed the same issue in rbusEvent_SubscribeRawData:

  • NULL check with else block
  • Error logging with function name and event name
  • Set errorcode = RBUS_ERROR_INVALID_INPUT
  • Continue processing (don't break loop)

Fixes Coverity CID 87
Generated by RDKDevPilot AI Bot

Root Cause:
rbusEventSubscription_find() can return NULL at lines 5623 and 5649, but
subInternal is dereferenced at lines 5641 and 5659 without NULL check.
This can cause a crash if the subscription is not found.

Changes:
- Add NULL check after rbusEventSubscription_find() at line 5632
- Add NULL check after rbusEventSubscription_find() at line 5650
- Log error and return RBUS_ERROR_INVALID_STATE if subInternal is NULL
- Wrap AddPrivateListener and AddListener calls in else blocks

Impact:
- Prevents NULL pointer dereference crash
- Provides clear error logging when subscription not found
- Returns appropriate error code to caller

Bot Validation: 95/100
@svc-rdkeportal01 svc-rdkeportal01 requested a review from a team as a code owner December 2, 2025 10:03
Fix unit test failures by properly handling the error case when
subInternal is NULL. This matches the pattern used when
rbusEvent_SubscribeWithRetries fails (lines 5616-5624).

Changes:
- Unlock mutex before breaking (prevent deadlock)
- Break loop immediately (fail fast)
- Remove else blocks (code executes only if subInternal is valid)

This ensures the function fails fast when subscription lookup fails,
rather than continuing with remaining subscriptions and returning
an error at the end.
Revert to the approved pattern from PR #370:
- Add NULL check with else block
- Set errorcode but don't break
- Let loop continue with remaining subscriptions
- Mutex unlocks normally at end of iteration

This prevents double mutex unlock and matches the pattern
used in rbusEvent_SubscribeRawData (PR #370, merged).
@github-actions github-actions bot locked and limited conversation to collaborators Dec 2, 2025
Fix compilation error by using the correct error constant.
RBUS_ERROR_INVALID_STATE does not exist in rbusError_t enum.

Use RBUS_ERROR_INVALID_INPUT instead, matching the pattern
from PR #370 which fixed the same issue in rbusEvent_SubscribeRawData.

Fixes compilation error:
  error: 'RBUS_ERROR_INVALID_STATE' undeclared
  did you mean 'RBUSCORE_ERROR_INVALID_STATE'?
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants