[SLOF] [PATCH v2 1/3] libc: Add a simple implementation of an assert() function

Greg Kurz groug at kaod.org
Tue Jun 5 21:00:50 AEST 2018


On Tue,  5 Jun 2018 12:11:47 +0200
Thomas Huth <thuth at redhat.com> wrote:

> ... useful for "this should never happen" situations, where
> you want to make sure that it really never happens.
> 
> Signed-off-by: Thomas Huth <thuth at redhat.com>
> ---
>  lib/libc/include/assert.h | 37 +++++++++++++++++++++++++++++++++++++
>  1 file changed, 37 insertions(+)
>  create mode 100644 lib/libc/include/assert.h
> 
> diff --git a/lib/libc/include/assert.h b/lib/libc/include/assert.h
> new file mode 100644
> index 0000000..e0e02ac
> --- /dev/null
> +++ b/lib/libc/include/assert.h
> @@ -0,0 +1,37 @@
> +/*****************************************************************************
> + * assert() macro definition
> + *
> + * Copyright 2018 Red Hat, Inc.
> + *
> + * This program and the accompanying materials are made available under
> + * the terms of the BSD License which accompanies this distribution, and
> + * is available at http://www.opensource.org/licenses/bsd-license.php
> + *
> + * Contributors:
> + *     Thomas Huth, Red Hat Inc. - initial implementation
> + *****************************************************************************/
> +
> +#ifndef SLIMLINE_ASSERT_H
> +#define SLIMLINE_ASSERT_H
> +
> +
> +#ifdef NDEBUG
> +
> +#define assert(cond) (void)
> +
> +#else
> +
> +#define assert(cond) \
> +	do { \
> +		if (!(cond)) { \
> +			fprintf(stderr, \
> +				"ERROR: Assertion '" #cond "' failed!\n" \
> +				"(function %s, file " __FILE__ ", line %i)\n", \
> +				__func__, __LINE__); \
> +			while (1) {}; \

The semicolon isn't needed after an empty block. Alternatively you could
drop the empty block and keep the semicolon.

It works anyway so,

Reviewed-by: Greg Kurz <groug at kaod.org>

> +		} \
> +	}  while (0)
> +
> +#endif
> +
> +#endif /* SLIMLINE_ASSERT_H */



More information about the SLOF mailing list