Skip to content
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

Abuse of unreachable() #2571

Closed
kostja opened this issue Jul 5, 2017 · 3 comments
Closed

Abuse of unreachable() #2571

kostja opened this issue Jul 5, 2017 · 3 comments
Assignees
Labels
bug Something isn't working good first issue Good for newcomers wontfix This will not be worked on

Comments

@kostja
Copy link
Contributor

kostja commented Jul 5, 2017

unreachable() was added to help compiler determine that a branch can not be reached. Unfortunately, unreachable() in release builds is a noop, and it's being used in all cases when the programmer thinks that a branch is unreachable, not when it really is.

unreachable() should lead to panic() in production, otherwise it's a security time bomb.

@kostja kostja added the bug Something isn't working label Jul 5, 2017
@kostja kostja added this to the 1.7.6 milestone Jul 5, 2017
@rtsisyk rtsisyk modified the milestones: 1.7.6, 1.7.7 Oct 21, 2017
@kostja kostja added the good first issue Good for newcomers label Feb 7, 2018
@Gerold103 Gerold103 assigned Gerold103 and unassigned rtsisyk Feb 9, 2018
@kostja kostja modified the milestones: 2.1.1, 1.9.0 Feb 11, 2018
@kostja kostja added this to the 2.1.1 milestone Feb 20, 2018
@kyukhin kyukhin modified the milestones: 2.1.1, 2.2.0 Dec 10, 2018
@kyukhin kyukhin modified the milestones: 2.2.0, 2.3.0 Apr 1, 2019
@kostja kostja modified the milestones: 2.3.1, 2.4.0 Aug 6, 2019
@kyukhin kyukhin modified the milestones: 2.4.1, wishlist Apr 10, 2020
@alyapunov
Copy link
Contributor

I don't like the idea.

  1. unreachable() as a macro for __builtin_unreachable() has different semantics with exit(..), abort(), panic(..) etc. Apart from them unreachable() tells compiler that the situation is impossible. Having a line if (expr) __builtin_unreachable(); a compiler may assume as a fact that expr is false and use the fact below and even above (!) in the code.

  2. BTW unreachable() in release build is not a noop. It is generally undefined but actually it's just never called. switch..case..default: unreachable(); can get SIGSEGV immediately on wrong value, not just leave some values in undefined state.

  3. We need unreachable() to tell compiler about impossible situation. There are optimizations that cannot be made without true unreachable(). And if you really make unreachable() implementation to do something it will immediately become 'reachable', and even if you leave it noreturn not all the optimizations will be applied.

  4. We can only allow unreachable() do something in debug build - nobody cares about optimizations in it. Actually it already has assert(0);. As for me I don't like it either - the semantics is changes, that must be another method, asssert_unreachable() idk.

  5. We have unreachable(), assert(..), panic(..) as different tools as it should be. We should not just replace all 'unreachables' (and 'asserts') with panic - we must use panic in those places where execution can ever reach. We must use corresponding tools for corresponding actions, not one tool for everything.

  6. Definitely there are places that must be fixed asap. I've just found a code:

default:
    *key_len = 0;
    unreachable();
    return NULL;

We all must understand that in release neither *key_len = 0; nor return NULL; are not even have compiled code. That's a wrong tool in a wrong place.

Sorry for many words but I wanted to explain why:

  1. We should leave unreachable() alone.
  2. Our team must use proper tools.
  3. We must inspect our code about unreachable() (and even assert()) usage. At least the code in the example above and entire c89adef (another example of that replacing of one tool with another is a bad idea).

@Totktonada
Copy link
Member

As fourth point I would propose to hold where each of those instruments should be used in our C Style Guide.

@kyukhin
Copy link
Contributor

kyukhin commented Jun 15, 2022

As fourth point I would propose to hold where each of those instruments should be used in our C Style Guide.

Fair enough. Feel free to submit a doc issue (or even PR). I totally agree with all points.

@kyukhin kyukhin closed this as completed Jun 15, 2022
@kyukhin kyukhin added the wontfix This will not be worked on label Jun 24, 2022
@kyukhin kyukhin removed this from the wishlist milestone Jun 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

6 participants