Re: [PATCH] *(int*)0 = 0 & variations

Andrew Morton (morton@nortelnetworks.com)
Thu, 24 Jun 1999 00:35:07 +1000


Riley Williams wrote:
>
> Perhaps the following patch will help. It defines a kassert() macro
> that reports the location where an assertion fails via printk with the
> KERN_DEBUG reporting level, and should apply cleanly against ANY Linux
> kernel release.
>
> To enable it, define DEBUG before including it; to disable it, don't.
>
> All that's required is for the relevant unit authors to add the
> relevant calls to their units.
>
> ===8<=== CUT ===>8===
> --- linux/include/linux/kassert.h~ Thu Jan 1 01:00:00 1970
> +++ linux/include/linux/kassert.h Wed Jun 23 14:37:54 1999
> @@ -0,0 +1,16 @@
> +/*
> + * Include definitions for kernel assertion checking
> + */
> +
> +#ifndef __KASSERT_H__
> +#define __KASSERT_H__
> +
> +#ifdef DEBUG
> +#define kassert(cond) if (!(cond)) \
> + printk(KERN_DEBUG "ASSERTION FAILURE: %s line %u: %s\n", \
> + __FILE__, __LINE__, "cond")
> +#else
> +#define kassert(cond)
> +#endif
> +
> +#endif
> ===8<=== CUT ===>8===

Sorry, Riley. This has the classic if-then problem. Try this
standalone version:

#ifdef DEBUG
#define kassert(cond) if (!(cond)) \
printf("ASSERTION FAILURE: %s line %u: %s\n", \
__FILE__, __LINE__, "cond")
#else
#define kassert(cond)
#endif

main()
{
if (0)
kassert(0);
else
printf("yes\n");
}

compiled with -DDEBUG it says nothing. Without DEBUG it says "yes".

The canonical fix: do { stuff } while (0)

#define kassert(cond) do { if (!(cond)) \
printf("ASSERTION FAILURE: %s line %u: %s\n", \
__FILE__, __LINE__, "cond"); while (0)

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/