Skip to content

Fix infinite calling loop #4800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Nodraak
Copy link
Contributor

@Nodraak Nodraak commented Jul 24, 2017

CLA accepted: username on mbed is nodraak

Description

Fix issue #4799. I renamed[1] the function to prevent resolving issues for the compiler and added the necessary const and cast to make everything compile and work.

Status

READY

Migrations

NO

[1]: I followed the convention used by the other methods addData and updateData: xxData for public methods and xxField for private methods.

Copy link
Member

@pan- pan- left a comment

Choose a reason for hiding this comment

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

Thanks for this submission. I'd prefer to conserve the const correctness of the member functions as well as there existing name:

  • const uint8_t* findField(DataType_t) const
  • uint8_t* findField(DataType_t)

Could you move the logic in the const overload and apply the const cast in the non const overload ?

uint8_t* findField(DataType_t type) { 
    return const_cast<uint8_t*>(static_cast<const GapAdvertisingData*>(this)->findField(type));
}

@Nodraak Nodraak force-pushed the fix/4799_ble_infinite_calling_loop branch from dcac58d to 2a39019 Compare July 24, 2017 12:18
@Nodraak
Copy link
Contributor Author

Nodraak commented Jul 24, 2017

I am not sure to have understood everything, but I have updated my PR with what made most sense. Compiled and tested, and still fixing the bug as expected :)

@pan-
Copy link
Member

pan- commented Jul 24, 2017

Ok, let me add some explanation then. I've requested two changes:

Keep the existing function names and signatures:

Goal is to preserve backward compatibility (might sound stupid because the function was not usable at all ...).

Factorize the logic into the const overload and invoke it in the non const overload:

findField is an accessor and should not modify the content of the object itself (const or non const version).
It is easier to enforce this behavior if the logic of the accessor lives in the const version of the function because any attempt to modify this will trigger a compile time error.

On the other hand, if the logic is in the non const version of findField and a latter patch introduce a logic change which mutate this then:

  • Compiler will not warn about mutation in the accessor.
  • A call to the const version will result in undefined behavior (from the C++ standard standpoint).

That's why it is preferable to apply the const_cast in the non const version of the overload and not the other way around.

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.

4 participants