diff mbox series

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

Message ID 1528193509-7063-2-git-send-email-thuth@redhat.com
State Accepted
Headers show
Series Clean-up and fix-up patches for pxelinux.cfg | expand

Commit Message

Thomas Huth June 5, 2018, 10:11 a.m. UTC
... useful for "this should never happen" situations, where
you want to make sure that it really never happens.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 lib/libc/include/assert.h | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 lib/libc/include/assert.h

Comments

Greg Kurz June 5, 2018, 11 a.m. UTC | #1
On Tue,  5 Jun 2018 12:11:47 +0200
Thomas Huth <thuth@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@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@kaod.org>

> +		} \
> +	}  while (0)
> +
> +#endif
> +
> +#endif /* SLIMLINE_ASSERT_H */
Thomas Huth June 5, 2018, 11:14 a.m. UTC | #2
On 05.06.2018 13:00, Greg Kurz wrote:
> On Tue,  5 Jun 2018 12:11:47 +0200
> Thomas Huth <thuth@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@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.

Oops, right. Alexey, in case there are no other reasons to respin, could
you fix this up when you pick up the patch? Or do you prefer if I resend
a v3?

> It works anyway so,
> 
> Reviewed-by: Greg Kurz <groug@kaod.org>

Thanks!

 Thomas
Alexey Kardashevskiy June 7, 2018, 6:49 a.m. UTC | #3
On 5/6/18 9:14 pm, Thomas Huth wrote:
> On 05.06.2018 13:00, Greg Kurz wrote:
>> On Tue,  5 Jun 2018 12:11:47 +0200
>> Thomas Huth <thuth@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@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.
> 
> Oops, right. Alexey, in case there are no other reasons to respin, could
> you fix this up when you pick up the patch? Or do you prefer if I resend
> a v3?

Nah, I fixed the commit and pushed out to github, thanks.

I'll send an update for qemu shortly, just to make sure I won't miss the
cutoff this time :)
diff mbox series

Patch

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) {}; \
+		} \
+	}  while (0)
+
+#endif
+
+#endif /* SLIMLINE_ASSERT_H */