mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-12 08:43:53 -05:00
Replace assert(false) with an unreachable() macro.
Taking a leaf out of the LLVM code base: this macro still includes an assert(false) so that the message will show up in a typical build, but it follows it up with a call to a function explicitly marked as no- return. So this ought to do a better job of convincing compilers that once a code path hits this function it _really doesn't_ have to still faff about with making up a bogus return value or filling in a variable that 'might be used uninitialised' in the following code that won't be reached anyway. I've gone through the existing code looking for the assert(false) / assert(0) idiom and replaced all the ones I found with the new macro, which also meant I could remove a few pointless return statements and variable initialisations that I'd already had to put in to placate compiler front ends.
This commit is contained in:
18
misc.h
18
misc.h
@ -11,8 +11,10 @@
|
||||
|
||||
#include <stdio.h> /* for FILE * */
|
||||
#include <stdarg.h> /* for va_list */
|
||||
#include <stdlib.h> /* for abort */
|
||||
#include <time.h> /* for struct tm */
|
||||
#include <limits.h> /* for INT_MAX/MIN */
|
||||
#include <assert.h> /* for assert (obviously) */
|
||||
|
||||
unsigned long parse_blocksize(const char *bs);
|
||||
char ctrlparse(char *s, char **next);
|
||||
@ -169,6 +171,22 @@ bool smemeq(const void *av, const void *bv, size_t len);
|
||||
|
||||
char *buildinfo(const char *newline);
|
||||
|
||||
/*
|
||||
* A function you can put at points in the code where execution should
|
||||
* never reach in the first place. Better than assert(false), or even
|
||||
* assert(false && "some explanatory message"), because some compilers
|
||||
* don't interpret assert(false) as a declaration of unreachability,
|
||||
* so they may still warn about pointless things like some variable
|
||||
* not being initialised on the unreachable code path.
|
||||
*
|
||||
* I follow the assertion with a call to abort() just in case someone
|
||||
* compiles with -DNDEBUG, and I wrap that abort inside my own
|
||||
* function labelled NORETURN just in case some unusual kind of system
|
||||
* header wasn't foresighted enough to label abort() itself that way.
|
||||
*/
|
||||
static inline NORETURN void unreachable_internal(void) { abort(); }
|
||||
#define unreachable(msg) (assert(false && msg), unreachable_internal())
|
||||
|
||||
/*
|
||||
* Debugging functions.
|
||||
*
|
||||
|
Reference in New Issue
Block a user