Skip to content

Minor cleanups to firebase_assert.h #1051

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
merged 2 commits into from
Apr 10, 2018
Merged

Minor cleanups to firebase_assert.h #1051

merged 2 commits into from
Apr 10, 2018

Conversation

wilhuff
Copy link
Contributor

@wilhuff wilhuff commented Apr 9, 2018

Remove a duplicate macro definition and clean up an extra const clang-tidy complains about.

@wilhuff wilhuff force-pushed the wilhuff/unreachable branch from 1b2dbf6 to e05f25d Compare April 9, 2018 18:59
@@ -76,10 +76,6 @@
} \
} while (0)

// Assert with custom message that is not compiled out in release builds.
Copy link
Member

Choose a reason for hiding this comment

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

Was removing this on purpose? (Seems otherwise unrelated to this PR.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a duplicate definition that I cleaned up while in here.

@@ -106,18 +102,26 @@
// Indicates an area of the code that cannot be reached (except possibly due to
// undefined behaviour or other similar badness). The only reasonable thing to
// do in these cases is to immediately abort.
#if defined(__clang__) || defined(__GNUC__)
// Clang, GCC, (and Intel) compilers have a builtin
#define FIREBASE_UNREACHABLE() __builtin_unreachable()
Copy link
Member

Choose a reason for hiding this comment

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

This (and __assume(0) below) fixes the compilation error (just like abort()) but unlike abort(), this will lead to undefined behaviour if the code is ever actually reached.

Assuming the reason FIREBASE_UNREACHABLE is called is due to an exhaustive switch statement, this would imply we're already in (mostly) undefined behaviour land, so this (probably) is no worse. But it means instead of crashing, the program will ... probably still crash immediately. But I suppose it could continue on, possibly returning arbitrary nonsense back to the caller.

Is this ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My attempts to force this to happen essentially showed that the program does crash immediately.

The win of using __builtin_unreachable() is:

  • the compiler can omit checks for whether or not the switch value is one that's handled
  • the compiler doesn't emit code to call abort()
  • code coverage doesn't "see" this as an actual case we never test

So from the strict point of view of just silencing the warning about the return value this is better, but yeah it's possible to end up here through programmer error and the result isn't as obviously useful as an abort() (it's a SIGSEGV on the mac with clang, but the stack is smashed and unreadable).

@wilhuff wilhuff changed the title Change FIREBASE_UNREACHABLE() to use compiler intrinics when available Minor cleanups to firebase_assert.h Apr 10, 2018
@wilhuff wilhuff merged commit 135e2ac into master Apr 10, 2018
@wilhuff wilhuff deleted the wilhuff/unreachable branch April 10, 2018 20:54
minafarid pushed a commit to minafarid/firebase-ios-sdk that referenced this pull request Jun 6, 2018
Remove a duplicate macro definition and clean up an extra const clang-tidy complains about.
@firebase firebase locked and limited conversation to collaborators Nov 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants