Skip to content

fix null pointer crashes#43150

Open
chrisdecenzo wants to merge 2 commits intoproject-chip:masterfrom
chrisdecenzo:cast18
Open

fix null pointer crashes#43150
chrisdecenzo wants to merge 2 commits intoproject-chip:masterfrom
chrisdecenzo:cast18

Conversation

@chrisdecenzo
Copy link
Contributor

Summary

fix crashes when not able to communicate with cached casting player

Testing

TBD

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses potential null pointer crashes by adding checks after calls to CastingPlayer::GetTargetCastingPlayer(). The changes are correct and improve the robustness of the code. Storing the result of GetTargetCastingPlayer() in a local variable is also a good practice for both safety and efficiency. I've updated the suggestions to use the VerifyOrReturnError macro for improved conciseness and consistency with recommended practices for error handling, as indicated by the ChipLogError calls.

Comment on lines +216 to +222
if (targetCastingPlayer == nullptr)
{
ChipLogError(AppServer,
"CastingPlayer::VerifyOrEstablishConnection() Target CastingPlayer no longer exists, "
"skipping connection handling");
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While this null check is correct and crucial, for conciseness and consistency with other checks in this file (e.g., line 92), consider using the VerifyOrReturnError macro. This can be applied to the other null check added in this file as well. The use of ChipLogError indicates an error condition, making VerifyOrReturnError the more appropriate choice for early returns, as per recommended practice.

For example:

CastingPlayer * targetCastingPlayer = CastingPlayer::GetTargetCastingPlayer();
VerifyOrReturnError(
    targetCastingPlayer != nullptr, CHIP_ERROR_INCORRECT_STATE,
    ChipLogError(AppServer,
                 "CastingPlayer::VerifyOrEstablishConnection() Target CastingPlayer no longer exists, "
                 "skipping connection handling"));
CastingPlayer * targetCastingPlayer = CastingPlayer::GetTargetCastingPlayer();
VerifyOrReturnError(
    targetCastingPlayer != nullptr, CHIP_ERROR_INCORRECT_STATE,
    ChipLogError(AppServer,
                 "CastingPlayer::VerifyOrEstablishConnection() Target CastingPlayer no longer exists, "
                 "skipping connection handling"));
References
  1. Prefer using VerifyOrReturnError for early returns, even with negated conditions, as it is the recommended practice. An explanatory comment should be added to clarify the logic if it's not immediately obvious.

Comment on lines +87 to +93
if (targetCastingPlayer == nullptr)
{
ChipLogError(AppServer,
"ChipDeviceEventHandler::Handle() Target CastingPlayer no longer exists, skipping connection "
"handling");
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This null check is a great addition for robustness. For better conciseness and consistency with the codebase's style, you could use the VerifyOrReturnError macro here. This suggestion also applies to the other null check added in this file. The use of ChipLogError indicates an error condition, making VerifyOrReturnError the more appropriate choice for early returns, as per recommended practice.

For example:

CastingPlayer * targetCastingPlayer = CastingPlayer::GetTargetCastingPlayer();
VerifyOrReturnError(targetCastingPlayer != nullptr, CHIP_ERROR_INCORRECT_STATE,
               ChipLogError(AppServer,
                            "ChipDeviceEventHandler::Handle() Target CastingPlayer no longer exists, skipping connection "
                            "handling"));
CastingPlayer * targetCastingPlayer = CastingPlayer::GetTargetCastingPlayer();
VerifyOrReturnError(targetCastingPlayer != nullptr, CHIP_ERROR_INCORRECT_STATE,
               ChipLogError(AppServer,
                            "ChipDeviceEventHandler::Handle() Target CastingPlayer no longer exists, skipping connection "
                            "handling"));
References
  1. Prefer using VerifyOrReturnError for early returns, even with negated conditions, as it is the recommended practice. An explanatory comment should be added to clarify the logic if it's not immediately obvious.

@github-actions
Copy link

PR #43150: Size comparison from 58568d5 to 018fc45

Full report (1 build for stm32)
platform target config section 58568d5 018fc45 change % change
stm32 light STM32WB5MM-DK FLASH 472856 472856 0 0.0
RAM 141208 141208 0 0.0

@github-actions
Copy link

PR #43150: Size comparison from 58568d5 to 7abdf59

Full report (1 build for stm32)
platform target config section 58568d5 7abdf59 change % change
stm32 light STM32WB5MM-DK FLASH 472856 472856 0 0.0
RAM 141208 141208 0 0.0

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments